Topic::loadOldPoll()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 2
b 0
f 0
nc 7
nop 1
dl 0
loc 25
rs 9.2222
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 */
13
14
use XoopsModules\Xoopspoll\{
15
    Poll
16
};
17
18
\defined('NEWBB_FUNCTIONS_INI') || require $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
19
20
/**
21
 * Class Topic
22
 */
23
class Topic extends \XoopsObject
24
{
25
    public int    $topic_id;
26
    public string $topic_title;
27
    public int    $topic_poster;
28
    public int    $topic_time;
29
    public int    $topic_views;
30
    public int    $topic_replies;
31
    public int    $topic_last_post_id;
32
    public int    $forum_id;
33
    public int    $topic_status;
34
    public int    $type_id;
35
    public int    $topic_sticky;
36
    public int    $topic_digest;
37
    public int    $digest_time;
38
    public int    $approved;
39
    public string $poster_name;
40
    public float  $rating;
41
    public int    $votes;
42
    public int    $topic_haspoll;
43
    public int    $poll_id;
44
    public string $topic_tags;
45
46
    public function __construct()
47
    {
48
        parent::__construct();
49
        $this->initVar('topic_id', \XOBJ_DTYPE_INT);
50
        $this->initVar('topic_title', \XOBJ_DTYPE_TXTBOX);
51
        $this->initVar('topic_poster', \XOBJ_DTYPE_INT);
52
        $this->initVar('topic_time', \XOBJ_DTYPE_INT);
53
        $this->initVar('topic_views', \XOBJ_DTYPE_INT);
54
        $this->initVar('topic_replies', \XOBJ_DTYPE_INT);
55
        $this->initVar('topic_last_post_id', \XOBJ_DTYPE_INT);
56
        $this->initVar('forum_id', \XOBJ_DTYPE_INT);
57
        $this->initVar('topic_status', \XOBJ_DTYPE_INT);
58
        $this->initVar('type_id', \XOBJ_DTYPE_INT);
59
        $this->initVar('topic_sticky', \XOBJ_DTYPE_INT);
60
        $this->initVar('topic_digest', \XOBJ_DTYPE_INT);
61
        $this->initVar('digest_time', \XOBJ_DTYPE_INT);
62
        $this->initVar('approved', \XOBJ_DTYPE_INT);
63
        $this->initVar('poster_name', \XOBJ_DTYPE_TXTBOX);
64
        $this->initVar('rating', \XOBJ_DTYPE_OTHER);
65
        $this->initVar('votes', \XOBJ_DTYPE_INT);
66
        $this->initVar('topic_haspoll', \XOBJ_DTYPE_INT);
67
        $this->initVar('poll_id', \XOBJ_DTYPE_INT);
68
        $this->initVar('topic_tags', \XOBJ_DTYPE_SOURCE);
69
    }
70
71
    // irmtfan add LAST_INSERT_ID to enhance the mysql performances
72
    public function incrementCounter(): void
73
    {
74
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('newbb_topics') . ' SET topic_views = LAST_INSERT_ID(topic_views + 1) WHERE topic_id =' . $this->getVar('topic_id');
75
        $GLOBALS['xoopsDB']->queryF($sql);
76
    }
77
78
    /**
79
     * Create full title of the topic
80
     * the title is composed of [type_name] if type_id is greater than 0 plus topic_title
81
     * @return string
82
     */
83
    public function getFullTitle(): string
84
    {
85
        $topic_title = $this->getVar('topic_title');
86
        if (!$this->getVar('type_id')) {
87
            return $topic_title;
88
        }
89
        $typeHandler = Helper::getInstance()->getHandler('Type');
90
        if (!$typeObject = $typeHandler->get($this->getVar('type_id'))) {
91
            return $topic_title;
92
        }
93
94
        require_once \dirname(__DIR__) . '/include/functions.topic.php';
95
96
        return \getTopicTitle($topic_title, $typeObject->getVar('type_name'), $typeObject->getVar('type_color'));
97
    }
98
99
    // START irmtfan loadOldPoll function
100
101
    /**
102
     * Load functions needed for old xoopspoll (older than version 1.4 by zyspec) and umfrage modules
103
     *
104
     * @param string|null $pollModule dirname of the poll module
105
     * @return string|false = the name of the old poll class eg: "XoopsPoll" | "Umfrage"
106
     */
107
    public function loadOldPoll(?string $pollModule = null)
108
    {
109
        static $classPoll = false;
110
        if ($classPoll && null === $pollModule) {
111
            return $classPoll;
112
        }
113
        $newbbConfig = \newbbLoadConfig();
114
        if (null !== $pollModule) {
115
            $newbbConfig['poll_module'] = $pollModule;
116
        }
117
        //        $relPath = $GLOBALS['xoops']->path('modules/' . $newbbConfig['poll_module'] . '/class/' . $newbbConfig['poll_module']);
118
        //        require_once $relPath . '.php';
119
        //        require_once $relPath . 'option.php';
120
        //        require_once $relPath . 'log.php';
121
        //        require_once $relPath . 'renderer.php';
122
        $classes = \get_declared_classes();
123
        foreach (\array_reverse($classes) as $class) {
124
            if (mb_strtolower($class) == $newbbConfig['poll_module']) {
125
                $classPoll = $class;
126
127
                return $classPoll;
128
            }
129
        }
130
131
        return false;
132
    }
133
134
    // END irmtfan loadOldPoll function
135
    // START irmtfan add deletePoll function
136
    /**
137
     * delete a poll in database
138
     *
139
     * @param int $poll_id
140
     * @return bool
141
     */
142
    public function deletePoll(int $poll_id): bool
143
    {
144
        if (empty($poll_id)) {
145
            return false;
146
        }
147
        /** @var \XoopsModuleHandler $moduleHandler */
148
        $moduleHandler     = \xoops_getHandler('module');
149
        $newbbConfig       = \newbbLoadConfig();
150
        $pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']);
151
        if (!\is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) {
152
            return false;
153
        }
154
        // new xoopspoll module
155
        if ($pollModuleHandler->getVar('version') >= 140) {
156
            /** @var \XoopsModules\Xoopspoll\PollHandler $pollHandler */
157
            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Newbb\Xoopspoll\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
158
            if ($pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) {
159
                /** @var \XoopsModules\XoopsPoll\OptionHandler $optionHandler */
160
                $optionHandler = \XoopsModules\Xoopspoll\Helper::getInstance()->getHandler('Option');
161
                $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
162
                /** @var \XoopsModules\XoopsPoll\LogHandler $logHandler */
163
                $logHandler = \XoopsModules\Xoopspoll\Helper::getInstance()->getHandler('Log');
164
                $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
165
                \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
166
            }
167
            // old Xoopspoll or Umfrage or any clone from them
168
        } else {
169
            $classPoll = $this->loadOldPoll();
170
            /** @var Poll $poll */
171
            $poll = new $classPoll($poll_id);
172
            if (false !== $poll->delete()) {
173
                $classOption = $classPoll . 'Option';
0 ignored issues
show
Bug introduced by
Are you sure $classPoll of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
                $classOption = /** @scrutinizer ignore-type */ $classPoll . 'Option';
Loading history...
174
                $classOption::deleteByPollId($poll->getVar('poll_id'));
175
                $classLog = $classPoll . 'Log';
176
                $classLog::deleteByPollId($poll->getVar('poll_id'));
177
                \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll->getVar('poll_id'));
178
            }
179
        } // end poll_module new or old
180
181
        return true;
182
    }
