/
listener.php
378 lines (326 loc) · 10 KB
/
listener.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
<?php
/**
*
* @package phpBB Extension - Post Numbers
* @copyright (c) 2016 kasimi - https://kasimi.net
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace kasimi\postnumbers\event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class listener implements EventSubscriberInterface
{
const PAGE_VIEWTOPIC = 'viewtopic';
const PAGE_REVIEW_REPLY = 'review_reply';
const PAGE_REVIEW_MCP = 'review_mcp';
/* @var \phpbb\user */
protected $user;
/* @var \phpbb\config\config */
protected $config;
/** @var \phpbb\request\request_interface */
protected $request;
/* @var \phpbb\template\template */
protected $template;
/* @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \rxu\FirstPostOnEveryPage\event\listener */
protected $firstPostOnEveryPage;
/** @var boolean */
protected $is_active = false;
/** @var string */
protected $page;
/** @var string */
protected $location;
/** @var int */
protected $first_post_num = -1;
/** @var int */
protected $offset = -1;
/**
* Constructor
*
* @param \phpbb\user $user
* @param \phpbb\config\config $config
* @param \phpbb\request\request_interface $request
* @param \phpbb\template\template $template
* @param \phpbb\db\driver\driver_interface $db
* @param \rxu\FirstPostOnEveryPage\event\listener $firstPostOnEveryPage
*/
public function __construct(
\phpbb\user $user,
\phpbb\config\config $config,
\phpbb\request\request_interface $request,
\phpbb\template\template $template,
\phpbb\db\driver\driver_interface $db,
\rxu\FirstPostOnEveryPage\event\listener $firstPostOnEveryPage = null
)
{
$this->user = $user;
$this->config = $config;
$this->request = $request;
$this->template = $template;
$this->db = $db;
$this->firstPostOnEveryPage = $firstPostOnEveryPage;
}
/**
* Register hooks
*/
static public function getSubscribedEvents()
{
return array(
'core.viewtopic_assign_template_vars_before' => 'init_viewtopic',
'core.viewtopic_modify_post_row' => 'postnum_in_viewtopic',
'core.posting_modify_template_vars' => 'init_topic_review',
'core.topic_review_modify_row' => 'postnum_in_topic_review',
'core.mcp_global_f_read_auth_after' => 'init_mcp_review',
'core.mcp_topic_review_modify_row' => 'postnum_in_mcp_review',
);
}
/**
* Prepare template data
*
* @param string $mode
*/
protected function init($mode)
{
$this->is_active = $this->cfg('enabled.' . $mode) && !$this->user->data['is_bot'];
if (!$this->is_active)
{
return;
}
switch ($this->cfg('location'))
{
case 1:
$this->location = 'subject';
break;
default:
$this->location = 'post_img';
break;
}
$template_data = array(
'S_POSTNUMBERS_LOCATION' => $this->location,
);
if ($this->location === 'post_img')
{
$this->user->add_lang_ext('kasimi/postnumbers', 'common');
$template_data = array_merge($template_data, array(
'S_POSTNUMBERS_CLIPBOARD' => $this->cfg('clipboard'),
'S_POSTNUMBERS_BOLD' => $this->cfg('bold'),
'POSTNUMBERS_PHPBB_VERSION' => phpbb_version_compare(PHPBB_VERSION, '3.1.0@dev', '>=') && phpbb_version_compare(PHPBB_VERSION, '3.2.0@dev', '<') ? '31x' : (phpbb_version_compare(PHPBB_VERSION, '3.2.0@dev', '>=') && phpbb_version_compare(PHPBB_VERSION, '3.3.0@dev', '<') ? '32x' : ''),
));
}
$this->template->assign_vars($template_data);
}
/**
* Event: core.viewtopic_assign_template_vars_before
*
* @param $event
*/
public function init_viewtopic($event)
{
$this->init(self::PAGE_VIEWTOPIC);
}
/**
* Event: core.viewtopic_modify_post_row
*
* @param $event
*/
public function postnum_in_viewtopic($event)
{
if ($this->is_active)
{
// Compatibility with the First Post On Every Page extension by rxu
// Remove reference to the listener if the topic we are viewing
// doesn't display the topic's first post on every page
if ($this->firstPostOnEveryPage !== null && !$event['topic_data']['topic_first_post_show'] && !$event['topic_data']['first_post_always_show'])
{
$this->firstPostOnEveryPage = null;
}
if ($post_num = $this->get_post_number($this->user->data['user_post_sortby_type'], $this->user->data['user_post_sortby_dir'], $this->user->data['user_post_show_days'], $event['row'], $event['topic_data']['topic_posts_approved'], $event['total_posts'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
}
}
/**
* Event: core.posting_modify_template_vars
*
* @param $event
*/
public function init_topic_review($event)
{
$this->init(self::PAGE_REVIEW_REPLY);
}
/**
* Event: core.topic_review_modify_row
*
* @param $event
*/
public function postnum_in_topic_review($event)
{
if ($this->is_active && $event['mode'] == 'topic_review')
{
if ($post_num = $this->get_post_number('t', 'd', 0, $event['row'], $event['total_posts'], $event['total_posts'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
}
}
/**
* Event: core.mcp_global_f_read_auth_after
*
* @param $event
*/
public function init_mcp_review($event)
{
if ($event['mode'] == 'topic_view')
{
$this->init(self::PAGE_REVIEW_MCP);
}
}
/**
* Event: core.mcp_topic_review_modify_row
*
* @param $event
*/
public function postnum_in_mcp_review($event)
{
if ($this->is_active)
{
if ($post_num = $this->get_post_number('t', 'a', 0, $event['row'], $event['topic_info']['topic_posts_approved'], $event['total'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
}
}
/**
* Returns the post number/ID of the $row
*
* @param string $default_sort_by
* @param string $default_sort_dir
* @param int $default_days
* @param array $row
* @param int $approved_posts
* @param int $total_posts
* @param int $start
* @return bool|int
*/
protected function get_post_number($default_sort_by, $default_sort_dir, $default_days, $row, $approved_posts, $total_posts, $start)
{
// If we display IDs, skip all checks and calculations and return immediately
if ($this->cfg('display_ids'))
{
return $row['post_id'];
}
// Don't calculate post number if
// - posts are not sorted by post time, or
// - we display them only for approved posts and this post is not approved
if ($this->request->variable('sk', $default_sort_by) != 't' || $this->cfg('skip_nonapproved') && $row['post_visibility'] != ITEM_APPROVED)
{
// Compatibility with the First Post On Every Page extension by rxu
// If the first post of the current page meets the above criteria, we need to
// make sure that the following post isn't labeled as post #1 in the next iteration.
$this->firstPostOnEveryPage = null;
return false;
}
$is_ascending = $this->request->variable('sd', $default_sort_dir) == 'a';
// Compatibility with the First Post On Every Page extension by rxu
if ($this->firstPostOnEveryPage !== null)
{
$this->firstPostOnEveryPage = null;
// First Post On Every Page extension is only active on viewtopic
if ($this->page === self::PAGE_VIEWTOPIC)
{
// If posts are sorted ascending, we display #1 on all except the first page.
// If posts are sorted descending, we always display #1.
if (!$is_ascending || $start != 0)
{
return 1;
}
}
}
// Initialize $first_post_num and $offset on first post
if ($this->first_post_num === -1)
{
$need_new_start = false;
// We only need to query the number of previous/non-approved posts in certain situations
if ($this->page === self::PAGE_REVIEW_REPLY || $this->request->variable('st', $default_days) != 0)
{
$need_new_start = true;
}
else if ($this->cfg('skip_nonapproved') && $approved_posts != $total_posts)
{
// We skip non-approved posts and the topic we are viewing contains
// at least one non-approved post. We only need to count posts if
// - posts are sorted ascending and we are not on the first page, or
// - posts are sorted descending and we are not on the last page
$need_new_start = $is_ascending ? $start > 0 : $total_posts - $start > $this->config['posts_per_page'];
}
if ($need_new_start)
{
$post_count = $this->get_post_count($row['topic_id'], $row['post_time']);
if ($is_ascending)
{
$start = $post_count;
}
else
{
$total_posts += $start == 0 ? $post_count - $total_posts + 1 : $post_count - $start;
}
}
$this->first_post_num = $is_ascending ? $start : $total_posts - $start;
$this->offset = $is_ascending ? 1 : 0;
}
return $this->first_post_num + ($is_ascending ? +$this->offset++ : -$this->offset++);
}
/**
* Injects a post's number into the row's POST_NUMBER and MINI_POST_IMG fields
*
* @param array $post_row
* @param int $post_num
* @return array
*/
protected function inject_post_num($post_row, $post_num)
{
$post_row['POSTNUMBER'] = $post_num;
if ($this->location === 'subject')
{
$post_row['POST_SUBJECT'] = '#' . $post_num . ' ' . $post_row['POST_SUBJECT'];
}
return $post_row;
}
/**
* Gets the number of approved/all posts in a topic that have been posted before a certain time
*
* @param int $topic_id
* @param int $post_time
* @return int
*/
protected function get_post_count($topic_id, $post_time)
{
$sql_where = array(
'p.topic_id = ' . (int) $topic_id,
'p.post_time < ' . (int) $post_time,
);
if ($this->cfg('skip_nonapproved'))
{
$sql_where[] = 'p.post_visibility = ' . (int) ITEM_APPROVED;
}
$sql = 'SELECT COUNT(*) AS count
FROM ' . POSTS_TABLE . ' p
WHERE ' . implode(' AND ', $sql_where);
$result = $this->db->sql_query($sql);
$count = (int) $this->db->sql_fetchfield('count');
$this->db->sql_freeresult($result);
return $count;
}
/**
* Quick access to this extension's config values
*
* @param string $key
* @return string
*/
protected function cfg($key)
{
return $this->config['kasimi.postnumbers.' . $key];
}
}