-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtopicsolved.php
360 lines (317 loc) · 9.37 KB
/
topicsolved.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
<?php
/**
* This file is part of the phpBB Topic Solved extension package.
*
* @copyright (c) Bryan Petty
* @license GNU General Public License, version 2 (GPL-2.0)
*
* @package tierra/topicsolved
*/
namespace tierra\topicsolved;
/**
* Core topic solved functionality used throughout the extension.
*
* @package tierra/topicsolved
*/
class topicsolved
{
/** No-one can mark topics as solved. */
const TOPIC_SOLVED_NO = 0;
/** Topic starter and moderators can mark topics as solved. */
const TOPIC_SOLVED_YES = 1;
/** Only moderators can mark topics as solved. */
const TOPIC_SOLVED_MOD = 2;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\user */
protected $user;
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
/** @var string core.root_path */
protected $root_path;
/** @var string core.php_ext */
protected $php_ext;
/**
* Constructor
*
* @param \phpbb\db\driver\driver_interface $db Database object
* @param \phpbb\user $user
* @param \phpbb\auth\auth $auth
* @param \phpbb\event\dispatcher_interface $dispatcher
* @param string $root_path core.root_path
* @param string $php_ext core.php_ext
*/
public function __construct(
\phpbb\db\driver\driver_interface $db,
\phpbb\user $user,
\phpbb\auth\auth $auth,
\phpbb\event\dispatcher_interface $dispatcher,
$root_path, $php_ext)
{
$this->db = $db;
$this->user = $user;
$this->auth = $auth;
$this->dispatcher = $dispatcher;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->user->add_lang_ext('tierra/topicsolved', 'common');
}
/**
* Determine if user is allowed to mark a post as solved or unsolved.
*
* @param string $solved Either "solved" or "unsolved".
* @param array $topic_data Topic to be solved or unsolved.
*
* @throws \phpbb\exception\runtime_exception
* if an invalid solved parameter is specified.
*
* @return bool User is authorized to (un)solve topic.
*/
public function user_can_solve_post($solved, $topic_data)
{
// Disallow (un)solving topic if post is global.
if ($topic_data['topic_type'] == POST_GLOBAL)
{
return false;
}
$forum_permission = array(
'solved' => 'forum_allow_solve',
'unsolved' => 'forum_allow_unsolve',
);
if (!array_key_exists($solved, $forum_permission))
{
throw new \phpbb\exception\runtime_exception(
'BAD_METHOD_CALL', array('user_can_solve_post'));
}
if (($topic_data[$forum_permission[$solved]] == topicsolved::TOPIC_SOLVED_MOD ||
$topic_data[$forum_permission[$solved]] == topicsolved::TOPIC_SOLVED_YES) &&
$this->auth->acl_get('m_', $topic_data['forum_id']))
{
return true;
}
else if ($topic_data[$forum_permission[$solved]] == topicsolved::TOPIC_SOLVED_YES &&
$topic_data['topic_poster'] == $this->user->data['user_id'] &&
$topic_data['topic_status'] == ITEM_UNLOCKED)
{
return true;
}
return false;
}
/**
* Fetches all topic solved data related to the given post.
*
* @param int $post_id ID of post to fetch topic/forum data for.
*
* @return mixed topic data, or false if not found
*/
public function get_topic_data($post_id)
{
$select_sql_array = array(
'SELECT' =>
't.topic_id, t.topic_poster, t.topic_status, t.topic_type, t.topic_solved, ' .
'f.forum_id, f.forum_allow_solve, f.forum_allow_unsolve, f.forum_lock_solved, ' .
'p.post_id',
'FROM' => array(
FORUMS_TABLE => 'f',
POSTS_TABLE => 'p',
TOPICS_TABLE => 't',
),
'WHERE' =>
'p.post_id = ' . (int) $post_id .
' AND t.topic_id = p.topic_id AND f.forum_id = t.forum_id',
);
$select_sql = $this->db->sql_build_query('SELECT', $select_sql_array);
$result = $this->db->sql_query($select_sql);
$topic_data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return $topic_data;
}
/**
* Update topic with the given data.
*
* @param int $topic_id Topic to update.
* @param array $data Topic data to update.
*
* @return mixed true if successful
*/
public function update_topic($topic_id, $data)
{
$update_sql = $this->db->sql_build_array('UPDATE', $data);
$result = $this->db->sql_query('
UPDATE ' . TOPICS_TABLE . '
SET ' . $update_sql . '
WHERE topic_id = ' . (int) $topic_id
);
return $result;
}
/**
* Marks a topic as solved.
*
* @param array $topic_data Topic to be marked as solved.
* @param int $post_id Post to mark as the solution.
*/
public function mark_solved($topic_data, $post_id)
{
// Database column values to set.
$column_data = array('topic_solved' => $post_id);
if ($topic_data['forum_lock_solved'] &&
$this->user_can_lock_post($topic_data['forum_id']))
{
$column_data['topic_status'] = ITEM_LOCKED;
}
$this->update_topic($topic_data['topic_id'], $column_data);
/**
* This event allows you to perform additional actions after a topic has been marked as solved.
*
* @event tierra.topicsolved.mark_solved_after
* @var array topic_data Array with general topic data
* @var array column_data Array with topic data that the database has been updated with
* @since 2.2.0
*/
$vars = array(
'topic_data',
'column_data',
);
extract($this->dispatcher->trigger_event('tierra.topicsolved.mark_solved_after', compact($vars)));
}
/**
* Marks a topic as unsolved.
*
* @param array $topic_data Topic to be marked as unsolved.
*/
public function mark_unsolved($topic_data)
{
// Database column values to set.
$column_data = array('topic_solved' => 0);
if ($topic_data['forum_lock_solved'] &&
$this->auth->acl_get('m_lock', $topic_data['forum_id']))
{
$column_data['topic_status'] = ITEM_UNLOCKED;
}
$this->update_topic($topic_data['topic_id'], $column_data);
/**
* This event allows you to perform additional actions after a topic has been marked as unsolved.
*
* @event tierra.topicsolved.mark_unsolved_after
* @var array topic_data Array with general topic data
* @var array column_data Array with topic data that the database has been updated with
* @since 2.2.0
*/
$vars = array(
'topic_data',
'column_data',
);
extract($this->dispatcher->trigger_event('tierra.topicsolved.mark_unsolved_after', compact($vars)));
}
/**
* Checks if the currently logged in user has permission to lock a post.
*
* Regular users won't have permission to solve any topics other than their
* own, and moderator permissions are forum based, so we only need to know
* the forum, not the post.
*
* @param int $forum_id Forum to check permissions on.
*
* @return bool true if user has permission to lock a post.
*/
public function user_can_lock_post($forum_id)
{
// Check if user is moderator with appropriate lock permission
if ($this->auth->acl_get('m_lock', $forum_id))
{
return true;
}
// Check if user has "lock own posts" permission
if ($this->auth->acl_get('f_user_lock', $forum_id))
{
return true;
}
return false;
}
/**
* Generate markup for the given solved indicator image.
*
* @param string $type One of "head", "list", or "post".
* @param string $alt Language code for title and alternative text.
* @param string $url Optional link to solved post.
*
* @return string HTML markup for image.
*/
public function image($type, $alt = '', $url = '')
{
$title = '';
$markup = $this->user->img('icon_solved_' . $type, $alt);
if (!empty($alt))
{
$alt = $this->user->lang($alt);
$title = ' title="' . htmlspecialchars($alt, ENT_QUOTES, 'UTF-8') . '"';
}
if (!empty($url))
{
$markup = sprintf('<a href="%s"%s>%s</a>',
htmlspecialchars($url, ENT_QUOTES, 'UTF-8'), $title, $markup);
}
return $markup;
}
/**
* Generate markup for the given solved indicator icon.
*
* @param string $color Color to use for the icon.
* @param string $alt Language code for title and alternative text.
* @param string $url Optional link to solved post.
*
* @return string HTML markup for icon.
*/
public function icon($color = '', $alt = '', $url = '')
{
$title = '';
if (empty($color))
{
$color = '00BF00';
}
$classes = 'fa fa-check-circle fa-fw';
/**
* This event makes it possible to customize the solved icon.
*
* @event tierra.topicsolved.render_icon
* @var string alt Alternative text label for link if a URL was provided.
* @var string classes CSS classes used for icon.
* @var string color Color applied to the icon.
* @var string url Link to the solved post.
* @since 2.3.0
*/
$vars = array('alt', 'classes', 'color', 'url');
extract($this->dispatcher->trigger_event('tierra.topicsolved.render_icon', compact($vars)));
$markup = sprintf(
'<i class="%1s" style="color: #%2s" aria-hidden="true"></i>',
$classes, $color
);
if (!empty($alt))
{
$alt = $this->user->lang($alt);
$title = ' title="' . htmlspecialchars($alt, ENT_QUOTES, 'UTF-8') . '"';
}
if (!empty($url))
{
$markup = sprintf('<a href="%s"%s>%s</a>',
htmlspecialchars($url, ENT_QUOTES, 'UTF-8'), $title, $markup);
}
return $markup;
}
/**
* Generate link to specific post (usually solution post).
*
* @param int $forum_id
* @param int $topic_id
* @param int $post_id
*
* @return string Relative URL to post
*/
public function get_link_to_post($forum_id, $topic_id, $post_id)
{
return append_sid("{$this->root_path}viewtopic.{$this->php_ext}",
"f=$forum_id&t=$topic_id&p=$post_id") . '#p' . $post_id;
}
}