| Total Complexity | 55 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Markasread 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 Markasread, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Markasread extends AbstractController |
||
| 25 | { |
||
| 26 | /** @var array used to redirect user to the correct boards when marking unread */ |
||
| 27 | private $_querystring_board_limits; |
||
| 28 | |||
| 29 | /** @var array used to remember user's sorting options when marking unread */ |
||
| 30 | private $_querystring_sort_limits; |
||
| 31 | |||
| 32 | /** @var bool if this is an api call */ |
||
| 33 | private $api = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This is the pre-dispatch function, actions common to all methods |
||
| 37 | */ |
||
| 38 | public function pre_dispatch() |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * This is the main function for markasread file |
||
| 54 | * |
||
| 55 | * markasread;sa=topic;t=###;topic=###.0;session Mark a topic unread |
||
| 56 | * markasread;sa=board;board=#.0;session Mark a board (all its topics) as read |
||
| 57 | * markasread;sa=board;c=#;start=0;session Mark a category read |
||
| 58 | * markasread;sa=all;session everything is read |
||
| 59 | * markasread;sa=unreadreplies;topics=6056-4692-6026-5817;session |
||
| 60 | */ |
||
| 61 | public function action_index() |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * This is the controller when using APIs. |
||
| 87 | * |
||
| 88 | * @uses Xml template generic_xml_buttons sub template |
||
| 89 | */ |
||
| 90 | public function action_index_api($action, $subAction): ?string |
||
| 91 | { |
||
| 92 | global $context, $txt; |
||
| 93 | |||
| 94 | // Setup for an Ajax response |
||
| 95 | theme()->getTemplates()->load('Xml'); |
||
| 96 | theme()->getLayers()->removeAll(); |
||
| 97 | $context['sub_template'] = 'generic_xml_buttons'; |
||
| 98 | |||
| 99 | // Guests can't mark things. |
||
| 100 | if ($this->user->is_guest) |
||
|
|
|||
| 101 | { |
||
| 102 | Txt::load('Errors'); |
||
| 103 | $context['xml_data'] = [ |
||
| 104 | 'error' => 1, |
||
| 105 | 'text' => $txt['not_guests'] |
||
| 106 | ]; |
||
| 107 | |||
| 108 | return ''; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Best have a valid session |
||
| 112 | if (checkSession('get', '', false)) |
||
| 113 | { |
||
| 114 | // Again, this is a special case, someone will deal with the others later :P |
||
| 115 | if ($this->_req->getQuery('sa') === 'all') |
||
| 116 | { |
||
| 117 | Txt::load('Errors'); |
||
| 118 | $context['xml_data'] = [ |
||
| 119 | 'error' => 1, |
||
| 120 | 'url' => getUrl('action', ['action' => 'markasread', 'sa' => 'all', '{session_data}']), |
||
| 121 | ]; |
||
| 122 | |||
| 123 | return ''; |
||
| 124 | } |
||
| 125 | |||
| 126 | obExit(false); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Dispatch to the right method |
||
| 130 | $action->dispatch($subAction); |
||
| 131 | |||
| 132 | // For the time being this is a special case, but in BoardIndex no, we don't want it |
||
| 133 | if ($this->_req->getQuery('sa') === 'all' || ($this->_req->getQuery('sa') === 'board' && !isset($this->_req->query->bi))) |
||
| 134 | { |
||
| 135 | $url_params = ['action' => 'unread', 'all', '{session_data}']; |
||
| 136 | if (!empty($this->_querystring_board_limits)) |
||
| 137 | { |
||
| 138 | $url_params += $this->_querystring_board_limits; |
||
| 139 | $url_params['start'] = 0; |
||
| 140 | } |
||
| 141 | |||
| 142 | if (!empty($this->_querystring_sort_limits)) |
||
| 143 | { |
||
| 144 | $url_params += $this->_querystring_sort_limits; |
||
| 145 | } |
||
| 146 | |||
| 147 | $context['xml_data'] = [ |
||
| 148 | 'text' => $txt['topic_alert_none'], |
||
| 149 | 'body' => str_replace('{unread_all_url}', getUrl('action', $url_params), $txt['unread_topics_visit_none']), |
||
| 150 | ]; |
||
| 151 | |||
| 152 | return ''; |
||
| 153 | } |
||
| 154 | |||
| 155 | // No need to output anything, just return to the button |
||
| 156 | obExit(false); |
||
| 157 | return null; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Marks boards as read (or unread) |
||
| 162 | * |
||
| 163 | * - Accessed by action=markasread;sa=all |
||
| 164 | */ |
||
| 165 | public function action_markboards(): ?string |
||
| 166 | { |
||
| 167 | global $modSettings; |
||
| 168 | |||
| 169 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
| 170 | |||
| 171 | // Find all the boards this user can see. |
||
| 172 | $boards = accessibleBoards(); |
||
| 173 | |||
| 174 | // Mark boards as read |
||
| 175 | if (!empty($boards)) |
||
| 176 | { |
||
| 177 | markBoardsRead($boards, isset($this->_req->query->unread), true); |
||
| 178 | } |
||
| 179 | |||
| 180 | $_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID']; |
||
| 181 | $redirectAction = ''; |
||
| 182 | if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false) |
||
| 183 | { |
||
| 184 | $redirectAction = 'action=unread'; |
||
| 185 | } |
||
| 186 | |||
| 187 | if (isset($_SESSION['topicseen_cache'])) |
||
| 188 | { |
||
| 189 | $_SESSION['topicseen_cache'] = []; |
||
| 190 | } |
||
| 191 | |||
| 192 | if (!empty($modSettings['default_forum_action']) && $redirectAction === '') |
||
| 193 | { |
||
| 194 | $redirectAction = getUrlQuery('action', $modSettings['default_forum_action']); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($this->api) |
||
| 198 | { |
||
| 199 | return ''; |
||
| 200 | } |
||
| 201 | |||
| 202 | redirectexit($redirectAction); |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Marks the selected topics as read. |
||
| 208 | * |
||
| 209 | * - Accessed by action=markasread;sa=unreadreplies |
||
| 210 | */ |
||
| 211 | public function action_markreplies(): ?string |
||
| 212 | { |
||
| 213 | global $modSettings; |
||
| 214 | |||
| 215 | // Make sure all the topics are integers! |
||
| 216 | $topics = array_map('intval', explode('-', $this->_req->query->topics)); |
||
| 217 | |||
| 218 | require_once(SUBSDIR . '/Topic.subs.php'); |
||
| 219 | $logged_topics = getLoggedTopics($this->user->id, $topics); |
||
| 220 | |||
| 221 | $markRead = []; |
||
| 222 | foreach ($topics as $id_topic) |
||
| 223 | { |
||
| 224 | $markRead[] = [$this->user->id, (int) $id_topic, $modSettings['maxMsgID'], (int) !empty($logged_topics[$id_topic])]; |
||
| 225 | } |
||
| 226 | |||
| 227 | markTopicsRead($markRead, true); |
||
| 228 | |||
| 229 | if (isset($_SESSION['topicseen_cache'])) |
||
| 230 | { |
||
| 231 | $_SESSION['topicseen_cache'] = []; |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($this->api) |
||
| 235 | { |
||
| 236 | return ''; |
||
| 237 | } |
||
| 238 | |||
| 239 | return redirectexit('action=unreadreplies'); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Mark a single topic as unread, returning to the board topic listing |
||
| 244 | * |
||
| 245 | * - Accessed by action=markasread;sa=topic;topic=123;t=123 |
||
| 246 | * - Button URL set in Display.php Controller |
||
| 247 | */ |
||
| 248 | public function action_marktopic_unread(): ?string |
||
| 249 | { |
||
| 250 | global $board, $topic; |
||
| 251 | |||
| 252 | require_once(SUBSDIR . '/Topic.subs.php'); |
||
| 253 | require_once(SUBSDIR . '/Messages.subs.php'); |
||
| 254 | |||
| 255 | // First, let's figure out what the latest message is. |
||
| 256 | $topicinfo = getTopicInfo($topic, 'all'); |
||
| 257 | $topic_msg_id = $this->_req->getQuery('t', 'intval'); |
||
| 258 | if (!empty($topic_msg_id)) |
||
| 259 | { |
||
| 260 | // If they read the whole topic, go back to the beginning. |
||
| 261 | if ($topic_msg_id >= $topicinfo['id_last_msg']) |
||
| 262 | { |
||
| 263 | $earlyMsg = 0; |
||
| 264 | } |
||
| 265 | // If they want to mark the whole thing read, same. |
||
| 266 | elseif ($topic_msg_id <= $topicinfo['id_first_msg']) |
||
| 267 | { |
||
| 268 | $earlyMsg = 0; |
||
| 269 | } |
||
| 270 | // Otherwise, get the latest message before the named one. |
||
| 271 | else |
||
| 272 | { |
||
| 273 | $earlyMsg = previousMessage($topic_msg_id, $topic); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | // Marking read from first page? That's the whole topic. |
||
| 277 | elseif ($this->_req->query->start == 0) |
||
| 278 | { |
||
| 279 | $earlyMsg = 0; |
||
| 280 | } |
||
| 281 | else |
||
| 282 | { |
||
| 283 | [$earlyMsg] = messageAt((int) $this->_req->query->start, $topic); |
||
| 284 | $earlyMsg--; |
||
| 285 | } |
||
| 286 | |||
| 287 | // Blam, unread! |
||
| 288 | markTopicsRead([$this->user->id, $topic, $earlyMsg, $topicinfo['unwatched']], true); |
||
| 289 | |||
| 290 | if ($this->api) |
||
| 291 | { |
||
| 292 | return ''; |
||
| 293 | } |
||
| 294 | |||
| 295 | return redirectexit('board=' . $board . '.0'); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Mark as read: boards, topics, unread replies. |
||
| 300 | * |
||
| 301 | * - Accessed by action=markasread;sa=board;board=1.0;session |
||
| 302 | * - Subactions: sa=topic, sa=all, sa=unreadreplies, sa=board |
||
| 303 | */ |
||
| 304 | public function action_markasread(): ?string |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the sorting parameters |
||
| 381 | */ |
||
| 382 | private function _setQuerystringSortLimits(): void |
||
| 383 | { |
||
| 384 | $sort_methods = [ |
||
| 385 | 'subject', |
||
| 386 | 'starter', |
||
| 387 | 'replies', |
||
| 388 | 'views', |
||
| 389 | 'first_post', |
||
| 390 | 'last_post' |
||
| 391 | ]; |
||
| 392 | |||
| 393 | // The default is the most logical: newest first. |
||
| 394 | if (!isset($this->_req->query->sort) || !in_array($this->_req->query->sort, $sort_methods)) |
||
| 395 | { |
||
| 396 | $this->_querystring_sort_limits = isset($this->_req->query->asc) ? ['asc'] : []; |
||
| 397 | } |
||
| 398 | // But, for other methods the default sort is ascending. |
||
| 399 | else |
||
| 400 | { |
||
| 401 | $this->_querystring_sort_limits = ['sort' => $this->_req->query->sort, isset($this->_req->query->desc) ? 'desc' : '']; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Mark a group of boards as read |
||
| 407 | * |
||
| 408 | * @param array $boards |
||
| 409 | */ |
||
| 410 | private function _markAsRead($boards): ?string |
||
| 440 | } |
||
| 441 | } |
||
| 442 |