| Total Complexity | 204 |
| Total Lines | 1129 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TopicRenderer 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 TopicRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Newbb; |
||
| 26 | class TopicRenderer |
||
| 27 | { |
||
| 28 | public $vars = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * reference to moduleConfig |
||
| 32 | */ |
||
| 33 | public $config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Current user has no access to current page |
||
| 37 | */ |
||
| 38 | private $noperm = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * For multiple forums |
||
| 42 | */ |
||
| 43 | public $is_multiple = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * force to parse vars (run against static vars) irmtfan |
||
| 47 | */ |
||
| 48 | public $force = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Vistitor's level: 0 - anonymous; 1 - user; 2 - moderator or admin |
||
| 52 | */ |
||
| 53 | public $userlevel = 0; |
||
| 54 | |||
| 55 | public $query = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * reference to an object handler |
||
| 59 | */ |
||
| 60 | private $handler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Requested page |
||
| 64 | */ |
||
| 65 | private $page = 'list.topic.php'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * query variables |
||
| 69 | */ |
||
| 70 | private $args = ['forum', 'uid', 'lastposter', 'type', 'status', 'mode', 'sort', 'order', 'start', 'since']; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Constructor |
||
| 74 | */ |
||
| 75 | // public function TopicRenderer() |
||
| 76 | public function __construct() |
||
| 77 | { |
||
| 78 | $this->handler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Access the only instance of this class |
||
| 83 | * @return TopicRenderer |
||
| 84 | */ |
||
| 85 | public static function getInstance() |
||
| 86 | { |
||
| 87 | static $instance; |
||
| 88 | if (null === $instance) { |
||
| 89 | $instance = new static(); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $instance; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function init() |
||
| 96 | { |
||
| 97 | $this->noperm = false; |
||
| 98 | $this->query = []; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $var |
||
| 103 | * @param $val |
||
| 104 | * @return array|int|string |
||
| 105 | */ |
||
| 106 | public function setVar($var, $val) |
||
| 107 | { |
||
| 108 | switch ($var) { |
||
| 109 | case 'forum': |
||
| 110 | if (is_numeric($val)) { |
||
| 111 | $val = (int)$val; |
||
| 112 | // START irmtfan - if the forum is array |
||
| 113 | } elseif (is_array($val)) { |
||
| 114 | $val = implode('|', $val); |
||
| 115 | //} elseif (!empty($val)) { |
||
| 116 | // $val = implode("|", array_map("intval", explode(", ", $val))); |
||
| 117 | } |
||
| 118 | // END irmtfan - if the forum is array |
||
| 119 | break; |
||
| 120 | |||
| 121 | case 'type': |
||
| 122 | case 'mode': |
||
| 123 | case 'order': |
||
| 124 | case 'start': |
||
| 125 | case 'since': |
||
| 126 | $val = (int)$val; |
||
| 127 | break; |
||
| 128 | |||
| 129 | case 'uid': // irmtfan add multi topic poster |
||
| 130 | case 'lastposter': // irmtfan add multi lastposter |
||
| 131 | break; |
||
| 132 | |||
| 133 | case 'status': |
||
| 134 | // START irmtfan to accept multiple status |
||
| 135 | $val = is_array($val) ? $val : [$val]; |
||
| 136 | $val = implode(',', $val); |
||
| 137 | //$val = (in_array($val, array_keys($this->getStatus( $this->userlevel ))) ) ? $val : "all"; //irmtfan no need to check if status is empty or not |
||
| 138 | //if ($val === "all" && !$this->is_multiple) $val = ""; irmtfan commented because it is done in sort |
||
| 139 | // END irmtfan to accept multiple status |
||
| 140 | break; |
||
| 141 | |||
| 142 | default: |
||
| 143 | break; |
||
| 144 | } |
||
| 145 | |||
| 146 | return $val; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $vars |
||
| 151 | */ |
||
| 152 | public function setVars(array $vars = []) |
||
| 153 | { |
||
| 154 | $this->init(); |
||
| 155 | |||
| 156 | foreach ($vars as $var => $val) { |
||
| 157 | if (!in_array($var, $this->args)) { |
||
| 158 | continue; |
||
| 159 | } |
||
| 160 | $this->vars[$var] = $this->setVar($var, $val); |
||
| 161 | } |
||
| 162 | $this->parseVars(); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param null $status |
||
| 167 | */ |
||
| 168 | public function myParseStatus($status = null) |
||
| 169 | { |
||
| 170 | switch ($status) { |
||
| 171 | case 'digest': |
||
| 172 | $this->query['where'][] = 't.topic_digest = 1'; |
||
| 173 | break; |
||
| 174 | |||
| 175 | case 'undigest': |
||
| 176 | $this->query['where'][] = 't.topic_digest = 0'; |
||
| 177 | break; |
||
| 178 | |||
| 179 | case 'sticky': |
||
| 180 | $this->query['where'][] = 't.topic_sticky = 1'; |
||
| 181 | break; |
||
| 182 | |||
| 183 | case 'unsticky': |
||
| 184 | $this->query['where'][] = 't.topic_sticky = 0'; |
||
| 185 | break; |
||
| 186 | |||
| 187 | case 'lock': |
||
| 188 | $this->query['where'][] = 't.topic_status = 1'; |
||
| 189 | break; |
||
| 190 | |||
| 191 | case 'unlock': |
||
| 192 | $this->query['where'][] = 't.topic_status = 0'; |
||
| 193 | break; |
||
| 194 | |||
| 195 | case 'poll': |
||
| 196 | $this->query['where'][] = 't.topic_haspoll = 1'; |
||
| 197 | break; |
||
| 198 | |||
| 199 | case 'unpoll': |
||
| 200 | $this->query['where'][] = 't.topic_haspoll = 0'; |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 'voted': |
||
| 204 | $this->query['where'][] = 't.votes > 0'; |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 'unvoted': |
||
| 208 | $this->query['where'][] = 't.votes < 1'; |
||
| 209 | break; |
||
| 210 | |||
| 211 | case 'replied': |
||
| 212 | $this->query['where'][] = 't.topic_replies > 0'; |
||
| 213 | break; |
||
| 214 | |||
| 215 | case 'unreplied': |
||
| 216 | $this->query['where'][] = 't.topic_replies < 1'; |
||
| 217 | break; |
||
| 218 | |||
| 219 | case 'viewed': |
||
| 220 | $this->query['where'][] = 't.topic_views > 0'; |
||
| 221 | break; |
||
| 222 | |||
| 223 | case 'unviewed': |
||
| 224 | $this->query['where'][] = 't.topic_views < 1'; |
||
| 225 | break; |
||
| 226 | |||
| 227 | case 'read': |
||
| 228 | // Skip |
||
| 229 | if (empty($this->config['read_mode'])) { |
||
| 230 | // Use database |
||
| 231 | } elseif (2 == $this->config['read_mode']) { |
||
| 232 | // START irmtfan use read_uid to find the unread posts when the user is logged in |
||
| 233 | $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 234 | if (!empty($read_uid)) { |
||
| 235 | $this->query['join'][] = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_reads_topic') . ' AS r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' '; |
||
| 236 | $this->query['where'][] = 'r.post_id = t.topic_last_post_id'; |
||
| 237 | } else { |
||
| 238 | } |
||
| 239 | // END irmtfan change criteria to get from uid p.uid = last post submit user id |
||
| 240 | // User cookie |
||
| 241 | } elseif (1 == $this->config['read_mode']) { |
||
| 242 | // START irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 243 | $startdate = !empty($this->vars['since']) ? (time() - newbbGetSinceTime($this->vars['since'])) : 0; |
||
| 244 | if ($lastvisit = max($GLOBALS['last_visit'], $startdate)) { |
||
| 245 | $readmode1query = ''; |
||
| 246 | if ($lastvisit > $startdate) { |
||
| 247 | $readmode1query = 'p.post_time < ' . $lastvisit; |
||
| 248 | } |
||
| 249 | $topics = []; |
||
| 250 | $topic_lastread = newbbGetCookie('LT', true); |
||
| 251 | if (count($topic_lastread) > 0) { |
||
| 252 | foreach ($topic_lastread as $id => $time) { |
||
| 253 | if ($time > $lastvisit) { |
||
| 254 | $topics[] = $id; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | if (count($topics) > 0) { |
||
| 259 | $topicquery = ' t.topic_id IN (' . implode(',', $topics) . ')'; |
||
| 260 | // because it should be OR |
||
| 261 | $readmode1query = !empty($readmode1query) ? '(' . $readmode1query . ' OR ' . $topicquery . ')' : $topicquery; |
||
| 262 | } |
||
| 263 | $this->query['where'][] = $readmode1query; |
||
| 264 | } |
||
| 265 | // END irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 266 | } |
||
| 267 | break; |
||
| 268 | |||
| 269 | case 'unread': |
||
| 270 | // Skip |
||
| 271 | if (empty($this->config['read_mode'])) { |
||
| 272 | // Use database |
||
| 273 | } elseif (2 == $this->config['read_mode']) { |
||
| 274 | // START irmtfan use read_uid to find the unread posts when the user is logged in |
||
| 275 | $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 276 | if (!empty($read_uid)) { |
||
| 277 | $this->query['join'][] = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_reads_topic') . ' AS r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' '; |
||
| 278 | $this->query['where'][] = '(r.read_id IS NULL OR r.post_id < t.topic_last_post_id)'; |
||
| 279 | } else { |
||
| 280 | } |
||
| 281 | // END irmtfan change criteria to get from uid p.uid = last post submit user id |
||
| 282 | // User cookie |
||
| 283 | } elseif (1 == $this->config['read_mode']) { |
||
| 284 | // START irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 285 | $startdate = !empty($this->vars['since']) ? (time() - newbbGetSinceTime($this->vars['since'])) : 0; |
||
| 286 | if ($lastvisit = max($GLOBALS['last_visit'], $startdate)) { |
||
| 287 | if ($lastvisit > $startdate) { |
||
| 288 | $this->query['where'][] = 'p.post_time > ' . $lastvisit; |
||
| 289 | } |
||
| 290 | $topics = []; |
||
| 291 | $topic_lastread = newbbGetCookie('LT', true); |
||
| 292 | if (count($topic_lastread) > 0) { |
||
| 293 | foreach ($topic_lastread as $id => $time) { |
||
| 294 | if ($time > $lastvisit) { |
||
| 295 | $topics[] = $id; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | if (count($topics) > 0) { |
||
| 300 | $this->query['where'][] = ' t.topic_id NOT IN (' . implode(',', $topics) . ')'; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | // END irmtfan fix read_mode = 1 bugs - for all users (member and anon) |
||
| 304 | } |
||
| 305 | break; |
||
| 306 | |||
| 307 | case 'pending': |
||
| 308 | if ($this->userlevel < 2) { |
||
| 309 | $this->noperm = true; |
||
| 310 | } else { |
||
| 311 | $this->query['where'][] = 't.approved = 0'; |
||
| 312 | } |
||
| 313 | break; |
||
| 314 | |||
| 315 | case 'deleted': |
||
| 316 | if ($this->userlevel < 2) { |
||
| 317 | $this->noperm = true; |
||
| 318 | } else { |
||
| 319 | $this->query['where'][] = 't.approved = -1'; |
||
| 320 | } |
||
| 321 | break; |
||
| 322 | |||
| 323 | case 'all': // For viewall.php; do not display sticky topics at first |
||
| 324 | case 'active': // same as 'all' |
||
| 325 | $this->query['where'][] = 't.approved = 1'; |
||
| 326 | break; |
||
| 327 | |||
| 328 | default: // irmtfan do nothing |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param $var |
||
| 335 | * @param $val |
||
| 336 | */ |
||
| 337 | public function parseVar($var, $val) |
||
| 338 | { |
||
| 339 | switch ($var) { |
||
| 340 | case 'forum': |
||
| 341 | /** @var Newbb\ForumHandler $forumHandler */ |
||
| 342 | $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum'); |
||
| 343 | // START irmtfan - get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums |
||
| 344 | // Get accessible forums |
||
| 345 | $accessForums = $forumHandler->getIdsByValues(array_map('intval', @explode('|', $val))); |
||
| 346 | // Filter specified forums if any |
||
| 347 | //if (!empty($val) && $_forums = @explode('|', $val)) { |
||
| 348 | //$accessForums = array_intersect($accessForums, array_map('intval', $_forums)); |
||
| 349 | //} |
||
| 350 | $this->vars['forum'] = $this->setVar('forum', $accessForums); |
||
| 351 | // END irmtfan - get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums |
||
| 352 | |||
| 353 | if (empty($accessForums)) { |
||
| 354 | $this->noperm = true; |
||
| 355 | // irmtfan - it just return return the forum_id only when the forum_id is the first allowed forum - no need for this code implode is enough removed. |
||
| 356 | //} elseif (count($accessForums) === 1) { |
||
| 357 | //$this->query["where"][] = "t.forum_id = " . $accessForums[0]; |
||
| 358 | } else { |
||
| 359 | $this->query['where'][] = 't.forum_id IN ( ' . implode(', ', $accessForums) . ' )'; |
||
| 360 | } |
||
| 361 | break; |
||
| 362 | |||
| 363 | case 'uid': // irmtfan add multi topic poster |
||
| 364 | if (-1 !== $val) { |
||
| 365 | $val = implode(',', array_map('intval', explode(',', $val))); |
||
| 366 | $this->query['where'][] = 't.topic_poster IN ( ' . $val . ' )'; |
||
| 367 | } |
||
| 368 | break; |
||
| 369 | case 'lastposter': // irmtfan add multi lastposter |
||
| 370 | if (-1 !== $val) { |
||
| 371 | $val = implode(',', array_map('intval', explode(',', $val))); |
||
| 372 | $this->query['where'][] = 'p.uid IN ( ' . $val . ' )'; |
||
| 373 | } |
||
| 374 | break; |
||
| 375 | |||
| 376 | case 'since': |
||
| 377 | if (!empty($val)) { |
||
| 378 | // START irmtfan if unread && read_mode = 1 and last_visit > startdate do not add where query | to accept multiple status |
||
| 379 | $startdate = time() - newbbGetSinceTime($val); |
||
| 380 | if (in_array('unread', explode(',', $this->vars['status'], true)) && 1 == $this->config['read_mode'] |
||
| 381 | && $GLOBALS['last_visit'] > $startdate) { |
||
| 382 | break; |
||
| 383 | } |
||
| 384 | // irmtfan digest_time | to accept multiple status |
||
| 385 | if (in_array('digest', explode(',', $this->vars['status'], true))) { |
||
| 386 | $this->query['where'][] = 't.digest_time > ' . $startdate; |
||
| 387 | } |
||
| 388 | // irmtfan - should be >= instead of = |
||
| 389 | $this->query['where'][] = 'p.post_time >= ' . $startdate; |
||
| 390 | // END irmtfan if unread && read_mode = 1 and last_visit > startdate do not add where query |
||
| 391 | } |
||
| 392 | break; |
||
| 393 | |||
| 394 | case 'type': |
||
| 395 | if (!empty($val)) { |
||
| 396 | $this->query['where'][] = 't.type_id = ' . $val; |
||
| 397 | } |
||
| 398 | break; |
||
| 399 | |||
| 400 | case 'status': |
||
| 401 | // START irmtfan to accept multiple status |
||
| 402 | $val = explode(',', $val); |
||
| 403 | // irmtfan - add 'all' to always parse t.approved = 1 |
||
| 404 | if (0 === count(array_intersect($val, ['all', 'active', 'pending', 'deleted']))) { |
||
| 405 | $val[] = 'all'; |
||
| 406 | } |
||
| 407 | foreach ($val as $key => $status) { |
||
| 408 | $this->myParseStatus($status); |
||
| 409 | } |
||
| 410 | // END irmtfan to accept multiple status |
||
| 411 | break; |
||
| 412 | |||
| 413 | case 'sort': |
||
| 414 | if ($sort = $this->getSort($val, 'sort')) { |
||
| 415 | $this->query['sort'][] = $sort . (empty($this->vars['order']) ? ' DESC' : ' ASC'); |
||
| 416 | } else { // irmtfan if sort is not in the list |
||
| 417 | $this->query['sort'][] = 't.topic_last_post_id' . (empty($this->vars['order']) ? ' DESC' : ' ASC'); |
||
| 418 | } |
||
| 419 | break; |
||
| 420 | |||
| 421 | default: |
||
| 422 | break; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function parseVars() |
||
| 430 | { |
||
| 431 | static $parsed; |
||
| 432 | // irmtfan - force to parse vars (run against static vars) |
||
| 433 | if (isset($parsed) && !$this->force) { |
||
| 434 | return true; |
||
| 435 | } |
||
| 436 | |||
| 437 | if (!isset($this->vars['forum'])) { |
||
| 438 | $this->vars['forum'] = null; |
||
| 439 | } |
||
| 440 | //irmtfan parse status for rendering topic correctly - if empty($_GET(status)) it will show all topics include deleted and pendings. 'all' instead of all |
||
| 441 | if (!isset($this->vars['status'])) { |
||
| 442 | $this->vars['status'] = 'all'; |
||
| 443 | } |
||
| 444 | // irmtfan if sort is not set or is empty get a default sort- if empty($_GET(sort)) | if sort=null eg: /list.topic.php?sort= |
||
| 445 | if (empty($this->vars['sort'])) { |
||
| 446 | $this->vars['sort'] = 'lastpost'; |
||
| 447 | } // use lastpost instead of sticky |
||
| 448 | |||
| 449 | foreach ($this->vars as $var => $val) { |
||
| 450 | $this->parseVar($var, $val); |
||
| 451 | if (empty($val)) { |
||
| 452 | unset($this->vars[$var]); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | $parsed = true; |
||
| 456 | |||
| 457 | return true; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param null $header |
||
| 462 | * @param null $var |
||
| 463 | * @return array|null |
||
| 464 | */ |
||
| 465 | public function getSort($header = null, $var = null) |
||
| 466 | { |
||
| 467 | $headers = [ |
||
| 468 | 'topic' => [ |
||
| 469 | 'title' => _MD_NEWBB_TOPICS, |
||
| 470 | 'sort' => 't.topic_title' |
||
| 471 | ], |
||
| 472 | 'forum' => [ |
||
| 473 | 'title' => _MD_NEWBB_FORUM, |
||
| 474 | 'sort' => 't.forum_id' |
||
| 475 | ], |
||
| 476 | 'poster' => [ |
||
| 477 | 'title' => _MD_NEWBB_TOPICPOSTER, /*irmtfan _MD_NEWBB_POSTER to _MD_NEWBB_TOPICPOSTER*/ |
||
| 478 | 'sort' => 't.topic_poster' |
||
| 479 | ], |
||
| 480 | 'replies' => [ |
||
| 481 | 'title' => _MD_NEWBB_REPLIES, |
||
| 482 | 'sort' => 't.topic_replies' |
||
| 483 | ], |
||
| 484 | 'views' => [ |
||
| 485 | 'title' => _MD_NEWBB_VIEWS, |
||
| 486 | 'sort' => 't.topic_views' |
||
| 487 | ], |
||
| 488 | 'lastpost' => [ // irmtfan show topic_page_jump_icon smarty |
||
| 489 | 'title' => _MD_NEWBB_LASTPOST, |
||
| 490 | /*irmtfan _MD_NEWBB_DATE to _MD_NEWBB_LASTPOSTTIME again change to _MD_LASTPOST*/ |
||
| 491 | 'sort' => 't.topic_last_post_id' |
||
| 492 | ], |
||
| 493 | // START irmtfan add more sorts |
||
| 494 | 'lastposttime' => [ // irmtfan same as lastpost |
||
| 495 | 'title' => _MD_NEWBB_LASTPOSTTIME, |
||
| 496 | 'sort' => 't.topic_last_post_id' |
||
| 497 | ], |
||
| 498 | 'lastposter' => [ // irmtfan |
||
| 499 | 'title' => _MD_NEWBB_POSTER, |
||
| 500 | 'sort' => 'p.uid',// poster uid |
||
| 501 | ], |
||
| 502 | 'lastpostmsgicon' => [ // irmtfan |
||
| 503 | 'title' => _MD_NEWBB_MESSAGEICON, |
||
| 504 | 'sort' => 'p.icon',// post message icon |
||
| 505 | ], |
||
| 506 | 'ratings' => [ |
||
| 507 | 'title' => _MD_NEWBB_RATINGS, |
||
| 508 | 'sort' => 't.rating', // irmtfan t.topic_rating to t.rating |
||
| 509 | ], |
||
| 510 | 'votes' => [ |
||
| 511 | 'title' => _MD_NEWBB_VOTES, |
||
| 512 | 'sort' => 't.votes' |
||
| 513 | ], |
||
| 514 | 'publish' => [ |
||
| 515 | 'title' => _MD_NEWBB_TOPICTIME, |
||
| 516 | 'sort' => 't.topic_id' |
||
| 517 | ], |
||
| 518 | 'digest' => [ |
||
| 519 | 'title' => _MD_NEWBB_DIGEST, |
||
| 520 | 'sort' => 't.digest_time' |
||
| 521 | ], |
||
| 522 | 'sticky' => [ |
||
| 523 | 'title' => _MD_NEWBB_STICKY, |
||
| 524 | 'sort' => 't.topic_sticky' |
||
| 525 | ], |
||
| 526 | 'lock' => [ |
||
| 527 | 'title' => _MD_NEWBB_LOCK, |
||
| 528 | 'sort' => 't.topic_status' |
||
| 529 | ], |
||
| 530 | 'poll' => [ |
||
| 531 | 'title' => _MD_NEWBB_POLL_POLL, |
||
| 532 | 'sort' => 't.poll_id' |
||
| 533 | ] |
||
| 534 | ]; |
||
| 535 | $types = $this->getTypes(); |
||
| 536 | if (!empty($types)) { |
||
| 537 | $headers['type'] = [ |
||
| 538 | 'title' => _MD_NEWBB_TYPE, |
||
| 539 | 'sort' => 't.type_id' |
||
| 540 | ]; |
||
| 541 | } |
||
| 542 | if (2 == $this->userlevel) { |
||
| 543 | $headers['approve'] = [ |
||
| 544 | 'title' => _MD_NEWBB_APPROVE, |
||
| 545 | 'sort' => 't.approved' |
||
| 546 | ]; |
||
| 547 | } |
||
| 548 | // END irmtfan add more sorts |
||
| 549 | if (empty($header) && empty($var)) { |
||
| 550 | return $headers; |
||
| 551 | } |
||
| 552 | if (!empty($var) && !empty($header)) { |
||
| 553 | return @$headers[$header][$var]; |
||
| 554 | } |
||
| 555 | if (empty($var)) { |
||
| 556 | return @$headers[$header]; |
||
| 557 | } |
||
| 558 | $ret = null; |
||
| 559 | foreach (array_keys($headers) as $key) { |
||
| 560 | $ret[$key] = @$headers[$key][$var]; |
||
| 561 | } |
||
| 562 | |||
| 563 | return $ret; |
||
| 564 | } |
||
| 565 | |||
| 566 | // START irmtfan add Display topic headers function |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param null $header |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | public function getHeader($header = null) |
||
| 573 | { |
||
| 574 | $headersSort = $this->getSort('', 'title'); |
||
| 575 | // additional headers - important: those cannot be in sort anyway |
||
| 576 | $headers = array_merge($headersSort, [ |
||
| 577 | 'attachment' => _MD_NEWBB_TOPICSHASATT, // show attachment smarty |
||
| 578 | 'read' => _MD_NEWBB_MARK_UNREAD . '|' . _MD_NEWBB_MARK_READ, // read/unread show topic_folder smarty |
||
| 579 | 'pagenav' => _MD_NEWBB_PAGENAV_DISPLAY, // show topic_page_jump smarty - sort by topic_replies? |
||
| 580 | ]); |
||
| 581 | |||
| 582 | return $this->getFromKeys($headers, $header); |
||
| 583 | } |
||
| 584 | |||
| 585 | // END irmtfan add Display topic headers function |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param null $type |
||
| 589 | * @param null $status |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function getStatus($type = null, $status = null) |
||
| 593 | { |
||
| 594 | $links = [ |
||
| 595 | //"" => "", /* irmtfan remove empty array */ |
||
| 596 | 'all' => _ALL, |
||
| 597 | 'digest' => _MD_NEWBB_DIGEST, |
||
| 598 | 'undigest' => _MD_NEWBB_UNDIGEST, // irmtfan add |
||
| 599 | 'sticky' => _MD_NEWBB_STICKY, // irmtfan add |
||
| 600 | 'unsticky' => _MD_NEWBB_UNSTICKY, // irmtfan add |
||
| 601 | 'lock' => _MD_NEWBB_LOCK, // irmtfan add |
||
| 602 | 'unlock' => _MD_NEWBB_UNLOCK, // irmtfan add |
||
| 603 | 'poll' => _MD_NEWBB_TOPICHASPOLL, // irmtfan add |
||
| 604 | 'unpoll' => _MD_NEWBB_TOPICHASNOTPOLL, // irmtfan add |
||
| 605 | 'voted' => _MD_NEWBB_VOTED, // irmtfan add |
||
| 606 | 'unvoted' => _MD_NEWBB_UNVOTED, // irmtfan add |
||
| 607 | 'viewed' => _MD_NEWBB_VIEWED, // irmtfan add |
||
| 608 | 'unviewed' => _MD_NEWBB_UNVIEWED, // irmtfan add |
||
| 609 | 'replied' => _MD_NEWBB_REPLIED, // irmtfan add |
||
| 610 | 'unreplied' => _MD_NEWBB_UNREPLIED, |
||
| 611 | 'read' => _MD_NEWBB_READ, // irmtfan add |
||
| 612 | 'unread' => _MD_NEWBB_UNREAD |
||
| 613 | ]; |
||
| 614 | $links_admin = [ |
||
| 615 | 'active' => _MD_NEWBB_TYPE_ADMIN, |
||
| 616 | 'pending' => _MD_NEWBB_TYPE_PENDING, |
||
| 617 | 'deleted' => _MD_NEWBB_TYPE_DELETED |
||
| 618 | ]; |
||
| 619 | |||
| 620 | // all status, for admin |
||
| 621 | if ($type > 1) { |
||
| 622 | $links = array_merge($links, $links_admin);// irmtfan to accept multiple status |
||
| 623 | } |
||
| 624 | |||
| 625 | return $this->getFromKeys($links, $status); // irmtfan to accept multiple status |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param \Smarty $xoopsTpl |
||
| 630 | * @throws \RuntimeException |
||
| 631 | */ |
||
| 632 | public function buildSelection(\Smarty $xoopsTpl) |
||
| 633 | { |
||
| 634 | $selection = ['action' => $this->page]; |
||
| 635 | $selection['vars'] = $this->vars; |
||
| 636 | require_once __DIR__ . '/../include/functions.forum.php'; |
||
| 637 | $forum_selected = empty($this->vars['forum']) ? null : explode('|', @$this->vars['forum']); |
||
| 638 | $selection['forum'] = '<select name="forum[]" multiple="multiple">'; |
||
| 639 | $selection['forum'] .= '<option value="0">' . _MD_NEWBB_ALL . '</option>'; |
||
| 640 | $selection['forum'] .= newbbForumSelectBox($forum_selected); |
||
| 641 | $selection['forum'] .= '</select>'; |
||
| 642 | |||
| 643 | $sort_selected = $this->vars['sort']; |
||
| 644 | $sorts = $this->getSort('', 'title'); |
||
| 645 | $selection['sort'] = "<select name='sort'>"; |
||
| 646 | if (!is_array($sorts)) { |
||
| 647 | throw new \RuntimeException('$sorts must be an array.'); |
||
| 648 | } |
||
| 649 | foreach ($sorts as $sort => $title) { |
||
| 650 | $selection['sort'] .= "<option value='" . $sort . "' " . (($sort == $sort_selected) ? " selected='selected'" : '') . '>' . $title . '</option>'; |
||
| 651 | } |
||
| 652 | $selection['sort'] .= '</select>'; |
||
| 653 | |||
| 654 | $selection['order'] = "<select name='order'>"; |
||
| 655 | $selection['order'] .= "<option value='0' " . (empty($this->vars['order']) ? " selected='selected'" : '') . '>' . _DESCENDING . '</option>'; |
||
| 656 | $selection['order'] .= "<option value='1' " . (!empty($this->vars['order']) ? " selected='selected'" : '') . '>' . _ASCENDING . '</option>'; |
||
| 657 | $selection['order'] .= '</select>'; |
||
| 658 | |||
| 659 | $since = isset($this->vars['since']) ? $this->vars['since'] : $this->config['since_default']; |
||
| 660 | $selection['since'] = newbbSinceSelectBox($since); |
||
| 661 | |||
| 662 | $xoopsTpl->assign_by_ref('selection', $selection); |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @param \Smarty $xoopsTpl |
||
| 667 | */ |
||
| 668 | public function buildSearch(\Smarty $xoopsTpl) |
||
| 669 | { |
||
| 670 | $search = []; |
||
| 671 | $search['forum'] = @$this->vars['forum']; |
||
| 672 | $search['since'] = @$this->vars['since']; |
||
| 673 | $search['searchin'] = 'both'; |
||
| 674 | |||
| 675 | $xoopsTpl->assign_by_ref('search', $search); |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param \Smarty $xoopsTpl |
||
| 680 | * @throws \RuntimeException |
||
| 681 | */ |
||
| 682 | public function buildHeaders(\Smarty $xoopsTpl) |
||
| 683 | { |
||
| 684 | $args = []; |
||
| 685 | foreach ($this->vars as $var => $val) { |
||
| 686 | if ('sort' === $var || 'order' === $var) { |
||
| 687 | continue; |
||
| 688 | } |
||
| 689 | $args[] = "{$var}={$val}"; |
||
| 690 | } |
||
| 691 | |||
| 692 | $headers = $this->getSort('', 'title'); |
||
| 693 | if (!is_array($headers)) { |
||
| 694 | throw new \RuntimeException('$headers must be an array.'); |
||
| 695 | } |
||
| 696 | foreach ($headers as $header => $title) { |
||
| 697 | $_args = ["sort={$header}"]; |
||
| 698 | if (@$this->vars['sort'] == $header) { |
||
| 699 | $_args[] = 'order=' . ((@$this->vars['order'] + 1) % 2); |
||
| 700 | } |
||
| 701 | $headers_data[$header]['title'] = $title; |
||
| 702 | $headers_data[$header]['link'] = $this->page . '?' . implode('&', array_merge($args, $_args)); |
||
| 703 | } |
||
| 704 | $xoopsTpl->assign_by_ref('headers', $headers_data); |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @param \Smarty $xoopsTpl |
||
| 709 | */ |
||
| 710 | public function buildFilters(\Smarty $xoopsTpl) |
||
| 711 | { |
||
| 712 | $args = []; |
||
| 713 | foreach ($this->vars as $var => $val) { |
||
| 714 | if ('status' === $var) { |
||
| 715 | continue; |
||
| 716 | } |
||
| 717 | $args[] = "{$var}={$val}"; |
||
| 718 | } |
||
| 719 | |||
| 720 | $links = $this->getStatus($this->userlevel); |
||
| 721 | |||
| 722 | $status = []; |
||
| 723 | foreach ($links as $link => $title) { |
||
| 724 | $_args = ["status={$link}"]; |
||
| 725 | $status[$link]['title'] = $title; |
||
| 726 | $status[$link]['link'] = $this->page . '?' . implode('&', array_merge($args, $_args)); |
||
| 727 | } |
||
| 728 | $xoopsTpl->assign_by_ref('filters', $status); |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @param null $type_id |
||
| 733 | * @return mixed |
||
| 734 | */ |
||
| 735 | public function getTypes($type_id = null) |
||
| 736 | { |
||
| 737 | static $types; |
||
| 738 | if (!isset($types)) { |
||
| 739 | /** @var Newbb\TypeHandler $typeHandler */ |
||
| 740 | $typeHandler = Newbb\Helper::getInstance()->getHandler('Type'); |
||
| 741 | |||
| 742 | $types = $typeHandler->getByForum(explode('|', @$this->vars['forum'])); |
||
| 743 | } |
||
| 744 | |||
| 745 | if (empty($type_id)) { |
||
| 746 | return $types; |
||
| 747 | } |
||
| 748 | |||
| 749 | return @$types[$type_id]; |
||
| 750 | } |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @param \Smarty $xoopsTpl |
||
| 754 | * @return bool |
||
| 755 | */ |
||
| 756 | public function buildTypes(\Smarty $xoopsTpl) |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @param \Smarty $xoopsTpl |
||
| 781 | * @return bool |
||
| 782 | */ |
||
| 783 | public function buildCurrent(\Smarty $xoopsTpl) |
||
| 784 | { |
||
| 785 | if (empty($this->vars['status']) && !$this->is_multiple) { |
||
| 786 | return true; |
||
| 787 | } |
||
| 788 | |||
| 789 | $args = []; |
||
| 790 | foreach ($this->vars as $var => $val) { |
||
| 791 | $args[] = "{$var}={$val}"; |
||
| 792 | } |
||
| 793 | |||
| 794 | $status = []; |
||
| 795 | $status['title'] = implode(',', $this->getStatus($this->userlevel, $this->vars['status'])); // irmtfan to accept multiple status |
||
| 796 | //$status['link'] = $this->page.(empty($this->vars['status']) ? '' : '?status='.$this->vars['status']); |
||
| 797 | $status['link'] = $this->page . (empty($args) ? '' : '?' . implode('&', $args)); |
||
| 798 | |||
| 799 | $xoopsTpl->assign_by_ref('current', $status); |
||
| 800 | } |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @param \Smarty $xoopsTpl |
||
| 804 | */ |
||
| 805 | public function buildPagenav(\Smarty $xoopsTpl) |
||
| 806 | { |
||
| 807 | $count_topic = $this->getCount(); |
||
| 808 | if ($count_topic > $this->config['topics_per_page']) { |
||
| 809 | $args = []; |
||
| 810 | foreach ($this->vars as $var => $val) { |
||
| 811 | if ('start' === $var) { |
||
| 812 | continue; |
||
| 813 | } |
||
| 814 | $args[] = "{$var}={$val}"; |
||
| 815 | } |
||
| 816 | require_once $GLOBALS['xoops']->path('class/pagenav.php'); |
||
| 817 | $nav = new \XoopsPageNav($count_topic, $this->config['topics_per_page'], @$this->vars['start'], 'start', implode('&', $args)); |
||
| 818 | if (isset($GLOBALS['xoopsModuleConfig']['do_rewrite'])) { |
||
| 819 | $nav->url = formatURL(Request::getString('SERVER_NAME', '', 'SERVER')) . ' /' . $nav->url; |
||
| 820 | } |
||
| 821 | if ('select' === $this->config['pagenav_display']) { |
||
| 822 | $navi = $nav->renderSelect(); |
||
| 823 | } elseif ('image' === $this->config['pagenav_display']) { |
||
| 824 | $navi = $nav->renderImageNav(4); |
||
| 825 | } else { |
||
| 826 | $navi = $nav->renderNav(4); |
||
| 827 | } |
||
| 828 | $xoopsTpl->assign('pagenav', $navi); |
||
| 829 | } else { |
||
| 830 | $xoopsTpl->assign('pagenav', ''); |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @return int |
||
| 836 | */ |
||
| 837 | public function getCount() |
||
| 838 | { |
||
| 839 | if ($this->noperm) { |
||
| 840 | return 0; |
||
| 841 | } |
||
| 842 | |||
| 843 | $selects = []; |
||
| 844 | $froms = []; |
||
| 845 | $joins = []; |
||
| 846 | $wheres = []; |
||
| 847 | |||
| 848 | // topic fields |
||
| 849 | $selects[] = 'COUNT(*)'; |
||
| 850 | |||
| 851 | $froms[] = $this->handler->db->prefix('newbb_topics') . ' AS t '; |
||
| 852 | $joins[] = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_posts') . ' AS p ON p.post_id = t.topic_last_post_id'; |
||
| 853 | $wheres[] = '1 = 1'; |
||
| 854 | |||
| 855 | $sql = ' SELECT ' . implode(', ', $selects) . ' FROM ' . implode(', ', $froms) . ' ' . implode(' ', $joins) . (!empty($this->query['join']) ? ' ' . implode(' ', $this->query['join']) : '') . // irmtfan bug fix: Undefined index: join when post_excerpt = 0 |
||
| 856 | ' WHERE ' . implode(' AND ', $wheres) . ' AND ' . @implode(' AND ', @$this->query['where']); |
||
| 857 | |||
| 858 | if (!$result = $this->handler->db->query($sql)) { |
||
| 859 | return 0; |
||
| 860 | } |
||
| 861 | list($count) = $this->handler->db->fetchRow($result); |
||
| 862 | |||
| 863 | return $count; |
||
| 864 | } |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @param \Smarty $xoopsTpl |
||
| 868 | * @return array|void |
||
| 869 | */ |
||
| 870 | public function renderTopics(\Smarty $xoopsTpl = null) |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | // START irmtfan to create an array from selected keys of an array |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * @param $array |
||
| 1139 | * @param null $keys |
||
| 1140 | * @return array |
||
| 1141 | */ |
||
| 1142 | public function getFromKeys($array, $keys = null) |
||
| 1143 | { |
||
| 1144 | if (empty($keys)) { |
||
| 1145 | return $array; |
||
| 1146 | } // all keys |
||
| 1147 | $keyarr = is_string($keys) ? explode(',', $keys) : $keys; |
||
| 1158 |
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.