| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | require_once 'Phwittr/User.php'; |
|---|
| 14 | require_once 'Phwittr/Self/Exception.php'; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | class Phwittr_Self extends Phwittr_User |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | public function __construct($id) |
|---|
| 63 | { |
|---|
| 64 | Phwittr_Config::configurePieceORM(); |
|---|
| 65 | |
|---|
| 66 | $mapper = Piece_ORM::getMapper('Users'); |
|---|
| 67 | $user = $mapper->findById($id); |
|---|
| 68 | if (is_null($user)) { |
|---|
| 69 | throw new Phwittr_Self_Exception(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | foreach ($user as $key => $value) { |
|---|
| 73 | $this->$key = $value; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | $this->_normalizePictureFilename($this); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | public function listByFollowers($page = 1) |
|---|
| 89 | { |
|---|
| 90 | $criteria->userId = $this->id; |
|---|
| 91 | |
|---|
| 92 | $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1)); |
|---|
| 93 | $statusList = $mapper->findAllByUserIdForListFollowersStatus($criteria); |
|---|
| 94 | if (count($statusList) == 0 && $page > 1) { |
|---|
| 95 | return $this->listByFollowers(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | $this->_checkNextPage($mapper, $page); |
|---|
| 99 | |
|---|
| 100 | foreach ($statusList as &$status) { |
|---|
| 101 | $this->_normalizePictureFilename($status); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | return $statusList; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | public function listReplies($page = 1) |
|---|
| 117 | { |
|---|
| 118 | $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1)); |
|---|
| 119 | $statusList = $mapper->findAllByReplyUserId($this->id); |
|---|
| 120 | if (count($statusList) == 0 && $page > 1) { |
|---|
| 121 | return $this->listReplies(); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | $this->_checkNextPage($mapper, $page); |
|---|
| 125 | |
|---|
| 126 | foreach ($statusList as &$status) { |
|---|
| 127 | $this->_normalizePictureFilename($status); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return $statusList; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | public function update($comment) |
|---|
| 143 | { |
|---|
| 144 | $status = Piece_ORM::createObject('Statuses'); |
|---|
| 145 | $status->userId = $this->id; |
|---|
| 146 | $status->comment = $comment; |
|---|
| 147 | $status->replyUserId = $this->_findReplyUserId($comment); |
|---|
| 148 | $status->createdAt = date('Y-m-d H:i:s'); |
|---|
| 149 | |
|---|
| 150 | $mapper = Piece_ORM::getMapper('Statuses'); |
|---|
| 151 | return $mapper->insert($status); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | public function delete($id) |
|---|
| 164 | { |
|---|
| 165 | $criteria->id = $id; |
|---|
| 166 | $criteria->userId = $this->id; |
|---|
| 167 | |
|---|
| 168 | $mapper = Piece_ORM::getMapper('Statuses'); |
|---|
| 169 | $result = $mapper->deleteByIdAndUserId($criteria); |
|---|
| 170 | if (!$result) { |
|---|
| 171 | throw new Phwittr_Self_Exception(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | return $result; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | public function follow($userId) |
|---|
| 187 | { |
|---|
| 188 | if ($userId == $this->id) { |
|---|
| 189 | throw new Phwittr_Self_Exception(); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | $user = new Phwittr_User($userId, $this->id, |
|---|
| 193 | PHWITTR_USER_CONSTRUCTOR_FIND_BY_ID |
|---|
| 194 | ); |
|---|
| 195 | if ($user->isProtected()) { |
|---|
| 196 | $entity = Piece_ORM::createObject('FriendRequests'); |
|---|
| 197 | $entity->userId = $userId; |
|---|
| 198 | $entity->requesterId = $this->id; |
|---|
| 199 | $entity->createdAt = date('Y-m-d H:i:s'); |
|---|
| 200 | $mapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 201 | } else { |
|---|
| 202 | $entity = Piece_ORM::createObject('Followers'); |
|---|
| 203 | $entity->userId = $this->id; |
|---|
| 204 | $entity->followId = $userId; |
|---|
| 205 | $entity->createdAt = date('Y-m-d H:i:s'); |
|---|
| 206 | $mapper = Piece_ORM::getMapper('Followers'); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | try { |
|---|
| 210 | $result = $mapper->insert($entity); |
|---|
| 211 | } catch (Exception $e) { |
|---|
| 212 | if ($e->getCode() === PIECE_ORM_ERROR_CONSTRAINT) { |
|---|
| 213 | throw new Phwittr_Self_Exception(); |
|---|
| 214 | } else { |
|---|
| 215 | throw new Exception(); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | public function removeFriend($userId) |
|---|
| 230 | { |
|---|
| 231 | if ($userId == $this->id) { |
|---|
| 232 | throw new Phwittr_Self_Exception(); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | $follower = Piece_ORM::createObject('Followers'); |
|---|
| 236 | $follower->userId = $this->id; |
|---|
| 237 | $follower->followId = $userId; |
|---|
| 238 | |
|---|
| 239 | $mapper = Piece_ORM::getMapper('Followers'); |
|---|
| 240 | $result = $mapper->delete($follower); |
|---|
| 241 | |
|---|
| 242 | if ($result) { |
|---|
| 243 | return true; |
|---|
| 244 | } else { |
|---|
| 245 | throw new Phwittr_Self_Exception(); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | public function cancel($requestId) |
|---|
| 259 | { |
|---|
| 260 | $requestMapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 261 | $friendRequest = $requestMapper->findById($requestId); |
|---|
| 262 | if (!$friendRequest) { |
|---|
| 263 | throw new Phwittr_Self_Exception(); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | if ($friendRequest->requesterId !== $this->id) { |
|---|
| 267 | throw new Phwittr_Self_Exception(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | $requestMapper->delete($friendRequest); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | public function accept($requestId) |
|---|
| 283 | { |
|---|
| 284 | $requestMapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 285 | $friendRequest = $requestMapper->findById($requestId); |
|---|
| 286 | if (!$friendRequest) { |
|---|
| 287 | throw new Phwittr_Self_Exception(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | if ($friendRequest->userId !== $this->id) { |
|---|
| 291 | throw new Phwittr_Self_Exception(); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | $entity1 = Piece_ORM::createObject('Followers'); |
|---|
| 295 | $entity2 = Piece_ORM::createObject('Followers'); |
|---|
| 296 | |
|---|
| 297 | $entity1->userId = $friendRequest->requesterId; |
|---|
| 298 | $entity1->followId = $friendRequest->userId; |
|---|
| 299 | $entity1->createdAt = date('Y-m-d H:i:s'); |
|---|
| 300 | |
|---|
| 301 | $entity2->userId = $friendRequest->userId; |
|---|
| 302 | $entity2->followId = $friendRequest->requesterId; |
|---|
| 303 | $entity2->createdAt = date('Y-m-d H:i:s'); |
|---|
| 304 | |
|---|
| 305 | $followerMapper = Piece_ORM::getMapper('Followers'); |
|---|
| 306 | |
|---|
| 307 | try { |
|---|
| 308 | if (!$followerMapper->findByUserIdAndFollowId($entity1)) { |
|---|
| 309 | $followerMapper->insert($entity1); |
|---|
| 310 | } |
|---|
| 311 | if (!$followerMapper->findByUserIdAndFollowId($entity2)) { |
|---|
| 312 | $followerMapper->insert($entity2); |
|---|
| 313 | } |
|---|
| 314 | $requestMapper->delete($friendRequest); |
|---|
| 315 | } catch (Exception $e) { |
|---|
| 316 | if ($e->getCode() === PIECE_ORM_ERROR_CONSTRAINT) { |
|---|
| 317 | throw new Phwittr_Self_Exception(); |
|---|
| 318 | } else { |
|---|
| 319 | throw new Exception(); |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | public function deny($requestId) |
|---|
| 334 | { |
|---|
| 335 | $requestMapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 336 | $friendRequest = $requestMapper->findById($requestId); |
|---|
| 337 | if (!$friendRequest) { |
|---|
| 338 | throw new Phwittr_Self_Exception(); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | if ($friendRequest->userId !== $this->id) { |
|---|
| 342 | throw new Phwittr_Self_Exception(); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | $requestMapper->delete($friendRequest); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | public function isFriend($userId) |
|---|
| 358 | { |
|---|
| 359 | $criteria = new stdClass(); |
|---|
| 360 | $criteria->userId = $this->id; |
|---|
| 361 | $criteria->followId = $userId; |
|---|
| 362 | |
|---|
| 363 | $mapper = Piece_ORM::getMapper('Followers'); |
|---|
| 364 | $followers = $mapper->findByUserIdAndFollowId($criteria); |
|---|
| 365 | |
|---|
| 366 | if ($followers) { |
|---|
| 367 | return 1; |
|---|
| 368 | } else { |
|---|
| 369 | return 0; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | public function getRequestId($userId) |
|---|
| 383 | { |
|---|
| 384 | $criteria = new stdClass(); |
|---|
| 385 | $criteria->userId = $userId; |
|---|
| 386 | $criteria->requesterId = $this->id; |
|---|
| 387 | |
|---|
| 388 | $mapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 389 | $friendRequest = $mapper->findByUserIdAndRequesterId($criteria); |
|---|
| 390 | |
|---|
| 391 | if ($friendRequest) { |
|---|
| 392 | return $friendRequest->id; |
|---|
| 393 | } else { |
|---|
| 394 | return false; |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | public function countFriendRequests() |
|---|
| 408 | { |
|---|
| 409 | if (!$this->privateFlag) { |
|---|
| 410 | return 0; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | $mapper = Piece_ORM::getMapper('FriendRequests'); |
|---|
| 414 | return count($mapper->findAllByUserId($this->id)); |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | public function listFriendRequests($page = 1) |
|---|
| 427 | { |
|---|
| 428 | $mapper = $this->_createUserListMapper($this->_limit * ($page - 1)); |
|---|
| 429 | $userList = $mapper->findAllByUserIdForListFriendRequests($this); |
|---|
| 430 | if (count($userList) == 0 && $page > 1) { |
|---|
| 431 | return $this->listFriendRequests(); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | $this->_checkNextPage($mapper, $page); |
|---|
| 435 | |
|---|
| 436 | foreach ($userList as &$user) { |
|---|
| 437 | $this->_normalizePictureFilename($user); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | return $userList; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | private function _findReplyUserId($comment) |
|---|
| 461 | { |
|---|
| 462 | $replyUserId = null; |
|---|
| 463 | if (preg_match('/^@([0-9a-zA-Z_]{1,20}+).*$/', $comment, $matches)) { |
|---|
| 464 | $replyUserName = $matches[1]; |
|---|
| 465 | |
|---|
| 466 | $mapper = Piece_ORM::getMapper('Users'); |
|---|
| 467 | $replyUser = $mapper->findByUserName($replyUserName); |
|---|
| 468 | if ($replyUser) { |
|---|
| 469 | $replyUserId = $replyUser->id; |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | return $replyUserId; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | private function _raiseException() |
|---|
| 484 | { |
|---|
| 485 | throw new Phwittr_Self_Exception(); |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | ?> |
|---|