| Total Complexity | 94 |
| Total Lines | 574 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TopicHandler 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 TopicHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class TopicHandler extends \XoopsPersistableObjectHandler |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @param \XoopsDatabase|null $db |
||
| 29 | */ |
||
| 30 | public function __construct(\XoopsDatabase $db = null) |
||
| 31 | { |
||
| 32 | parent::__construct($db, 'newbb_topics', Topic::class, 'topic_id', 'topic_title'); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param mixed $id |
||
| 37 | * @param null|array $fields |
||
| 38 | * @return mixed|null |
||
| 39 | */ |
||
| 40 | public function get($id = null, $fields = null) //get($id, $var = null) |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param \XoopsObject $object |
||
| 61 | * @param bool $force |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | public function insert(\XoopsObject $object, $force = true) |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $object |
||
| 87 | * @param bool $force |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function approve($object, $force = false) |
||
| 91 | { |
||
| 92 | $topic_id = $object->getVar('topic_id'); |
||
| 93 | if ($force) { |
||
| 94 | $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . " SET approved = -1 WHERE topic_id = {$topic_id}"; |
||
| 95 | } else { |
||
| 96 | $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . " SET approved = 1 WHERE topic_id = {$topic_id}"; |
||
| 97 | } |
||
| 98 | if (!$result = $this->db->queryF($sql)) { |
||
| 99 | //xoops_error($this->db->error()); |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 103 | $postsObject = $postHandler->getAll(new \Criteria('topic_id', $topic_id)); |
||
| 104 | foreach (\array_keys($postsObject) as $post_id) { |
||
| 105 | $postHandler->approve($postsObject[$post_id]); |
||
| 106 | } |
||
| 107 | unset($postsObject); |
||
| 108 | $statsHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Stats'); |
||
| 109 | $statsHandler->update($object->getVar('forum_id'), 'topic'); |
||
| 110 | |||
| 111 | return true; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * get previous/next topic |
||
| 116 | * |
||
| 117 | * @param int $topic_id current topic ID |
||
| 118 | * @param int $action |
||
| 119 | * <ul> |
||
| 120 | * <li> -1: previous </li> |
||
| 121 | * <li> 0: current </li> |
||
| 122 | * <li> 1: next </li> |
||
| 123 | * </ul> |
||
| 124 | * @param int $forum_id the scope for moving |
||
| 125 | * <ul> |
||
| 126 | * <li> >0 : inside the forum </li> |
||
| 127 | * <li> <= 0: global </li> |
||
| 128 | * </ul> |
||
| 129 | * @access public |
||
| 130 | * @return mixed|null|\XoopsObject |
||
| 131 | */ |
||
| 132 | public function &getByMove($topic_id, $action, $forum_id = 0) |
||
| 133 | { |
||
| 134 | $topic = null; |
||
| 135 | if (!empty($action)) { |
||
| 136 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE 1=1' . (($forum_id > 0) ? ' AND forum_id=' . (int)$forum_id : '') . ' AND topic_id ' . (($action > 0) ? '>' : '<') . (int)$topic_id . ' ORDER BY topic_id ' . (($action > 0) ? 'ASC' : 'DESC') . ' LIMIT 1'; |
||
| 137 | $result = $this->db->query($sql); |
||
| 138 | if ($result) { |
||
| 139 | $row = $this->db->fetchArray($result); |
||
| 140 | if ($row) { |
||
| 141 | $topic = $this->create(false); |
||
| 142 | $topic->assignVars($row); |
||
| 143 | |||
| 144 | return $topic; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | $topic = $this->get($topic_id); |
||
| 149 | |||
| 150 | return $topic; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $post_id |
||
| 155 | * @return null|\XoopsObject |
||
| 156 | */ |
||
| 157 | public function &getByPost($post_id) |
||
| 158 | { |
||
| 159 | $topic = null; |
||
| 160 | $sql = 'SELECT t.* FROM ' . $this->db->prefix('newbb_topics') . ' t, ' . $this->db->prefix('newbb_posts') . ' p |
||
| 161 | WHERE t.topic_id = p.topic_id AND p.post_id = ' . (int)$post_id; |
||
| 162 | $result = $this->db->query($sql); |
||
| 163 | if (!$result) { |
||
| 164 | //xoops_error($this->db->error()); |
||
| 165 | return $topic; |
||
| 166 | } |
||
| 167 | $row = $this->db->fetchArray($result); |
||
| 168 | $topic = $this->create(false); |
||
| 169 | $topic->assignVars($row); |
||
| 170 | |||
| 171 | return $topic; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param Topic $topic |
||
| 176 | * @param string $type |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getPostCount($topic, $type = '') |
||
| 180 | { |
||
| 181 | switch ($type) { |
||
| 182 | case 'pending': |
||
| 183 | $approved = 0; |
||
| 184 | break; |
||
| 185 | case 'deleted': |
||
| 186 | $approved = -1; |
||
| 187 | break; |
||
| 188 | default: |
||
| 189 | $approved = 1; |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | $criteria = new \CriteriaCompo(new \Criteria('topic_id', $topic->getVar('topic_id'))); |
||
| 193 | $criteria->add(new \Criteria('approved', $approved)); |
||
| 194 | /** @var Newbb\PostHandler $postHandler */ |
||
| 195 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 196 | $count = $postHandler->getCount($criteria); |
||
| 197 | |||
| 198 | return $count; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param $topic_id |
||
| 203 | * @return null|Newbb\Post |
||
| 204 | */ |
||
| 205 | public function &getTopPost($topic_id) |
||
| 206 | { |
||
| 207 | $post = null; |
||
| 208 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p, |
||
| 209 | ' . $this->db->prefix('newbb_posts_text') . ' t |
||
| 210 | WHERE |
||
| 211 | p.topic_id = ' . $topic_id . ' AND p.pid = 0 |
||
| 212 | AND t.post_id = p.post_id'; |
||
| 213 | |||
| 214 | $result = $this->db->query($sql); |
||
| 215 | if (!$result) { |
||
| 216 | //xoops_error($this->db->error()); |
||
| 217 | return $post; |
||
| 218 | } |
||
| 219 | /** @var Newbb\PostHandler $postHandler */ |
||
| 220 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 221 | $myrow = $this->db->fetchArray($result); |
||
| 222 | /** @var Newbb\Post $post */ |
||
| 223 | $post = $postHandler->create(false); |
||
| 224 | $post->assignVars($myrow); |
||
| 225 | |||
| 226 | return $post; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param $topic_id |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function getTopPostId($topic_id) |
||
| 234 | { |
||
| 235 | $sql = 'SELECT MIN(post_id) AS post_id FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id = ' . $topic_id . ' AND pid = 0'; |
||
| 236 | $result = $this->db->query($sql); |
||
| 237 | if (!$result) { |
||
| 238 | //xoops_error($this->db->error()); |
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | [$post_id] = $this->db->fetchRow($result); |
||
| 242 | |||
| 243 | return $post_id; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param $topic |
||
| 248 | * @param string $order |
||
| 249 | * @param int $perpage |
||
| 250 | * @param $start |
||
| 251 | * @param int $post_id |
||
| 252 | * @param string $type |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function &getAllPosts($topic, $order = 'ASC', $perpage = 10, &$start, $post_id = 0, $type = '') |
||
| 256 | { |
||
| 257 | $ret = []; |
||
| 258 | $perpage = ((int)$perpage > 0) ? (int)$perpage : (empty($GLOBALS['xoopsModuleConfig']['posts_per_page']) ? 10 : $GLOBALS['xoopsModuleConfig']['posts_per_page']); |
||
| 259 | $start = (int)$start; |
||
| 260 | switch ($type) { |
||
| 261 | case 'pending': |
||
| 262 | $approveCriteria = ' AND p.approved = 0'; |
||
| 263 | break; |
||
| 264 | case 'deleted': |
||
| 265 | $approveCriteria = ' AND p.approved = -1'; |
||
| 266 | break; |
||
| 267 | default: |
||
| 268 | $approveCriteria = ' AND p.approved = 1'; |
||
| 269 | break; |
||
| 270 | } |
||
| 271 | |||
| 272 | if ($post_id) { |
||
| 273 | if ('DESC' === $order) { |
||
| 274 | $operator_for_position = '>'; |
||
| 275 | } else { |
||
| 276 | $order = 'ASC'; |
||
| 277 | $operator_for_position = '<'; |
||
| 278 | } |
||
| 279 | //$approveCriteria = ' AND approved = 1'; // any others? |
||
| 280 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_posts') . ' AS p WHERE p.topic_id=' . (int)$topic->getVar('topic_id') . $approveCriteria . " AND p.post_id $operator_for_position $post_id"; |
||
| 281 | $result = $this->db->query($sql); |
||
| 282 | if (!$result) { |
||
| 283 | //xoops_error($this->db->error()); |
||
| 284 | return $ret; |
||
| 285 | } |
||
| 286 | [$position] = $this->db->fetchRow($result); |
||
| 287 | $start = (int)($position / $perpage) * $perpage; |
||
| 288 | } |
||
| 289 | |||
| 290 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p, ' . $this->db->prefix('newbb_posts_text') . ' t WHERE p.topic_id=' . $topic->getVar('topic_id') . ' AND p.post_id = t.post_id' . $approveCriteria . " ORDER BY p.post_id $order"; |
||
| 291 | $result = $this->db->query($sql, $perpage, $start); |
||
| 292 | if (!$result) { |
||
| 293 | //xoops_error($this->db->error()); |
||
| 294 | return $ret; |
||
| 295 | } |
||
| 296 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 297 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 298 | $post = $postHandler->create(false); |
||
| 299 | $post->assignVars($myrow); |
||
| 300 | $ret[$myrow['post_id']] = $post; |
||
| 301 | unset($post); |
||
| 302 | } |
||
| 303 | |||
| 304 | return $ret; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param $postArray |
||
| 309 | * @param int $pid |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public function &getPostTree($postArray, $pid = 0) |
||
| 313 | { |
||
| 314 | // require_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php'); |
||
| 315 | $NewBBTree = new Newbb\Tree('newbb_posts'); |
||
| 316 | $NewBBTree->setPrefix(' '); |
||
| 317 | $NewBBTree->setPostArray($postArray); |
||
| 318 | $NewBBTree->getPostTree($postsArray, $pid); |
||
| 319 | |||
| 320 | return $postsArray; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param $topic |
||
| 325 | * @param $postArray |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | public function showTreeItem($topic, &$postArray) |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $topic |
||
| 357 | * @param bool $isApproved |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getAllPosters($topic, $isApproved = true) |
||
| 361 | { |
||
| 362 | $sql = 'SELECT DISTINCT uid FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=' . $topic->getVar('topic_id') . ' AND uid>0'; |
||
| 363 | if ($isApproved) { |
||
| 364 | $sql .= ' AND approved = 1'; |
||
| 365 | } |
||
| 366 | $result = $this->db->query($sql); |
||
| 367 | if (!$result) { |
||
| 368 | //xoops_error($this->db->error()); |
||
| 369 | return []; |
||
| 370 | } |
||
| 371 | $ret = []; |
||
| 372 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 373 | $ret[] = $myrow['uid']; |
||
| 374 | } |
||
| 375 | |||
| 376 | return $ret; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param Topic|\XoopsObject $topic |
||
| 381 | * @param bool $force |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public function delete(\XoopsObject $topic, $force = true) |
||
| 385 | { |
||
| 386 | $topic_id = \is_object($topic) ? $topic->getVar('topic_id') : (int)$topic; |
||
| 387 | if (empty($topic_id)) { |
||
| 388 | return false; |
||
| 389 | } |
||
| 390 | $postObject = $this->getTopPost($topic_id); |
||
| 391 | /** @var Newbb\PostHandler $postHandler */ |
||
| 392 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 393 | $postHandler->delete($postObject, false, $force); |
||
| 394 | |||
| 395 | $newbbConfig = \newbbLoadConfig(); |
||
| 396 | /** @var \XoopsModules\Tag\TagHandler $tagHandler */ |
||
| 397 | if (!empty($newbbConfig['do_tag']) && \class_exists('TagFormTag') && $tagHandler = Tag\Helper::getInstance()->getHandler('Tag')) { //@xoops_getModuleHandler('tag', 'tag', true)) { |
||
| 398 | $tagHandler->updateByItem([], $topic_id, 'newbb'); |
||
| 399 | } |
||
| 400 | |||
| 401 | return true; |
||
| 402 | } |
||
| 403 | |||
| 404 | // get permission |
||
| 405 | // parameter: $type: 'post', 'view', 'reply', 'edit', 'delete', 'addpoll', 'vote', 'attach' |
||
| 406 | // $gperm_names = "'forum_can_post', 'forum_can_view', 'forum_can_reply', 'forum_can_edit', 'forum_can_delete', 'forum_can_addpoll', 'forum_can_vote', 'forum_can_attach', 'forum_can_noapprove'"; |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param Newbb\Forum $forum |
||
| 410 | * @param int $topic_locked |
||
| 411 | * @param string $type |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function getPermission($forum, $topic_locked = 0, $type = 'view') |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * clean orphan items from database |
||
| 440 | * |
||
| 441 | * @param string $table_link |
||
| 442 | * @param string $field_link |
||
| 443 | * @param string $field_object |
||
| 444 | * @return bool true on success |
||
| 445 | */ |
||
| 446 | public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan() |
||
| 447 | { |
||
| 448 | $this->deleteAll(new \Criteria('topic_time', 0), true, true); |
||
| 449 | parent::cleanOrphan($this->db->prefix('newbb_forums'), 'forum_id'); |
||
| 450 | parent::cleanOrphan($this->db->prefix('newbb_posts'), 'topic_id'); |
||
| 451 | |||
| 452 | return true; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * clean expired objects from database |
||
| 457 | * |
||
| 458 | * @param int $expire time limit for expiration |
||
| 459 | * @return bool true on success |
||
| 460 | */ |
||
| 461 | public function cleanExpires($expire = 0) |
||
| 462 | { |
||
| 463 | // irmtfan if 0 no cleanup look include/plugin.php |
||
| 464 | if (!\func_num_args()) { |
||
| 465 | $newbbConfig = \newbbLoadConfig(); |
||
| 466 | $expire = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7; |
||
| 467 | $expire = $expire * 24 * 3600; // days to seconds |
||
| 468 | } |
||
| 469 | if (empty($expire)) { |
||
| 470 | return false; |
||
| 471 | } |
||
| 472 | $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<=')); |
||
| 473 | $crit_expire->add(new \Criteria('topic_time', \time() - (int)$expire, '<')); |
||
| 474 | |||
| 475 | return $this->deleteAll($crit_expire, true/*, true*/); |
||
| 476 | } |
||
| 477 | |||
| 478 | // START irmtfan - rewrite topic synchronization function. add pid sync and remove hard-code db access |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param null $object |
||
| 482 | * @param bool $force |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | public function synchronization($object = null, $force = true) |
||
| 486 | { |
||
| 487 | if (!\is_object($object)) { |
||
| 488 | $object = $this->get((int)$object); |
||
| 489 | } |
||
| 490 | if (!\is_object($object) || !$object->getVar('topic_id')) { |
||
| 491 | return false; |
||
| 492 | } |
||
| 493 | |||
| 494 | /** @var Newbb\PostHandler $postHandler */ |
||
| 495 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
| 496 | $criteria = new \CriteriaCompo(); |
||
| 497 | $criteria->add(new \Criteria('topic_id', $object->getVar('topic_id')), 'AND'); |
||
| 498 | $criteria->add(new \Criteria('approved', 1), 'AND'); |
||
| 499 | $post_ids = $postHandler->getIds($criteria); |
||
| 500 | if (empty($post_ids)) { |
||
| 501 | return false; |
||
| 502 | } |
||
| 503 | $last_post = \max($post_ids); |
||
| 504 | $top_post = \min($post_ids); |
||
| 505 | $topic_replies = \count($post_ids) - 1; |
||
| 506 | if ($object->getVar('topic_last_post_id') != $last_post) { |
||
| 507 | $object->setVar('topic_last_post_id', $last_post); |
||
| 508 | } |
||
| 509 | if ($object->getVar('topic_replies') != $topic_replies) { |
||
| 510 | $object->setVar('topic_replies', $topic_replies); |
||
| 511 | } |
||
| 512 | $b1 = $this->insert($object, $force); |
||
| 513 | $criteria->add(new \Criteria('post_id', $top_post, '<>'), 'AND'); |
||
| 514 | $criteria->add(new \Criteria('pid', '(' . \implode(', ', $post_ids) . ')', 'NOT IN'), 'AND'); |
||
| 515 | $b2 = $postHandler->updateAll('pid', $top_post, $criteria, $force); |
||
| 516 | $criteria = new \CriteriaCompo(); |
||
| 517 | $criteria->add(new \Criteria('post_id', $top_post, '='), 'AND'); |
||
| 518 | $b3 = $postHandler->updateAll('pid', 0, $criteria, $force); |
||
| 519 | |||
| 520 | return ($b1 && $b2 && $b3); |
||
| 521 | } |
||
| 522 | |||
| 523 | // END irmtfan - rewrite topic synchronization function. add pid sync and remove hard-code db access |
||
| 524 | // START irmtfan getActivePolls |
||
| 525 | |||
| 526 | /** |
||
| 527 | * get all active poll modules in the current xoops installtion. |
||
| 528 | * @access public |
||
| 529 | * @return array $pollDirs = array($dirname1=>$dirname1, $dirname2=>$dirname2, ...) dirnames of all active poll modules |
||
| 530 | */ |
||
| 531 | public function getActivePolls() |
||
| 532 | { |
||
| 533 | $pollDirs = []; |
||
| 534 | $allDirs = \xoops_getActiveModules(); |
||
| 535 | foreach ($allDirs as $dirname) { |
||
| 536 | // pollresults.php file is exist in all xoopspoll versions and umfrage versions |
||
| 537 | if (\file_exists($GLOBALS['xoops']->path('modules/' . $dirname . '/pollresults.php'))) { |
||
| 538 | $pollDirs[$dirname] = $dirname; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | return $pollDirs; |
||
| 543 | } |
||
| 544 | |||
| 545 | // END irmtfan getActivePolls |
||
| 546 | |||
| 547 | // START irmtfan findPollModule |
||
| 548 | |||
| 549 | /** |
||
| 550 | * find poll module that is in used in the current newbb installtion. |
||
| 551 | * @access public |
||
| 552 | * @param array $pollDirs dirnames of all active poll modules |
||
| 553 | * @return bool|string $dir_def | true | false |
||
| 554 | * $dir_def: dirname of poll module that is in used in the current newbb installtion. |
||
| 555 | * true: no poll module is installed | newbb has no topic with poll | newbb has no topic |
||
| 556 | * false: errors (see below xoops_errors) |
||
| 557 | */ |
||
| 558 | public function findPollModule(array $pollDirs = []) |
||
| 599 | } |
||
| 600 | |||
| 601 | // END irmtfan findPollModule |
||
| 602 | } |
||
| 603 |