| 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 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | public function listByFollowers($page = 1) |
|---|
| 87 | { |
|---|
| 88 | $criteria->userId = $this->id; |
|---|
| 89 | |
|---|
| 90 | $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1)); |
|---|
| 91 | $statusList = $mapper->findAllByUserIdForListFollowersStatus($criteria); |
|---|
| 92 | if (count($statusList) == 0 && $page > 2) { |
|---|
| 93 | return $this->listByFollowers(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | $this->_checkNextPage($mapper, $page); |
|---|
| 97 | |
|---|
| 98 | return $statusList; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | public function listReplies($page = 1) |
|---|
| 111 | { |
|---|
| 112 | $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1)); |
|---|
| 113 | $statusList = $mapper->findAllByReplyUserId($this->id); |
|---|
| 114 | if (count($statusList) == 0 && $page > 2) { |
|---|
| 115 | return $this->listReplies(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | $this->_checkNextPage($mapper, $page); |
|---|
| 119 | |
|---|
| 120 | return $statusList; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | public function update($comment) |
|---|
| 133 | { |
|---|
| 134 | $status = Piece_ORM::createObject('Statuses'); |
|---|
| 135 | $status->userId = $this->id; |
|---|
| 136 | $status->comment = $comment; |
|---|
| 137 | $status->replyUserId = $this->_findReplyUserId($comment); |
|---|
| 138 | $status->createdAt = date('Y-m-d H:i:s'); |
|---|
| 139 | |
|---|
| 140 | $mapper = Piece_ORM::getMapper('Statuses'); |
|---|
| 141 | return $mapper->insert($status); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | public function delete($id) |
|---|
| 154 | { |
|---|
| 155 | $criteria->id = $id; |
|---|
| 156 | $criteria->userId = $this->id; |
|---|
| 157 | |
|---|
| 158 | $mapper = Piece_ORM::getMapper('Statuses'); |
|---|
| 159 | $result = $mapper->deleteByIdAndUserId($criteria); |
|---|
| 160 | if (!$result) { |
|---|
| 161 | throw new Phwittr_Self_Exception(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | return $result; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | private function _findReplyUserId($comment) |
|---|
| 183 | { |
|---|
| 184 | $replyUserId = null; |
|---|
| 185 | if (preg_match('/^@([0-9a-zA-Z_]{1,20}+).*$/', $comment, $matches)) { |
|---|
| 186 | $replyUserName = $matches[1]; |
|---|
| 187 | |
|---|
| 188 | $mapper = Piece_ORM::getMapper('Users'); |
|---|
| 189 | $replyUser = $mapper->findByUserName($replyUserName); |
|---|
| 190 | if ($replyUser) { |
|---|
| 191 | $replyUserId = $replyUser->id; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | return $replyUserId; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | ?> |
|---|