Total Complexity | 58 |
Total Lines | 610 |
Duplicated Lines | 0 % |
Changes | 21 | ||
Bugs | 0 | Features | 0 |
Complex classes like admin_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class admin_controller |
||
23 | { |
||
24 | /* @var \sylver35\smiliescat\core\category */ |
||
25 | protected $category; |
||
26 | |||
27 | /** @var \phpbb\config\config */ |
||
28 | protected $config; |
||
29 | |||
30 | /** @var \phpbb\db\driver\driver_interface */ |
||
31 | protected $db; |
||
32 | |||
33 | /** @var \phpbb\pagination */ |
||
34 | protected $pagination; |
||
35 | |||
36 | /** @var \phpbb\request\request */ |
||
37 | protected $request; |
||
38 | |||
39 | /** @var \phpbb\template\template */ |
||
40 | protected $template; |
||
41 | |||
42 | /** @var \phpbb\user */ |
||
43 | protected $user; |
||
44 | |||
45 | /** @var \phpbb\language\language */ |
||
46 | protected $language; |
||
47 | |||
48 | /** @var \phpbb\log\log */ |
||
49 | protected $log; |
||
50 | |||
51 | /** @var string phpBB root path */ |
||
52 | protected $root_path; |
||
53 | |||
54 | /** @var string Custom form action */ |
||
55 | protected $u_action; |
||
56 | |||
57 | /** |
||
58 | * The database tables |
||
59 | * |
||
60 | * @var string */ |
||
61 | protected $smilies_category_table; |
||
62 | |||
63 | /** |
||
64 | * Constructor |
||
65 | */ |
||
66 | public function __construct(category $category, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table) |
||
79 | } |
||
80 | |||
81 | public function acp_smilies_category($id, $action) |
||
82 | { |
||
83 | $this->language->add_lang('acp/posting'); |
||
84 | $start = (int) $this->request->variable('start', 0); |
||
85 | $select = (int) $this->request->variable('select', -1); |
||
86 | $cat_id = (int) $this->request->variable('cat_id', 0); |
||
87 | $ex_cat = (int) $this->request->variable('ex_cat', 0); |
||
88 | $list = $this->request->variable('list', [0]); |
||
89 | $form_key = 'sylver35/smiliescat'; |
||
90 | add_form_key($form_key); |
||
91 | |||
92 | if ($action) |
||
93 | { |
||
94 | switch ($action) |
||
95 | { |
||
96 | case 'edit': |
||
97 | $this->edit_smiley($id, $start); |
||
98 | break; |
||
99 | |||
100 | case 'edit_multi': |
||
101 | $list = $this->request->variable('mark', [0]); |
||
102 | $this->edit_multi_smiley($list, $start); |
||
103 | break; |
||
104 | |||
105 | case 'modify': |
||
106 | if (!check_form_key($form_key)) |
||
107 | { |
||
108 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
109 | } |
||
110 | |||
111 | $this->modify_smiley($id, $cat_id, $ex_cat); |
||
112 | trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&start=' . $start . '#acp_smilies_category')); |
||
113 | break; |
||
114 | |||
115 | case 'modify_list': |
||
116 | foreach ($list as $smiley) |
||
117 | { |
||
118 | $this->modify_smiley($smiley, $cat_id); |
||
119 | } |
||
120 | trigger_error($this->language->lang('SMILIES_EDITED', count($list)) . adm_back_link($this->u_action . '&start=' . $start . '#acp_smilies_category')); |
||
121 | break; |
||
122 | } |
||
123 | |||
124 | $this->template->assign_var('IN_ACTION', true); |
||
|
|||
125 | } |
||
126 | else |
||
127 | { |
||
128 | $this->extract_list_smilies($select, $start); |
||
129 | |||
130 | $this->template->assign_vars([ |
||
131 | 'LIST_CATEGORY' => $this->category->select_categories($select, true, true), |
||
132 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
133 | 'U_BACK' => ($select) ? $this->u_action : '', |
||
134 | ]); |
||
135 | } |
||
136 | |||
137 | $this->template->assign_vars([ |
||
138 | 'CATEGORIE_SMILIES' => true, |
||
139 | ]); |
||
140 | } |
||
141 | |||
142 | public function acp_categories_config($id, $action, $mode) |
||
143 | { |
||
144 | $this->language->add_lang('acp/language'); |
||
145 | $form_key = 'sylver35/smiliescat'; |
||
146 | add_form_key($form_key); |
||
147 | |||
148 | if ($action) |
||
149 | { |
||
150 | if (in_array($action, ['config_cat', 'add_cat', 'edit_cat']) && !check_form_key($form_key)) |
||
151 | { |
||
152 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
153 | } |
||
154 | |||
155 | switch ($action) |
||
156 | { |
||
157 | case 'config_cat': |
||
158 | $this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15)); |
||
159 | |||
160 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time()); |
||
161 | trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action)); |
||
162 | break; |
||
163 | |||
164 | case 'add': |
||
165 | $this->add_cat(); |
||
166 | break; |
||
167 | |||
168 | case 'add_cat': |
||
169 | $this->add_category(); |
||
170 | break; |
||
171 | |||
172 | case 'edit': |
||
173 | $this->edit_cat((int) $id); |
||
174 | break; |
||
175 | |||
176 | case 'edit_cat': |
||
177 | $this->edit_category((int) $id); |
||
178 | break; |
||
179 | |||
180 | case 'delete': |
||
181 | if (confirm_box(true)) |
||
182 | { |
||
183 | $this->delete_cat((int) $id); |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([ |
||
188 | 'mode' => $mode, |
||
189 | 'id' => $id, |
||
190 | 'action' => $action, |
||
191 | ])); |
||
192 | } |
||
193 | break; |
||
194 | } |
||
195 | |||
196 | $this->template->assign_vars([ |
||
197 | 'IN_ACTION' => true, |
||
198 | ]); |
||
199 | } |
||
200 | else |
||
201 | { |
||
202 | $this->category->adm_list_cat($this->u_action); |
||
203 | } |
||
204 | |||
205 | $this->template->assign_vars([ |
||
206 | 'CATEGORIE_CONFIG' => true, |
||
207 | 'SMILIES_PER_PAGE_CAT' => $this->config['smilies_per_page_cat'], |
||
208 | 'U_ACTION_CONFIG' => $this->u_action . '&action=config_cat', |
||
209 | 'U_ADD' => $this->u_action . '&action=add', |
||
210 | ]); |
||
211 | } |
||
212 | |||
213 | private function modify_smiley($id, $cat_id, $ex_cat = -1) |
||
228 | } |
||
229 | |||
230 | private function update_cat_smiley($cat_id, $ex_cat) |
||
231 | { |
||
232 | // Increment nb value if wanted |
||
233 | if ($cat_id) |
||
234 | { |
||
235 | if ($this->category->get_first_order() === $cat_id) |
||
236 | { |
||
237 | if ($this->category->get_cat_nb($cat_id) === 0) |
||
238 | { |
||
239 | $this->config->set('smilies_category_nb', $cat_id); |
||
240 | } |
||
241 | } |
||
242 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb + 1 WHERE cat_id = ' . $cat_id); |
||
243 | } |
||
244 | |||
245 | // Decrement nb value if wanted |
||
246 | if ($ex_cat) |
||
247 | { |
||
248 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb - 1 WHERE cat_id = ' . $ex_cat); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | private function extract_list_smilies($select, $start) |
||
253 | { |
||
254 | $i = 0; |
||
255 | $cat = -1; |
||
256 | $lang = $this->user->lang_name; |
||
257 | $smilies_count = (int) $this->category->smilies_count($select); |
||
258 | |||
259 | if ($select === 0) |
||
260 | { |
||
261 | $sql = 'SELECT * |
||
262 | FROM ' . SMILIES_TABLE . ' |
||
263 | WHERE category = 0 |
||
264 | ORDER BY smiley_order ASC'; |
||
265 | } |
||
266 | else |
||
267 | { |
||
268 | $sql = $this->db->sql_build_query('SELECT', [ |
||
269 | 'SELECT' => 's.*, c.*', |
||
270 | 'FROM' => [SMILIES_TABLE => 's'], |
||
271 | 'LEFT_JOIN' => [ |
||
272 | [ |
||
273 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
274 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
275 | ], |
||
276 | ], |
||
277 | 'WHERE' => ($select === -1) ? "code <> ''" : "cat_id = $select AND code <> ''", |
||
278 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
279 | ]); |
||
280 | } |
||
281 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
282 | while ($row = $this->db->sql_fetchrow($result)) |
||
283 | { |
||
284 | $row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
285 | $this->template->assign_block_vars('items', [ |
||
286 | 'SPACER_CAT' => ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '', |
||
287 | 'IMG_SRC' => $row['smiley_url'], |
||
288 | 'WIDTH' => $row['smiley_width'], |
||
289 | 'HEIGHT' => $row['smiley_height'], |
||
290 | 'ID' => $row['smiley_id'], |
||
291 | 'CODE' => $row['code'], |
||
292 | 'EMOTION' => $row['emotion'], |
||
293 | 'CATEGORY' => $row['cat_name'], |
||
294 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
295 | ]); |
||
296 | $i++; |
||
297 | |||
298 | // Keep this value in memory |
||
299 | $cat = (int) $row['category']; |
||
300 | } |
||
301 | $this->db->sql_freeresult($result); |
||
302 | |||
303 | $this->template->assign_vars([ |
||
304 | 'NB_SMILIES' => $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count), |
||
305 | 'U_SMILIES' => $this->root_path . $this->config['smilies_path'] . '/', |
||
306 | 'U_MODIFY' => $this->u_action . '&action=edit_multi&start=' . $start, |
||
307 | ]); |
||
308 | |||
309 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
310 | } |
||
311 | |||
312 | private function delete_cat($id) |
||
313 | { |
||
314 | $sql = 'SELECT cat_title, cat_order |
||
315 | FROM ' . $this->smilies_category_table . ' |
||
316 | WHERE cat_id = ' . $id; |
||
317 | $result = $this->db->sql_query($sql); |
||
318 | $row = $this->db->sql_fetchrow($result); |
||
319 | $title = $row['cat_title']; |
||
320 | $order = (int) $row['cat_order']; |
||
321 | $this->db->sql_freeresult($result); |
||
322 | |||
323 | $this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id); |
||
324 | |||
325 | // Decrement orders if needed |
||
326 | $sql_decrement = 'SELECT cat_id, cat_order |
||
327 | FROM ' . $this->smilies_category_table . ' |
||
328 | WHERE cat_order > ' . $order; |
||
329 | $result = $this->db->sql_query($sql_decrement); |
||
330 | while ($row = $this->db->sql_fetchrow($result)) |
||
331 | { |
||
332 | $new_order = (int) $row['cat_order'] - 1; |
||
333 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_order = ' . $new_order . ' WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order']); |
||
334 | } |
||
335 | $this->db->sql_freeresult($result); |
||
336 | |||
337 | // Reset appropriate smilies category id |
||
338 | $this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id); |
||
339 | |||
340 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]); |
||
341 | trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action)); |
||
342 | } |
||
343 | |||
344 | private function add_category() |
||
396 | } |
||
397 | |||
398 | private function edit_category($id) |
||
399 | { |
||
400 | $sql_in = []; |
||
401 | $title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true)); |
||
402 | $order = (int) $this->request->variable('order', 0); |
||
403 | $cat_nb = (int) $this->request->variable('cat_nb', 0); |
||
404 | |||
405 | $sql = 'SELECT lang_id, lang_iso |
||
406 | FROM ' . LANG_TABLE . " |
||
407 | ORDER BY lang_id ASC"; |
||
408 | $result = $this->db->sql_query($sql); |
||
409 | while ($row = $this->db->sql_fetchrow($result)) |
||
410 | { |
||
411 | $iso = strtolower($row['lang_iso']); |
||
412 | $lang = (string) $this->request->variable("lang_$iso", '', true); |
||
413 | $sort = (string) $this->request->variable("sort_$iso", ''); |
||
414 | $name = $this->category->capitalize($this->request->variable("name_$iso", '', true)); |
||
415 | |||
416 | if ($name === '') |
||
417 | { |
||
418 | trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&action=edit&id=' . $id), E_USER_WARNING); |
||
419 | } |
||
420 | else |
||
421 | { |
||
422 | if ($sort === 'edit') |
||
423 | { |
||
424 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_name = '" . $this->db->sql_escape($name) . "', cat_title = '" . $this->db->sql_escape($title) . "', cat_nb = $cat_nb WHERE cat_lang = '" . $this->db->sql_escape($lang) . "' AND cat_id = $id"); |
||
425 | } |
||
426 | else if ($sort === 'create') |
||
427 | { |
||
428 | $sql_in[] = [ |
||
429 | 'cat_id' => $id, |
||
430 | 'cat_order' => $order, |
||
431 | 'cat_lang' => $lang, |
||
432 | 'cat_name' => $name, |
||
433 | 'cat_title' => $title, |
||
434 | 'cat_nb' => $cat_nb, |
||
435 | ]; |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | $this->db->sql_freeresult($result); |
||
440 | |||
441 | $this->db->sql_multi_insert($this->smilies_category_table, $sql_in); |
||
442 | |||
443 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]); |
||
444 | trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action)); |
||
445 | } |
||
446 | |||
447 | private function add_cat() |
||
448 | { |
||
449 | $max = (int) $this->category->get_max_order() + 1; |
||
450 | $sql = 'SELECT lang_local_name, lang_iso |
||
451 | FROM ' . LANG_TABLE . ' |
||
452 | ORDER BY lang_id ASC'; |
||
453 | $result = $this->db->sql_query($sql); |
||
454 | while ($row = $this->db->sql_fetchrow($result)) |
||
455 | { |
||
456 | $this->template->assign_block_vars('categories', [ |
||
457 | 'CAT_LANG' => $row['lang_local_name'], |
||
458 | 'CAT_ISO' => $row['lang_iso'], |
||
459 | 'CAT_ORDER' => $max, |
||
460 | ]); |
||
461 | } |
||
462 | $this->db->sql_freeresult($result); |
||
463 | |||
464 | $this->template->assign_vars([ |
||
465 | 'IN_CAT_ACTION' => true, |
||
466 | 'IN_ADD_ACTION' => true, |
||
467 | 'CAT_ORDER' => $max, |
||
468 | 'U_BACK' => $this->u_action, |
||
469 | 'U_ADD_CAT' => $this->u_action . '&action=add_cat', |
||
470 | ]); |
||
471 | } |
||
472 | |||
473 | private function edit_cat($id) |
||
474 | { |
||
475 | // Get total lang id... |
||
476 | $sql = 'SELECT COUNT(lang_id) as total |
||
477 | FROM ' . LANG_TABLE; |
||
478 | $result = $this->db->sql_query($sql); |
||
479 | $total = (int) $this->db->sql_fetchfield('total'); |
||
480 | $this->db->sql_freeresult($result); |
||
481 | |||
482 | $title = ''; |
||
483 | $list_id = []; |
||
484 | $i = $cat_order = $cat_nb = 0; |
||
485 | $sql = $this->db->sql_build_query('SELECT', [ |
||
486 | 'SELECT' => 'l.*, c.*', |
||
487 | 'FROM' => [LANG_TABLE => 'l'], |
||
488 | 'LEFT_JOIN' => [ |
||
489 | [ |
||
490 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
491 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
492 | ], |
||
493 | ], |
||
494 | 'WHERE' => 'cat_id = ' . $id, |
||
495 | 'ORDER_BY' => 'lang_id ASC', |
||
496 | ]); |
||
497 | $result = $this->db->sql_query($sql); |
||
498 | while ($row = $this->db->sql_fetchrow($result)) |
||
499 | { |
||
500 | $this->template->assign_block_vars('category_lang', [ |
||
501 | 'CAT_LANG' => $row['lang_local_name'], |
||
502 | 'CAT_ISO' => $row['lang_iso'], |
||
503 | 'CAT_ORDER' => $row['cat_order'], |
||
504 | 'CAT_ID' => $row['cat_id'], |
||
505 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
506 | 'CAT_SORT' => 'edit', |
||
507 | ]); |
||
508 | $i++; |
||
509 | $list_id[$i] = $row['lang_id']; |
||
510 | $cat_order = $row['cat_order']; |
||
511 | $title = $row['cat_title']; |
||
512 | $cat_nb = $row['cat_nb']; |
||
513 | } |
||
514 | $this->db->sql_freeresult($result); |
||
515 | |||
516 | // Add rows for empty langs in this category |
||
517 | if ($i !== $total) |
||
518 | { |
||
519 | $sql = $this->db->sql_build_query('SELECT', [ |
||
520 | 'SELECT' => '*', |
||
521 | 'FROM' => [LANG_TABLE => 'l'], |
||
522 | 'WHERE' => $this->db->sql_in_set('lang_id', $list_id, true, true), |
||
523 | ]); |
||
524 | $result = $this->db->sql_query($sql); |
||
525 | while ($row = $this->db->sql_fetchrow($result)) |
||
526 | { |
||
527 | $this->template->assign_block_vars('category_lang', [ |
||
528 | 'CAT_LANG' => $row['lang_local_name'], |
||
529 | 'CAT_ISO' => $row['lang_iso'], |
||
530 | 'CAT_ORDER' => $cat_order, |
||
531 | 'CAT_ID' => $id, |
||
532 | 'CAT_TRANSLATE' => '', |
||
533 | 'CAT_SORT' => 'create', |
||
534 | ]); |
||
535 | } |
||
536 | $this->db->sql_freeresult($result); |
||
537 | } |
||
538 | |||
539 | $this->template->assign_vars([ |
||
540 | 'IN_CAT_ACTION' => true, |
||
541 | 'CAT_ORDER' => $cat_order, |
||
542 | 'CAT_NB' => $cat_nb, |
||
543 | 'CAT_TITLE' => $title, |
||
544 | 'U_BACK' => $this->u_action, |
||
545 | 'U_EDIT_CAT' => $this->u_action . '&action=edit_cat&id=' . $id, |
||
546 | ]); |
||
547 | } |
||
548 | |||
549 | private function edit_smiley($id, $start) |
||
580 | } |
||
581 | |||
582 | private function edit_multi_smiley($list, $start) |
||
619 | ]); |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Set page url |
||
624 | * |
||
625 | * @param string $u_action Custom form action |
||
626 | * @return null |
||
627 | * @access public |
||
628 | */ |
||
629 | public function set_page_url($u_action) |
||
632 | } |
||
633 | } |
||
634 |