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