| Total Complexity | 84 |
| Total Lines | 501 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 579 | class PostHandler extends \XoopsPersistableObjectHandler |
||
| 580 | { |
||
| 581 | /** |
||
| 582 | * @param null|\XoopsDatabase $db |
||
| 583 | */ |
||
| 584 | public function __construct(\XoopsDatabase $db = null) |
||
| 585 | { |
||
| 586 | parent::__construct($db, 'bb_posts', 'Post', 'post_id', 'subject'); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param mixed|null $id |
||
| 591 | * @param null $fields |
||
| 592 | * @return null|\XoopsObject |
||
| 593 | */ |
||
| 594 | public function get($id = null, $fields = null) |
||
| 595 | { |
||
| 596 | $id = (int)$id; |
||
| 597 | $post = null; |
||
| 598 | $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; |
||
| 599 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
| 600 | if ($array) { |
||
| 601 | $post = $this->create(false); |
||
| 602 | $post->assignVars($array); |
||
| 603 | } |
||
| 604 | |||
| 605 | return $post; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param int $topic_id |
||
| 610 | * @param int $limit |
||
| 611 | * @param int $approved |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | public function &getByLimit($topic_id, $limit, $approved = 1) |
||
| 638 | } |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param $post |
||
| 642 | * @return mixed |
||
| 643 | */ |
||
| 644 | public function getPostForPDF($post) |
||
| 645 | { |
||
| 646 | return $post->getPostBody(true); |
||
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param $post |
||
| 651 | * @return mixed |
||
| 652 | */ |
||
| 653 | public function getPostForPrint($post) |
||
| 654 | { |
||
| 655 | return $post->getPostBody(); |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param $post |
||
| 660 | * @param bool $force |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function approve(&$post, $force = false) |
||
| 664 | { |
||
| 665 | if (empty($post)) { |
||
| 666 | return false; |
||
| 667 | } |
||
| 668 | if (is_numeric($post)) { |
||
| 669 | $post = $this->get($post); |
||
| 670 | } else { |
||
| 671 | $post->unsetNew(); |
||
| 672 | } |
||
| 673 | $post_id = $post->getVar('post_id'); |
||
| 674 | $wasApproved = $post->getVar('approved'); |
||
| 675 | if (empty($force) && $wasApproved) { |
||
| 676 | return true; |
||
| 677 | } |
||
| 678 | $post->setVar('approved', 1); |
||
| 679 | $this->insert($post, true); |
||
| 680 | |||
| 681 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 682 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 683 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 684 | if ($topic_obj->getVar('topic_last_post_id') < $post->getVar('post_id')) { |
||
| 685 | $topic_obj->setVar('topic_last_post_id', $post->getVar('post_id')); |
||
| 686 | } |
||
| 687 | if ($post->isTopic()) { |
||
| 688 | $topic_obj->setVar('approved', 1); |
||
| 689 | } else { |
||
| 690 | $topic_obj->setVar('topic_replies', $topic_obj->getVar('topic_replies') + 1); |
||
| 691 | } |
||
| 692 | $topicHandler->insert($topic_obj, true); |
||
| 693 | |||
| 694 | /** @var Newbb\ForumHandler $forumHandler */ |
||
| 695 | $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum'); |
||
| 696 | $forum_obj = $forumHandler->get($post->getVar('forum_id')); |
||
| 697 | if ($forum_obj->getVar('forum_last_post_id') < $post->getVar('post_id')) { |
||
| 698 | $forum_obj->setVar('forum_last_post_id', $post->getVar('post_id')); |
||
| 699 | } |
||
| 700 | $forum_obj->setVar('forum_posts', $forum_obj->getVar('forum_posts') + 1); |
||
| 701 | if ($post->isTopic()) { |
||
| 702 | $forum_obj->setVar('forum_topics', $forum_obj->getVar('forum_topics') + 1); |
||
| 703 | } |
||
| 704 | $forumHandler->insert($forum_obj, true); |
||
| 705 | |||
| 706 | // Update user stats |
||
| 707 | if ($post->getVar('uid') > 0) { |
||
| 708 | $memberHandler = xoops_getHandler('member'); |
||
| 709 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 710 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
| 711 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
| 712 | $res = $memberHandler->insertUser($poster, true); |
||
| 713 | unset($poster); |
||
| 714 | } |
||
| 715 | } |
||
| 716 | |||
| 717 | // Update forum stats |
||
| 718 | $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats'); |
||
| 719 | $statsHandler->update($post->getVar('forum_id'), 'post'); |
||
| 720 | if ($post->isTopic()) { |
||
| 721 | $statsHandler->update($post->getVar('forum_id'), 'topic'); |
||
| 722 | } |
||
| 723 | |||
| 724 | return true; |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @param \XoopsObject $post |
||
| 729 | * @param bool $force |
||
| 730 | * @return bool |
||
| 731 | */ |
||
| 732 | public function insert(\XoopsObject $post, $force = true) |
||
| 733 | { |
||
| 734 | global $xoopsUser; |
||
| 735 | |||
| 736 | // Set the post time |
||
| 737 | // The time should be "publish" time. To be adjusted later |
||
| 738 | if (!$post->getVar('post_time')) { |
||
| 739 | $post->setVar('post_time', time()); |
||
| 740 | } |
||
| 741 | |||
| 742 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 743 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 744 | // Verify the topic ID |
||
| 745 | $topic_id = $post->getVar('topic_id'); |
||
| 746 | if ($topic_id) { |
||
| 747 | $topic_obj = $topicHandler->get($topic_id); |
||
| 748 | // Invalid topic OR the topic is no approved and the post is not top post |
||
| 749 | if (!$topic_obj// || (!$post->isTopic() && $topic_obj->getVar("approved") < 1) |
||
| 750 | ) { |
||
| 751 | return false; |
||
| 752 | } |
||
| 753 | } |
||
| 754 | if (empty($topic_id)) { |
||
| 755 | $post->setVar('topic_id', 0); |
||
| 756 | $post->setVar('pid', 0); |
||
| 757 | $post->setNew(); |
||
| 758 | $topic_obj = $topicHandler->create(); |
||
| 759 | } |
||
| 760 | $textHandler = Newbb\Helper::getInstance()->getHandler('Text'); |
||
| 761 | $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr']; |
||
| 762 | if ($post->isNew()) { |
||
| 763 | if (!$topic_id = $post->getVar('topic_id')) { |
||
| 764 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 765 | $topic_obj->setVar('topic_poster', $post->getVar('uid')); |
||
| 766 | $topic_obj->setVar('forum_id', $post->getVar('forum_id')); |
||
| 767 | $topic_obj->setVar('topic_time', $post->getVar('post_time')); |
||
| 768 | $topic_obj->setVar('poster_name', $post->getVar('poster_name'), true); |
||
| 769 | $topic_obj->setVar('approved', $post->getVar('approved'), true); |
||
| 770 | if (!$topic_id = $topicHandler->insert($topic_obj, $force)) { |
||
| 771 | $post->deleteAttachment(); |
||
| 772 | $post->setErrors('insert topic error'); |
||
| 773 | |||
| 774 | return false; |
||
| 775 | } |
||
| 776 | $post->setVar('topic_id', $topic_id); |
||
| 777 | |||
| 778 | $pid = 0; |
||
| 779 | $post->setVar('pid', 0); |
||
| 780 | } elseif (!$post->getVar('pid')) { |
||
| 781 | $pid = $topicHandler->getTopPostId($topic_id); |
||
| 782 | $post->setVar('pid', $pid); |
||
| 783 | } |
||
| 784 | |||
| 785 | $text_obj = $textHandler->create(); |
||
| 786 | foreach ($post_text_vars as $key) { |
||
| 787 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 788 | } |
||
| 789 | $post->destroyVars($post_text_vars); |
||
| 790 | if (!$post_id = parent::insert($post, $force)) { |
||
| 791 | return false; |
||
| 792 | } |
||
| 793 | $text_obj->setVar('post_id', $post_id); |
||
| 794 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 795 | $this->delete($post); |
||
| 796 | $post->setErrors('post text insert error'); |
||
| 797 | |||
| 798 | return false; |
||
| 799 | } |
||
| 800 | if ($post->getVar('approved') > 0) { |
||
| 801 | $this->approve($post, true); |
||
| 802 | } |
||
| 803 | $post->setVar('post_id', $post_id); |
||
| 804 | } else { |
||
| 805 | if ($post->isTopic()) { |
||
| 806 | if ($post->getVar('subject') != $topic_obj->getVar('topic_title')) { |
||
| 807 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 808 | } |
||
| 809 | if ($post->getVar('approved') != $topic_obj->getVar('approved')) { |
||
| 810 | $topic_obj->setVar('approved', $post->getVar('approved')); |
||
| 811 | } |
||
| 812 | if (!$result = $topicHandler->insert($topic_obj, $force)) { |
||
| 813 | $post->setErrors('update topic error'); |
||
| 814 | |||
| 815 | return false; |
||
| 816 | } |
||
| 817 | } |
||
| 818 | $text_obj = $textHandler->get($post->getVar('post_id')); |
||
| 819 | $text_obj->setDirty(); |
||
| 820 | foreach ($post_text_vars as $key) { |
||
| 821 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 822 | } |
||
| 823 | $post->destroyVars($post_text_vars); |
||
| 824 | if (!$post_id = parent::insert($post, $force)) { |
||
| 825 | return false; |
||
| 826 | } |
||
| 827 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 828 | $post->setErrors('update post text error'); |
||
| 829 | |||
| 830 | return false; |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | return $post->getVar('post_id'); |
||
| 835 | } |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @param \XoopsObject $post |
||
| 839 | * @param bool $isDeleteOne |
||
| 840 | * @param bool $force |
||
| 841 | * @return bool |
||
| 842 | */ |
||
| 843 | public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false) |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * @param $post |
||
| 876 | * @param bool $force |
||
| 877 | * @return bool |
||
| 878 | */ |
||
| 879 | public function _delete($post, $force = false) |
||
| 880 | { |
||
| 881 | global $xoopsModule; |
||
| 882 | |||
| 883 | if (!is_object($post) || 0 == $post->getVar('post_id')) { |
||
| 884 | return false; |
||
| 885 | } |
||
| 886 | |||
| 887 | /* Set active post as deleted */ |
||
| 888 | if ($post->getVar('approved') > 0 && empty($force)) { |
||
| 889 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id'); |
||
| 890 | if (!$result = $this->db->queryF($sql)) { |
||
| 891 | } |
||
| 892 | /* delete pending post directly */ |
||
| 893 | } else { |
||
| 894 | $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts'), $post->getVar('post_id')); |
||
| 895 | if (!$result = $this->db->queryF($sql)) { |
||
| 896 | $post->setErrors('delte post error: ' . $sql); |
||
| 897 | |||
| 898 | return false; |
||
| 899 | } |
||
| 900 | $post->deleteAttachment(); |
||
| 901 | |||
| 902 | $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts_text'), $post->getVar('post_id')); |
||
| 903 | if (!$result = $this->db->queryF($sql)) { |
||
| 904 | $post->setErrors('Could not remove post text: ' . $sql); |
||
| 905 | |||
| 906 | return false; |
||
| 907 | } |
||
| 908 | } |
||
| 909 | |||
| 910 | if ($post->isTopic()) { |
||
| 911 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 912 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 913 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 914 | if ($topic_obj instanceof Newbb\Topic) { |
||
| 915 | if (($topic_obj->getVar('approved') > 0) && empty($force)) { |
||
| 916 | $topiccount_toupdate = 1; |
||
| 917 | $topic_obj->setVar('approved', -1); |
||
| 918 | $topicHandler->insert($topic_obj); |
||
| 919 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 920 | } else { |
||
| 921 | if ($topic_obj->getVar('approved') > 0) { |
||
| 922 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 923 | } |
||
| 924 | $poll_id = $topic_obj->getVar('poll_id'); |
||
| 925 | if ($poll_id > 0) { |
||
| 926 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 927 | $moduleHandler = xoops_getHandler('module'); |
||
| 928 | $poll_moduleHandler = $moduleHandler->getByDirname('xoopspoll'); |
||
| 929 | if (($poll_moduleHandler instanceof \XoopsModuleHandler) && $poll_moduleHandler->isactive()) { |
||
| 930 | $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll'); |
||
| 931 | if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) { |
||
| 932 | $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option'); |
||
| 933 | $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 934 | $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log'); |
||
| 935 | $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 936 | xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id); |
||
| 937 | } |
||
| 938 | } |
||
| 939 | } |
||
| 940 | } |
||
| 941 | |||
| 942 | $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_topics'), $post->getVar('topic_id')); |
||
| 943 | if (!$result = $this->db->queryF($sql)) { |
||
| 944 | //xoops_error($this->db->error()); |
||
| 945 | } |
||
| 946 | $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_votedata'), $post->getVar('topic_id')); |
||
| 947 | if (!$result = $this->db->queryF($sql)) { |
||
| 948 | //xoops_error($this->db->error()); |
||
| 949 | } |
||
| 950 | } |
||
| 951 | } else { |
||
| 952 | $sql = 'UPDATE ' . $this->db->prefix('bb_topics') . ' t |
||
| 953 | LEFT JOIN ' . $this->db->prefix('bb_posts') . ' p ON p.topic_id = t.topic_id |
||
| 954 | SET t.topic_last_post_id = p.post_id |
||
| 955 | WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . ' |
||
| 956 | AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('bb_posts') . ' WHERE topic_id=t.topic_id)'; |
||
| 957 | if (!$result = $this->db->queryF($sql)) { |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | $postcount_toupdate = $post->getVar('approved'); |
||
| 962 | |||
| 963 | if ($postcount_toupdate > 0) { |
||
| 964 | // Update user stats |
||
| 965 | if ($post->getVar('uid') > 0) { |
||
| 966 | $memberHandler = xoops_getHandler('member'); |
||
| 967 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 968 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
| 969 | $poster->setVar('posts', $poster->getVar('posts') - 1); |
||
| 970 | $res = $memberHandler->insertUser($poster, true); |
||
| 971 | unset($poster); |
||
| 972 | } |
||
| 973 | } |
||
| 974 | |||
| 975 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE pid=' . $post->getVar('post_id'); |
||
| 976 | if (!$result = $this->db->queryF($sql)) { |
||
| 977 | //xoops_error($this->db->error()); |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | return true; |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * @param null $criteria |
||
| 986 | * @return int |
||
| 987 | */ |
||
| 988 | public function getPostCount($criteria = null) |
||
| 989 | { |
||
| 990 | return parent::getCount($criteria); |
||
| 991 | } |
||
| 992 | |||
| 993 | /* |
||
| 994 | * TODO: combining viewtopic.php |
||
| 995 | */ |
||
| 996 | |||
| 997 | /** |
||
| 998 | * @param null $criteria |
||
| 999 | * @param int $limit |
||
| 1000 | * @param int $start |
||
| 1001 | * @param null $join |
||
| 1002 | * @return array |
||
| 1003 | */ |
||
| 1004 | public function &getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null) |
||
| 1005 | { |
||
| 1006 | $ret = []; |
||
| 1007 | $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'; |
||
| 1008 | if (!empty($join)) { |
||
| 1009 | $sql .= $join; |
||
| 1010 | } |
||
| 1011 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
| 1012 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 1013 | if ('' != $criteria->getSort()) { |
||
| 1014 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 1015 | } |
||
| 1016 | } |
||
| 1017 | $result = $this->db->query($sql, (int)$limit, (int)$start); |
||
| 1018 | if (!$result) { |
||
| 1019 | //xoops_error($this->db->error()); |
||
| 1020 | return $ret; |
||
| 1021 | } |
||
| 1022 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 1023 | $post = $this->create(false); |
||
| 1024 | $post->assignVars($myrow); |
||
| 1025 | $ret[$myrow['post_id']] = $post; |
||
| 1026 | unset($post); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | return $ret; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * clean orphan items from database |
||
| 1034 | * |
||
| 1035 | * @return bool true on success |
||
| 1036 | */ |
||
| 1037 | public function cleanOrphan() |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * clean expired objects from database |
||
| 1068 | * |
||
| 1069 | * @param int $expire time limit for expiration |
||
| 1070 | * @return bool true on success |
||
| 1071 | */ |
||
| 1072 | public function cleanExpires($expire = 0) |
||
| 1080 | } |
||
| 1081 | } |
||
| 1082 |