Total Complexity | 51 |
Total Lines | 495 |
Duplicated Lines | 0 % |
Changes | 18 | ||
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() |
||
149 | )); |
||
150 | } |
||
151 | |||
152 | public function acp_categories_config() |
||
243 | )); |
||
244 | } |
||
245 | |||
246 | private function move_cat($action, $id) |
||
247 | { |
||
248 | $id = (int) $id; |
||
249 | // Get current order id and title... |
||
250 | $sql = 'SELECT cat_order, cat_title |
||
251 | FROM ' . $this->smilies_category_table . ' |
||
252 | WHERE cat_id = ' . $id; |
||
253 | $result = $this->db->sql_query($sql); |
||
254 | $row = $this->db->sql_fetchrow($result); |
||
255 | $current_order = (int) $row['cat_order']; |
||
256 | $title = $row['cat_title']; |
||
257 | $this->db->sql_freeresult($result); |
||
258 | |||
259 | $switch_order_id = $this->set_order($action, $current_order); |
||
260 | if ($switch_order_id === 0) |
||
261 | { |
||
262 | return; |
||
263 | } |
||
264 | |||
265 | $sql = 'UPDATE ' . $this->smilies_category_table . " |
||
266 | SET cat_order = $current_order |
||
267 | WHERE cat_order = $switch_order_id |
||
268 | AND cat_id <> $id"; |
||
269 | $this->db->sql_query($sql); |
||
270 | $move_executed = (bool) $this->db->sql_affectedrows(); |
||
271 | |||
272 | // Only update the other entry too if the previous entry got updated |
||
273 | if ($move_executed) |
||
274 | { |
||
275 | $sql = 'UPDATE ' . $this->smilies_category_table . " |
||
276 | SET cat_order = $switch_order_id |
||
277 | WHERE cat_order = $current_order |
||
278 | AND cat_id = $id"; |
||
279 | $this->db->sql_query($sql); |
||
280 | } |
||
281 | |||
282 | $this->category->reset_first_cat((int) $current_order, (int) $switch_order_id); |
||
283 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), array($title)); |
||
284 | |||
285 | trigger_error($this->language->lang('SC_MOVE_SUCCESS') . adm_back_link($this->u_action)); |
||
286 | } |
||
287 | |||
288 | private function set_order($action, $current_order) |
||
289 | { |
||
290 | $switch_order_id = 0; |
||
291 | if ($current_order === 1 && $action === 'move_up') |
||
292 | { |
||
293 | return $switch_order_id; |
||
294 | } |
||
295 | |||
296 | $max_order = (int) $this->category->get_max_order(); |
||
297 | |||
298 | if (($current_order === $max_order) && ($action === 'move_down')) |
||
299 | { |
||
300 | return $switch_order_id; |
||
301 | } |
||
302 | |||
303 | // on move_down, switch position with next order_id... |
||
304 | // on move_up, switch position with previous order_id... |
||
305 | $switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1; |
||
306 | |||
307 | return $switch_order_id; |
||
308 | } |
||
309 | |||
310 | private function extract_list_smilies($select, $start) |
||
311 | { |
||
312 | $i = 0; |
||
313 | $cat = -1; |
||
314 | $lang = $this->user->lang_name; |
||
315 | $smilies_count = (int) $this->category->smilies_count($select); |
||
316 | |||
317 | if ($select === 0) |
||
318 | { |
||
319 | $sql = $this->db->sql_build_query('SELECT', array( |
||
320 | 'SELECT' => '*', |
||
321 | 'FROM' => array(SMILIES_TABLE => ''), |
||
322 | 'WHERE' => 'category = 0', |
||
323 | 'ORDER_BY' => 'smiley_order ASC', |
||
324 | )); |
||
325 | } |
||
326 | else |
||
327 | { |
||
328 | $sql = $this->db->sql_build_query('SELECT', array( |
||
329 | 'SELECT' => 's.*, c.*', |
||
330 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
331 | 'LEFT_JOIN' => array( |
||
332 | array( |
||
333 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
334 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
335 | ), |
||
336 | ), |
||
337 | 'WHERE' => ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''", |
||
338 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
339 | )); |
||
340 | } |
||
341 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
342 | while ($row = $this->db->sql_fetchrow($result)) |
||
343 | { |
||
344 | $row['category'] = isset($row['category']) ? $row['category'] : 0; |
||
345 | $row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
346 | |||
347 | $this->template->assign_block_vars('items', array( |
||
348 | 'SPACER_CAT' => ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '', |
||
349 | 'IMG_SRC' => $row['smiley_url'], |
||
350 | 'WIDTH' => $row['smiley_width'], |
||
351 | 'HEIGHT' => $row['smiley_height'], |
||
352 | 'CODE' => $row['code'], |
||
353 | 'EMOTION' => $row['emotion'], |
||
354 | 'CATEGORY' => $row['cat_name'], |
||
355 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
356 | )); |
||
357 | $i++; |
||
358 | |||
359 | // Keep this value in memory |
||
360 | $cat = (int) $row['category']; |
||
361 | } |
||
362 | $this->db->sql_freeresult($result); |
||
363 | |||
364 | $this->template->assign_vars(array( |
||
365 | 'NB_SMILIES' => $smilies_count, |
||
366 | 'U_SMILIES' => $this->root_path . $this->config['smilies_path'] . '/', |
||
367 | )); |
||
368 | |||
369 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
370 | } |
||
371 | |||
372 | private function delete_cat($id) |
||
373 | { |
||
374 | $id = (int) $id; |
||
375 | $sql = 'SELECT cat_title, cat_order |
||
376 | FROM ' . $this->smilies_category_table . ' |
||
377 | WHERE cat_id = ' . $id; |
||
378 | $result = $this->db->sql_query($sql); |
||
379 | $row = $this->db->sql_fetchrow($result); |
||
380 | $title = $row['cat_title']; |
||
381 | $order = $row['cat_order']; |
||
382 | $this->db->sql_freeresult($result); |
||
383 | |||
384 | $sql_delete = 'DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id; |
||
385 | $this->db->sql_query($sql_delete); |
||
386 | |||
387 | // Decrement orders if needed |
||
388 | $sql_decrement = 'SELECT cat_id, cat_order |
||
389 | FROM ' . $this->smilies_category_table . ' |
||
390 | WHERE cat_order > ' . (int) $order; |
||
391 | $result = $this->db->sql_query($sql_decrement); |
||
392 | while ($row = $this->db->sql_fetchrow($result)) |
||
393 | { |
||
394 | $new_order = (int) $row['cat_order'] - 1; |
||
395 | $sql_order = 'UPDATE ' . $this->smilies_category_table . ' |
||
396 | SET cat_order = ' . $new_order . ' |
||
397 | WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order']; |
||
398 | $this->db->sql_query($sql_order); |
||
399 | } |
||
400 | $this->db->sql_freeresult($result); |
||
401 | |||
402 | // Reset appropriate smilies category id |
||
403 | $sql_update = 'UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id; |
||
404 | $this->db->sql_query($sql_update); |
||
405 | |||
406 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title)); |
||
407 | |||
408 | trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action)); |
||
409 | } |
||
410 | |||
411 | private function add_category() |
||
457 | } |
||
458 | |||
459 | private function edit_category($id) |
||
460 | { |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Set page url |
||
509 | * |
||
510 | * @param string $u_action Custom form action |
||
511 | * @return null |
||
512 | * @access public |
||
513 | */ |
||
514 | public function set_page_url($u_action) |
||
517 | } |
||
518 | } |
||
519 |