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