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