Total Complexity | 188 |
Total Lines | 1039 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ForumHandler 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 ForumHandler, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Newbb; |
||
27 | class ForumHandler extends \XoopsPersistableObjectHandler |
||
28 | { |
||
29 | /** |
||
30 | * @param null|\XoopsDatabase $db |
||
31 | */ |
||
32 | public function __construct(\XoopsDatabase $db) |
||
33 | { |
||
34 | parent::__construct($db, 'newbb_forums', Forum::class, 'forum_id', 'forum_name'); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param \XoopsObject $object |
||
39 | * @param bool $force |
||
40 | * @return bool |
||
41 | * @internal param \XoopsObject $forum |
||
42 | */ |
||
43 | |||
44 | public function insert(\XoopsObject $object, $force = true) //insert($forum) |
||
45 | { |
||
46 | $forum = $object; |
||
47 | if (!parent::insert($forum, true)) { |
||
48 | return false; |
||
49 | } |
||
50 | |||
51 | if ($forum->isNew()) { |
||
52 | $this->applyPermissionTemplate($forum); |
||
53 | } |
||
54 | |||
55 | return $forum->getVar('forum_id'); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param \XoopsObject $forum |
||
60 | * @param bool $force |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function delete(\XoopsObject $forum, $force = false) //delete(&$forum) |
||
64 | { |
||
65 | global $xoopsModule; |
||
66 | // RMV-NOTIFY |
||
67 | xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'forum', $forum->getVar('forum_id')); |
||
68 | // Get list of all topics in forum, to delete them too |
||
69 | /** @var Newbb\TopicHandler $topicHandler */ |
||
70 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
71 | $topicHandler->deleteAll(new \Criteria('forum_id', $forum->getVar('forum_id')), true, true); |
||
72 | $this->updateAll('parent_forum', $forum->getVar('parent_forum'), new \Criteria('parent_forum', $forum->getVar('forum_id'))); |
||
73 | $this->deletePermission($forum); |
||
74 | |||
75 | return parent::delete($forum); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $perm |
||
80 | * @return mixed |
||
81 | */ |
||
82 | public function getIdsByPermission($perm = 'access') |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param int $cat |
||
91 | * @param string $permission |
||
92 | * @param null $tags |
||
93 | * @param bool $asObject |
||
94 | * @return array |
||
95 | */ |
||
96 | public function &getByPermission($cat = 0, $permission = 'access', $tags = null, $asObject = true) |
||
97 | { |
||
98 | $_cachedForums = []; |
||
99 | if (!$valid_ids = $this->getIdsByPermission($permission)) { |
||
100 | return $_cachedForums; |
||
101 | } |
||
102 | |||
103 | $criteria = new \CriteriaCompo(new \Criteria('forum_id', '(' . implode(', ', $valid_ids) . ')', 'IN')); |
||
104 | if (is_numeric($cat) && $cat > 0) { |
||
105 | $criteria->add(new \Criteria('cat_id', (int)$cat)); |
||
106 | } elseif (is_array($cat) && count($cat) > 0) { |
||
107 | $criteria->add(new \Criteria('cat_id', '(' . implode(', ', $cat) . ')', 'IN')); |
||
108 | } |
||
109 | $criteria->setSort('forum_order'); |
||
110 | $criteria->setOrder('ASC'); |
||
111 | $_cachedForums = $this->getAll($criteria, $tags, $asObject); |
||
112 | |||
113 | return $_cachedForums; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param int $categoryid |
||
118 | * @param string $permission |
||
119 | * @param bool $asObject |
||
120 | * @param null $tags |
||
121 | * @return array |
||
122 | */ |
||
123 | public function &getForumsByCategory($categoryid = 0, $permission = '', $asObject = true, $tags = null) |
||
124 | { |
||
125 | $forums = $this->getByPermission($categoryid, $permission, $tags); |
||
126 | if ($asObject) { |
||
127 | return $forums; |
||
128 | } |
||
129 | |||
130 | $forums_array = []; |
||
131 | $array_cat = []; |
||
132 | $array_forum = []; |
||
133 | if (!is_array($forums)) { |
||
134 | return []; |
||
135 | } |
||
136 | foreach (array_keys($forums) as $forumid) { |
||
137 | $forum = $forums[$forumid]; |
||
138 | $forums_array[$forum->getVar('parent_forum')][$forumid] = [ |
||
139 | 'cid' => $forum->getVar('cat_id'), |
||
140 | 'title' => $forum->getVar('forum_name') |
||
141 | ]; |
||
142 | } |
||
143 | if (!isset($forums_array[0])) { |
||
144 | $ret = []; |
||
145 | |||
146 | return $ret; |
||
147 | } |
||
148 | foreach ($forums_array[0] as $key => $forum) { |
||
149 | if (isset($forums_array[$key])) { |
||
150 | $forum['sub'] = $forums_array[$key]; |
||
151 | } |
||
152 | $array_forum[$forum['cid']][$key] = $forum; |
||
153 | } |
||
154 | ksort($array_forum); |
||
155 | unset($forums, $forums_array); |
||
156 | |||
157 | return $array_forum; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param $forum |
||
162 | * @param null $criteria |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getAllTopics(&$forum, $criteria = null) |
||
166 | { |
||
167 | global $myts, $viewAllForums; |
||
168 | $startdate = ''; |
||
169 | |||
170 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.render.php'); |
||
171 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.session.php'); |
||
172 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.time.php'); |
||
173 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.read.php'); |
||
174 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.topic.php'); |
||
175 | |||
176 | $criteria_vars = ['startdate', 'start', 'sort', 'order', 'type', 'status', 'excerpt']; |
||
177 | foreach ($criteria_vars as $var) { |
||
178 | ${$var} = $criteria[$var]; |
||
179 | } |
||
180 | |||
181 | $topic_lastread = newbbGetCookie('LT', true); |
||
182 | $criteria_forum = ''; |
||
183 | if (is_object($forum)) { |
||
184 | $criteria_forum = ' AND t.forum_id = ' . $forum->getVar('forum_id'); |
||
185 | $hot_threshold = $forum->getVar('hot_threshold'); |
||
186 | } else { |
||
187 | $hot_threshold = 10; |
||
188 | if (is_array($forum) && count($forum) > 0) { |
||
189 | $criteria_forum = ' AND t.forum_id IN (' . implode(',', array_keys($forum)) . ')'; |
||
190 | } elseif (!empty($forum)) { |
||
191 | $criteria_forum = ' AND t.forum_id =' . (int)$forum; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | $criteria_post = $startdate ? ' p.post_time > ' . $startdate : ' 1 = 1 '; |
||
196 | $criteria_topic = empty($type) ? '' : " AND t.type_id={$type}"; |
||
197 | $criteria_extra = ''; |
||
198 | $criteria_approve = ' AND t.approved = 1'; |
||
199 | $post_on = ' p.post_id = t.topic_last_post_id'; |
||
200 | $leftjoin = ' LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.post_id = t.topic_last_post_id'; |
||
201 | $sort_array = []; |
||
202 | switch ($status) { |
||
203 | case 'digest': |
||
204 | $criteria_extra = ' AND t.topic_digest = 1'; |
||
205 | break; |
||
206 | |||
207 | case 'unreplied': |
||
208 | $criteria_extra = ' AND t.topic_replies < 1'; |
||
209 | break; |
||
210 | |||
211 | case 'unread': |
||
212 | if (empty($GLOBALS['xoopsModuleConfig']['read_mode'])) { |
||
213 | } elseif (2 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
214 | // START irmtfan use read_uid to find the unread posts when the user is logged in |
||
215 | $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
216 | if (!empty($read_uid)) { |
||
217 | $leftjoin .= ' LEFT JOIN ' . $this->db->prefix('newbb_reads_topic') . ' r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' '; |
||
218 | $criteria_post .= ' AND (r.read_id IS NULL OR r.post_id < t.topic_last_post_id)'; |
||
219 | } else { |
||
220 | } |
||
221 | // END irmtfan use read_uid to find the unread posts when the user is logged in |
||
222 | } elseif (1 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
223 | // START irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
224 | if ($time_criterion = max($GLOBALS['last_visit'], $startdate)) { |
||
225 | $criteria_post = ' p.post_time > ' . $time_criterion; // for all users |
||
226 | $topics = []; |
||
227 | $topic_lastread = newbbGetCookie('LT', true); |
||
228 | if (count($topic_lastread) > 0) { |
||
229 | foreach ($topic_lastread as $id => $time) { |
||
230 | if ($time > $time_criterion) { |
||
231 | $topics[] = $id; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | if (count($topics) > 0) { |
||
236 | $criteria_extra = ' AND t.topic_id NOT IN (' . implode(',', $topics) . ')'; |
||
237 | } |
||
238 | } |
||
239 | // END irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
240 | } |
||
241 | break; |
||
242 | case 'pending': |
||
243 | $post_on = ' p.topic_id = t.topic_id'; |
||
244 | $criteria_post .= ' AND p.pid = 0'; |
||
245 | $criteria_approve = ' AND t.approved = 0'; |
||
246 | break; |
||
247 | |||
248 | case 'deleted': |
||
249 | $criteria_approve = ' AND t.approved = -1'; |
||
250 | break; |
||
251 | |||
252 | case 'all': // For viewall.php; do not display sticky topics at first |
||
253 | case 'active': // same as "all" |
||
254 | break; |
||
255 | |||
256 | default: |
||
257 | if ($startdate > 0) { |
||
258 | $criteria_post = ' (p.post_time > ' . $startdate . ' OR t.topic_sticky=1)'; |
||
259 | } |
||
260 | $sort_array[] = 't.topic_sticky DESC'; |
||
261 | break; |
||
262 | } |
||
263 | |||
264 | $select = 't.*, ' . ' p.post_time as last_post_time, p.poster_name as last_poster_name, p.icon, p.post_id, p.uid'; |
||
265 | $from = $this->db->prefix('newbb_topics') . ' t ' . $leftjoin; |
||
266 | $where = $criteria_post . $criteria_topic . $criteria_forum . $criteria_extra . $criteria_approve; |
||
267 | |||
268 | if ($excerpt) { |
||
269 | $select .= ', p.post_karma, p.require_reply, pt.post_text'; |
||
270 | $from .= ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' pt ON pt.post_id = t.topic_last_post_id'; |
||
271 | } |
||
272 | if ('u.uname' === $sort) { |
||
273 | $sort = 't.topic_poster'; |
||
274 | } |
||
275 | |||
276 | $sort_array[] = trim($sort . ' ' . $order); |
||
277 | $sortby = implode(', ', array_filter($sort_array)); |
||
278 | if (empty($sortby)) { |
||
279 | $sortby = 't.topic_last_post_id DESC'; |
||
280 | } |
||
281 | |||
282 | $sql = 'SELECT ' . $select . ' FROM ' . $from . ' WHERE ' . $where . ' ORDER BY ' . $sortby; |
||
283 | |||
284 | if (!$result = $this->db->query($sql, $GLOBALS['xoopsModuleConfig']['topics_per_page'], $start)) { |
||
285 | redirect_header('index.php', 2, _MD_NEWBB_ERROROCCURED); |
||
286 | } |
||
287 | |||
288 | $sticky = 0; |
||
289 | $topics = []; |
||
290 | $posters = []; |
||
291 | $reads = []; |
||
292 | $types = []; |
||
293 | |||
294 | /** @var Newbb\TypeHandler $typeHandler */ |
||
295 | $typeHandler = Newbb\Helper::getInstance()->getHandler('Type'); |
||
296 | $typen = $typeHandler->getByForum($forum->getVar('forum_id')); |
||
297 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
298 | if ($myrow['topic_sticky']) { |
||
299 | ++$sticky; |
||
300 | } |
||
301 | |||
302 | // ------------------------------------------------------ |
||
303 | // topic_icon: priority: sticky -> digest -> regular |
||
304 | |||
305 | if ($myrow['topic_haspoll']) { |
||
306 | if ($myrow['topic_sticky']) { |
||
307 | $topic_icon = newbbDisplayImage('topic_sticky', _MD_NEWBB_TOPICSTICKY) . '<br>' . newbbDisplayImage('poll', _MD_NEWBB_TOPICHASPOLL); |
||
308 | } else { |
||
309 | $topic_icon = newbbDisplayImage('poll', _MD_NEWBB_TOPICHASPOLL); |
||
310 | } |
||
311 | } elseif ($myrow['topic_sticky']) { |
||
312 | $topic_icon = newbbDisplayImage('topic_sticky', _MD_NEWBB_TOPICSTICKY); |
||
313 | } elseif (!empty($myrow['icon'])) { |
||
314 | $topic_icon = '<img src="' . XOOPS_URL . '/images/subject/' . htmlspecialchars($myrow['icon']) . '" alt="" />'; |
||
315 | } else { |
||
316 | $topic_icon = '<img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" />'; |
||
317 | } |
||
318 | |||
319 | // ------------------------------------------------------ |
||
320 | // rating_img |
||
321 | $rating = number_format($myrow['rating'] / 2, 0); |
||
322 | // irmtfan - add alt key for rating |
||
323 | if ($rating < 1) { |
||
324 | $rating_img = newbbDisplayImage('blank'); |
||
325 | } else { |
||
326 | $rating_img = newbbDisplayImage('rate' . $rating, constant('_MD_NEWBB_RATE' . $rating)); |
||
327 | } |
||
328 | // ------------------------------------------------------ |
||
329 | // topic_page_jump |
||
330 | $topic_page_jump = ''; |
||
331 | $topic_page_jump_icon = ''; |
||
332 | $totalpages = ceil(($myrow['topic_replies'] + 1) / $GLOBALS['xoopsModuleConfig']['posts_per_page']); |
||
333 | if ($totalpages > 1) { |
||
334 | $topic_page_jump .= ' '; |
||
335 | $append = false; |
||
336 | for ($i = 1; $i <= $totalpages; ++$i) { |
||
337 | if ($i > 3 && $i < $totalpages) { |
||
338 | if (!$append) { |
||
339 | $topic_page_jump .= '...'; |
||
340 | $append = true; |
||
341 | } |
||
342 | } else { |
||
343 | $topic_page_jump .= '[<a href="' . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $myrow['topic_id'] . '&start=' . (($i - 1) * $GLOBALS['xoopsModuleConfig']['posts_per_page']) . '">' . $i . '</a>]'; |
||
344 | // irmtfan remove here and move |
||
345 | //$topic_page_jump_icon = "<a href='" . XOOPS_URL . "/modules/newbb/viewtopic.php?post_id=" . $myrow['post_id'] . "&start=" . (($i - 1) * $GLOBALS['xoopsModuleConfig']['posts_per_page']) . "'>" . newbbDisplayImage('lastposticon',_MD_NEWBB_GOTOLASTPOST) . '</a>'; |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | // irmtfan - move here for both topics with and without pages |
||
350 | $topic_page_jump_icon = "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $myrow['post_id'] . "'>" . newbbDisplayImage('lastposticon', _MD_NEWBB_GOTOLASTPOST) . '</a>'; |
||
351 | |||
352 | // ------------------------------------------------------ |
||
353 | // => topic array |
||
354 | $forum_link = ''; |
||
355 | if (!empty($viewAllForums[$myrow['forum_id']])) { |
||
356 | $forum_link = '<a href="' . XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $myrow['forum_id'] . '">' . $viewAllForums[$myrow['forum_id']]['forum_name'] . '</a>'; |
||
357 | } |
||
358 | |||
359 | $topic_title = $myts->htmlSpecialChars($myrow['topic_title']); |
||
360 | // irmtfan remove here and move to for loop |
||
361 | //if ($myrow['type_id'] > 0) { |
||
362 | //$topic_title = '<span style="color:'.$typen[$myrow["type_id"]]["type_color"].'">['.$typen[$myrow["type_id"]]["type_name"].']</span> '.$topic_title.''; |
||
363 | //} |
||
364 | if ($myrow['topic_digest']) { |
||
365 | $topic_title = "<span class='digest'>" . $topic_title . '</span>'; |
||
366 | } |
||
367 | |||
368 | if (0 == $excerpt) { |
||
369 | $topic_excerpt = ''; |
||
370 | } elseif (($myrow['post_karma'] > 0 || $myrow['require_reply'] > 0) && !newbbIsAdmin($forum)) { |
||
371 | $topic_excerpt = ''; |
||
372 | } else { |
||
373 | $topic_excerpt = xoops_substr(newbbHtml2text($myts->displayTarea($myrow['post_text'])), 0, $excerpt); |
||
374 | $topic_excerpt = str_replace('[', '[', $myts->htmlSpecialChars($topic_excerpt)); |
||
375 | } |
||
376 | // START irmtfan move here |
||
377 | $topics[$myrow['topic_id']] = [ |
||
378 | 'topic_id' => $myrow['topic_id'], |
||
379 | 'topic_icon' => $topic_icon, |
||
380 | 'type_id' => $myrow['type_id'], |
||
381 | //'type_text' => $topic_prefix,/*irmtfan remove here and move to for loop*/ |
||
382 | 'topic_title' => $topic_title, |
||
383 | //'topic_link' => XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $myrow['topic_id'], |
||
384 | 'topic_link' => 'viewtopic.php?topic_id=' . $myrow['topic_id'], |
||
385 | 'rating_img' => $rating_img, |
||
386 | 'topic_page_jump' => $topic_page_jump, |
||
387 | 'topic_page_jump_icon' => $topic_page_jump_icon, |
||
388 | 'topic_replies' => $myrow['topic_replies'], |
||
389 | |||
390 | 'topic_digest' => $myrow['topic_digest'], |
||
391 | //mb |
||
392 | |||
393 | 'topic_poster_uid' => $myrow['topic_poster'], |
||
394 | 'topic_poster_name' => $myts->htmlSpecialChars($myrow['poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous']), |
||
395 | 'topic_views' => $myrow['topic_views'], |
||
396 | 'topic_time' => newbbFormatTimestamp($myrow['topic_time']), |
||
397 | 'topic_last_posttime' => newbbFormatTimestamp($myrow['last_post_time']), |
||
398 | 'topic_last_poster_uid' => $myrow['uid'], |
||
399 | 'topic_last_poster_name' => $myts->htmlSpecialChars($myrow['last_poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous']), |
||
400 | 'topic_forum_link' => $forum_link, |
||
401 | 'topic_excerpt' => $topic_excerpt, |
||
402 | 'stick' => empty($myrow['topic_sticky']), |
||
403 | 'stats' => [ |
||
404 | $myrow['topic_status'], |
||
405 | $myrow['topic_digest'], |
||
406 | $myrow['topic_replies'] |
||
407 | ], |
||
408 | /* irmtfan uncomment use ib the for loop*/ |
||
409 | //"topic_poster" => $topic_poster,/*irmtfan remove here and move to for loop*/ |
||
410 | //"topic_last_poster" => $topic_last_poster,/*irmtfan remove here and move to for loop*/ |
||
411 | //"topic_folder" => newbbDisplayImage($topic_folder,$topic_folder_text),/*irmtfan remove here and move to for loop*/ |
||
412 | ]; |
||
413 | // END irmtfan move here |
||
414 | /* users */ |
||
415 | $posters[$myrow['topic_poster']] = 1; |
||
416 | $posters[$myrow['uid']] = 1; |
||
417 | // reads |
||
418 | if (!empty($GLOBALS['xoopsModuleConfig']['read_mode'])) { |
||
419 | $reads[$myrow['topic_id']] = (1 == $GLOBALS['xoopsModuleConfig']['read_mode']) ? $myrow['last_post_time'] : $myrow['topic_last_post_id']; |
||
420 | } |
||
421 | }// irmtfan while end |
||
422 | // START irmtfan move to a for loop |
||
423 | $posters_name = newbbGetUnameFromIds(array_keys($posters), $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
424 | //$topic_poster = newbbGetUnameFromId($myrow['topic_poster'], $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
425 | //$topic_last_poster = newbbGetUnameFromId($myrow['uid'], $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
426 | $topic_isRead = newbbIsRead('topic', $reads); |
||
427 | foreach (array_keys($topics) as $id) { |
||
428 | $topics[$id]['topic_read'] = empty($topic_isRead[$id]) ? 0 : 1; // add topic-read/topic-new smarty variable |
||
429 | if (!empty($topics[$id]['type_id']) && isset($typen[$topics[$id]['type_id']])) { |
||
430 | $topics[$id]['topic_title'] = getTopicTitle($topics[$id]['topic_title'], $typen[$topics[$id]['type_id']]['type_name'], $typen[$topics[$id]['type_id']]['type_color']); |
||
431 | } |
||
432 | //$topic_prefix = (!empty($typen[$myrow['type_id']])) ? getTopicTitle("", $typen[$myrow['type_id']]["type_name"], $typen[$myrow['type_id']]["type_color"]) : ""; |
||
433 | $topics[$id]['topic_poster'] = !empty($posters_name[$topics[$id]['topic_poster_uid']]) ? $posters_name[$topics[$id]['topic_poster_uid']] : $topics[$id]['topic_poster_name']; |
||
434 | $topics[$id]['topic_last_poster'] = !empty($posters_name[$topics[$id]['topic_last_poster_uid']]) ? $posters_name[$topics[$id]['topic_last_poster_uid']] : $topics[$id]['topic_last_poster_name']; |
||
435 | |||
436 | // ------------------------------------------------------ |
||
437 | // topic_folder: priority: newhot -> hot/new -> regular |
||
438 | list($topic_status, $topic_digest, $topic_replies) = $topics[$id]['stats']; |
||
439 | if (1 == $topic_status) { |
||
440 | $topic_folder = 'topic_locked'; |
||
441 | $topic_folder_text = _MD_NEWBB_TOPICLOCKED; |
||
442 | } else { |
||
443 | if ($topic_digest) { |
||
444 | $topic_folder = 'topic_digest'; |
||
445 | $topic_folder_text = _MD_NEWBB_TOPICDIGEST; |
||
446 | } elseif ($topic_replies >= $hot_threshold) { |
||
447 | $topic_folder = empty($topic_isRead[$id]) ? 'topic_hot_new' : 'topic_hot'; |
||
448 | $topic_folder_text = empty($topic_isRead[$id]) ? _MD_NEWBB_MORETHAN : _MD_NEWBB_MORETHAN2; |
||
449 | } else { |
||
450 | $topic_folder = empty($topic_isRead[$id]) ? 'topic_new' : 'topic'; |
||
451 | $topic_folder_text = empty($topic_isRead[$id]) ? _MD_NEWBB_NEWPOSTS : _MD_NEWBB_NONEWPOSTS; |
||
452 | } |
||
453 | } |
||
454 | $topics[$id]['topic_folder'] = newbbDisplayImage($topic_folder, $topic_folder_text); |
||
455 | unset($topics[$id]['topic_poster_name'], $topics[$id]['topic_last_poster_name'], $topics[$id]['stats']); |
||
456 | } // irmtfan end for loop |
||
457 | // END irmtfan move to a for loop |
||
458 | if (count($topics) > 0) { |
||
459 | $sql = ' SELECT DISTINCT topic_id FROM ' . $this->db->prefix('newbb_posts') . " WHERE attachment != ''" . ' AND topic_id IN (' . implode(',', array_keys($topics)) . ')'; |
||
460 | if ($result = $this->db->query($sql)) { |
||
461 | while (list($topic_id) = $this->db->fetchRow($result)) { |
||
462 | $topics[$topic_id]['attachment'] = ' ' . newbbDisplayImage('attachment', _MD_NEWBB_TOPICSHASATT); |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | |||
467 | return [$topics, $sticky]; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @param $forum |
||
472 | * @param $startdate |
||
473 | * @param $type |
||
474 | * @return null|int |
||
475 | */ |
||
476 | public function getTopicCount(&$forum, $startdate, $type) |
||
477 | { |
||
478 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.session.php'); |
||
479 | |||
480 | $criteria_extra = ''; |
||
481 | $criteria_approve = ' AND t.approved = 1'; // any others? |
||
482 | $leftjoin = ' LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.post_id = t.topic_last_post_id'; |
||
483 | $criteria_post = ' p.post_time > ' . $startdate; |
||
484 | switch ($type) { |
||
485 | case 'digest': |
||
486 | $criteria_extra = ' AND topic_digest = 1'; |
||
487 | break; |
||
488 | case 'unreplied': |
||
489 | $criteria_extra = ' AND topic_replies < 1'; |
||
490 | break; |
||
491 | case 'unread': |
||
492 | if (empty($GLOBALS['xoopsModuleConfig']['read_mode'])) { |
||
493 | } elseif (2 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
494 | // START irmtfan use read_uid to find the unread posts when the user is logged in |
||
495 | |||
496 | $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
497 | if (!empty($read_uid)) { |
||
498 | $leftjoin .= ' LEFT JOIN ' . $this->db->prefix('newbb_reads_topic') . ' r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' '; |
||
499 | $criteria_post .= ' AND (r.read_id IS NULL OR r.post_id < t.topic_last_post_id)'; |
||
500 | } else { |
||
501 | } |
||
502 | // END irmtfan use read_uid to find the unread posts when the user is logged in |
||
503 | } elseif (1 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
504 | // START irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
505 | if ($time_criterion = max($GLOBALS['last_visit'], $startdate)) { |
||
506 | $criteria_post = ' p.post_time > ' . $time_criterion; // for all users |
||
507 | $topics = []; |
||
508 | $topic_lastread = newbbGetCookie('LT', true); |
||
509 | if (count($topic_lastread) > 0) { |
||
510 | foreach ($topic_lastread as $id => $time) { |
||
511 | if ($time > $time_criterion) { |
||
512 | $topics[] = $id; |
||
513 | } |
||
514 | } |
||
515 | } |
||
516 | if (count($topics) > 0) { |
||
517 | $criteria_extra = ' AND t.topic_id NOT IN (' . implode(',', $topics) . ')'; |
||
518 | } |
||
519 | } |
||
520 | // END irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
521 | } |
||
522 | break; |
||
523 | case 'pending': |
||
524 | $criteria_approve = ' AND t.approved = 0'; |
||
525 | break; |
||
526 | case 'deleted': |
||
527 | $criteria_approve = ' AND t.approved = -1'; |
||
528 | break; |
||
529 | case 'all': |
||
530 | break; |
||
531 | default: |
||
532 | $criteria_post = ' (p.post_time > ' . $startdate . ' OR t.topic_sticky=1)'; |
||
533 | break; |
||
534 | } |
||
535 | $criteria_forum = ''; |
||
536 | if (is_object($forum)) { |
||
537 | $criteria_forum = ' AND t.forum_id = ' . $forum->getVar('forum_id'); |
||
538 | } else { |
||
539 | if (is_array($forum) && count($forum) > 0) { |
||
540 | $criteria_forum = ' AND t.forum_id IN (' . implode(',', array_keys($forum)) . ')'; |
||
541 | } elseif (!empty($forum)) { |
||
542 | $criteria_forum = ' AND t.forum_id =' . (int)$forum; |
||
543 | } |
||
544 | } |
||
545 | |||
546 | $sql = 'SELECT COUNT(*) AS count FROM ' . $this->db->prefix('newbb_topics') . ' t ' . $leftjoin; |
||
547 | $sql .= ' WHERE ' . $criteria_post . $criteria_forum . $criteria_extra . $criteria_approve; |
||
548 | if (!$result = $this->db->query($sql)) { |
||
549 | //xoops_error($this->db->error().'<br>'.$sql); |
||
550 | return null; |
||
551 | } |
||
552 | $myrow = $this->db->fetchArray($result); |
||
553 | $count = $myrow['count']; |
||
554 | |||
555 | return $count; |
||
556 | } |
||
557 | |||
558 | // get permission |
||
559 | |||
560 | /** |
||
561 | * @param $forum |
||
562 | * @param string $type |
||
563 | * @param bool $checkCategory |
||
564 | * @return bool |
||
565 | */ |
||
566 | public function getPermission($forum, $type = 'access', $checkCategory = true) |
||
567 | { |
||
568 | global $xoopsModule; |
||
569 | static $_cachedPerms; |
||
570 | |||
571 | if ('all' === $type) { |
||
572 | return true; |
||
573 | } |
||
574 | |||
575 | include_once __DIR__ . '/../include/functions.user.php'; |
||
576 | if (newbbIsAdmin($forum)) { |
||
577 | return true; |
||
578 | } |
||
579 | //if ($GLOBALS["xoopsUserIsAdmin"] && $xoopsModule->getVar("dirname") === "newbb") { |
||
580 | //return true; |
||
581 | //} |
||
582 | |||
583 | if (!is_object($forum)) { |
||
584 | $forum = $this->get($forum); |
||
585 | } |
||
586 | |||
587 | if (!empty($checkCategory)) { |
||
588 | /** @var Newbb\CategoryHandler $categoryHandler */ |
||
589 | $categoryHandler = Newbb\Helper::getInstance()->getHandler('Category'); |
||
590 | $categoryPerm = $categoryHandler->getPermission($forum->getVar('cat_id')); |
||
591 | if (!$categoryPerm) { |
||
592 | return false; |
||
593 | } |
||
594 | } |
||
595 | |||
596 | $type = strtolower($type); |
||
597 | // START irmtfan commented and removed |
||
598 | //if ('moderate' === $type) { |
||
599 | //require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.user.php'); |
||
600 | //$permission = newbbIsModerator($forum); |
||
601 | //} else { |
||
602 | $forum_id = $forum->getVar('forum_id'); |
||
603 | /** var Newbb\PermissionHandler $permHandler */ |
||
604 | $permHandler = Newbb\Helper::getInstance()->getHandler('Permission'); |
||
605 | $permission = $permHandler->getPermission('forum', $type, $forum_id); |
||
606 | //} |
||
607 | // END irmtfan commented and removed |
||
608 | return $permission; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * @param $forum |
||
613 | * @return mixed |
||
614 | */ |
||
615 | public function deletePermission(&$forum) |
||
616 | { |
||
617 | /** var Newbb\PermissionHandler $permHandler */ |
||
618 | $permHandler = Newbb\Helper::getInstance()->getHandler('Permission'); |
||
619 | |||
620 | return $permHandler->deleteByForum($forum->getVar('forum_id')); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * @param $forum |
||
625 | * @return mixed |
||
626 | */ |
||
627 | public function applyPermissionTemplate(&$forum) |
||
628 | { |
||
629 | /** var Newbb\PermissionHandler $permHandler */ |
||
630 | $permHandler = Newbb\Helper::getInstance()->getHandler('Permission'); |
||
631 | |||
632 | return $permHandler->applyTemplate($forum->getVar('forum_id')); |
||
633 | } |
||
634 | |||
635 | /* |
||
636 | function isForum($forum) |
||
637 | { |
||
638 | $count = false; |
||
639 | $sql = 'SELECT COUNT(*) as count FROM ' . $this->db->prefix("newbb_forums"); |
||
640 | $sql .= ' WHERE forum_id=' . $forum ; |
||
641 | if ($result = $this->db->query($sql)) { |
||
642 | $myrow = $this->db->fetchArray($result); |
||
643 | $count = $myrow['count']; |
||
644 | } |
||
645 | |||
646 | return $count; |
||
647 | } |
||
648 | */ |
||
649 | |||
650 | /** |
||
651 | * clean orphan forums from database |
||
652 | * @param string $table_link |
||
653 | * @param string $field_link |
||
654 | * @param string $field_object |
||
655 | * @param array $forum_ids forum IDs |
||
656 | * @return bool true on success |
||
657 | */ |
||
658 | // START irmtfan rewrite forum cleanOrphan function. add parent_forum and cat_id orphan check |
||
659 | // public function cleanOrphan(array $forum_ids = array()) |
||
660 | public function cleanOrphan($table_link = '', $field_link = '', $field_object = '', $forum_ids = []) |
||
661 | { |
||
662 | // check parent_forum orphan forums |
||
663 | if (empty($forum_ids)) { |
||
664 | $forum_ids = $this->getIds(); |
||
665 | } |
||
666 | if (empty($forum_ids)) { |
||
667 | return false; |
||
668 | } |
||
669 | /* |
||
670 | $sql = " UPDATE ".$GLOBALS['xoopsDB']->prefix("newbb_forums"). |
||
671 | " SET parent_forum = 0". |
||
672 | " WHERE (parent_forum NOT IN ( ".$forum_ids."))". |
||
673 | " OR parent_forum = forum_id"; |
||
674 | */ |
||
675 | $criteria = new \CriteriaCompo(); |
||
676 | $criteria->add(new \Criteria('parent_forum', '(' . implode(', ', $forum_ids) . ')', 'NOT IN'), 'AND'); |
||
677 | $criteria->add(new \Criteria('parent_forum', '`forum_id`', '='), 'OR'); |
||
678 | $b1 = $this->updateAll('parent_forum', 0, $criteria, true); |
||
679 | // check cat_id orphan forums |
||
680 | $categoryHandler = Newbb\Helper::getInstance()->getHandler('Category'); |
||
681 | $cat_ids = $categoryHandler->getIds(); |
||
682 | if (empty($cat_ids)) { |
||
683 | return false; |
||
684 | } |
||
685 | $criteria = new \CriteriaCompo(); |
||
686 | $criteria->add(new \Criteria('cat_id', '(' . implode(', ', $cat_ids) . ')', 'NOT IN'), 'AND'); |
||
687 | $b2 = $this->updateAll('cat_id', $cat_ids[0], $criteria, true); |
||
688 | |||
689 | return ($b1 && $b2); |
||
690 | } |
||
691 | // END irmtfan rewrite forum cleanOrphan function. add parent_forum and cat_id orphan check |
||
692 | |||
693 | /** |
||
694 | * forum data synchronization |
||
695 | * |
||
696 | * @param mixed $object null for all forums; integer for forum_id; object for forum object |
||
697 | * @return bool |
||
698 | * @internal param int $mode 1 for stats only; 2 for forum index data only; 0 for both |
||
699 | * |
||
700 | */ |
||
701 | public function synchronization($object = null) |
||
748 | } |
||
749 | |||
750 | /** |
||
751 | * @param null $passedSubForums |
||
752 | * @return array |
||
753 | */ |
||
754 | public function getSubforumStats($passedSubForums = null) |
||
755 | { |
||
756 | $stats = []; |
||
757 | |||
758 | require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.forum.php'); |
||
759 | |||
760 | $subForumTree = newbbGetSubForum(); |
||
761 | if (empty($passedSubForums)) { |
||
762 | $sub_forums = $subForumTree; |
||
763 | } else { |
||
764 | foreach ($passedSubForums as $id) { |
||
765 | $sub_forums[$id] = isset($subForumTree[$id]) ? $subForumTree[$id] : null; |
||
766 | } |
||
767 | } |
||
768 | |||
769 | $forums_id = []; |
||
770 | foreach (array_keys($sub_forums) as $id) { |
||
771 | if (empty($sub_forums[$id])) { |
||
772 | continue; |
||
773 | } |
||
774 | $forums_id = array_merge($forums_id, $sub_forums[$id]); |
||
775 | } |
||
776 | if (!$forums_id) { |
||
777 | return $stats; |
||
778 | } |
||
779 | $sql = ' SELECT forum_posts AS posts, forum_topics AS topics, forum_id AS id' . ' FROM ' . $this->table . ' WHERE forum_id IN (' . implode(', ', $forums_id) . ')'; |
||
780 | if (!$result = $this->db->query($sql)) { |
||
781 | return $stats; |
||
782 | } |
||
783 | |||
784 | $forum_stats = []; |
||
785 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
786 | $forum_stats[$row['id']] = ['topics' => $row['topics'], 'posts' => $row['posts']]; |
||
787 | } |
||
788 | |||
789 | foreach (array_keys($sub_forums) as $id) { |
||
790 | if (empty($sub_forums[$id])) { |
||
791 | continue; |
||
792 | } |
||
793 | $stats[$id] = ['topics' => 0, 'posts' => 0]; |
||
794 | foreach ($sub_forums[$id] as $fid) { |
||
795 | $stats[$id]['topics'] += $forum_stats[$fid]['topics']; |
||
796 | $stats[$id]['posts'] += $forum_stats[$fid]['posts']; |
||
797 | } |
||
798 | } |
||
799 | |||
800 | return $stats; |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * @param $forums |
||
805 | * @param int $length_title_index |
||
806 | * @param int $count_subforum |
||
807 | * @return array |
||
808 | */ |
||
809 | public function &display($forums, $length_title_index = 30, $count_subforum = 1) |
||
927 | } |
||
928 | |||
929 | /** |
||
930 | * get a hierarchical tree of forums |
||
931 | * |
||
932 | * {@link newbbTree} |
||
933 | * |
||
934 | * @param int $cat_id category ID |
||
935 | * @param int $pid Top forum ID |
||
936 | * @param string $permission permission type |
||
937 | * @param string $prefix prefix for display |
||
938 | * @param string $tags variables to fetch |
||
939 | * @return array associative array of category IDs and sanitized titles |
||
940 | */ |
||
941 | public function &getTree($cat_id = 0, $pid = 0, $permission = 'access', $prefix = '--', $tags = null) |
||
942 | { |
||
943 | $pid = (int)$pid; |
||
944 | $perm_string = $permission; |
||
945 | if (!is_array($tags) || 0 === count($tags)) { |
||
946 | $tags = ['forum_id', 'parent_forum', 'forum_name', 'forum_order', 'cat_id']; |
||
947 | } |
||
948 | $forumsObject = $this->getByPermission($cat_id, $perm_string, $tags); |
||
949 | |||
950 | require_once __DIR__ . '/tree.php'; |
||
951 | $forums_structured = []; |
||
952 | foreach (array_keys($forumsObject) as $key) { |
||
953 | $forums_structured[$forumsObject[$key]->getVar('cat_id')][$key] = $forumsObject[$key]; |
||
954 | } |
||
955 | |||
956 | foreach (array_keys($forums_structured) as $cid) { |
||
957 | $tree = new ObjectTree($forums_structured[$cid]); |
||
958 | $forum_array[$cid] = $tree->makeTree($prefix, $pid, $tags); |
||
959 | unset($tree); |
||
960 | } |
||
961 | |||
962 | return $forum_array; |
||
963 | } |
||
964 | |||
965 | /** |
||
966 | * get a hierarchical array tree of forums |
||
967 | * |
||
968 | * {@link newbbTree} |
||
969 | * |
||
970 | * @param int $cat_id category ID |
||
971 | * @param int $pid Top forum ID |
||
972 | * @param string $permission permission type |
||
973 | * @param string $tags variables to fetch |
||
974 | * @param integer $depth level of subcategories |
||
975 | * @return array associative array of category IDs and sanitized titles |
||
976 | */ |
||
977 | public function &getArrayTree($cat_id = 0, $pid = 0, $permission = 'access', $tags = null, $depth = 0) |
||
978 | { |
||
979 | $pid = (int)$pid; |
||
980 | $perm_string = $permission; |
||
981 | if (!is_array($tags) || 0 === count($tags)) { |
||
982 | $tags = ['forum_id', 'parent_forum', 'forum_name', 'forum_order', 'cat_id']; |
||
983 | } |
||
984 | $forumsObject = $this->getByPermission($cat_id, $perm_string, $tags); |
||
985 | |||
986 | require_once __DIR__ . '/tree.php'; |
||
987 | $forums_structured = []; |
||
988 | foreach (array_keys($forumsObject) as $key) { |
||
989 | $forumObject =& $forumsObject[$key]; |
||
990 | $forums_structured[$forumObject->getVar('cat_id')][$key] = $forumsObject[$key]; |
||
991 | } |
||
992 | foreach (array_keys($forums_structured) as $cid) { |
||
993 | $tree = new ObjectTree($forums_structured[$cid]); |
||
994 | $forum_array[$cid] = $tree->makeArrayTree($pid, $tags, $depth); |
||
995 | unset($tree); |
||
996 | } |
||
997 | |||
998 | return $forum_array; |
||
999 | } |
||
1000 | |||
1001 | /** |
||
1002 | * @param $object |
||
1003 | * @return array|null |
||
1004 | */ |
||
1005 | public function &getParents($object) |
||
1023 | } |
||
1024 | |||
1025 | // START irmtfan - get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums |
||
1026 | |||
1027 | /** |
||
1028 | * function for get forum Ids by positive and negative values |
||
1029 | * |
||
1030 | * @param int|text $values : positive values = forums | negative values = cats | $values=0 = all valid forums, $permission , true/false $parse_cats |
||
1031 | * @param string $permission |
||
1032 | * @param bool $parse_cats |
||
1033 | * @return array|mixed $validForums |
||
1034 | */ |
||
1035 | public function getIdsByValues($values = 0, $permission = 'access', $parse_cats = true) |
||
1069 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.