| Total Complexity | 85 |
| Total Lines | 503 |
| 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 declare(strict_types=1); |
||
| 603 | class PostHandler extends \XoopsPersistableObjectHandler |
||
| 604 | { |
||
| 605 | /** |
||
| 606 | * @param null|\XoopsDatabase $db |
||
| 607 | */ |
||
| 608 | public function __construct(\XoopsDatabase $db = null) |
||
| 609 | { |
||
| 610 | parent::__construct($db, 'bb_posts', 'Post', 'post_id', 'subject'); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param mixed|null $id |
||
| 615 | * @param null $fields |
||
| 616 | * @return null|\XoopsObject |
||
| 617 | */ |
||
| 618 | public function get($id = null, $fields = null) |
||
| 619 | { |
||
| 620 | $id = (int)$id; |
||
| 621 | $post = null; |
||
| 622 | $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; |
||
| 623 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
| 624 | if ($array) { |
||
| 625 | $post = $this->create(false); |
||
| 626 | $post->assignVars($array); |
||
| 627 | } |
||
| 628 | |||
| 629 | return $post; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param int $topic_id |
||
| 634 | * @param int $limit |
||
| 635 | * @param int $approved |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | public function &getByLimit($topic_id, $limit, $approved = 1) |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param $post |
||
| 668 | * @return mixed |
||
| 669 | */ |
||
| 670 | public function getPostForPDF($post) |
||
| 671 | { |
||
| 672 | return $post->getPostBody(true); |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param $post |
||
| 677 | * @return mixed |
||
| 678 | */ |
||
| 679 | public function getPostForPrint($post) |
||
| 680 | { |
||
| 681 | return $post->getPostBody(); |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param \XoopsObject $post |
||
| 686 | * @param bool $force |
||
| 687 | * @return bool |
||
| 688 | */ |
||
| 689 | public function approve(&$post, $force = false) |
||
| 690 | { |
||
| 691 | if (empty($post)) { |
||
| 692 | return false; |
||
| 693 | } |
||
| 694 | if (is_numeric($post)) { |
||
| 695 | $post = $this->get($post); |
||
| 696 | } else { |
||
| 697 | $post->unsetNew(); |
||
| 698 | } |
||
| 699 | $post_id = $post->getVar('post_id'); |
||
| 700 | $wasApproved = $post->getVar('approved'); |
||
| 701 | if (empty($force) && $wasApproved) { |
||
| 702 | return true; |
||
| 703 | } |
||
| 704 | $post->setVar('approved', 1); |
||
| 705 | $this->insert($post, true); |
||
| 706 | |||
| 707 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 708 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 709 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 710 | if ($topic_obj->getVar('topic_last_post_id') < $post->getVar('post_id')) { |
||
| 711 | $topic_obj->setVar('topic_last_post_id', $post->getVar('post_id')); |
||
| 712 | } |
||
| 713 | if ($post->isTopic()) { |
||
| 714 | $topic_obj->setVar('approved', 1); |
||
| 715 | } else { |
||
| 716 | $topic_obj->setVar('topic_replies', $topic_obj->getVar('topic_replies') + 1); |
||
| 717 | } |
||
| 718 | $topicHandler->insert($topic_obj, true); |
||
| 719 | |||
| 720 | /** @var Newbb\ForumHandler $forumHandler */ |
||
| 721 | $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum'); |
||
| 722 | $forum_obj = $forumHandler->get($post->getVar('forum_id')); |
||
| 723 | if ($forum_obj->getVar('forum_last_post_id') < $post->getVar('post_id')) { |
||
| 724 | $forum_obj->setVar('forum_last_post_id', $post->getVar('post_id')); |
||
| 725 | } |
||
| 726 | $forum_obj->setVar('forum_posts', $forum_obj->getVar('forum_posts') + 1); |
||
| 727 | if ($post->isTopic()) { |
||
| 728 | $forum_obj->setVar('forum_topics', $forum_obj->getVar('forum_topics') + 1); |
||
| 729 | } |
||
| 730 | $forumHandler->insert($forum_obj, true); |
||
| 731 | |||
| 732 | // Update user stats |
||
| 733 | if ($post->getVar('uid') > 0) { |
||
| 734 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 735 | $memberHandler = xoops_getHandler('member'); |
||
| 736 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 737 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
| 738 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
| 739 | $res = $memberHandler->insertUser($poster, true); |
||
| 740 | unset($poster); |
||
| 741 | } |
||
| 742 | } |
||
| 743 | |||
| 744 | // Update forum stats |
||
| 745 | $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats'); |
||
| 746 | $statsHandler->update($post->getVar('forum_id'), 'post'); |
||
| 747 | if ($post->isTopic()) { |
||
| 748 | $statsHandler->update($post->getVar('forum_id'), 'topic'); |
||
| 749 | } |
||
| 750 | |||
| 751 | return true; |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param \XoopsObject $post |
||
| 756 | * @param bool $force |
||
| 757 | * @return bool |
||
| 758 | */ |
||
| 759 | public function insert(\XoopsObject $post, $force = true) |
||
| 760 | { |
||
| 761 | global $xoopsUser; |
||
| 762 | |||
| 763 | // Set the post time |
||
| 764 | // The time should be "publish" time. To be adjusted later |
||
| 765 | if (!$post->getVar('post_time')) { |
||
| 766 | $post->setVar('post_time', time()); |
||
| 767 | } |
||
| 768 | |||
| 769 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 770 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 771 | // Verify the topic ID |
||
| 772 | $topic_id = $post->getVar('topic_id'); |
||
| 773 | if ($topic_id) { |
||
| 774 | $topic_obj = $topicHandler->get($topic_id); |
||
| 775 | // Invalid topic OR the topic is no approved and the post is not top post |
||
| 776 | if (!$topic_obj// || (!$post->isTopic() && $topic_obj->getVar("approved") < 1) |
||
| 777 | ) { |
||
| 778 | return false; |
||
| 779 | } |
||
| 780 | } |
||
| 781 | if (empty($topic_id)) { |
||
| 782 | $post->setVar('topic_id', 0); |
||
| 783 | $post->setVar('pid', 0); |
||
| 784 | $post->setNew(); |
||
| 785 | $topic_obj = $topicHandler->create(); |
||
| 786 | } |
||
| 787 | $textHandler = Newbb\Helper::getInstance()->getHandler('Text'); |
||
| 788 | $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr']; |
||
| 789 | if ($post->isNew()) { |
||
| 790 | if (!$topic_id = $post->getVar('topic_id')) { |
||
| 791 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 792 | $topic_obj->setVar('topic_poster', $post->getVar('uid')); |
||
| 793 | $topic_obj->setVar('forum_id', $post->getVar('forum_id')); |
||
| 794 | $topic_obj->setVar('topic_time', $post->getVar('post_time')); |
||
| 795 | $topic_obj->setVar('poster_name', $post->getVar('poster_name'), true); |
||
| 796 | $topic_obj->setVar('approved', $post->getVar('approved'), true); |
||
| 797 | if (!$topic_id = $topicHandler->insert($topic_obj, $force)) { |
||
| 798 | $post->deleteAttachment(); |
||
| 799 | $post->setErrors('insert topic error'); |
||
| 800 | |||
| 801 | return false; |
||
| 802 | } |
||
| 803 | $post->setVar('topic_id', $topic_id); |
||
| 804 | |||
| 805 | $pid = 0; |
||
| 806 | $post->setVar('pid', 0); |
||
| 807 | } elseif (!$post->getVar('pid')) { |
||
| 808 | $pid = $topicHandler->getTopPostId($topic_id); |
||
| 809 | $post->setVar('pid', $pid); |
||
| 810 | } |
||
| 811 | |||
| 812 | $text_obj = $textHandler->create(); |
||
| 813 | foreach ($post_text_vars as $key) { |
||
| 814 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 815 | } |
||
| 816 | $post->destroyVars($post_text_vars); |
||
| 817 | if (!$post_id = parent::insert($post, $force)) { |
||
| 818 | return false; |
||
| 819 | } |
||
| 820 | $text_obj->setVar('post_id', $post_id); |
||
| 821 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 822 | $this->delete($post); |
||
| 823 | $post->setErrors('post text insert error'); |
||
| 824 | |||
| 825 | return false; |
||
| 826 | } |
||
| 827 | if ($post->getVar('approved') > 0) { |
||
| 828 | $this->approve($post, true); |
||
| 829 | } |
||
| 830 | $post->setVar('post_id', $post_id); |
||
| 831 | } else { |
||
| 832 | if ($post->isTopic()) { |
||
| 833 | if ($post->getVar('subject') != $topic_obj->getVar('topic_title')) { |
||
| 834 | $topic_obj->setVar('topic_title', $post->getVar('subject', 'n')); |
||
| 835 | } |
||
| 836 | if ($post->getVar('approved') != $topic_obj->getVar('approved')) { |
||
| 837 | $topic_obj->setVar('approved', $post->getVar('approved')); |
||
| 838 | } |
||
| 839 | if (!$result = $topicHandler->insert($topic_obj, $force)) { |
||
| 840 | $post->setErrors('update topic error'); |
||
| 841 | |||
| 842 | return false; |
||
| 843 | } |
||
| 844 | } |
||
| 845 | $text_obj = $textHandler->get($post->getVar('post_id')); |
||
| 846 | $text_obj->setDirty(); |
||
| 847 | foreach ($post_text_vars as $key) { |
||
| 848 | $text_obj->vars[$key] = $post->vars[$key]; |
||
| 849 | } |
||
| 850 | $post->destroyVars($post_text_vars); |
||
| 851 | if (!$post_id = parent::insert($post, $force)) { |
||
| 852 | return false; |
||
| 853 | } |
||
| 854 | if (!$textHandler->insert($text_obj, $force)) { |
||
| 855 | $post->setErrors('update post text error'); |
||
| 856 | |||
| 857 | return false; |
||
| 858 | } |
||
| 859 | } |
||
| 860 | |||
| 861 | return $post->getVar('post_id'); |
||
| 862 | } |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @param \XoopsObject $post |
||
| 866 | * @param bool $isDeleteOne |
||
| 867 | * @param bool $force |
||
| 868 | * @return bool |
||
| 869 | */ |
||
| 870 | public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false) |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @param $post |
||
| 903 | * @param bool $force |
||
| 904 | * @return bool |
||
| 905 | */ |
||
| 906 | public function _delete($post, $force = false) |
||
| 907 | { |
||
| 908 | global $xoopsModule; |
||
| 909 | |||
| 910 | if (!is_object($post) || 0 == $post->getVar('post_id')) { |
||
| 911 | return false; |
||
| 912 | } |
||
| 913 | |||
| 914 | /* Set active post as deleted */ |
||
| 915 | if ($post->getVar('approved') > 0 && empty($force)) { |
||
| 916 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id'); |
||
| 917 | if (!$result = $this->db->queryF($sql)) { |
||
| 918 | } |
||
| 919 | /* delete pending post directly */ |
||
| 920 | } else { |
||
| 921 | $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts'), $post->getVar('post_id')); |
||
| 922 | if (!$result = $this->db->queryF($sql)) { |
||
| 923 | $post->setErrors('delte post error: ' . $sql); |
||
| 924 | |||
| 925 | return false; |
||
| 926 | } |
||
| 927 | $post->deleteAttachment(); |
||
| 928 | |||
| 929 | $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts_text'), $post->getVar('post_id')); |
||
| 930 | if (!$result = $this->db->queryF($sql)) { |
||
| 931 | $post->setErrors('Could not remove post text: ' . $sql); |
||
| 932 | |||
| 933 | return false; |
||
| 934 | } |
||
| 935 | } |
||
| 936 | |||
| 937 | if ($post->isTopic()) { |
||
| 938 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 939 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 940 | $topic_obj = $topicHandler->get($post->getVar('topic_id')); |
||
| 941 | if ($topic_obj instanceof Newbb\Topic) { |
||
| 942 | if (($topic_obj->getVar('approved') > 0) && empty($force)) { |
||
| 943 | $topiccount_toupdate = 1; |
||
| 944 | $topic_obj->setVar('approved', -1); |
||
| 945 | $topicHandler->insert($topic_obj); |
||
| 946 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 947 | } else { |
||
| 948 | if ($topic_obj->getVar('approved') > 0) { |
||
| 949 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id')); |
||
| 950 | } |
||
| 951 | $poll_id = $topic_obj->getVar('poll_id'); |
||
| 952 | if ($poll_id > 0) { |
||
| 953 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 954 | $moduleHandler = xoops_getHandler('module'); |
||
| 955 | $poll_moduleHandler = $moduleHandler->getByDirname('xoopspoll'); |
||
| 956 | if (($poll_moduleHandler instanceof \XoopsModuleHandler) && $poll_moduleHandler->isactive()) { |
||
| 957 | $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll'); |
||
| 958 | if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) { |
||
| 959 | $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option'); |
||
| 960 | $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 961 | $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log'); |
||
| 962 | $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
||
| 963 | xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id); |
||
| 964 | } |
||
| 965 | } |
||
| 966 | } |
||
| 967 | } |
||
| 968 | |||
| 969 | $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_topics'), $post->getVar('topic_id')); |
||
| 970 | if (!$result = $this->db->queryF($sql)) { |
||
| 971 | //xoops_error($this->db->error()); |
||
| 972 | } |
||
| 973 | $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_votedata'), $post->getVar('topic_id')); |
||
| 974 | if (!$result = $this->db->queryF($sql)) { |
||
| 975 | //xoops_error($this->db->error()); |
||
| 976 | } |
||
| 977 | } |
||
| 978 | } else { |
||
| 979 | $sql = 'UPDATE ' . $this->db->prefix('bb_topics') . ' t |
||
| 980 | LEFT JOIN ' . $this->db->prefix('bb_posts') . ' p ON p.topic_id = t.topic_id |
||
| 981 | SET t.topic_last_post_id = p.post_id |
||
| 982 | WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . ' |
||
| 983 | AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('bb_posts') . ' WHERE topic_id=t.topic_id)'; |
||
| 984 | if (!$result = $this->db->queryF($sql)) { |
||
| 985 | } |
||
| 986 | } |
||
| 987 | |||
| 988 | $postcount_toupdate = $post->getVar('approved'); |
||
| 989 | |||
| 990 | if ($postcount_toupdate > 0) { |
||
| 991 | // Update user stats |
||
| 992 | if ($post->getVar('uid') > 0) { |
||
| 993 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 994 | $memberHandler = xoops_getHandler('member'); |
||
| 995 | $poster = $memberHandler->getUser($post->getVar('uid')); |
||
| 996 | if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) { |
||
| 997 | $poster->setVar('posts', $poster->getVar('posts') - 1); |
||
| 998 | $res = $memberHandler->insertUser($poster, true); |
||
| 999 | unset($poster); |
||
| 1000 | } |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE pid=' . $post->getVar('post_id'); |
||
| 1004 | if (!$result = $this->db->queryF($sql)) { |
||
| 1005 | //xoops_error($this->db->error()); |
||
| 1006 | } |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | return true; |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * @param null $criteria |
||
| 1014 | * @return int |
||
| 1015 | */ |
||
| 1016 | public function getPostCount($criteria = null) |
||
| 1017 | { |
||
| 1018 | return parent::getCount($criteria); |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | /* |
||
| 1022 | * TODO: combining viewtopic.php |
||
| 1023 | */ |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * @param null $criteria |
||
| 1027 | * @param int $limit |
||
| 1028 | * @param int $start |
||
| 1029 | * @param null $join |
||
| 1030 | * @return array |
||
| 1031 | */ |
||
| 1032 | public function &getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null) |
||
| 1033 | { |
||
| 1034 | $ret = []; |
||
| 1035 | $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'; |
||
| 1036 | if (!empty($join)) { |
||
| 1037 | $sql .= $join; |
||
| 1038 | } |
||
| 1039 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
| 1040 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 1041 | if ('' != $criteria->getSort()) { |
||
| 1042 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 1043 | } |
||
| 1044 | } |
||
| 1045 | $result = $this->db->query($sql, (int)$limit, (int)$start); |
||
| 1046 | if ($result) { |
||
| 1047 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 1048 | $post = $this->create(false); |
||
| 1049 | $post->assignVars($myrow); |
||
| 1050 | $ret[$myrow['post_id']] = $post; |
||
| 1051 | unset($post); |
||
| 1052 | } |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | return $ret; |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * clean orphan items from database |
||
| 1060 | * |
||
| 1061 | * @return bool true on success |
||
| 1062 | */ |
||
| 1063 | public function cleanOrphan() |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * clean expired objects from database |
||
| 1094 | * |
||
| 1095 | * @param int $expire time limit for expiration |
||
| 1096 | * @return bool true on success |
||
| 1097 | */ |
||
| 1098 | public function cleanExpires($expire = 0) |
||
| 1106 | } |
||
| 1107 | } |
||
| 1108 |