Total Complexity | 56 |
Total Lines | 519 |
Duplicated Lines | 0 % |
Changes | 10 | ||
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_categories_config() |
||
326 | )); |
||
327 | } |
||
328 | |||
329 | public function acp_smilies_category() |
||
330 | { |
||
331 | $this->language->add_lang('acp/posting'); |
||
332 | $action = $this->request->variable('action', ''); |
||
333 | $start = $this->request->variable('start', 0); |
||
334 | $select = $this->request->variable('select', -1); |
||
335 | $id = $this->request->variable('id', -1); |
||
336 | $form_key = 'sylver35/smiliescat'; |
||
337 | add_form_key($form_key); |
||
338 | |||
339 | if ($action) |
||
340 | { |
||
341 | switch ($action) |
||
342 | { |
||
343 | case 'edit': |
||
344 | |||
345 | $this->category->adm_edit_smiley($id, $this->u_action, $start); |
||
346 | |||
347 | break; |
||
348 | |||
349 | case 'modify': |
||
350 | |||
351 | if (!check_form_key($form_key)) |
||
352 | { |
||
353 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
354 | } |
||
355 | |||
356 | $cat_id = $this->request->variable('cat_id', 0); |
||
357 | $ex_cat = $this->request->variable('ex_cat', 0); |
||
358 | |||
359 | $sql = 'UPDATE ' . SMILIES_TABLE . " SET category = $cat_id WHERE smiley_id = $id"; |
||
360 | $this->db->sql_query($sql); |
||
361 | |||
362 | // Decrement nb value if wanted |
||
363 | if ($ex_cat != 0) |
||
364 | { |
||
365 | $sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb - 1 WHERE cat_id = $ex_cat"; |
||
366 | $this->db->sql_query($sql); |
||
367 | } |
||
368 | // Increment nb value if wanted |
||
369 | if ($cat_id != 0) |
||
370 | { |
||
371 | $sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb + 1 WHERE cat_id = $cat_id"; |
||
372 | $this->db->sql_query($sql); |
||
373 | } |
||
374 | |||
375 | trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&start=' . $start . '#acp_smilies_category')); |
||
376 | |||
377 | break; |
||
378 | } |
||
379 | |||
380 | $this->template->assign_vars(array( |
||
381 | 'IN_ACTION' => true, |
||
382 | )); |
||
383 | } |
||
384 | else |
||
385 | { |
||
386 | $this->extract_list_smilies($select, $start); |
||
387 | } |
||
388 | |||
389 | $this->template->assign_vars(array( |
||
390 | 'CATEGORIE_SMILIES' => true, |
||
391 | )); |
||
392 | } |
||
393 | |||
394 | private function move_cat($action, $id) |
||
453 | ); |
||
454 | } |
||
455 | |||
456 | private function extract_list_smilies($select, $start) |
||
457 | { |
||
458 | $cat = $i = 0; |
||
459 | $smiley_url = ''; |
||
460 | $lang = $this->user->lang_name; |
||
461 | $smilies_count = $this->category->smilies_count($select); |
||
462 | $cat_title = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
463 | $where = ($select !== -1) ? "cat_id = $select" : 'smiley_id > 0'; |
||
464 | |||
465 | if ($select !== 0) |
||
466 | { |
||
467 | $sql = $this->db->sql_build_query('SELECT', array( |
||
468 | 'SELECT' => 's.*, c.*', |
||
469 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
470 | 'LEFT_JOIN' => array( |
||
471 | array( |
||
472 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
473 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
474 | ), |
||
475 | ), |
||
476 | 'WHERE' => "$where AND code <> ''", |
||
477 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
478 | )); |
||
479 | } |
||
480 | else |
||
481 | { |
||
482 | $sql = $this->db->sql_build_query('SELECT', array( |
||
483 | 'SELECT' => '*', |
||
484 | 'FROM' => array(SMILIES_TABLE => ''), |
||
485 | 'WHERE' => "category = 0", |
||
486 | 'ORDER_BY' => 'smiley_order ASC', |
||
487 | )); |
||
488 | } |
||
489 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
490 | while ($row = $this->db->sql_fetchrow($result)) |
||
491 | { |
||
492 | if ($smiley_url === $row['smiley_url']) |
||
493 | { |
||
494 | continue; |
||
495 | } |
||
496 | $on_spacer = ($cat !== $row['category']) ? true : false; |
||
497 | $title = ($row['category'] == 0) ? $this->language->lang('SC_SMILIES_NO_CATEGORY') : $this->language->lang('SC_CATEGORY_IN', $row['cat_name']); |
||
498 | $this->template->assign_block_vars('items', array( |
||
499 | 'S_SPACER_CAT' => $on_spacer, |
||
500 | 'SPACER_CAT' => $title, |
||
501 | 'IMG_SRC' => $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'], |
||
502 | 'WIDTH' => $row['smiley_width'], |
||
503 | 'HEIGHT' => $row['smiley_height'], |
||
504 | 'CODE' => $row['code'], |
||
505 | 'EMOTION' => $row['emotion'], |
||
506 | 'CATEGORY' => (isset($row['cat_name'])) ? $row['cat_name'] : '', |
||
507 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
508 | )); |
||
509 | $i++; |
||
510 | $smiley_url = $row['smiley_url']; |
||
511 | $cat = $row['category']; |
||
512 | $cat_title = ($select > 0) ? $row['cat_name'] : $cat_title; |
||
513 | } |
||
514 | $this->db->sql_freeresult($result); |
||
515 | $empty_row = ($i == 0) ? true : false; |
||
516 | |||
517 | $this->template->assign_vars(array( |
||
518 | 'NB_SMILIES' => $smilies_count, |
||
519 | 'EMPTY_ROW' => $empty_row, |
||
520 | 'LIST_CATEGORY' => $this->category->select_categories($select), |
||
521 | 'S_SPACER_ANY' => ($cat === 0) ? true : false, |
||
522 | 'S_CAT_SELECT' => ($select) ? true : false, |
||
523 | 'CAT_SELECT_TITLE' => ($select) ? $this->language->lang('SC_CATEGORY_IN', $cat_title) : '', |
||
524 | 'U_BACK' => ($select) ? $this->u_action : false, |
||
525 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
526 | )); |
||
527 | |||
528 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Set page url |
||
533 | * |
||
534 | * @param string $u_action Custom form action |
||
535 | * @return null |
||
536 | * @access public |
||
537 | */ |
||
538 | public function set_page_url($u_action) |
||
541 | } |
||
542 | } |
||
543 |