1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Advanced BBCode Box |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2013 Matt Friedman |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace vse\abbc3\core; |
12
|
|
|
|
13
|
|
|
use phpbb\db\driver\driver_interface; |
14
|
|
|
use phpbb\request\request; |
15
|
|
|
use phpbb\user; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ABBC3 ACP manager class |
19
|
|
|
*/ |
20
|
|
|
class acp_manager |
21
|
|
|
{ |
22
|
|
|
/** @var driver_interface */ |
23
|
|
|
protected $db; |
24
|
|
|
|
25
|
|
|
/** @var request */ |
26
|
|
|
protected $request; |
27
|
|
|
|
28
|
|
|
/** @var user */ |
29
|
|
|
protected $user; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param driver_interface $db |
35
|
|
|
* @param request $request |
36
|
|
|
* @param user $user |
37
|
|
|
* @access public |
38
|
|
|
*/ |
39
|
34 |
|
public function __construct(driver_interface $db, request $request, user $user) |
40
|
|
|
{ |
41
|
34 |
|
$this->db = $db; |
42
|
34 |
|
$this->request = $request; |
43
|
34 |
|
$this->user = $user; |
44
|
34 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Update BBCode order fields in the db on move up/down |
48
|
|
|
* |
49
|
|
|
* @param string $action The action move_up|move_down |
50
|
|
|
* @access public |
51
|
|
|
*/ |
52
|
10 |
|
public function move($action) |
53
|
|
|
{ |
54
|
10 |
|
$bbcode_id = $this->request->variable('id', 0); |
55
|
|
|
|
56
|
10 |
|
if (!check_link_hash($this->request->variable('hash', ''), $action . $bbcode_id)) |
57
|
10 |
|
{ |
58
|
1 |
|
trigger_error($this->user->lang('FORM_INVALID'), E_USER_WARNING); |
59
|
|
|
} |
60
|
|
|
|
61
|
9 |
|
$current_order = $this->get_bbcode_order($bbcode_id); |
62
|
|
|
|
63
|
|
|
// First one can't be moved up |
64
|
9 |
|
if ($current_order <= 1 && $action === 'move_up') |
65
|
9 |
|
{ |
66
|
2 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
$updated = $this->update_bbcode_orders($current_order, $action); |
70
|
|
|
|
71
|
7 |
|
$this->resynchronize_bbcode_order(); |
72
|
|
|
|
73
|
7 |
|
$this->send_json_response($updated); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Update BBCode order fields in the db on drag_drop |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
*/ |
81
|
3 |
|
public function drag_drop() |
82
|
|
|
{ |
83
|
3 |
|
if (!$this->request->is_ajax()) |
84
|
3 |
|
{ |
85
|
1 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Get the bbcodes html table's name |
89
|
2 |
|
$tablename = $this->request->variable('tablename', ''); |
90
|
|
|
|
91
|
|
|
// Fetch the posted list |
92
|
2 |
|
$bbcodes_list = (array) $this->request->variable($tablename, array(0 => '')); |
93
|
|
|
|
94
|
|
|
// First one is the header, skip it |
95
|
2 |
|
unset($bbcodes_list[0]); |
96
|
|
|
|
97
|
2 |
|
$this->db->sql_transaction('begin'); |
98
|
2 |
|
foreach ($bbcodes_list as $order => $bbcode_id) |
99
|
|
|
{ |
100
|
2 |
|
$this->db->sql_query($this->update_bbcode_order($bbcode_id, $order)); |
101
|
2 |
|
} |
102
|
2 |
|
$this->db->sql_transaction('commit'); |
103
|
|
|
|
104
|
2 |
|
$this->resynchronize_bbcode_order(); |
105
|
|
|
|
106
|
2 |
|
$this->send_json_response(true); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve the maximum value from the bbcode_order field stored in the db |
111
|
|
|
* |
112
|
|
|
* @return int The maximum order |
113
|
|
|
* @access public |
114
|
|
|
*/ |
115
|
1 |
|
public function get_max_bbcode_order() |
116
|
|
|
{ |
117
|
1 |
|
return $this->get_max_column_value('bbcode_order'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the bbcode_group data from the posted form |
122
|
|
|
* |
123
|
|
|
* @return string The usergroup id numbers, comma delimited, or empty |
124
|
|
|
* @access public |
125
|
|
|
*/ |
126
|
3 |
|
public function get_bbcode_group_form_data() |
127
|
|
|
{ |
128
|
3 |
|
$bbcode_group = $this->request->variable('bbcode_group', array(0)); |
129
|
3 |
|
$bbcode_group = (!sizeof($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group); |
130
|
|
|
|
131
|
3 |
|
return $bbcode_group; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the bbcode_group data from the database |
136
|
|
|
* |
137
|
|
|
* @param int $bbcode_id Custom BBCode id |
138
|
|
|
* @return array Custom BBCode user group ids |
139
|
|
|
* @access public |
140
|
|
|
*/ |
141
|
6 |
|
public function get_bbcode_group_data($bbcode_id) |
142
|
|
|
{ |
143
|
|
|
$sql = 'SELECT bbcode_group |
144
|
6 |
|
FROM ' . BBCODES_TABLE . ' |
145
|
6 |
|
WHERE bbcode_id = ' . (int) $bbcode_id; |
146
|
6 |
|
$result = $this->db->sql_query($sql); |
147
|
6 |
|
$row = $this->db->sql_fetchrow($result); |
148
|
6 |
|
$this->db->sql_freeresult($result); |
149
|
|
|
|
150
|
6 |
|
return explode(',', $row['bbcode_group']); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the bbcode_group data from the database, |
155
|
|
|
* for every BBCode that has groups assigned |
156
|
|
|
* |
157
|
|
|
* @return array Custom BBCode user group ids for each BBCode, by name |
158
|
|
|
* @access public |
159
|
|
|
*/ |
160
|
1 |
|
public function get_bbcode_groups_data() |
161
|
|
|
{ |
162
|
|
|
$sql = 'SELECT bbcode_tag, bbcode_group |
163
|
1 |
|
FROM ' . BBCODES_TABLE . " |
164
|
1 |
|
WHERE bbcode_group > ''"; |
165
|
1 |
|
$result = $this->db->sql_query($sql); |
166
|
1 |
|
$groups = array(); |
167
|
1 |
|
while ($row = $this->db->sql_fetchrow($result)) |
168
|
|
|
{ |
169
|
1 |
|
$groups[$row['bbcode_tag']] = $row['bbcode_group']; |
170
|
1 |
|
} |
171
|
1 |
|
$this->db->sql_freeresult($result); |
172
|
|
|
|
173
|
1 |
|
return $groups; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Generate a select box containing user groups |
178
|
|
|
* |
179
|
|
|
* @param array $select_id The user groups to mark as selected |
180
|
|
|
* @return string HTML markup of user groups select box for the form |
181
|
|
|
* @access public |
182
|
|
|
*/ |
183
|
5 |
|
public function bbcode_group_select_options(array $select_id = array()) |
184
|
|
|
{ |
185
|
|
|
// Get all groups except bots |
186
|
|
|
$sql = 'SELECT group_id, group_name, group_type |
187
|
5 |
|
FROM ' . GROUPS_TABLE . " |
188
|
|
|
WHERE group_name <> 'BOTS' |
189
|
5 |
|
ORDER BY group_name ASC"; |
190
|
5 |
|
$result = $this->db->sql_query($sql); |
191
|
|
|
|
192
|
5 |
|
$group_options = ''; |
193
|
5 |
|
while ($row = $this->db->sql_fetchrow($result)) |
194
|
|
|
{ |
195
|
5 |
|
$selected = in_array($row['group_id'], $select_id) ? ' selected="selected"' : ''; |
196
|
5 |
|
$group_options .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . ($row['group_type'] == GROUP_SPECIAL ? $this->user->lang('G_' . $row['group_name']) : $row['group_name']) . '</option>'; |
197
|
5 |
|
} |
198
|
5 |
|
$this->db->sql_freeresult($result); |
199
|
|
|
|
200
|
5 |
|
return $group_options; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Resynchronize the Custom BBCodes order field |
205
|
|
|
* (Originally based on Custom BBCode Sorting MOD by RMcGirr83) |
206
|
|
|
* |
207
|
|
|
* @access public |
208
|
|
|
*/ |
209
|
14 |
|
public function resynchronize_bbcode_order() |
210
|
|
|
{ |
211
|
14 |
|
$this->db->sql_transaction('begin'); |
212
|
|
|
|
213
|
|
|
$sql = 'SELECT bbcode_id, bbcode_order |
214
|
14 |
|
FROM ' . BBCODES_TABLE . ' |
215
|
14 |
|
ORDER BY bbcode_order, bbcode_id'; |
216
|
14 |
|
$result = $this->db->sql_query($sql); |
217
|
|
|
|
218
|
14 |
|
$order = 0; |
219
|
14 |
|
while ($row = $this->db->sql_fetchrow($result)) |
220
|
|
|
{ |
221
|
14 |
|
if (++$order != $row['bbcode_order']) |
222
|
14 |
|
{ |
223
|
7 |
|
$this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order)); |
224
|
7 |
|
} |
225
|
14 |
|
} |
226
|
14 |
|
$this->db->sql_freeresult($result); |
227
|
|
|
|
228
|
14 |
|
$this->db->sql_transaction('commit'); |
229
|
14 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the bbcode_order value for a bbcode |
233
|
|
|
* |
234
|
|
|
* @param int $bbcode_id ID of the bbcode |
235
|
|
|
* @return int The bbcode's order |
236
|
|
|
* @access protected |
237
|
|
|
*/ |
238
|
9 |
|
protected function get_bbcode_order($bbcode_id) |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
$sql = 'SELECT bbcode_order |
241
|
9 |
|
FROM ' . BBCODES_TABLE . " |
242
|
9 |
|
WHERE bbcode_id = $bbcode_id"; |
243
|
9 |
|
$result = $this->db->sql_query($sql); |
244
|
9 |
|
$bbcode_order = $this->db->sql_fetchfield('bbcode_order'); |
245
|
9 |
|
$this->db->sql_freeresult($result); |
246
|
|
|
|
247
|
9 |
|
return (int) $bbcode_order; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Update the bbcode orders for bbcodes moved up/down |
252
|
|
|
* |
253
|
|
|
* @param int $bbcode_order Value of the bbcode order |
254
|
|
|
* @param string $action The action move_up|move_down |
255
|
|
|
* @return mixed Number of the affected rows by the last query |
256
|
|
|
* false if no query has been run before |
257
|
|
|
* @access protected |
258
|
|
|
*/ |
259
|
7 |
|
protected function update_bbcode_orders($bbcode_order, $action) |
260
|
|
|
{ |
261
|
7 |
|
$amount = ($action === 'move_up') ? -1 : 1; |
262
|
|
|
|
263
|
7 |
|
$order_total = $bbcode_order * 2 + $amount; |
264
|
|
|
|
265
|
7 |
|
$sql = 'UPDATE ' . BBCODES_TABLE . ' |
266
|
7 |
|
SET bbcode_order = ' . $order_total . ' - bbcode_order |
267
|
7 |
|
WHERE ' . $this->db->sql_in_set('bbcode_order', array( |
268
|
7 |
|
$bbcode_order, |
269
|
7 |
|
$bbcode_order + $amount, |
270
|
7 |
|
)); |
271
|
7 |
|
$this->db->sql_query($sql); |
272
|
|
|
|
273
|
7 |
|
return $this->db->sql_affectedrows(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Build SQL query to update a bbcode order value |
278
|
|
|
* |
279
|
|
|
* @param int $bbcode_id ID of the bbcode |
280
|
|
|
* @param int $bbcode_order Value of the bbcode order |
281
|
|
|
* @return string The SQL query to run |
282
|
|
|
* @access protected |
283
|
|
|
*/ |
284
|
8 |
|
protected function update_bbcode_order($bbcode_id, $bbcode_order) |
285
|
|
|
{ |
286
|
8 |
|
return 'UPDATE ' . BBCODES_TABLE . ' |
287
|
8 |
|
SET bbcode_order = ' . (int) $bbcode_order . ' |
288
|
8 |
|
WHERE bbcode_id = ' . (int) $bbcode_id; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Retrieve the maximum value in a column from the bbcodes table |
293
|
|
|
* |
294
|
|
|
* @param string $column Name of the column (bbcode_id|bbcode_order) |
295
|
|
|
* @return int The maximum value in the column |
296
|
|
|
* @access protected |
297
|
|
|
*/ |
298
|
3 |
|
protected function get_max_column_value($column) |
299
|
|
|
{ |
300
|
3 |
|
$sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum |
301
|
3 |
|
FROM ' . BBCODES_TABLE; |
302
|
3 |
|
$result = $this->db->sql_query($sql); |
303
|
3 |
|
$maximum = $this->db->sql_fetchfield('maximum'); |
304
|
3 |
|
$this->db->sql_freeresult($result); |
305
|
|
|
|
306
|
3 |
|
return (int) $maximum; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Send a JSON response |
311
|
|
|
* |
312
|
|
|
* @param bool $content The content of the JSON response (true|false) |
313
|
|
|
* @access protected |
314
|
|
|
*/ |
315
|
9 |
|
protected function send_json_response($content) |
316
|
|
|
{ |
317
|
9 |
|
if ($this->request->is_ajax()) |
318
|
9 |
|
{ |
319
|
3 |
|
$json_response = new \phpbb\json_response; |
320
|
3 |
|
$json_response->send(array( |
321
|
3 |
|
'success' => (bool) $content, |
322
|
3 |
|
)); |
323
|
|
|
} |
324
|
6 |
|
} |
325
|
|
|
} |
326
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.