1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Handles all mark as read options, boards, topics, replies |
||
5 | * |
||
6 | * @package ElkArte Forum |
||
7 | * @copyright ElkArte Forum contributors |
||
8 | * @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
||
9 | * |
||
10 | * @version 2.0 dev |
||
11 | * |
||
12 | */ |
||
13 | |||
14 | namespace ElkArte\Controller; |
||
15 | |||
16 | use ElkArte\AbstractController; |
||
17 | use ElkArte\Action; |
||
18 | use ElkArte\Languages\Txt; |
||
19 | |||
20 | /** |
||
21 | * This class handles a part of the actions to mark boards, topics, or replies, |
||
22 | * as read/unread. |
||
23 | */ |
||
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() |
||
39 | { |
||
40 | $this->api = $this->getApi() === 'xml'; |
||
41 | |||
42 | // We will check these items in the ajax function |
||
43 | if (!$this->api) |
||
44 | { |
||
45 | // Guests can't mark things. |
||
46 | is_not_guest(); |
||
47 | |||
48 | checkSession('get'); |
||
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() |
||
62 | { |
||
63 | global $context; |
||
64 | |||
65 | $subActions = array( |
||
66 | 'all' => array($this, 'action_markboards'), |
||
67 | 'unreadreplies' => array($this, 'action_markreplies'), |
||
68 | 'topic' => array($this, 'action_marktopic_unread'), |
||
69 | 'markasread' => array($this, 'action_markasread') |
||
70 | ); |
||
71 | |||
72 | $action = new Action('markasread'); |
||
73 | $subAction = $action->initialize($subActions, 'markasread'); |
||
74 | $context['sub_action'] = $subAction; |
||
75 | |||
76 | if ($this->api) |
||
77 | { |
||
78 | $this->action_index_api($action, $subAction); |
||
79 | return ''; |
||
80 | } |
||
81 | |||
82 | $action->dispatch($subAction); |
||
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) |
||
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) |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
101 | { |
||
102 | Txt::load('Errors'); |
||
103 | $context['xml_data'] = array( |
||
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'] = array( |
||
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'] = array( |
||
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 | } |
||
158 | |||
159 | /** |
||
160 | * Marks boards as read (or unread) |
||
161 | * |
||
162 | * - Accessed by action=markasread;sa=all |
||
163 | */ |
||
164 | public function action_markboards() |
||
165 | { |
||
166 | global $modSettings; |
||
167 | |||
168 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
169 | |||
170 | // Find all the boards this user can see. |
||
171 | $boards = accessibleBoards(); |
||
172 | |||
173 | // Mark boards as read |
||
174 | if (!empty($boards)) |
||
175 | { |
||
176 | markBoardsRead($boards, isset($this->_req->query->unread), true); |
||
177 | } |
||
178 | |||
179 | $_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID']; |
||
180 | $redirectAction = ''; |
||
181 | if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false) |
||
182 | { |
||
183 | $redirectAction = 'action=unread'; |
||
184 | } |
||
185 | |||
186 | if (isset($_SESSION['topicseen_cache'])) |
||
187 | { |
||
188 | $_SESSION['topicseen_cache'] = array(); |
||
189 | } |
||
190 | |||
191 | if (!empty($modSettings['default_forum_action']) && $redirectAction === '') |
||
192 | { |
||
193 | $redirectAction = getUrlQuery('action', $modSettings['default_forum_action']); |
||
194 | } |
||
195 | |||
196 | if ($this->api) |
||
197 | { |
||
198 | return ''; |
||
199 | } |
||
200 | |||
201 | return redirectexit($redirectAction); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Marks the selected topics as read. |
||
206 | * |
||
207 | * - Accessed by action=markasread;sa=unreadreplies |
||
208 | */ |
||
209 | public function action_markreplies() |
||
210 | { |
||
211 | global $modSettings; |
||
212 | |||
213 | // Make sure all the topics are integers! |
||
214 | $topics = array_map('intval', explode('-', $this->_req->query->topics)); |
||
215 | |||
216 | require_once(SUBSDIR . '/Topic.subs.php'); |
||
217 | $logged_topics = getLoggedTopics($this->user->id, $topics); |
||
0 ignored issues
–
show
The property
id does not exist on ElkArte\Helper\ValuesContainer . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
218 | |||
219 | $markRead = array(); |
||
220 | foreach ($topics as $id_topic) |
||
221 | { |
||
222 | $markRead[] = array($this->user->id, (int) $id_topic, $modSettings['maxMsgID'], (int) !empty($logged_topics[$id_topic])); |
||
223 | } |
||
224 | |||
225 | markTopicsRead($markRead, true); |
||
226 | |||
227 | if (isset($_SESSION['topicseen_cache'])) |
||
228 | { |
||
229 | $_SESSION['topicseen_cache'] = array(); |
||
230 | } |
||
231 | |||
232 | if ($this->api) |
||
233 | { |
||
234 | return ''; |
||
235 | } |
||
236 | |||
237 | return redirectexit('action=unreadreplies'); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Mark a single topic as unread, returning to the board topic listing |
||
242 | * |
||
243 | * - Accessed by action=markasread;sa=topic;topic=123;t=123 |
||
244 | * - Button URL set in Display.php Controller |
||
245 | */ |
||
246 | public function action_marktopic_unread() |
||
247 | { |
||
248 | global $board, $topic; |
||
249 | |||
250 | require_once(SUBSDIR . '/Topic.subs.php'); |
||
251 | require_once(SUBSDIR . '/Messages.subs.php'); |
||
252 | |||
253 | // First, let's figure out what the latest message is. |
||
254 | $topicinfo = getTopicInfo($topic, 'all'); |
||
255 | $topic_msg_id = $this->_req->getQuery('t', 'intval'); |
||
256 | if (!empty($topic_msg_id)) |
||
257 | { |
||
258 | // If they read the whole topic, go back to the beginning. |
||
259 | if ($topic_msg_id >= $topicinfo['id_last_msg']) |
||
260 | { |
||
261 | $earlyMsg = 0; |
||
262 | } |
||
263 | // If they want to mark the whole thing read, same. |
||
264 | elseif ($topic_msg_id <= $topicinfo['id_first_msg']) |
||
265 | { |
||
266 | $earlyMsg = 0; |
||
267 | } |
||
268 | // Otherwise, get the latest message before the named one. |
||
269 | else |
||
270 | { |
||
271 | $earlyMsg = previousMessage($topic_msg_id, $topic); |
||
272 | } |
||
273 | } |
||
274 | // Marking read from first page? That's the whole topic. |
||
275 | elseif ($this->_req->query->start == 0) |
||
276 | { |
||
277 | $earlyMsg = 0; |
||
278 | } |
||
279 | else |
||
280 | { |
||
281 | [$earlyMsg] = messageAt((int) $this->_req->query->start, $topic); |
||
282 | $earlyMsg--; |
||
283 | } |
||
284 | |||
285 | // Blam, unread! |
||
286 | markTopicsRead(array($this->user->id, $topic, $earlyMsg, $topicinfo['unwatched']), true); |
||
0 ignored issues
–
show
The property
id does not exist on ElkArte\Helper\ValuesContainer . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
287 | |||
288 | if ($this->api) |
||
289 | { |
||
290 | return ''; |
||
291 | } |
||
292 | |||
293 | return redirectexit('board=' . $board . '.0'); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Mark as read: boards, topics, unread replies. |
||
298 | * |
||
299 | * - Accessed by action=markasread;sa=board;board=1.0;session |
||
300 | * - Subactions: sa=topic, sa=all, sa=unreadreplies, sa=board |
||
301 | */ |
||
302 | public function action_markasread() |
||
303 | { |
||
304 | global $board, $board_info; |
||
305 | |||
306 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
307 | |||
308 | $categories = array(); |
||
309 | $boards = array(); |
||
310 | |||
311 | if (isset($this->_req->query->c)) |
||
312 | { |
||
313 | $categories = array_map('intval', explode(',', $this->_req->query->c)); |
||
314 | } |
||
315 | |||
316 | if (isset($this->_req->query->boards)) |
||
317 | { |
||
318 | $boards = array_map('intval', explode(',', $this->_req->query->boards)); |
||
319 | } |
||
320 | |||
321 | if (!empty($board)) |
||
322 | { |
||
323 | $boards[] = (int) $board; |
||
324 | } |
||
325 | |||
326 | if (isset($this->_req->query->children) && !empty($boards)) |
||
327 | { |
||
328 | // Mark all children of the boards we got (selected by the user). |
||
329 | $boards = addChildBoards($boards); |
||
330 | } |
||
331 | |||
332 | $boards = array_keys(boardsPosts($boards, $categories)); |
||
333 | |||
334 | if (empty($boards)) |
||
335 | { |
||
336 | if ($this->api) |
||
337 | { |
||
338 | return ''; |
||
339 | } |
||
340 | |||
341 | return redirectexit(); |
||
342 | } |
||
343 | |||
344 | // Mark boards as read. |
||
345 | markBoardsRead($boards, isset($this->_req->query->unread), true); |
||
346 | |||
347 | foreach ($boards as $b) |
||
348 | { |
||
349 | if (isset($_SESSION['topicseen_cache'][$b])) |
||
350 | { |
||
351 | $_SESSION['topicseen_cache'][$b] = array(); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | $this->_querystring_board_limits = $this->_req->getQuery('sa') === 'board' ? ['boards' => implode(',', $boards), 'start' => '%d'] : []; |
||
356 | |||
357 | $this->_setQuerystringSortLimits(); |
||
358 | |||
359 | $this->_markAsRead($boards); |
||
360 | |||
361 | if (empty($board_info['parent']) && !$this->api) |
||
362 | { |
||
363 | return redirectexit(); |
||
364 | } |
||
365 | |||
366 | if ($this->api) |
||
367 | { |
||
368 | return ''; |
||
369 | } |
||
370 | |||
371 | return redirectexit('board=' . $board_info['parent'] . '.0'); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Sets the sorting parameters |
||
376 | */ |
||
377 | private function _setQuerystringSortLimits() |
||
378 | { |
||
379 | $sort_methods = [ |
||
380 | 'subject', |
||
381 | 'starter', |
||
382 | 'replies', |
||
383 | 'views', |
||
384 | 'first_post', |
||
385 | 'last_post' |
||
386 | ]; |
||
387 | |||
388 | // The default is the most logical: newest first. |
||
389 | if (!isset($this->_req->query->sort) || !in_array($this->_req->query->sort, $sort_methods)) |
||
390 | { |
||
391 | $this->_querystring_sort_limits = isset($this->_req->query->asc) ? ['asc'] : []; |
||
392 | } |
||
393 | // But, for other methods the default sort is ascending. |
||
394 | else |
||
395 | { |
||
396 | $this->_querystring_sort_limits = ['sort' => $this->_req->query->sort, isset($this->_req->query->desc) ? 'desc' : '']; |
||
397 | } |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Mark a group of boards as read |
||
402 | * |
||
403 | * @param array $boards |
||
404 | */ |
||
405 | private function _markAsRead($boards) |
||
406 | { |
||
407 | global $board; |
||
408 | |||
409 | // Want to mark as unread, nothing to do here |
||
410 | if (isset($this->_req->query->unread)) |
||
411 | { |
||
412 | return ''; |
||
413 | } |
||
414 | |||
415 | // Find all boards with the parents in the board list |
||
416 | $boards_to_add = accessibleBoards(null, $boards); |
||
417 | if (!empty($boards_to_add)) |
||
418 | { |
||
419 | markBoardsRead($boards_to_add); |
||
420 | } |
||
421 | |||
422 | $redirectAction = 'board=' . $board . '.0'; |
||
423 | if (empty($board)) |
||
424 | { |
||
425 | $redirectAction = ''; |
||
426 | } |
||
427 | |||
428 | if ($this->api) |
||
429 | { |
||
430 | return ''; |
||
431 | } |
||
432 | |||
433 | return redirectexit($redirectAction); |
||
434 | } |
||
435 | } |
||
436 |