| Total Complexity | 54 |
| Total Lines | 539 |
| 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->category->edit_smiley($id, $start, $this->u_action); |
||
| 98 | break; |
||
| 99 | |||
| 100 | case 'edit_multi': |
||
| 101 | $list = $this->request->variable('mark', [0]); |
||
| 102 | $this->category->edit_multi_smiley($list, $start, $this->u_action); |
||
| 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_vars([ |
||
| 125 | 'IN_ACTION' => true, |
||
| 126 | ]); |
||
| 127 | } |
||
| 128 | else |
||
| 129 | { |
||
| 130 | $this->extract_list_smilies($select, $start); |
||
| 131 | |||
| 132 | $this->template->assign_vars([ |
||
| 133 | 'LIST_CATEGORY' => $this->category->select_categories($select, true, true), |
||
| 134 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
| 135 | 'U_BACK' => ($select) ? $this->u_action : '', |
||
| 136 | ]); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->template->assign_vars([ |
||
| 140 | 'CATEGORIE_SMILIES' => true, |
||
| 141 | ]); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function acp_categories_config($id, $action, $mode) |
||
| 145 | { |
||
| 146 | $this->language->add_lang('acp/language'); |
||
| 147 | $form_key = 'sylver35/smiliescat'; |
||
| 148 | add_form_key($form_key); |
||
| 149 | |||
| 150 | if ($action) |
||
| 151 | { |
||
| 152 | if (in_array($action, ['config_cat', 'add_cat', 'edit_cat']) && !check_form_key($form_key)) |
||
| 153 | { |
||
| 154 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 155 | } |
||
| 156 | |||
| 157 | switch ($action) |
||
| 158 | { |
||
| 159 | case 'config_cat': |
||
| 160 | $this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15)); |
||
| 161 | |||
| 162 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time()); |
||
| 163 | trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action)); |
||
| 164 | break; |
||
| 165 | |||
| 166 | case 'add': |
||
| 167 | $this->add_cat(); |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 'add_cat': |
||
| 171 | $this->add_category(); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'edit': |
||
| 175 | $this->edit_cat((int) $id); |
||
| 176 | break; |
||
| 177 | |||
| 178 | case 'edit_cat': |
||
| 179 | $this->edit_category((int) $id); |
||
| 180 | break; |
||
| 181 | |||
| 182 | case 'delete': |
||
| 183 | if (confirm_box(true)) |
||
| 184 | { |
||
| 185 | $this->delete_cat((int) $id); |
||
| 186 | } |
||
| 187 | else |
||
| 188 | { |
||
| 189 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([ |
||
| 190 | 'mode' => $mode, |
||
| 191 | 'id' => $id, |
||
| 192 | 'action' => $action, |
||
| 193 | ])); |
||
| 194 | } |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | |||
| 198 | $this->template->assign_vars([ |
||
| 199 | 'IN_ACTION' => true, |
||
| 200 | ]); |
||
| 201 | } |
||
| 202 | else |
||
| 203 | { |
||
| 204 | $this->category->adm_list_cat($this->u_action); |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->template->assign_vars([ |
||
| 208 | 'CATEGORIE_CONFIG' => true, |
||
| 209 | 'SMILIES_PER_PAGE_CAT' => $this->config['smilies_per_page_cat'], |
||
| 210 | 'U_ACTION_CONFIG' => $this->u_action . '&action=config_cat', |
||
| 211 | 'U_ADD' => $this->u_action . '&action=add', |
||
| 212 | ]); |
||
| 213 | } |
||
| 214 | |||
| 215 | private function modify_smiley($id, $cat_id, $ex_cat = -1) |
||
| 230 | } |
||
| 231 | |||
| 232 | private function update_cat_smiley($cat_id, $ex_cat) |
||
| 233 | { |
||
| 234 | // Increment nb value if wanted |
||
| 235 | if ($cat_id) |
||
| 236 | { |
||
| 237 | if ($this->category->get_first_order() === $cat_id) |
||
| 238 | { |
||
| 239 | if ($this->category->get_cat_nb($cat_id) === 0) |
||
| 240 | { |
||
| 241 | $this->config->set('smilies_category_nb', $cat_id); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb + 1 WHERE cat_id = ' . $cat_id); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Decrement nb value if wanted |
||
| 248 | if ($ex_cat) |
||
| 249 | { |
||
| 250 | $this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb - 1 WHERE cat_id = ' . $ex_cat); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | private function extract_list_smilies($select, $start) |
||
| 255 | { |
||
| 256 | $i = 0; |
||
| 257 | $cat = -1; |
||
| 258 | $lang = $this->user->lang_name; |
||
| 259 | $smilies_count = (int) $this->category->smilies_count($select); |
||
| 260 | |||
| 261 | if ($select === 0) |
||
| 262 | { |
||
| 263 | $sql = 'SELECT * |
||
| 264 | FROM ' . SMILIES_TABLE . ' |
||
| 265 | WHERE category = 0 |
||
| 266 | ORDER BY smiley_order ASC'; |
||
| 267 | } |
||
| 268 | else |
||
| 269 | { |
||
| 270 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 271 | 'SELECT' => 's.*, c.*', |
||
| 272 | 'FROM' => [SMILIES_TABLE => 's'], |
||
| 273 | 'LEFT_JOIN' => [ |
||
| 274 | [ |
||
| 275 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
| 276 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
| 277 | ], |
||
| 278 | ], |
||
| 279 | 'WHERE' => ($select === -1) ? "code <> ''" : "cat_id = $select AND code <> ''", |
||
| 280 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
| 281 | ]); |
||
| 282 | } |
||
| 283 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
| 284 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 285 | { |
||
| 286 | $row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
| 287 | $this->template->assign_block_vars('items', [ |
||
| 288 | 'SPACER_CAT' => ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '', |
||
| 289 | 'IMG_SRC' => $row['smiley_url'], |
||
| 290 | 'WIDTH' => $row['smiley_width'], |
||
| 291 | 'HEIGHT' => $row['smiley_height'], |
||
| 292 | 'ID' => $row['smiley_id'], |
||
| 293 | 'CODE' => $row['code'], |
||
| 294 | 'EMOTION' => $row['emotion'], |
||
| 295 | 'CATEGORY' => $row['cat_name'], |
||
| 296 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
| 297 | ]); |
||
| 298 | $i++; |
||
| 299 | |||
| 300 | // Keep this value in memory |
||
| 301 | $cat = (int) $row['category']; |
||
| 302 | } |
||
| 303 | $this->db->sql_freeresult($result); |
||
| 304 | |||
| 305 | $this->template->assign_vars([ |
||
| 306 | 'NB_SMILIES' => $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count), |
||
| 307 | 'U_SMILIES' => $this->root_path . $this->config['smilies_path'] . '/', |
||
| 308 | 'U_MODIFY' => $this->u_action . '&action=edit_multi&start=' . $start, |
||
| 309 | ]); |
||
| 310 | |||
| 311 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
| 312 | } |
||
| 313 | |||
| 314 | private function delete_cat($id) |
||
| 315 | { |
||
| 316 | $sql = 'SELECT cat_title, cat_order |
||
| 317 | FROM ' . $this->smilies_category_table . ' |
||
| 318 | WHERE cat_id = ' . $id; |
||
| 319 | $result = $this->db->sql_query($sql); |
||
| 320 | $row = $this->db->sql_fetchrow($result); |
||
| 321 | $title = $row['cat_title']; |
||
| 322 | $order = (int) $row['cat_order']; |
||
| 323 | $this->db->sql_freeresult($result); |
||
| 324 | |||
| 325 | $this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id); |
||
| 326 | |||
| 327 | // Decrement orders if needed |
||
| 328 | $sql_decrement = 'SELECT cat_id, cat_order |
||
| 329 | FROM ' . $this->smilies_category_table . ' |
||
| 330 | WHERE cat_order > ' . $order; |
||
| 331 | $result = $this->db->sql_query($sql_decrement); |
||
| 332 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 333 | { |
||
| 334 | $new_order = (int) $row['cat_order'] - 1; |
||
| 335 | $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']); |
||
| 336 | } |
||
| 337 | $this->db->sql_freeresult($result); |
||
| 338 | |||
| 339 | // Reset appropriate smilies category id |
||
| 340 | $this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id); |
||
| 341 | |||
| 342 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]); |
||
| 343 | trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action)); |
||
| 344 | } |
||
| 345 | |||
| 346 | private function add_category() |
||
| 398 | } |
||
| 399 | |||
| 400 | private function edit_category($id) |
||
| 401 | { |
||
| 402 | $sql_in = []; |
||
| 403 | $title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true)); |
||
| 404 | $order = (int) $this->request->variable('order', 0); |
||
| 405 | $cat_nb = (int) $this->request->variable('cat_nb', 0); |
||
| 406 | |||
| 407 | $sql = 'SELECT lang_id, lang_iso |
||
| 408 | FROM ' . LANG_TABLE . " |
||
| 409 | ORDER BY lang_id ASC"; |
||
| 410 | $result = $this->db->sql_query($sql); |
||
| 411 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 412 | { |
||
| 413 | $iso = strtolower($row['lang_iso']); |
||
| 414 | $lang = (string) $this->request->variable("lang_$iso", '', true); |
||
| 415 | $sort = (string) $this->request->variable("sort_$iso", ''); |
||
| 416 | $name = $this->category->capitalize($this->request->variable("name_$iso", '', true)); |
||
| 417 | |||
| 418 | if ($name === '') |
||
| 419 | { |
||
| 420 | trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&action=edit&id=' . $id), E_USER_WARNING); |
||
| 421 | } |
||
| 422 | else |
||
| 423 | { |
||
| 424 | if ($sort === 'edit') |
||
| 425 | { |
||
| 426 | $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"); |
||
| 427 | } |
||
| 428 | else if ($sort === 'create') |
||
| 429 | { |
||
| 430 | $sql_in[] = [ |
||
| 431 | 'cat_id' => $id, |
||
| 432 | 'cat_order' => $order, |
||
| 433 | 'cat_lang' => $lang, |
||
| 434 | 'cat_name' => $name, |
||
| 435 | 'cat_title' => $title, |
||
| 436 | 'cat_nb' => $cat_nb, |
||
| 437 | ]; |
||
| 438 | } |
||
| 439 | } |
||
| 440 | } |
||
| 441 | $this->db->sql_freeresult($result); |
||
| 442 | |||
| 443 | $this->db->sql_multi_insert($this->smilies_category_table, $sql_in); |
||
| 444 | |||
| 445 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]); |
||
| 446 | trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action)); |
||
| 447 | } |
||
| 448 | |||
| 449 | private function add_cat() |
||
| 450 | { |
||
| 451 | $max = (int) $this->category->get_max_order() + 1; |
||
| 452 | $sql = 'SELECT lang_local_name, lang_iso |
||
| 453 | FROM ' . LANG_TABLE . ' |
||
| 454 | ORDER BY lang_id ASC'; |
||
| 455 | $result = $this->db->sql_query($sql); |
||
| 456 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 457 | { |
||
| 458 | $this->template->assign_block_vars('categories', [ |
||
| 459 | 'CAT_LANG' => $row['lang_local_name'], |
||
| 460 | 'CAT_ISO' => $row['lang_iso'], |
||
| 461 | 'CAT_ORDER' => $max, |
||
| 462 | ]); |
||
| 463 | } |
||
| 464 | $this->db->sql_freeresult($result); |
||
| 465 | |||
| 466 | $this->template->assign_vars([ |
||
| 467 | 'IN_CAT_ACTION' => true, |
||
| 468 | 'IN_ADD_ACTION' => true, |
||
| 469 | 'CAT_ORDER' => $max, |
||
| 470 | 'U_BACK' => $this->u_action, |
||
| 471 | 'U_ADD_CAT' => $this->u_action . '&action=add_cat', |
||
| 472 | ]); |
||
| 473 | } |
||
| 474 | |||
| 475 | private function edit_cat($id) |
||
| 548 | ]); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set page url |
||
| 553 | * |
||
| 554 | * @param string $u_action Custom form action |
||
| 555 | * @return null |
||
| 556 | * @access public |
||
| 557 | */ |
||
| 558 | public function set_page_url($u_action) |
||
| 563 |