/
Recent.php
499 lines (426 loc) · 13.9 KB
/
Recent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
/**
* Find and retrieve information about recently posted topics, messages, and the like.
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* This file contains code covered by:
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
*
* @version 2.0 dev
*
*/
namespace ElkArte\Controller;
use ElkArte\AbstractController;
use ElkArte\Exceptions\Exception;
use ElkArte\FrontpageInterface;
/**
* Retrieve information about recent posts
*/
class Recent extends AbstractController implements FrontpageInterface
{
/**
* The object that will retrieve the data
*
* @var \ElkArte\Recent
*/
private $_grabber;
/**
* Sets range to query
*
* @var int[]
*/
private $_maxMsgID;
/**
* The url for the recent action
*
* @var string
*/
private $_base_url;
/**
* The number of posts found
*
* @var int
*/
private $_total_posts;
/**
* The starting place for pagination
*
* @var
*/
private $_start;
/**
* The permissions own/any for use in the query
*
* @var array
*/
private $_permissions = array();
/**
* Pass to pageindex, to use "url.page" instead of "url;start=page"
*
* @var bool
*/
private $_flex_start = false;
/**
* Number of posts per page
*
* @var int
*/
private $_num_per_page = 10;
/**
* {@inheritdoc}
*/
public static function frontPageHook(&$default_action)
{
add_integration_function('integrate_menu_buttons', 'MessageIndex_Controller::addForumButton', '', false);
add_integration_function('integrate_current_action', 'MessageIndex_Controller::fixCurrentAction', '', false);
$default_action = array(
'controller' => 'Recent_Controller',
'function' => 'action_recent_front'
);
}
/**
* {@inheritdoc}
*/
public static function frontPageOptions()
{
parent::frontPageOptions();
theme()->addInlineJavascript('
$(\'#front_page\').on(\'change\', function() {
var $base = $(\'#recent_frontpage\').parent();
if ($(this).val() == \'Recent_Controller\')
{
$base.fadeIn();
$base.prev().fadeIn();
}
else
{
$base.fadeOut();
$base.prev().fadeOut();
}
}).change();', true);
return array(array('int', 'recent_frontpage'));
}
/**
* Called before any other action method in this class.
*
* - Allows for initializations, such as default values or
* loading templates or language files.
*/
public function pre_dispatch()
{
// Prefetching + lots of MySQL work = bad mojo.
stop_prefetching();
// Some common method dependencies
require_once(SUBSDIR . '/Recent.subs.php');
require_once(SUBSDIR . '/Boards.subs.php');
// There might be - and are - different permissions between any and own.
$this->_permissions = array(
'own' => array(
'post_reply_own' => 'can_reply',
'delete_own' => 'can_delete',
),
'any' => array(
'post_reply_any' => 'can_reply',
'mark_any_notify' => 'can_mark_notify',
'delete_any' => 'can_delete',
'like_posts' => 'can_like'
)
);
}
/**
* Intended entry point for recent controller class.
*
* @see \ElkArte\AbstractController::action_index()
*/
public function action_index()
{
// Figure out what action to do, thinking, thinking ...
$this->action_recent();
}
/**
* Find the ten most recent posts.
*
* Accessed by action=recent.
*/
public function action_recent()
{
global $txt, $context, $modSettings, $board;
// Start up a new recent posts grabber
$this->_grabber = new \ElkArte\Recent($this->user->id);
// Set or use a starting point for pagination
$this->_start = $this->_req->getQuery('start', 'intval', 0);
// Recent posts by category id's
if (!empty($this->_req->query->c) && empty($board))
{
$categories = $this->_recentPostsCategory();
}
// Or recent posts by board id's?
elseif (!empty($this->_req->query->boards))
{
$this->_recentPostsBoards();
}
// Or just the recent posts for a specific board
elseif (!empty($board))
{
$this->_recentPostsBoard();
}
// All the recent posts across boards and categories it is then
else
{
$this->_recentPostsAll();
}
if (!empty($this->_maxMsgID))
{
$this->_grabber->setEarliestMsg(max(0, $modSettings['maxMsgID'] - $this->_maxMsgID[0] - $this->_start * $this->_maxMsgID[1]));
}
// Set up the pageindex
$context['page_index'] = constructPageIndex($this->_base_url, $this->_start, min(100, $this->_total_posts), $this->_num_per_page, !empty($this->_flex_start));
// Rest of the items for the template
theme()->getTemplates()->load('Recent');
$context['page_title'] = $txt['recent_posts'];
$context['sub_template'] = 'recent';
$quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
// Linktree
$context['linktree'][] = array(
'url' => getUrl('action', ['action' => 'recent'] + (empty($board) ? (empty($categories) ? [] : ['c' => implode(',', $categories)]) : ['board' => $board . '.0'])),
'name' => $context['page_title']
);
// Nothing here... Or at least, nothing you can see...
if (!$this->_grabber->findRecentMessages($this->_start, $this->_num_per_page))
{
$context['posts'] = array();
}
else
{
$context['posts'] = $this->_grabber->getRecentPosts($this->_start, $this->_permissions);
}
// Load any likes for the messages
$context['likes'] = $this->_getLikes($context['posts']);
foreach ($context['posts'] as $counter => $post)
{
// Some posts - the first posts - can't just be deleted.
$context['posts'][$counter]['tests']['can_delete'] = $context['posts'][$counter]['tests']['can_delete'] && $context['posts'][$counter]['delete_possible'];
// And some cannot be quoted...
$context['posts'][$counter]['tests']['can_quote'] = $context['posts'][$counter]['tests']['can_reply'] && $quote_enabled;
// Likes are always a bit particular
$post['you_liked'] = !empty($context['likes'][$counter]['member'])
&& isset($context['likes'][$counter]['member'][$this->user->id]);
$post['use_likes'] = !empty($post['tests']['can_like']) && allowedTo('like_posts') && empty($context['is_locked'])
&& ($post['poster']['id'] != $this->user->id || !empty($modSettings['likeAllowSelf']))
&& (empty($modSettings['likeMinPosts']) || $modSettings['likeMinPosts'] <= $this->user->posts);
$post['like_counter'] = !empty($context['likes'][$counter]['count']) ? $context['likes'][$counter]['count'] : 0;
$post['can_like'] = $post['use_likes'] && !$post['you_liked'];
$post['can_unlike'] = $post['use_likes'] && $post['you_liked'];
$post['likes_enabled'] = !empty($modSettings['likes_enabled']) && ($post['use_likes'] || ($post['like_counter'] != 0));
// Let's add some buttons here!
$context['posts'][$counter]['buttons'] = $this->_addButtons($post, $context['posts'][$counter]['tests']);
}
}
/**
* Set up for getting recent posts on a category basis
*/
private function _recentPostsCategory()
{
global $modSettings, $context;
$categories = array_map('intval', explode(',', $this->_req->query->c));
if (count($categories) === 1)
{
require_once(SUBSDIR . '/Categories.subs.php');
$name = categoryName($categories[0]);
if (empty($name))
{
throw new Exception('no_access', false);
}
$context['linktree'][] = array(
'url' => getUrl('action', $modSettings['default_forum_action']) . '#c' . $categories[0],
'name' => $name
);
}
// Find the number of posts in these category's, exclude the recycle board.
$boards_posts = boardsPosts(array(), $categories, false, false);
$this->_total_posts = (int) array_sum($boards_posts);
$boards = array_keys($boards_posts);
if (empty($boards))
{
throw new Exception('error_no_boards_selected');
}
// The query for getting the messages
$this->_grabber->setBoards($boards);
// If this category has a significant number of posts in it...
if ($this->_total_posts > 100 && $this->_total_posts > $modSettings['totalMessages'] / 15)
{
$this->_maxMsgID = array(400, 7);
}
$this->_base_url = '{scripturl}?action=recent;c=' . implode(',', $categories);
return $categories;
}
/**
* Setup for finding recent posts based on a list of boards
*/
private function _recentPostsBoards()
{
global $modSettings;
$this->_req->query->boards = array_map('intval', explode(',', $this->_req->query->boards));
// Fetch the number of posts for the supplied board IDs
$boards_posts = boardsPosts($this->_req->query->boards, array());
$this->_total_posts = (int) array_sum($boards_posts);
$boards = array_keys($boards_posts);
// No boards, your request ends here
if (empty($boards))
{
throw new Exception('error_no_boards_selected');
}
// Build the query for finding the messages
$this->_grabber->setBoards($boards);
// If these boards have a significant number of posts in them...
if ($this->_total_posts > 100 && $this->_total_posts > $modSettings['totalMessages'] / 12)
{
$this->_maxMsgID = array(500, 9);
}
$this->_base_url = '{scripturl}?action=recent;boards=' . implode(',', $this->_req->query->boards);
}
/**
* Setup for finding recent posts for a single board
*/
private function _recentPostsBoard()
{
global $modSettings, $board;
$board_data = fetchBoardsInfo(array('boards' => $board), array('selects' => 'posts'));
$this->_total_posts = $board_data[(int) $board]['num_posts'];
$this->_grabber->setBoards($board);
// If this board has a significant number of posts in it...
if ($this->_total_posts > 80 && $this->_total_posts > $modSettings['totalMessages'] / $this->_num_per_page)
{
$this->_maxMsgID = array(600, 10);
}
$this->_base_url = '{scripturl}?action=recent;board=' . $board . '.%1$d';
$this->_flex_start = true;
}
/**
* Setup to find all the recent posts across all boards and categories
*/
private function _recentPostsAll()
{
global $modSettings;
$this->_total_posts = sumRecentPosts();
$this->_grabber->setVisibleBoards(max(0, $modSettings['maxMsgID'] - 100 - $this->_start * 6), !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? $modSettings['recycle_board'] : 0);
// Set up the pageindex
$this->_base_url = '{scripturl}?action=recent';
}
/**
* Loads the likes for the set of recent messages
*
* @param array $messages
*
* @return array|int[]
* @throws \Exception
*/
private function _getLikes($messages)
{
global $modSettings;
$likes = array();
// Load in the likes for this group of messages
if (!empty($modSettings['likes_enabled']))
{
// Just the message id please
$messages = array_column($messages, 'id');
require_once(SUBSDIR . '/Likes.subs.php');
$likes = loadLikes($messages, true);
theme()->getLayers()->addBefore('load_likes_button', 'body');
}
return $likes;
}
/**
* Create the buttons array for this post
* Array is used by template_button_strip(), see that function for parameter details
*
* @param array $post Details of this post
* @param array $tests array holding true false values for various test keys, like can_quote;
* @return array
*/
private function _addButtons($post, $tests)
{
global $txt;
$postButtons = [
// How about... even... remove it entirely?!
'remove' => [
'url' => getUrl('action', ['action' => 'deletemsg', 'msg' => $post['id'], 'topic' => $post['topic'], 'recent', '{session_data}']),
'text' => 'remove',
'icon' => 'delete',
'enabled' => $tests['can_delete'],
'custom' => 'onclick="return confirm(' . JavaScriptEscape($txt['remove_message'] . '?') . ');"',
],
// Can we request notification of topics?
'notify' => [
'url' => getUrl('action', ['action' => 'notify', 'topic' => $post['topic'] . '.' . $post['start']]),
'text' => 'notify',
'icon' => 'comment',
'enabled' => $tests['can_mark_notify'],
],
// If they *can* reply?
'reply' => [
'url' => getUrl('action', ['action' => 'post', 'topic' => $post['topic'] . '.' . $post['start']]),
'text' => 'reply',
'icon' => 'modify',
'enabled' => $tests['can_reply'],
],
// If they *can* quote?
'quote' => [
'text' => 'quote',
'url' => getUrl('action', ['action' => 'post', 'topic' => $post['topic'] . '.' . $post['start'], 'quote' => $post['id']]),
'class' => 'quote_button last',
'icon' => 'quote',
'enabled' => $tests['can_quote'],
],
// If they *can* like / unlike / just see totals
'react' => [
'text' => 'like_post',
'url' => 'javascript:void(0);',
'custom' => 'onclick="likePosts.prototype.likeUnlikePosts(event,' . $post['id'] . ',' . $post['topic'] . '); return false;"',
'linkclass' => 'react_button',
'icon' => 'thumbsup',
'enabled' => $post['likes_enabled'] && $post['can_like'],
'counter' => $post['like_counter'] ?? 0,
],
'unreact' => [
'text' => 'unlike_post',
'url' => 'javascript:void(0);',
'custom' => 'onclick="likePosts.prototype.likeUnlikePosts(event,' . $post['id'] . ',' . $post['topic'] . '); return false;"',
'linkclass' => 'unreact_button',
'icon' => 'thumbsdown',
'enabled' => $post['likes_enabled'] && $post['can_unlike'],
'counter' => $post['like_counter'] ?? 0,
],
'liked' => [
'text' => 'likes',
'url' => 'javascript:void(0);',
'custom' => 'onclick="this.blur();"',
'icon' => 'thumbsup',
'enabled' => $post['likes_enabled'] && !$post['can_unlike'] && !$post['can_like'],
'counter' => $post['like_counter'] ?? 0,
],
];
// Drop all non-enabled ones
return array_filter($postButtons, function ($button) {
return !isset($button['enabled']) ||$button['enabled'] !== false;
});
}
/**
* Intended entry point for recent controller class.
*
* @see Action_Controller::action_index()
*/
public function action_recent_front()
{
global $modSettings;
if (isset($modSettings['recent_frontpage']))
{
$this->_num_per_page = $modSettings['recent_frontpage'];
}
// Figure out what action to do, thinking, thinking ...
$this->action_recent();
}
}