1 | <?php |
||
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 | 34 | public function __construct(driver_interface $db, helper $group_helper, request $request, user $user) |
|
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 | 7 | $updated = $this->db->sql_affectedrows(); |
|
93 | |||
94 | // Resync bbcode_order |
||
95 | 7 | $this->resynchronize_bbcode_order(); |
|
96 | |||
97 | // Send a JSON response if this was an AJAX request |
||
98 | 7 | $this->send_json_response($updated); |
|
99 | 6 | } |
|
100 | |||
101 | /** |
||
102 | * Update BBCode order fields in the db on drag_drop |
||
103 | * |
||
104 | * @return null |
||
105 | * @access public |
||
106 | */ |
||
107 | 3 | public function drag_drop() |
|
108 | { |
||
109 | 3 | if (!$this->request->is_ajax()) |
|
110 | 3 | { |
|
111 | 1 | return; |
|
112 | } |
||
113 | |||
114 | // Get the bbcodes html table's name |
||
115 | 2 | $tablename = $this->request->variable('tablename', ''); |
|
116 | |||
117 | // Fetch the posted list |
||
118 | 2 | $bbcodes_list = $this->request->variable($tablename, array(0 => '')); |
|
119 | |||
120 | 2 | $this->db->sql_transaction('begin'); |
|
121 | |||
122 | // Run through the list |
||
123 | 2 | foreach ($bbcodes_list as $order => $bbcode_id) |
|
124 | { |
||
125 | // First one is the header, skip it |
||
126 | 2 | if ($order == 0) |
|
127 | 2 | { |
|
128 | 1 | continue; |
|
129 | } |
||
130 | |||
131 | 2 | $this->db->sql_query($this->update_bbcode_order($bbcode_id, $order)); |
|
132 | 2 | } |
|
133 | |||
134 | 2 | $this->db->sql_transaction('commit'); |
|
135 | |||
136 | // Resync bbcode_order |
||
137 | 2 | $this->resynchronize_bbcode_order(); |
|
138 | |||
139 | // Send an AJAX JSON response |
||
140 | 2 | $this->send_json_response(true); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Retrieve the maximum value from the bbcode_order field stored in the db |
||
145 | * |
||
146 | * @return int The maximum order |
||
147 | * @access public |
||
148 | */ |
||
149 | 1 | public function get_max_bbcode_order() |
|
150 | { |
||
151 | 1 | return $this->get_max_column_value('bbcode_order'); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get the bbcode_group data from the posted form |
||
156 | * |
||
157 | * @return string The usergroup id numbers, comma delimited, or empty |
||
158 | * @access public |
||
159 | */ |
||
160 | 3 | public function get_bbcode_group_form_data() |
|
161 | { |
||
162 | 3 | $bbcode_group = $this->request->variable('bbcode_group', array(0)); |
|
163 | 3 | $bbcode_group = (!sizeof($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group); |
|
164 | |||
165 | 3 | return $bbcode_group; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get the bbcode_group data from the database |
||
170 | * |
||
171 | * @param int $bbcode_id Custom BBCode id |
||
172 | * @return array Custom BBCode user group ids |
||
173 | * @access public |
||
174 | */ |
||
175 | 6 | public function get_bbcode_group_data($bbcode_id) |
|
176 | { |
||
177 | $sql = 'SELECT bbcode_group |
||
178 | 6 | FROM ' . BBCODES_TABLE . ' |
|
179 | 6 | WHERE bbcode_id = ' . (int) $bbcode_id; |
|
180 | 6 | $result = $this->db->sql_query($sql); |
|
181 | 6 | $row = $this->db->sql_fetchrow($result); |
|
182 | 6 | $this->db->sql_freeresult($result); |
|
183 | |||
184 | 6 | return explode(',', $row['bbcode_group']); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get the bbcode_group data from the database, |
||
189 | * for every BBCode that has groups assigned |
||
190 | * |
||
191 | * @return array Custom BBCode user group ids for each BBCode, by name |
||
192 | * @access public |
||
193 | */ |
||
194 | 1 | public function get_bbcode_groups_data() |
|
195 | { |
||
196 | $sql = 'SELECT bbcode_tag, bbcode_group |
||
197 | 1 | FROM ' . BBCODES_TABLE . " |
|
198 | 1 | WHERE bbcode_group > ''"; |
|
199 | 1 | $result = $this->db->sql_query($sql); |
|
200 | 1 | $groups = array(); |
|
201 | 1 | while ($row = $this->db->sql_fetchrow($result)) |
|
202 | { |
||
203 | 1 | $groups[$row['bbcode_tag']] = $row['bbcode_group']; |
|
204 | 1 | } |
|
205 | 1 | $this->db->sql_freeresult($result); |
|
206 | |||
207 | 1 | return $groups; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Generate a select box containing user groups |
||
212 | * |
||
213 | * @param array $select_id The user groups to mark as selected |
||
214 | * @return string HTML markup of user groups select box for the form |
||
215 | * @access public |
||
216 | */ |
||
217 | 5 | public function bbcode_group_select_options($select_id = array()) |
|
218 | { |
||
219 | // Get all groups except bots |
||
220 | $sql = 'SELECT group_id, group_name, group_type |
||
221 | 5 | FROM ' . GROUPS_TABLE . " |
|
222 | WHERE group_name <> 'BOTS' |
||
223 | 5 | ORDER BY group_name ASC"; |
|
224 | 5 | $result = $this->db->sql_query($sql); |
|
225 | |||
226 | 5 | $group_options = ''; |
|
227 | 5 | while ($row = $this->db->sql_fetchrow($result)) |
|
228 | { |
||
229 | 5 | $selected = (in_array($row['group_id'], $select_id)) ? ' selected="selected"' : ''; |
|
230 | 5 | $group_options .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . $this->group_helper->get_name($row['group_name']) . '</option>'; |
|
231 | 5 | } |
|
232 | 5 | $this->db->sql_freeresult($result); |
|
233 | |||
234 | 5 | return $group_options; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Resynchronize the Custom BBCodes order field |
||
239 | * (Originally based on Custom BBCode Sorting MOD by RMcGirr83) |
||
240 | * |
||
241 | * @return null |
||
242 | * @access public |
||
243 | */ |
||
244 | 14 | public function resynchronize_bbcode_order() |
|
245 | { |
||
246 | 14 | $this->db->sql_transaction('begin'); |
|
247 | |||
248 | $sql = 'SELECT bbcode_id, bbcode_order |
||
249 | 14 | FROM ' . BBCODES_TABLE . ' |
|
250 | 14 | ORDER BY bbcode_order, bbcode_id'; |
|
251 | 14 | $result = $this->db->sql_query($sql); |
|
252 | |||
253 | 14 | $order = 0; |
|
254 | 14 | while ($row = $this->db->sql_fetchrow($result)) |
|
255 | { |
||
256 | 14 | if (++$order != $row['bbcode_order']) |
|
257 | 14 | { |
|
258 | 7 | $this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order)); |
|
259 | 7 | } |
|
260 | 14 | } |
|
261 | 14 | $this->db->sql_freeresult($result); |
|
262 | |||
263 | 14 | $this->db->sql_transaction('commit'); |
|
264 | 14 | } |
|
265 | |||
266 | /** |
||
267 | * Build SQL query to update a bbcode order value |
||
268 | * |
||
269 | * @param int $bbcode_id ID of the bbcode |
||
270 | * @param int $bbcode_order Value of the bbcode order |
||
271 | * @return string The SQL query to run |
||
272 | * @access protected |
||
273 | */ |
||
274 | 8 | protected function update_bbcode_order($bbcode_id, $bbcode_order) |
|
275 | { |
||
276 | 8 | return 'UPDATE ' . BBCODES_TABLE . ' |
|
277 | 8 | SET bbcode_order = ' . (int) $bbcode_order . ' |
|
278 | 8 | WHERE bbcode_id = ' . (int) $bbcode_id; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Increment |
||
283 | * |
||
284 | * @param string $action The action move_up|move_down |
||
285 | * @return int Increment amount: Move up -1. Move down +1. |
||
286 | * @access protected |
||
287 | */ |
||
288 | 7 | protected function increment($action) |
|
289 | { |
||
290 | 7 | return ($action == 'move_up') ? -1 : 1; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Retrieve the maximum value in a column from the bbcodes table |
||
295 | * |
||
296 | * @param string $column Name of the column (bbcode_id|bbcode_order) |
||
297 | * @return int The maximum value in the column |
||
298 | * @access protected |
||
299 | */ |
||
300 | 3 | protected function get_max_column_value($column) |
|
301 | { |
||
302 | 3 | $sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum |
|
303 | 3 | FROM ' . BBCODES_TABLE; |
|
304 | 3 | $result = $this->db->sql_query($sql); |
|
305 | 3 | $maximum = $this->db->sql_fetchfield('maximum'); |
|
306 | 3 | $this->db->sql_freeresult($result); |
|
307 | |||
308 | 3 | return (int) $maximum; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Send a JSON response |
||
313 | * |
||
314 | * @param bool $content The content of the JSON response (true|false) |
||
315 | * @return null |
||
316 | * @access protected |
||
317 | */ |
||
318 | 9 | protected function send_json_response($content) |
|
328 | } |
||
329 |