Total Complexity | 57 |
Total Lines | 523 |
Duplicated Lines | 0 % |
Changes | 11 | ||
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() |
||
311 | )); |
||
312 | } |
||
313 | |||
314 | public function acp_smilies_category() |
||
315 | { |
||
316 | $this->language->add_lang('acp/posting'); |
||
317 | $action = $this->request->variable('action', ''); |
||
318 | $start = $this->request->variable('start', 0); |
||
319 | $select = $this->request->variable('select', -1); |
||
320 | $id = $this->request->variable('id', -1); |
||
321 | $form_key = 'sylver35/smiliescat'; |
||
322 | add_form_key($form_key); |
||
323 | |||
324 | if ($action) |
||
325 | { |
||
326 | switch ($action) |
||
327 | { |
||
328 | case 'edit': |
||
329 | |||
330 | $this->category->adm_edit_smiley($id, $this->u_action, $start); |
||
331 | |||
332 | break; |
||
333 | |||
334 | case 'modify': |
||
335 | |||
336 | if (!check_form_key($form_key)) |
||
337 | { |
||
338 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
339 | } |
||
340 | |||
341 | $cat_id = $this->request->variable('cat_id', 0); |
||
342 | $ex_cat = $this->request->variable('ex_cat', 0); |
||
343 | |||
344 | $sql = 'UPDATE ' . SMILIES_TABLE . " SET category = $cat_id WHERE smiley_id = $id"; |
||
345 | $this->db->sql_query($sql); |
||
346 | |||
347 | // Decrement nb value if wanted |
||
348 | if ($ex_cat != 0) |
||
349 | { |
||
350 | $sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb - 1 WHERE cat_id = $ex_cat"; |
||
351 | $this->db->sql_query($sql); |
||
352 | } |
||
353 | // Increment nb value if wanted |
||
354 | if ($cat_id != 0) |
||
355 | { |
||
356 | $sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb + 1 WHERE cat_id = $cat_id"; |
||
357 | $this->db->sql_query($sql); |
||
358 | } |
||
359 | |||
360 | trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&start=' . $start . '#acp_smilies_category')); |
||
361 | |||
362 | break; |
||
363 | } |
||
364 | |||
365 | $this->template->assign_vars(array( |
||
366 | 'IN_ACTION' => true, |
||
367 | )); |
||
368 | } |
||
369 | else |
||
370 | { |
||
371 | $this->extract_list_smilies($select, $start); |
||
372 | } |
||
373 | |||
374 | $this->template->assign_vars(array( |
||
375 | 'CATEGORIE_SMILIES' => true, |
||
376 | )); |
||
377 | } |
||
378 | |||
379 | private function move_cat($action, $id) |
||
380 | { |
||
381 | // Get current order id and title... |
||
382 | $sql = 'SELECT cat_order, cat_title |
||
383 | FROM ' . $this->smilies_category_table . " |
||
384 | WHERE cat_id = $id"; |
||
385 | $result = $this->db->sql_query($sql); |
||
386 | $row = $this->db->sql_fetchrow($result); |
||
387 | $current_order = $row['cat_order']; |
||
388 | $title = $row['cat_title']; |
||
389 | $this->db->sql_freeresult($result); |
||
390 | |||
391 | if ($current_order == 1 && $action == 'move_up') |
||
392 | { |
||
393 | return array( |
||
394 | 'return' => false, |
||
395 | 'title' => '', |
||
396 | ); |
||
397 | } |
||
398 | |||
399 | $max_order = $this->category->get_max_order(); |
||
400 | |||
401 | if ($current_order == $max_order && $action == 'move_down') |
||
402 | { |
||
403 | return array( |
||
404 | 'return' => false, |
||
405 | 'title' => '', |
||
406 | ); |
||
407 | } |
||
408 | |||
409 | // on move_down, switch position with next order_id... |
||
410 | // on move_up, switch position with previous order_id... |
||
411 | $switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1; |
||
412 | |||
413 | $sql = 'UPDATE ' . $this->smilies_category_table . " |
||
414 | SET cat_order = $current_order |
||
415 | WHERE cat_order = $switch_order_id |
||
416 | AND cat_id <> $id"; |
||
417 | $this->db->sql_query($sql); |
||
418 | $move_executed = (bool) $this->db->sql_affectedrows(); |
||
419 | |||
420 | // Only update the other entry too if the previous entry got updated |
||
421 | if ($move_executed) |
||
422 | { |
||
423 | $sql = 'UPDATE ' . $this->smilies_category_table . " |
||
424 | SET cat_order = $switch_order_id |
||
425 | WHERE cat_order = $current_order |
||
426 | AND cat_id = $id"; |
||
427 | $this->db->sql_query($sql); |
||
428 | |||
429 | if ($switch_order_id == 1) |
||
430 | { |
||
431 | $this->config->set('smilies_category_nb', $id); |
||
432 | } |
||
433 | } |
||
434 | |||
435 | return array( |
||
436 | 'return' => true, |
||
437 | 'title' => $title, |
||
438 | ); |
||
439 | } |
||
440 | |||
441 | private function extract_list_smilies($select, $start) |
||
442 | { |
||
443 | $cat = $i = 0; |
||
444 | $smiley_url = ''; |
||
445 | $lang = $this->user->lang_name; |
||
446 | $smilies_count = $this->category->smilies_count($select); |
||
447 | $cat_title = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
448 | $where = ($select !== -1) ? "cat_id = $select AND " : ''; |
||
449 | |||
450 | if ($select !== 0) |
||
451 | { |
||
452 | $sql = $this->db->sql_build_query('SELECT', array( |
||
453 | 'SELECT' => 's.*, c.*', |
||
454 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
455 | 'LEFT_JOIN' => array( |
||
456 | array( |
||
457 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
458 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
459 | ), |
||
460 | ), |
||
461 | 'WHERE' => "$where code <> ''", |
||
462 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
463 | )); |
||
464 | } |
||
465 | else |
||
466 | { |
||
467 | $sql = $this->db->sql_build_query('SELECT', array( |
||
468 | 'SELECT' => '*', |
||
469 | 'FROM' => array(SMILIES_TABLE => ''), |
||
470 | 'WHERE' => "category = 0", |
||
471 | 'ORDER_BY' => 'smiley_order ASC', |
||
472 | )); |
||
473 | } |
||
474 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
475 | while ($row = $this->db->sql_fetchrow($result)) |
||
476 | { |
||
477 | if ($smiley_url === $row['smiley_url']) |
||
478 | { |
||
479 | continue; |
||
480 | } |
||
481 | $on_spacer = ($cat !== $row['category']) ? true : false; |
||
482 | $title = ($row['category'] == 0) ? $this->language->lang('SC_SMILIES_NO_CATEGORY') : $this->language->lang('SC_CATEGORY_IN', $row['cat_name']); |
||
483 | $this->template->assign_block_vars('items', array( |
||
484 | 'S_SPACER_CAT' => $on_spacer, |
||
485 | 'SPACER_CAT' => $title, |
||
486 | 'IMG_SRC' => $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'], |
||
487 | 'WIDTH' => $row['smiley_width'], |
||
488 | 'HEIGHT' => $row['smiley_height'], |
||
489 | 'CODE' => $row['code'], |
||
490 | 'EMOTION' => $row['emotion'], |
||
491 | 'CATEGORY' => (isset($row['cat_name'])) ? $row['cat_name'] : '', |
||
492 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
493 | )); |
||
494 | $i++; |
||
495 | $smiley_url = $row['smiley_url']; |
||
496 | $cat = $row['category']; |
||
497 | $cat_title = ($select > 0) ? $row['cat_name'] : $cat_title; |
||
498 | } |
||
499 | $this->db->sql_freeresult($result); |
||
500 | $empty_row = ($i == 0) ? true : false; |
||
501 | |||
502 | $this->template->assign_vars(array( |
||
503 | 'NB_SMILIES' => $smilies_count, |
||
504 | 'EMPTY_ROW' => $empty_row, |
||
505 | 'LIST_CATEGORY' => $this->category->select_categories($select), |
||
506 | 'S_SPACER_ANY' => ($cat == 0) ? true : false, |
||
507 | 'S_CAT_SELECT' => ($select) ? true : false, |
||
508 | 'CAT_SELECT_TITLE' => ($select) ? $this->language->lang('SC_CATEGORY_IN', $cat_title) : '', |
||
509 | 'U_BACK' => ($select) ? $this->u_action : false, |
||
510 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
511 | )); |
||
512 | |||
513 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
514 | } |
||
515 | |||
516 | private function delete_cat($action, $id) |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Set page url |
||
537 | * |
||
538 | * @param string $u_action Custom form action |
||
539 | * @return null |
||
540 | * @access public |
||
541 | */ |
||
542 | public function set_page_url($u_action) |
||
545 | } |
||
546 | } |
||
547 |