| Total Complexity | 50 |
| Total Lines | 538 |
| 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 | $form_key = 'sylver35/smiliescat'; |
||
| 87 | add_form_key($form_key); |
||
| 88 | |||
| 89 | if ($action) |
||
| 90 | { |
||
| 91 | switch ($action) |
||
| 92 | { |
||
| 93 | case 'edit': |
||
| 94 | $this->edit_smiley($id, $start); |
||
| 95 | break; |
||
| 96 | |||
| 97 | case 'modify': |
||
| 98 | if (!check_form_key($form_key)) |
||
| 99 | { |
||
| 100 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->modify_smiley($id, (int) $this->request->variable('cat_id', 0), (int) $this->request->variable('ex_cat', 0)); |
||
| 104 | trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&start=' . $start . '#acp_smilies_category')); |
||
| 105 | break; |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->template->assign_vars([ |
||
| 109 | 'IN_ACTION' => true, |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | else |
||
| 113 | { |
||
| 114 | $this->extract_list_smilies($select, $start); |
||
| 115 | |||
| 116 | $this->template->assign_vars([ |
||
| 117 | 'LIST_CATEGORY' => $this->category->select_categories($select, true, true), |
||
| 118 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
| 119 | 'U_BACK' => ($select) ? $this->u_action : '', |
||
| 120 | ]); |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->template->assign_vars([ |
||
| 124 | 'CATEGORIE_SMILIES' => true, |
||
| 125 | ]); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function acp_categories_config($id, $action, $mode) |
||
| 196 | ]); |
||
| 197 | } |
||
| 198 | |||
| 199 | private function modify_smiley($id, $cat_id, $ex_cat) |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | private function extract_list_smilies($select, $start) |
||
| 224 | { |
||
| 225 | $i = 0; |
||
| 226 | $cat = -1; |
||
| 227 | $lang = $this->user->lang_name; |
||
| 228 | $smilies_count = (int) $this->category->smilies_count($select); |
||
| 229 | |||
| 230 | if ($select === 0) |
||
| 231 | { |
||
| 232 | $sql = 'SELECT * |
||
| 233 | FROM ' . SMILIES_TABLE . ' |
||
| 234 | WHERE category = 0 |
||
| 235 | ORDER BY smiley_order ASC'; |
||
| 236 | } |
||
| 237 | else |
||
| 238 | { |
||
| 239 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 240 | 'SELECT' => 's.*, c.*', |
||
| 241 | 'FROM' => [SMILIES_TABLE => 's'], |
||
| 242 | 'LEFT_JOIN' => [ |
||
| 243 | [ |
||
| 244 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
| 245 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
| 246 | ], |
||
| 247 | ], |
||
| 248 | 'WHERE' => ($select === -1) ? "code <> ''" : "cat_id = $select AND code <> ''", |
||
| 249 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
| 250 | ]); |
||
| 251 | } |
||
| 252 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
| 253 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 254 | { |
||
| 255 | $row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
| 256 | $this->template->assign_block_vars('items', [ |
||
| 257 | 'SPACER_CAT' => ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '', |
||
| 258 | 'IMG_SRC' => $row['smiley_url'], |
||
| 259 | 'WIDTH' => $row['smiley_width'], |
||
| 260 | 'HEIGHT' => $row['smiley_height'], |
||
| 261 | 'CODE' => $row['code'], |
||
| 262 | 'EMOTION' => $row['emotion'], |
||
| 263 | 'CATEGORY' => $row['cat_name'], |
||
| 264 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
| 265 | ]); |
||
| 266 | $i++; |
||
| 267 | |||
| 268 | // Keep this value in memory |
||
| 269 | $cat = (int) $row['category']; |
||
| 270 | } |
||
| 271 | $this->db->sql_freeresult($result); |
||
| 272 | |||
| 273 | $this->template->assign_vars([ |
||
| 274 | 'NB_SMILIES' => $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count), |
||
| 275 | 'U_SMILIES' => $this->root_path . $this->config['smilies_path'] . '/', |
||
| 276 | ]); |
||
| 277 | |||
| 278 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
| 279 | } |
||
| 280 | |||
| 281 | private function delete_cat($id) |
||
| 282 | { |
||
| 283 | $sql = 'SELECT cat_title, cat_order |
||
| 284 | FROM ' . $this->smilies_category_table . ' |
||
| 285 | WHERE cat_id = ' . $id; |
||
| 286 | $result = $this->db->sql_query($sql); |
||
| 287 | $row = $this->db->sql_fetchrow($result); |
||
| 288 | $title = $row['cat_title']; |
||
| 289 | $order = (int) $row['cat_order']; |
||
| 290 | $this->db->sql_freeresult($result); |
||
| 291 | |||
| 292 | $this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id); |
||
| 293 | |||
| 294 | // Decrement orders if needed |
||
| 295 | $sql_decrement = 'SELECT cat_id, cat_order |
||
| 296 | FROM ' . $this->smilies_category_table . ' |
||
| 297 | WHERE cat_order > ' . $order; |
||
| 298 | $result = $this->db->sql_query($sql_decrement); |
||
| 299 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 300 | { |
||
| 301 | $new_order = (int) $row['cat_order'] - 1; |
||
| 302 | $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']); |
||
| 303 | } |
||
| 304 | $this->db->sql_freeresult($result); |
||
| 305 | |||
| 306 | // Reset appropriate smilies category id |
||
| 307 | $this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id); |
||
| 308 | |||
| 309 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]); |
||
| 310 | trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action)); |
||
| 311 | } |
||
| 312 | |||
| 313 | private function add_category() |
||
| 365 | } |
||
| 366 | |||
| 367 | private function edit_category($id) |
||
| 368 | { |
||
| 369 | $sql_in = []; |
||
| 370 | $title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true)); |
||
| 371 | $order = (int) $this->request->variable('order', 0); |
||
| 372 | $cat_nb = (int) $this->request->variable('cat_nb', 0); |
||
| 373 | |||
| 374 | $sql = 'SELECT lang_id, lang_iso |
||
| 375 | FROM ' . LANG_TABLE . " |
||
| 376 | ORDER BY lang_id ASC"; |
||
| 377 | $result = $this->db->sql_query($sql); |
||
| 378 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 379 | { |
||
| 380 | $iso = strtolower($row['lang_iso']); |
||
| 381 | $lang = (string) $this->request->variable("lang_$iso", '', true); |
||
| 382 | $sort = (string) $this->request->variable("sort_$iso", ''); |
||
| 383 | $name = $this->category->capitalize($this->request->variable("name_$iso", '', true)); |
||
| 384 | |||
| 385 | if ($name === '') |
||
| 386 | { |
||
| 387 | trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&action=edit&id=' . $id), E_USER_WARNING); |
||
| 388 | } |
||
| 389 | else |
||
| 390 | { |
||
| 391 | if ($sort === 'edit') |
||
| 392 | { |
||
| 393 | $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"); |
||
| 394 | } |
||
| 395 | else if ($sort === 'create') |
||
| 396 | { |
||
| 397 | $sql_in[] = [ |
||
| 398 | 'cat_id' => $id, |
||
| 399 | 'cat_order' => $order, |
||
| 400 | 'cat_lang' => $lang, |
||
| 401 | 'cat_name' => $name, |
||
| 402 | 'cat_title' => $title, |
||
| 403 | 'cat_nb' => $cat_nb, |
||
| 404 | ]; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | $this->db->sql_freeresult($result); |
||
| 409 | |||
| 410 | $this->db->sql_multi_insert($this->smilies_category_table, $sql_in); |
||
| 411 | |||
| 412 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]); |
||
| 413 | trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action)); |
||
| 414 | } |
||
| 415 | |||
| 416 | private function add_cat() |
||
| 417 | { |
||
| 418 | $max = (int) $this->category->get_max_order() + 1; |
||
| 419 | $sql = 'SELECT lang_local_name, lang_iso |
||
| 420 | FROM ' . LANG_TABLE . ' |
||
| 421 | ORDER BY lang_id ASC'; |
||
| 422 | $result = $this->db->sql_query($sql); |
||
| 423 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 424 | { |
||
| 425 | $this->template->assign_block_vars('categories', [ |
||
| 426 | 'CAT_LANG' => $row['lang_local_name'], |
||
| 427 | 'CAT_ISO' => $row['lang_iso'], |
||
| 428 | 'CAT_ORDER' => $max, |
||
| 429 | ]); |
||
| 430 | } |
||
| 431 | $this->db->sql_freeresult($result); |
||
| 432 | |||
| 433 | $this->template->assign_vars([ |
||
| 434 | 'IN_CAT_ACTION' => true, |
||
| 435 | 'IN_ADD_ACTION' => true, |
||
| 436 | 'CAT_ORDER' => $max, |
||
| 437 | 'U_BACK' => $this->u_action, |
||
| 438 | 'U_ADD_CAT' => $this->u_action . '&action=add_cat', |
||
| 439 | ]); |
||
| 440 | } |
||
| 441 | |||
| 442 | private function edit_cat($id) |
||
| 443 | { |
||
| 444 | // Get total lang id... |
||
| 445 | $sql = 'SELECT COUNT(lang_id) as total |
||
| 446 | FROM ' . LANG_TABLE; |
||
| 447 | $result = $this->db->sql_query($sql); |
||
| 448 | $total = (int) $this->db->sql_fetchfield('total'); |
||
| 449 | $this->db->sql_freeresult($result); |
||
| 450 | |||
| 451 | $title = ''; |
||
| 452 | $list_id = []; |
||
| 453 | $i = $cat_order = $cat_nb = 0; |
||
| 454 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 455 | 'SELECT' => 'l.*, c.*', |
||
| 456 | 'FROM' => [LANG_TABLE => 'l'], |
||
| 457 | 'LEFT_JOIN' => [ |
||
| 458 | [ |
||
| 459 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
| 460 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
| 461 | ], |
||
| 462 | ], |
||
| 463 | 'WHERE' => 'cat_id = ' . $id, |
||
| 464 | 'ORDER_BY' => 'lang_id ASC', |
||
| 465 | ]); |
||
| 466 | $result = $this->db->sql_query($sql); |
||
| 467 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 468 | { |
||
| 469 | $this->template->assign_block_vars('category_lang', [ |
||
| 470 | 'CAT_LANG' => $row['lang_local_name'], |
||
| 471 | 'CAT_ISO' => $row['lang_iso'], |
||
| 472 | 'CAT_ORDER' => $row['cat_order'], |
||
| 473 | 'CAT_ID' => $row['cat_id'], |
||
| 474 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
| 475 | 'CAT_SORT' => 'edit', |
||
| 476 | ]); |
||
| 477 | $i++; |
||
| 478 | $list_id[$i] = $row['lang_id']; |
||
| 479 | $cat_order = $row['cat_order']; |
||
| 480 | $title = $row['cat_title']; |
||
| 481 | $cat_nb = $row['cat_nb']; |
||
| 482 | } |
||
| 483 | $this->db->sql_freeresult($result); |
||
| 484 | |||
| 485 | // Add rows for empty langs in this category |
||
| 486 | if ($i !== $total) |
||
| 487 | { |
||
| 488 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 489 | 'SELECT' => '*', |
||
| 490 | 'FROM' => [LANG_TABLE => 'l'], |
||
| 491 | 'WHERE' => $this->db->sql_in_set('lang_id', $list_id, true, true), |
||
| 492 | ]); |
||
| 493 | $result = $this->db->sql_query($sql); |
||
| 494 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 495 | { |
||
| 496 | $this->template->assign_block_vars('category_lang', [ |
||
| 497 | 'CAT_LANG' => $row['lang_local_name'], |
||
| 498 | 'CAT_ISO' => $row['lang_iso'], |
||
| 499 | 'CAT_ORDER' => $cat_order, |
||
| 500 | 'CAT_ID' => $id, |
||
| 501 | 'CAT_TRANSLATE' => '', |
||
| 502 | 'CAT_SORT' => 'create', |
||
| 503 | ]); |
||
| 504 | } |
||
| 505 | $this->db->sql_freeresult($result); |
||
| 506 | } |
||
| 507 | |||
| 508 | $this->template->assign_vars([ |
||
| 509 | 'IN_CAT_ACTION' => true, |
||
| 510 | 'CAT_ORDER' => $cat_order, |
||
| 511 | 'CAT_NB' => $cat_nb, |
||
| 512 | 'CAT_TITLE' => $title, |
||
| 513 | 'U_BACK' => $this->u_action, |
||
| 514 | 'U_EDIT_CAT' => $this->u_action . '&action=edit_cat&id=' . $id, |
||
| 515 | ]); |
||
| 516 | } |
||
| 517 | |||
| 518 | private function edit_smiley($id, $start) |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Set page url |
||
| 552 | * |
||
| 553 | * @param string $u_action Custom form action |
||
| 554 | * @return null |
||
| 555 | * @access public |
||
| 556 | */ |
||
| 557 | public function set_page_url($u_action) |
||
| 560 | } |
||
| 561 | } |
||
| 562 |