183
184
    // END irmtfan add deletePoll function
185
    // START irmtfan add getPoll function
186
    /**
187
     * get a poll object from a poll module.
188
     * note: can be used to find if a poll exist in a module
189
     * @param int         $poll_id
190
     * @param string|null $pollModule dirname of the poll module
191
     * @return bool|\XoopsObject poll
192
     */
193
    public function getPoll(int $poll_id, string $pollModule = null)
194
    {
195
        if (empty($poll_id)) {
196
            return false;
197
        }
198
        /** @var \XoopsModuleHandler $moduleHandler */
199
        $moduleHandler = \xoops_getHandler('module');
200
        $newbbConfig   = \newbbLoadConfig();
201
        if (null !== $pollModule) {
202
            $newbbConfig['poll_module'] = $pollModule;
203
        }
204
205
        $pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']);
206
        if (!\is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) {
207
            return false;
208
        }
209
        // new xoopspoll module
210
        if ($pollModuleHandler->getVar('version') >= 140) {
211
            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
212
            $pollObject  = $pollHandler->get($poll_id);
213
            // old xoopspoll or umfrage or any clone from them
214
        } else {
215
            $classPoll  = $this->loadOldPoll($newbbConfig['poll_module']);
216
            $pollObject = new $classPoll($poll_id);
217
        } // end poll_module new or old
218
219
        return $pollObject;
220
    }
221
    // END irmtfan add getPoll function
222
}
223