| Total Complexity | 100 |
| Total Lines | 595 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PostHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PostHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class PostHandler extends \XoopsPersistableObjectHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @param null|\XoopsDatabase $db |
||
| 39 | */ |
||
| 40 | public function __construct(\XoopsDatabase $db = null) |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param mixed|null $id |
||
| 47 | * @param null $fields |
||
|
|
|||
| 48 | * @return null|\XoopsObject |
||
| 49 | */ |
||
| 50 | public function get($id = null, $fields = null) |
||
| 51 | { |
||
| 52 | $id = (int)$id; |
||
| 53 | $post = null; |
||
| 54 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('bb_posts') . ' p LEFT JOIN ' . $this->db->prefix('bb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id; |
||
| 55 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
| 56 | if ($array) { |
||
| 57 | $post = $this->create(false); |
||
| 58 | $post->assignVars($array); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $post; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param int $topic_id |
||
| 66 | * @param int $limit |
||
| 67 | * @param int $approved |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | // public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true) |
||
| 71 | public function &getByLimit($topic_id, $limit, $approved = 1) |
||
| 72 | { |
||
| 73 | $sql = 'SELECT p.*, t.*, tp.topic_status FROM ' |
||
| 74 | . $this->db->prefix('bb_posts') |
||
| 75 | . ' p LEFT JOIN ' |
||
| 76 | . $this->db->prefix('bb_posts_text') |
||
| 77 | . ' t ON p.post_id=t.post_id LEFT JOIN ' |
||
| 78 | . $this->db->prefix('bb_topics') |
||
| 79 | . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id=' |
||
| 80 | . $topic_id |
||
| 81 | . ' AND p.approved =' |
||
| 82 | . $approved |
||
| 83 | . ' ORDER BY p.post_time DESC'; |
||
| 84 | $result = $this->db->query($sql, $limit, 0); |
||
| 85 | $ret = []; |
||
| 86 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 87 | $post = $this->create(false); |
||
| 88 | $post->assignVars($myrow); |
||
| 89 | |||
| 90 | $ret[$myrow['post_id']] = $post; |
||
| 91 | unset($post); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $ret; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param $post |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | public function getPostForPDF($post) |
||
| 102 | { |
||
| 103 | return $post->getPostBody(true); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param $post |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | public function getPostForPrint($post) |
||
| 111 | { |
||
| 112 | return $post->getPostBody(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $post |
||
| 117 | * @param bool $force |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function approve(&$post, $force = false) |
||
| 121 | { |
||
| 122 | if (empty($post)) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | if (\is_numeric($post)) { |
||
| 126 | $post = $this->get($post); |
||
| 127 | } |
||
| 128 | $post_id = $post->getVar('post_id'); |
||
| 129 | |||
| 130 | $wasApproved = $post->getVar('approved'); |
||
| 131 | // irmtfan approve post if the approved = 0 (pending) or -1 (deleted) |
||
| 132 | if (empty($force) && $wasApproved > 0) { |
||
| 133 | return true; |
||
| 134 | } |
||
| 135 | $post->setVar('approved', 1); |
||
| 136 | $this->insert($post, true); |
||
| 137 | |||
| 138 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 139 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 140 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 141 | if ($topic_obj->getVar('topic_last_post_id') < $post->getVar('post_id')) { |
||
| 142 | $topic_obj->setVar('topic_last_post_id', $post->getVar('post_id')); |
||
| 143 | } |
||
| 144 | if ($post->isTopic()) { |
||
| 145 | $topic_obj->setVar('approved', 1); |
||
| 146 | } else { |
||
| 147 | $topic_obj->setVar('topic_replies', $topic_obj->getVar('topic_replies') + 1); |
||
| 148 | } |
||
| 149 | $topicHandler->insert($topic_obj, true); |
||
| 150 | |||
| 151 | /** @var Newbb\ForumHandler $forumHandler */ |
||
| 152 | $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum'); |
||
| 153 | $forum_obj = $forumHandler->get($post->getVar('forum_id')); |
||
| 154 | if ($forum_obj->getVar('forum_last_post_id') < $post->getVar('post_id')) { |
||
| 155 | $forum_obj->setVar('forum_last_post_id', $post->getVar('post_id')); |
||
| 156 | } |
||
| 157 | $forum_obj->setVar('forum_posts', $forum_obj->getVar('forum_posts') + 1); |
||
| 158 | if ($post->isTopic()) { |
||
| 159 | $forum_obj->setVar('forum_topics', $forum_obj->getVar('forum_topics') + 1); |
||
| 160 | } |
||
| 161 | $forumHandler->insert($forum_obj, true); |
||
| 162 | |||
| 163 | // Update user stats |
||
| 164 | if ($post->getVar('uid') > 0) { |
||
| 165 | $memberHandler = \xoops_getHandler('member'); |
||
| 166 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 167 | if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) { |
||
| 168 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
| 169 | $res = $memberHandler->insertUser($poster, true); |
||
| 170 | unset($poster); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // Update forum stats |
||
| 175 | $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats'); |
||
| 176 | $statsHandler->update($post->getVar('forum_id'), 'post'); |
||
| 177 | if ($post->isTopic()) { |
||
| 178 | $statsHandler->update($post->getVar('forum_id'), 'topic'); |
||
| 179 | } |
||
| 180 | |||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param \XoopsObject $post |
||
| 186 | * @param bool $force |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function insert(\XoopsObject $post, $force = true) |
||
| 190 | { |
||
| 191 | // Set the post time |
||
| 192 | // The time should be 'publish' time. To be adjusted later |
||
| 193 | if (!$post->getVar('post_time')) { |
||
| 194 | $post->setVar('post_time', \time()); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 198 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 199 | // Verify the topic ID |
||
| 200 | $topic_id = $post->getVar('topic_id'); |
||
| 201 | if ($topic_id) { |
||
| 202 | $topic_obj = $topicHandler->get($topic_id); |
||
| 203 | // Invalid topic OR the topic is no approved and the post is not top post |
||
| 204 | if (!$topic_obj// || (!$post->isTopic() && $topic_obj->getVar("approved") < 1) |
||
| 205 | ) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | if (empty($topic_id)) { |
||
| 210 | $post->setVar('topic_id', 0); |
||
| 211 | $post->setVar('pid', 0); |
||
| 212 | $post->setNew(); |
||
| 213 | $topic_obj = $topicHandler->create(); |
||
| 214 | } |
||
| 215 | $textHandler = Newbb\Helper::getInstance()->getHandler('Text'); |
||
| 216 | $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr']; |
||
| 217 | if ($post->isNew()) { |
||
| 218 | if (!$topic_id = $post->getVar('topic_id')) { |
||
| 219 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 220 | $topic_obj->setVar('topic_poster', $post->getVar('uid')); |
||
| 221 | $topic_obj->setVar('forum_id', $post->getVar('forum_id')); |
||
| 222 | $topic_obj->setVar('topic_time', $post->getVar('post_time')); |
||
| 223 | $topic_obj->setVar('poster_name', $post->getVar('poster_name'), true); |
||
| 224 | $topic_obj->setVar('approved', $post->getVar('approved'), true); |
||
| 225 | |||
| 226 | if (!$topic_id = $topicHandler->insert($topic_obj, $force)) { |
||
| 227 | $post->deleteAttachment(); |
||
| 228 | $post->setErrors('insert topic error'); |
||
| 229 | |||
| 230 | //xoops_error($topic_obj->getErrors()); |
||
| 231 | return false; |
||
| 232 | } |
||
| 233 | $post->setVar('topic_id', $topic_id); |
||
| 234 | |||
| 235 | $pid = 0; |
||
| 236 | $post->setVar('pid', 0); |
||
| 237 | } elseif (!$post->getVar('pid')) { |
||
| 238 | $pid = $topicHandler->getTopPostId($topic_id); |
||
| 239 | $post->setVar('pid', $pid); |
||
| 240 | } |
||
| 241 | |||
| 242 | $text_obj = $textHandler->create(); |
||
| 243 | foreach ($post_text_vars as $key) { |
||
| 244 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 245 | } |
||
| 246 | $post->destroyVars($post_text_vars); |
||
| 247 | if (!$post_id = parent::insert($post, $force)) { |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | $post->unsetNew(); |
||
| 251 | |||
| 252 | $text_obj->setVar('post_id', $post_id); |
||
| 253 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 254 | $this->delete($post); |
||
| 255 | $post->setErrors('post text insert error'); |
||
| 256 | |||
| 257 | //xoops_error($text_obj->getErrors()); |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | if ($post->getVar('approved') > 0) { |
||
| 261 | $this->approve($post, true); |
||
| 262 | } |
||
| 263 | $post->setVar('post_id', $post_id); |
||
| 264 | } else { |
||
| 265 | if ($post->isTopic()) { |
||
| 266 | if ($post->getVar('subject') !== $topic_obj->getVar('topic_title')) { |
||
| 267 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 268 | } |
||
| 269 | if ($post->getVar('approved') !== $topic_obj->getVar('approved')) { |
||
| 270 | $topic_obj->setVar('approved', $post->getVar('approved')); |
||
| 271 | } |
||
| 272 | $topic_obj->setDirty(); |
||
| 273 | if (!$result = $topicHandler->insert($topic_obj, $force)) { |
||
| 274 | $post->setErrors('update topic error'); |
||
| 275 | |||
| 276 | // xoops_error($topic_obj->getErrors()); |
||
| 277 | return false; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | $text_obj = $textHandler->get($post->getVar('post_id')); |
||
| 281 | $text_obj->setDirty(); |
||
| 282 | foreach ($post_text_vars as $key) { |
||
| 283 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 284 | } |
||
| 285 | $post->destroyVars($post_text_vars); |
||
| 286 | if (!$post_id = parent::insert($post, $force)) { |
||
| 287 | // xoops_error($post->getErrors()); |
||
| 288 | return false; |
||
| 289 | } |
||
| 290 | $post->unsetNew(); |
||
| 291 | |||
| 292 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 293 | $post->setErrors('update post text error'); |
||
| 294 | |||
| 295 | // xoops_error($text_obj->getErrors()); |
||
| 296 | return false; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return $post->getVar('post_id'); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param \XoopsObject $post |
||
| 305 | * @param bool $isDeleteOne |
||
| 306 | * @param bool $force |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function delete($post, $isDeleteOne = true, $force = false) |
||
| 310 | { |
||
| 311 | $retVal = false; |
||
| 312 | if (($post instanceof Post) && ($post->getVar('post_id') > 0)) { |
||
| 313 | if ($isDeleteOne) { |
||
| 314 | if ($post->isTopic()) { |
||
| 315 | $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id'))); |
||
| 316 | $criteria->add(new \Criteria('approved', 1)); |
||
| 317 | $criteria->add(new \Criteria('pid', 0, '>')); |
||
| 318 | if (!($this->getPostCount($criteria) > 0)) { |
||
| 319 | $retVal = $this->_delete($post, $force); |
||
| 320 | } |
||
| 321 | } else { |
||
| 322 | $retVal = $this->_delete($post, $force); |
||
| 323 | } |
||
| 324 | } else { // want to delete multiple posts |
||
| 325 | //@TODO: test replacement of XoopsTree with XoopsObjectTree |
||
| 326 | require_once $GLOBALS['xoops']->path('class/tree.php'); |
||
| 327 | // get tree with this object as the root |
||
| 328 | $myObjTree = new \XoopsObjectTree($this->getAll(), 'post_id', 'pid', $post->getVar('post_id')); |
||
| 329 | $arr = $myObjtree->getAllChild(); // get all children of this object |
||
| 330 | /* |
||
| 331 | require_once $GLOBALS['xoops']->path("class/xoopstree.php"); |
||
| 332 | $mytree = new \XoopsTree($this->db->prefix("bb_posts"), "post_id", "pid"); |
||
| 333 | $arr = $mytree->getAllChild($post->getVar('post_id')); |
||
| 334 | */ |
||
| 335 | // irmtfan - delete children in a reverse order |
||
| 336 | $success = true; |
||
| 337 | for ($i = \count($arr) - 1; $i >= 0; $i--) { |
||
| 338 | $childpost = $this->create(false); |
||
| 339 | $childpost->assignVars($arr[$i]); |
||
| 340 | $thisSuccess = $this->_delete($childpost, $force); |
||
| 341 | $success = $success && $thisSuccess; |
||
| 342 | unset($childpost); |
||
| 343 | } |
||
| 344 | if ($success) { |
||
| 345 | // if we successfully deleted all children then try and delete this post |
||
| 346 | $retVal = $this->_delete($post, $force); |
||
| 347 | } else { |
||
| 348 | // did not successfully delete all children so don't delete this post |
||
| 349 | $retVal = false; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | return $retVal; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param $post |
||
| 359 | * @param bool $force |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | private function _delete($post, $force = false) |
||
| 363 | { |
||
| 364 | if ((!$post instanceof Post) || (0 === $post->getVar('post_id'))) { |
||
| 365 | return false; |
||
| 366 | } |
||
| 367 | |||
| 368 | /* Set active post as deleted */ |
||
| 369 | if (empty($force) && ($post->getVar('approved') > 0)) { |
||
| 370 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id'); |
||
| 371 | if (!$result = $this->db->queryF($sql)) { |
||
| 372 | //@TODO: add error check here |
||
| 373 | } |
||
| 374 | } else { /* delete pending post directly */ |
||
| 375 | $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts'), $post->getVar('post_id')); |
||
| 376 | if (!$result = $this->db->queryF($sql)) { |
||
| 377 | $post->setErrors('delte post error: ' . $sql); |
||
| 378 | |||
| 379 | return false; |
||
| 380 | } |
||
| 381 | $post->deleteAttachment(); |
||
| 382 | |||
| 383 | $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts_text'), $post->getVar('post_id')); |
||
| 384 | if (!$result = $this->db->queryF($sql)) { |
||
| 385 | $post->setErrors('Could not remove post text: ' . $sql); |
||
| 386 | |||
| 387 | return false; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($post->isTopic()) { |
||
| 392 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 393 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 394 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 395 | if (empty($force) && \is_object($topic_obj) && $topic_obj->getVar('approved') > 0) { |
||
| 396 | $topiccount_toupdate = 1; |
||
| 397 | $topic_obj->setVar('approved', -1); |
||
| 398 | $topicHandler->insert($topic_obj); |
||
| 399 | \xoops_notification_deletebyitem($GLOBALS['xoopsModule']->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 400 | } else { |
||
| 401 | if (\is_object($topic_obj)) { |
||
| 402 | if ($topic_obj->getVar('approved') > 0) { |
||
| 403 | \xoops_notification_deletebyitem($GLOBALS['xoopsModule']->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 404 | } |
||
| 405 | |||
| 406 | $poll_id = $topic_obj->getVar('poll_id'); |
||
| 407 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 408 | $moduleHandler = \xoops_getHandler('module'); |
||
| 409 | if ($poll_id > 0) { |
||
| 410 | $poll_moduleHandler = $moduleHandler->getByDirname('xoopspoll'); |
||
| 411 | if (($poll_moduleHandler instanceof \XoopsModuleHandler) && $poll_moduleHandler->isactive()) { |
||
| 412 | $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll'); |
||
| 413 | if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) { |
||
| 414 | $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option'); |
||
| 415 | $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 416 | $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log'); |
||
| 417 | $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 418 | \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id); |
||
| 419 | } |
||
| 420 | } else { |
||
| 421 | $poll_moduleHandler = $moduleHandler->getByDirname('umfrage'); |
||
| 422 | if (($poll_moduleHandler instanceof \XoopsModuleHandler) |
||
| 423 | && $poll_moduleHandler->isactive()) { |
||
| 424 | require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrage.php'); |
||
| 425 | require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrageoption.php'); |
||
| 426 | require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragelog.php'); |
||
| 427 | require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragerenderer.php'); |
||
| 428 | |||
| 429 | $poll = new \Umfrage($poll_id); |
||
| 430 | if (false !== $poll->delete()) { |
||
| 431 | \UmfrageOption::deleteByPollId($poll_id); |
||
| 432 | \UmfrageLog::deleteByPollId($poll_id); |
||
| 433 | \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_topics'), $post->getVar('topic_id')); |
||
| 441 | if (!$result = $this->db->queryF($sql)) { |
||
| 442 | // xoops_error($this->db->error()); |
||
| 443 | } |
||
| 444 | $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_votedata'), $post->getVar('topic_id')); |
||
| 445 | if (!$result = $this->db->queryF($sql)) { |
||
| 446 | // xoops_error($this->db->error()); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | } else { |
||
| 450 | $sql = 'UPDATE ' |
||
| 451 | . $this->db->prefix('bb_topics') |
||
| 452 | . ' t ' |
||
| 453 | . 'LEFT JOIN ' |
||
| 454 | . $this->db->prefix('bb_posts') |
||
| 455 | . ' p ON p.topic_id = t.topic_id ' |
||
| 456 | . 'SET t.topic_last_post_id = p.post_id ' |
||
| 457 | . 'WHERE t.topic_last_post_id = ' |
||
| 458 | . $post->getVar('post_id') |
||
| 459 | . ' ' |
||
| 460 | . 'AND p.post_id = (SELECT MAX(post_id) FROM ' |
||
| 461 | . $this->db->prefix('bb_posts') |
||
| 462 | . ' ' |
||
| 463 | . 'WHERE topic_id=t.topic_id)'; |
||
| 464 | if (!$result = $this->db->queryF($sql)) { |
||
| 465 | //@TODO: add error checking here |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | $postcount_toupdate = $post->getVar('approved'); |
||
| 470 | |||
| 471 | if ($postcount_toupdate > 0) { |
||
| 472 | // Update user stats |
||
| 473 | if ($post->getVar('uid') > 0) { |
||
| 474 | $memberHandler = \xoops_getHandler('member'); |
||
| 475 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 476 | if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) { |
||
| 477 | $poster->setVar('posts', $poster->getVar('posts') - 1); |
||
| 478 | $res = $memberHandler->insertUser($poster, true); |
||
| 479 | unset($poster); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | // irmtfan - just update the pid for approved posts when the post is not topic (pid=0) |
||
| 483 | if (!$post->isTopic()) { |
||
| 484 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id'); |
||
| 485 | if (!$result = $this->db->queryF($sql)) { |
||
| 486 | // xoops_error($this->db->error()); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | return true; |
||
| 492 | } |
||
| 493 | |||
| 494 | // START irmtfan enhance getPostCount when there is join (read_mode = 2) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param null $criteria |
||
| 498 | * @param null $join |
||
| 499 | * @return int|null |
||
| 500 | */ |
||
| 501 | public function getPostCount($criteria = null, $join = null) |
||
| 523 | } |
||
| 524 | |||
| 525 | // END irmtfan enhance getPostCount when there is join (read_mode = 2) |
||
| 526 | |||
| 527 | /* |
||
| 528 | *@TODO: combining viewtopic.php |
||
| 529 | */ |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param null $criteria |
||
| 533 | * @param int $limit |
||
| 534 | * @param int $start |
||
| 535 | * @param null $join |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | public function &getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null) |
||
| 539 | { |
||
| 540 | $ret = []; |
||
| 541 | $sql = 'SELECT p.*, t.* ' . 'FROM ' . $this->db->prefix('bb_posts') . ' AS p ' . 'LEFT JOIN ' . $this->db->prefix('bb_posts_text') . ' AS t ON t.post_id = p.post_id'; |
||
| 542 | if (!empty($join)) { |
||
| 543 | $sql .= (0 === mb_strpos($join, ' ')) ? $join : ' ' . $join; |
||
| 544 | } |
||
| 545 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
| 546 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 547 | if ('' !== $criteria->getSort()) { |
||
| 548 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 549 | } |
||
| 550 | } |
||
| 551 | $result = $this->db->query($sql, (int)$limit, (int)$start); |
||
| 552 | if (!$result) { |
||
| 553 | // xoops_error($this->db->error()); |
||
| 554 | return $ret; |
||
| 555 | } |
||
| 556 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 557 | $post = $this->create(false); |
||
| 558 | $post->assignVars($myrow); |
||
| 559 | $ret[$myrow['post_id']] = $post; |
||
| 560 | unset($post); |
||
| 561 | } |
||
| 562 | |||
| 563 | return $ret; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function synchronization() |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * clean orphan items from database |
||
| 577 | * |
||
| 578 | * @return bool true on success |
||
| 579 | */ |
||
| 580 | public function cleanOrphan() |
||
| 581 | { |
||
| 582 | $this->deleteAll(new \Criteria('post_time', 0), true, true); |
||
| 583 | parent::cleanOrphan($this->db->prefix('bb_topics'), 'topic_id'); |
||
| 584 | parent::cleanOrphan($this->db->prefix('bb_posts_text'), 'post_id'); |
||
| 585 | |||
| 586 | if ($this->mysql_major_version() >= 4) { /* for MySQL 4.1+ */ |
||
| 587 | $sql = 'DELETE FROM ' . $this->db->prefix('bb_posts_text') . ' ' . 'WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )'; |
||
| 588 | } else { /* for 4.0+ */ |
||
| 589 | /* */ |
||
| 590 | $sql = 'DELETE ' . $this->db->prefix('bb_posts_text') . ' FROM ' . $this->db->prefix('bb_posts_text') . ' ' . 'LEFT JOIN ' . $this->table . ' AS aa ON ' . $this->db->prefix('bb_posts_text') . '.post_id = aa.post_id ' . ' ' . 'WHERE (aa.post_id IS NULL)'; |
||
| 591 | |||
| 592 | // Alternative for 4.1+ |
||
| 593 | /* |
||
| 594 | $sql = "DELETE bb FROM ".$this->db->prefix("bb_posts_text")." AS bb" . " " |
||
| 595 | . "LEFT JOIN ".$this->table." AS aa ON bb.post_id = aa.post_id " . " " |
||
| 596 | . "WHERE (aa.post_id IS NULL)"; |
||
| 597 | */ |
||
| 598 | } |
||
| 599 | if (!$result = $this->db->queryF($sql)) { |
||
| 600 | // xoops_error($this->db->error()); |
||
| 601 | return false; |
||
| 602 | } |
||
| 603 | |||
| 604 | return true; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * clean expired objects from database |
||
| 609 | * |
||
| 610 | * @param int $expire time limit for expiration |
||
| 611 | * @return bool true on success |
||
| 612 | */ |
||
| 613 | public function cleanExpires($expire = 0) |
||
| 630 | } |
||
| 631 | } |
||
| 632 |