| Conditions | 68 |
| Paths | > 20000 |
| Total Lines | 299 |
| Code Lines | 199 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 ($forum && \is_array($forum)) { |
||
| 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 | case 'unreplied': |
||
| 207 | $criteria_extra = ' AND t.topic_replies < 1'; |
||
| 208 | break; |
||
| 209 | case 'unread': |
||
| 210 | if (empty($GLOBALS['xoopsModuleConfig']['read_mode'])) { |
||
| 211 | } elseif (2 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
| 212 | // START irmtfan use read_uid to find the unread posts when the user is logged in |
||
| 213 | $read_uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 214 | if (!empty($read_uid)) { |
||
| 215 | $leftjoin .= ' LEFT JOIN ' . $this->db->prefix('newbb_reads_topic') . ' r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' '; |
||
| 216 | $criteria_post .= ' AND (r.read_id IS NULL OR r.post_id < t.topic_last_post_id)'; |
||
| 217 | } |
||
| 218 | // END irmtfan use read_uid to find the unread posts when the user is logged in |
||
| 219 | } elseif (1 == $GLOBALS['xoopsModuleConfig']['read_mode']) { |
||
| 220 | // START irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 221 | $time_criterion = \max($GLOBALS['last_visit'], $startdate); |
||
| 222 | if ($time_criterion) { |
||
| 223 | $criteria_post = ' p.post_time > ' . $time_criterion; // for all users |
||
| 224 | $topics = []; |
||
| 225 | $topic_lastread = \newbbGetCookie('LT', true); |
||
| 226 | if (\count($topic_lastread) > 0) { |
||
| 227 | foreach ($topic_lastread as $id => $time) { |
||
| 228 | if ($time > $time_criterion) { |
||
| 229 | $topics[] = $id; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | if (\count($topics) > 0) { |
||
| 234 | $criteria_extra = ' AND t.topic_id NOT IN (' . \implode(',', $topics) . ')'; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | // END irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 238 | } |
||
| 239 | break; |
||
| 240 | case 'pending': |
||
| 241 | $post_on = ' p.topic_id = t.topic_id'; |
||
| 242 | $criteria_post .= ' AND p.pid = 0'; |
||
| 243 | $criteria_approve = ' AND t.approved = 0'; |
||
| 244 | break; |
||
| 245 | case 'deleted': |
||
| 246 | $criteria_approve = ' AND t.approved = -1'; |
||
| 247 | break; |
||
| 248 | case 'all': // For viewall.php; do not display sticky topics at first |
||
| 249 | case 'active': // same as "all" |
||
| 250 | break; |
||
| 251 | default: |
||
| 252 | if ($startdate > 0) { |
||
| 253 | $criteria_post = ' (p.post_time > ' . $startdate . ' OR t.topic_sticky=1)'; |
||
| 254 | } |
||
| 255 | $sort_array[] = 't.topic_sticky DESC'; |
||
| 256 | break; |
||
| 257 | } |
||
| 258 | |||
| 259 | $select = 't.*, ' . ' p.post_time as last_post_time, p.poster_name as last_poster_name, p.icon, p.post_id, p.uid'; |
||
| 260 | $from = $this->db->prefix('newbb_topics') . ' t ' . $leftjoin; |
||
| 261 | $where = $criteria_post . $criteria_topic . $criteria_forum . $criteria_extra . $criteria_approve; |
||
| 262 | |||
| 263 | if ($excerpt) { |
||
| 264 | $select .= ', p.post_karma, p.require_reply, pt.post_text'; |
||
| 265 | $from .= ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' pt ON pt.post_id = t.topic_last_post_id'; |
||
| 266 | } |
||
| 267 | if ('u.uname' === $sort) { |
||
| 268 | $sort = 't.topic_poster'; |
||
| 269 | } |
||
| 270 | |||
| 271 | $sort_array[] = \trim($sort . ' ' . $order); |
||
| 272 | $sortby = \implode(', ', \array_filter($sort_array)); |
||
| 273 | if (empty($sortby)) { |
||
| 274 | $sortby = 't.topic_last_post_id DESC'; |
||
| 275 | } |
||
| 276 | |||
| 277 | $sql = 'SELECT ' . $select . ' FROM ' . $from . ' WHERE ' . $where . ' ORDER BY ' . $sortby; |
||
| 278 | |||
| 279 | if (!$result = $this->db->query($sql, $GLOBALS['xoopsModuleConfig']['topics_per_page'], $start)) { |
||
| 280 | \redirect_header('index.php', 2, \_MD_NEWBB_ERROROCCURED); |
||
| 281 | } |
||
| 282 | |||
| 283 | $sticky = 0; |
||
| 284 | $topics = []; |
||
| 285 | $posters = []; |
||
| 286 | $reads = []; |
||
| 287 | $types = []; |
||
| 288 | |||
| 289 | /** @var Newbb\TypeHandler $typeHandler */ |
||
| 290 | $typeHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Type'); |
||
| 291 | $typen = $typeHandler->getByForum($forum->getVar('forum_id')); |
||
| 292 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 293 | if ($myrow['topic_sticky']) { |
||
| 294 | ++$sticky; |
||
| 295 | } |
||
| 296 | |||
| 297 | // ------------------------------------------------------ |
||
| 298 | // topic_icon: priority: sticky -> digest -> regular |
||
| 299 | |||
| 300 | if ($myrow['topic_haspoll']) { |
||
| 301 | if ($myrow['topic_sticky']) { |
||
| 302 | $topic_icon = \newbbDisplayImage('topic_sticky', \_MD_NEWBB_TOPICSTICKY) . '<br>' . \newbbDisplayImage('poll', \_MD_NEWBB_TOPICHASPOLL); |
||
| 303 | } else { |
||
| 304 | $topic_icon = \newbbDisplayImage('poll', \_MD_NEWBB_TOPICHASPOLL); |
||
| 305 | } |
||
| 306 | } elseif ($myrow['topic_sticky']) { |
||
| 307 | $topic_icon = \newbbDisplayImage('topic_sticky', \_MD_NEWBB_TOPICSTICKY); |
||
| 308 | } elseif (!empty($myrow['icon'])) { |
||
| 309 | $topic_icon = '<img src="' . XOOPS_URL . '/images/subject/' . \htmlspecialchars($myrow['icon'], \ENT_QUOTES | \ENT_HTML5) . '" alt="" >'; |
||
| 310 | } else { |
||
| 311 | $topic_icon = '<img src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" >'; |
||
| 312 | } |
||
| 313 | |||
| 314 | // ------------------------------------------------------ |
||
| 315 | // rating_img |
||
| 316 | $rating = \number_format($myrow['rating'] / 2, 0); |
||
| 317 | // irmtfan - add alt key for rating |
||
| 318 | if ($rating < 1) { |
||
| 319 | $rating_img = \newbbDisplayImage('blank'); |
||
| 320 | } else { |
||
| 321 | $rating_img = \newbbDisplayImage('rate' . $rating, \constant('_MD_NEWBB_RATE' . $rating)); |
||
| 322 | } |
||
| 323 | // ------------------------------------------------------ |
||
| 324 | // topic_page_jump |
||
| 325 | $topic_page_jump = ''; |
||
| 326 | $topic_page_jump_icon = ''; |
||
| 327 | $totalpages = \ceil(($myrow['topic_replies'] + 1) / $GLOBALS['xoopsModuleConfig']['posts_per_page']); |
||
| 328 | if ($totalpages > 1) { |
||
| 329 | $topic_page_jump .= ' '; |
||
| 330 | $append = false; |
||
| 331 | for ($i = 1; $i <= $totalpages; ++$i) { |
||
| 332 | if ($i > 3 && $i < $totalpages) { |
||
| 333 | if (!$append) { |
||
| 334 | $topic_page_jump .= '...'; |
||
| 335 | $append = true; |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | $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>]'; |
||
| 339 | // irmtfan remove here and move |
||
| 340 | //$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>'; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | // irmtfan - move here for both topics with and without pages |
||
| 345 | $topic_page_jump_icon = "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $myrow['post_id'] . "'>" . \newbbDisplayImage('lastposticon', _MD_NEWBB_GOTOLASTPOST) . '</a>'; |
||
| 346 | |||
| 347 | // ------------------------------------------------------ |
||
| 348 | // => topic array |
||
| 349 | $forum_link = ''; |
||
| 350 | if (!empty($viewAllForums[$myrow['forum_id']])) { |
||
| 351 | $forum_link = '<a href="' . XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $myrow['forum_id'] . '">' . $viewAllForums[$myrow['forum_id']]['forum_name'] . '</a>'; |
||
| 352 | } |
||
| 353 | |||
| 354 | $topic_title = $myts->htmlSpecialChars($myrow['topic_title']); |
||
| 355 | // irmtfan remove here and move to for loop |
||
| 356 | //if ($myrow['type_id'] > 0) { |
||
| 357 | //$topic_title = '<span style="color:'.$typen[$myrow["type_id"]]["type_color"].'">['.$typen[$myrow["type_id"]]["type_name"].']</span> '.$topic_title.''; |
||
| 358 | //} |
||
| 359 | if ($myrow['topic_digest']) { |
||
| 360 | $topic_title = "<span class='digest'>" . $topic_title . '</span>'; |
||
| 361 | } |
||
| 362 | |||
| 363 | if (0 == $excerpt) { |
||
| 364 | $topic_excerpt = ''; |
||
| 365 | } elseif (($myrow['post_karma'] > 0 || $myrow['require_reply'] > 0) && !\newbbIsAdmin($forum)) { |
||
| 366 | $topic_excerpt = ''; |
||
| 367 | } else { |
||
| 368 | $topic_excerpt = \xoops_substr(\newbbHtml2text($myts->displayTarea($myrow['post_text'])), 0, $excerpt); |
||
| 369 | $topic_excerpt = \str_replace('[', '[', $myts->htmlSpecialChars($topic_excerpt)); |
||
| 370 | } |
||
| 371 | // START irmtfan move here |
||
| 372 | $topics[$myrow['topic_id']] = [ |
||
| 373 | 'topic_id' => $myrow['topic_id'], |
||
| 374 | 'topic_icon' => $topic_icon, |
||
| 375 | 'type_id' => $myrow['type_id'], |
||
| 376 | //'type_text' => $topic_prefix,/*irmtfan remove here and move to for loop*/ |
||
| 377 | 'topic_title' => $topic_title, |
||
| 378 | //'topic_link' => XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $myrow['topic_id'], |
||
| 379 | 'topic_link' => 'viewtopic.php?topic_id=' . $myrow['topic_id'], |
||
| 380 | 'rating_img' => $rating_img, |
||
| 381 | 'topic_page_jump' => $topic_page_jump, |
||
| 382 | 'topic_page_jump_icon' => $topic_page_jump_icon, |
||
| 383 | 'topic_replies' => $myrow['topic_replies'], |
||
| 384 | |||
| 385 | 'topic_digest' => $myrow['topic_digest'], |
||
| 386 | //mb |
||
| 387 | |||
| 388 | 'topic_poster_uid' => $myrow['topic_poster'], |
||
| 389 | 'topic_poster_name' => $myts->htmlSpecialChars($myrow['poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous']), |
||
| 390 | 'topic_views' => $myrow['topic_views'], |
||
| 391 | 'topic_time' => \newbbFormatTimestamp($myrow['topic_time']), |
||
| 392 | 'topic_last_posttime' => \newbbFormatTimestamp($myrow['last_post_time']), |
||
| 393 | 'topic_last_poster_uid' => $myrow['uid'], |
||
| 394 | 'topic_last_poster_name' => $myts->htmlSpecialChars($myrow['last_poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous']), |
||
| 395 | 'topic_forum_link' => $forum_link, |
||
| 396 | 'topic_excerpt' => $topic_excerpt, |
||
| 397 | 'stick' => empty($myrow['topic_sticky']), |
||
| 398 | 'stats' => [ |
||
| 399 | $myrow['topic_status'], |
||
| 400 | $myrow['topic_digest'], |
||
| 401 | $myrow['topic_replies'], |
||
| 402 | ], |
||
| 403 | /* irmtfan uncomment use ib the for loop*/ |
||
| 404 | //"topic_poster" => $topic_poster,/*irmtfan remove here and move to for loop*/ |
||
| 405 | //"topic_last_poster" => $topic_last_poster,/*irmtfan remove here and move to for loop*/ |
||
| 406 | //"topic_folder" => newbbDisplayImage($topic_folder,$topic_folder_text),/*irmtfan remove here and move to for loop*/ |
||
| 407 | ]; |
||
| 408 | // END irmtfan move here |
||
| 409 | /* users */ |
||
| 410 | $posters[$myrow['topic_poster']] = 1; |
||
| 411 | $posters[$myrow['uid']] = 1; |
||
| 412 | // reads |
||
| 413 | if (!empty($GLOBALS['xoopsModuleConfig']['read_mode'])) { |
||
| 414 | $reads[$myrow['topic_id']] = (1 == $GLOBALS['xoopsModuleConfig']['read_mode']) ? $myrow['last_post_time'] : $myrow['topic_last_post_id']; |
||
| 415 | } |
||
| 416 | }// irmtfan while end |
||
| 417 | // START irmtfan move to a for loop |
||
| 418 | $posters_name = \newbbGetUnameFromIds(\array_keys($posters), $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
| 419 | //$topic_poster = newbbGetUnameFromId($myrow['topic_poster'], $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
| 420 | //$topic_last_poster = newbbGetUnameFromId($myrow['uid'], $GLOBALS['xoopsModuleConfig']['show_realname'], true); |
||
| 421 | $topic_isRead = \newbbIsRead('topic', $reads); |
||
| 422 | foreach (\array_keys($topics) as $id) { |
||
| 423 | $topics[$id]['topic_read'] = empty($topic_isRead[$id]) ? 0 : 1; // add topic-read/topic-new smarty variable |
||
| 424 | if (!empty($topics[$id]['type_id']) && isset($typen[$topics[$id]['type_id']])) { |
||
| 425 | $topics[$id]['topic_title'] = \getTopicTitle($topics[$id]['topic_title'], $typen[$topics[$id]['type_id']]['type_name'], $typen[$topics[$id]['type_id']]['type_color']); |
||
| 426 | } |
||
| 427 | //$topic_prefix = (!empty($typen[$myrow['type_id']])) ? getTopicTitle("", $typen[$myrow['type_id']]["type_name"], $typen[$myrow['type_id']]["type_color"]) : ""; |
||
| 428 | $topics[$id]['topic_poster'] = !empty($posters_name[$topics[$id]['topic_poster_uid']]) ? $posters_name[$topics[$id]['topic_poster_uid']] : $topics[$id]['topic_poster_name']; |
||
| 429 | $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']; |
||
| 430 | |||
| 431 | // ------------------------------------------------------ |
||
| 432 | // topic_folder: priority: newhot -> hot/new -> regular |
||
| 433 | [$topic_status, $topic_digest, $topic_replies] = $topics[$id]['stats']; |
||
| 434 | if (1 == $topic_status) { |
||
| 435 | $topic_folder = 'topic_locked'; |
||
| 436 | $topic_folder_text = \_MD_NEWBB_TOPICLOCKED; |
||
| 437 | } else { |
||
| 438 | if ($topic_digest) { |
||
| 439 | $topic_folder = 'topic_digest'; |
||
| 440 | $topic_folder_text = \_MD_NEWBB_TOPICDIGEST; |
||
| 441 | } elseif ($topic_replies >= $hot_threshold) { |
||
| 442 | $topic_folder = empty($topic_isRead[$id]) ? 'topic_hot_new' : 'topic_hot'; |
||
| 443 | $topic_folder_text = empty($topic_isRead[$id]) ? \_MD_NEWBB_MORETHAN : \_MD_NEWBB_MORETHAN2; |
||
| 444 | } else { |
||
| 445 | $topic_folder = empty($topic_isRead[$id]) ? 'topic_new' : 'topic'; |
||
| 446 | $topic_folder_text = empty($topic_isRead[$id]) ? \_MD_NEWBB_NEWPOSTS : \_MD_NEWBB_NONEWPOSTS; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | $topics[$id]['topic_folder'] = \newbbDisplayImage($topic_folder, $topic_folder_text); |
||
| 450 | unset($topics[$id]['topic_poster_name'], $topics[$id]['topic_last_poster_name'], $topics[$id]['stats']); |
||
| 451 | } // irmtfan end for loop |
||
| 452 | // END irmtfan move to a for loop |
||
| 453 | if (\count($topics) > 0) { |
||
| 454 | $sql = ' SELECT DISTINCT topic_id FROM ' . $this->db->prefix('newbb_posts') . " WHERE attachment != ''" . ' AND topic_id IN (' . \implode(',', \array_keys($topics)) . ')'; |
||
| 455 | $result = $this->db->query($sql); |
||
| 456 | if ($result) { |
||
| 457 | while (list($topic_id) = $this->db->fetchRow($result)) { |
||
| 458 | $topics[$topic_id]['attachment'] = ' ' . \newbbDisplayImage('attachment', \_MD_NEWBB_TOPICSHASATT); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | return [$topics, $sticky]; |
||
| 464 | } |
||
| 1072 |