Total Complexity | 63 |
Total Lines | 570 |
Duplicated Lines | 0 % |
Changes | 21 | ||
Bugs | 0 | Features | 0 |
Complex classes like category 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 category, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class category |
||
23 | { |
||
24 | /** @var \phpbb\cache\driver\driver_interface */ |
||
25 | protected $cache; |
||
26 | |||
27 | /** @var \phpbb\db\driver\driver_interface */ |
||
28 | protected $db; |
||
29 | |||
30 | /** @var \phpbb\config\config */ |
||
31 | protected $config; |
||
32 | |||
33 | /* @var \phpbb\controller\helper */ |
||
34 | protected $helper; |
||
35 | |||
36 | /** @var \phpbb\user */ |
||
37 | protected $user; |
||
38 | |||
39 | /** @var \phpbb\language\language */ |
||
40 | protected $language; |
||
41 | |||
42 | /** @var \phpbb\template\template */ |
||
43 | protected $template; |
||
44 | |||
45 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
46 | protected $ext_manager; |
||
47 | |||
48 | /** @var \phpbb\log\log */ |
||
49 | protected $log; |
||
50 | |||
51 | /** |
||
52 | * The database tables |
||
53 | * |
||
54 | * @var string */ |
||
55 | protected $smilies_category_table; |
||
56 | |||
57 | /** @var string phpBB root path */ |
||
58 | protected $root_path; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | */ |
||
63 | public function __construct(cache $cache, db $db, config $config, helper $helper, user $user, language $language, template $template, manager $ext_manager, log $log, $smilies_category_table, $root_path) |
||
64 | { |
||
65 | $this->cache = $cache; |
||
66 | $this->db = $db; |
||
67 | $this->config = $config; |
||
68 | $this->helper = $helper; |
||
69 | $this->user = $user; |
||
70 | $this->language = $language; |
||
71 | $this->template = $template; |
||
72 | $this->ext_manager = $ext_manager; |
||
73 | $this->log = $log; |
||
74 | $this->smilies_category_table = $smilies_category_table; |
||
75 | $this->root_path = $root_path; |
||
76 | } |
||
77 | |||
78 | public function get_version() |
||
94 | } |
||
95 | |||
96 | public function smilies_count($cat, $compact = false) |
||
97 | { |
||
98 | $sql = $this->db->sql_build_query('SELECT', array( |
||
99 | 'SELECT' => (!$compact) ? 'COUNT(DISTINCT smiley_id) AS smilies_count' : 'COUNT(DISTINCT smiley_url) AS smilies_count', |
||
100 | 'FROM' => array(SMILIES_TABLE => ''), |
||
101 | 'WHERE' => ($cat > -1) ? 'category = ' . (int) $cat : "code <> ''", |
||
102 | )); |
||
103 | $result = $this->db->sql_query($sql); |
||
104 | $nb = (int) $this->db->sql_fetchfield('smilies_count'); |
||
105 | $this->db->sql_freeresult($result); |
||
106 | |||
107 | return $nb; |
||
108 | } |
||
109 | |||
110 | public function select_categories($cat, $modify = false, $empty = false) |
||
111 | { |
||
112 | $lang = $this->user->lang_name; |
||
113 | $select = '<option disabled="disabled">' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>'; |
||
114 | if ($modify) |
||
115 | { |
||
116 | $select .= '<option value="-1"' . (($cat == -1) ? ' selected="selected"' : '') . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>'; |
||
117 | } |
||
118 | |||
119 | $sql = 'SELECT * |
||
120 | FROM ' . $this->smilies_category_table . " |
||
121 | WHERE cat_lang = '$lang' |
||
122 | ORDER BY cat_order ASC"; |
||
123 | $result = $this->db->sql_query($sql); |
||
124 | while ($row = $this->db->sql_fetchrow($result)) |
||
125 | { |
||
126 | if (!$row['cat_nb'] && $empty) |
||
127 | { |
||
128 | continue; |
||
129 | } |
||
130 | $row['cat_name'] = $row['cat_name'] ? $row['cat_name'] : $row['cat_title']; |
||
131 | $selected = ($cat === (int) $row['cat_id']) ? ' selected="selected"' : ''; |
||
132 | $select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>'; |
||
133 | } |
||
134 | $this->db->sql_freeresult($result); |
||
135 | |||
136 | $selected_default = (!$cat) ? ' selected="selected"' : ''; |
||
137 | $select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>'; |
||
138 | |||
139 | return $select; |
||
140 | } |
||
141 | |||
142 | public function capitalize($var) |
||
143 | { |
||
144 | return $this->db->sql_escape(ucfirst(strtolower(trim($var)))); |
||
145 | } |
||
146 | |||
147 | public function get_max_order() |
||
148 | { |
||
149 | // Get max order id... |
||
150 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
151 | FROM ' . $this->smilies_category_table; |
||
152 | $result = $this->db->sql_query($sql); |
||
153 | $max = $this->db->sql_fetchfield('maxi', $result); |
||
154 | $this->db->sql_freeresult($result); |
||
155 | |||
156 | return (int) $max; |
||
157 | } |
||
158 | |||
159 | public function get_max_id() |
||
160 | { |
||
161 | // Get max id... |
||
162 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
163 | FROM ' . $this->smilies_category_table; |
||
164 | $result = $this->db->sql_query($sql); |
||
165 | $id_max = (int) $this->db->sql_fetchfield('id_max', $result); |
||
166 | $this->db->sql_freeresult($result); |
||
167 | |||
168 | return $id_max; |
||
169 | } |
||
170 | |||
171 | public function get_first_order() |
||
172 | { |
||
173 | // Get first order id... |
||
174 | $sql = 'SELECT cat_order, cat_id |
||
175 | FROM ' . $this->smilies_category_table . ' |
||
176 | ORDER BY cat_order ASC'; |
||
177 | $result = $this->db->sql_query_limit($sql, 1); |
||
178 | $mini = (int) $this->db->sql_fetchfield('cat_id'); |
||
179 | $this->db->sql_freeresult($result); |
||
180 | |||
181 | return $mini; |
||
182 | } |
||
183 | |||
184 | public function extract_list_categories($cat) |
||
185 | { |
||
186 | $title = ''; |
||
187 | $cat_order = $i = 0; |
||
188 | $lang = $this->user->lang_name; |
||
189 | $sql = $this->db->sql_build_query('SELECT', array( |
||
190 | 'SELECT' => '*', |
||
191 | 'FROM' => array($this->smilies_category_table => ''), |
||
192 | 'WHERE' => "cat_lang = '$lang'", |
||
193 | 'ORDER_BY' => 'cat_order ASC', |
||
194 | )); |
||
195 | $result = $this->db->sql_query($sql); |
||
196 | while ($row = $this->db->sql_fetchrow($result)) |
||
197 | { |
||
198 | // Choose only non-empty categories |
||
199 | if ($row['cat_nb']) |
||
200 | { |
||
201 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
202 | $this->template->assign_block_vars('categories', array( |
||
203 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
204 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
205 | 'CAT_ID' => $row['cat_id'], |
||
206 | 'CAT_ORDER' => $row['cat_order'], |
||
207 | 'CAT_NAME' => $row['cat_name'] ? $row['cat_name'] : $row['cat_title'], |
||
208 | 'CAT_NB' => $row['cat_nb'], |
||
209 | )); |
||
210 | $i++; |
||
211 | |||
212 | // Keep these values in memory |
||
213 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
214 | $cat_order = $row['cat_order']; |
||
215 | } |
||
216 | } |
||
217 | $this->db->sql_freeresult($result); |
||
218 | |||
219 | // Add the Unclassified category if not empty |
||
220 | $nb = $this->smilies_count(0); |
||
221 | if ($nb) |
||
222 | { |
||
223 | $this->template->assign_block_vars('categories', array( |
||
224 | 'CLASS' => ($cat === 0) ? 'cat-active' : 'cat-inactive', |
||
225 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
226 | 'CAT_ID' => 0, |
||
227 | 'CAT_ORDER' => $cat_order + 1, |
||
228 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
229 | 'CAT_NB' => $nb, |
||
230 | )); |
||
231 | } |
||
232 | |||
233 | return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
234 | } |
||
235 | |||
236 | public function shoutbox_smilies($event) |
||
280 | )); |
||
281 | } |
||
282 | |||
283 | public function shoutbox_smilies_popup($event) |
||
284 | { |
||
285 | $cat = (int) $event['cat']; |
||
286 | if ($cat > -1) |
||
287 | { |
||
288 | $i = 0; |
||
289 | $smilies = array(); |
||
290 | $lang = $this->user->lang_name; |
||
291 | $cat_name = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
292 | |||
293 | if ($cat > 0) |
||
294 | { |
||
295 | $sql = 'SELECT cat_name |
||
296 | FROM ' . $this->smilies_category_table . " |
||
297 | WHERE cat_lang = '$lang' |
||
298 | AND cat_id = $cat"; |
||
299 | $result = $this->db->sql_query_limit($sql, 1); |
||
300 | $row = $this->db->sql_fetchrow($result); |
||
301 | $cat_name = $row['cat_name']; |
||
302 | $this->db->sql_freeresult($result); |
||
303 | } |
||
304 | |||
305 | $sql = $this->db->sql_build_query('SELECT', array( |
||
306 | 'SELECT' => 'smiley_url, MIN(smiley_id) AS smiley_id, MIN(code) AS code, MIN(smiley_order) AS min_smiley_order, MIN(smiley_width) AS smiley_width, MIN(smiley_height) AS smiley_height, MIN(emotion) AS emotion', |
||
307 | 'FROM' => array(SMILIES_TABLE => ''), |
||
308 | 'WHERE' => 'category = ' . $cat, |
||
309 | 'GROUP_BY' => 'smiley_url', |
||
310 | 'ORDER_BY' => 'min_smiley_order ASC', |
||
311 | )); |
||
312 | $result = $this->db->sql_query($sql); |
||
313 | while ($row = $this->db->sql_fetchrow($result)) |
||
314 | { |
||
315 | $smilies[$i] = array( |
||
316 | 'nb' => (int) $i, |
||
317 | 'code' => (string) $row['code'], |
||
318 | 'emotion' => (string) $row['emotion'], |
||
319 | 'image' => (string) $row['smiley_url'], |
||
320 | 'width' => (int) $row['smiley_width'], |
||
321 | 'height' => (int) $row['smiley_height'], |
||
322 | ); |
||
323 | $i++; |
||
324 | } |
||
325 | |||
326 | $event['content'] = array_merge($event['content'], array( |
||
327 | 'in_cat' => true, |
||
328 | 'cat' => $cat, |
||
329 | 'total' => $i, |
||
330 | 'smilies' => $smilies, |
||
331 | 'emptyRow' => (!$i) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : '', |
||
332 | 'title' => $this->language->lang('SC_CATEGORY_IN', $cat_name), |
||
333 | )); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | public function set_order($action, $current_order) |
||
356 | } |
||
357 | |||
358 | public function adm_add_cat($u_action) |
||
359 | { |
||
360 | $max = $this->get_max_order(); |
||
361 | $sql = 'SELECT lang_local_name, lang_iso |
||
362 | FROM ' . LANG_TABLE . ' |
||
363 | ORDER BY lang_id ASC'; |
||
364 | $result = $this->db->sql_query($sql); |
||
365 | while ($row = $this->db->sql_fetchrow($result)) |
||
366 | { |
||
367 | $this->template->assign_block_vars('categories', array( |
||
368 | 'CAT_LANG' => $row['lang_local_name'], |
||
369 | 'CAT_ISO' => $row['lang_iso'], |
||
370 | 'CAT_ORDER' => $max + 1, |
||
371 | )); |
||
372 | } |
||
373 | $this->db->sql_freeresult($result); |
||
374 | |||
375 | $this->template->assign_vars(array( |
||
376 | 'IN_CAT_ACTION' => true, |
||
377 | 'IN_ADD_ACTION' => true, |
||
378 | 'CAT_ORDER' => $max + 1, |
||
379 | 'U_BACK' => $u_action, |
||
380 | 'U_ADD_CAT' => $u_action . '&action=add_cat', |
||
381 | )); |
||
382 | } |
||
383 | |||
384 | public function adm_edit_cat($id, $u_action) |
||
385 | { |
||
386 | // Get total lang id... |
||
387 | $sql = 'SELECT COUNT(lang_id) as total |
||
388 | FROM ' . LANG_TABLE; |
||
389 | $result = $this->db->sql_query($sql); |
||
390 | $total = (int) $this->db->sql_fetchfield('total', $result); |
||
391 | $this->db->sql_freeresult($result); |
||
392 | |||
393 | $title = ''; |
||
394 | $i = $cat_order = 0; |
||
395 | $list_id = []; |
||
396 | $sql = $this->db->sql_build_query('SELECT', array( |
||
397 | 'SELECT' => 'l.*, c.*', |
||
398 | 'FROM' => array(LANG_TABLE => 'l'), |
||
399 | 'LEFT_JOIN' => array( |
||
400 | array( |
||
401 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
402 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
403 | ), |
||
404 | ), |
||
405 | 'WHERE' => 'cat_id = ' . $id, |
||
406 | 'ORDER_BY' => 'lang_id ASC', |
||
407 | )); |
||
408 | $result = $this->db->sql_query($sql); |
||
409 | while ($row = $this->db->sql_fetchrow($result)) |
||
410 | { |
||
411 | $this->template->assign_block_vars('category_lang', array( |
||
412 | 'CAT_LANG' => $row['lang_local_name'], |
||
413 | 'CAT_ISO' => $row['lang_iso'], |
||
414 | 'CAT_ORDER' => $row['cat_order'], |
||
415 | 'CAT_ID' => $row['cat_id'], |
||
416 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
417 | 'CAT_SORT' => 'edit', |
||
418 | )); |
||
419 | $i++; |
||
420 | $list_id[$i] = $row['lang_id']; |
||
421 | $cat_order = $row['cat_order']; |
||
422 | $title = $row['cat_title']; |
||
423 | } |
||
424 | $this->db->sql_freeresult($result); |
||
425 | |||
426 | // Add rows for empty langs in this category |
||
427 | if ($i !== $total) |
||
428 | { |
||
429 | $sql = $this->db->sql_build_query('SELECT', array( |
||
430 | 'SELECT' => '*', |
||
431 | 'FROM' => array(LANG_TABLE => 'l'), |
||
432 | 'WHERE' => $this->db->sql_in_set('lang_id', $list_id, true, true), |
||
433 | )); |
||
434 | $result = $this->db->sql_query($sql); |
||
435 | while ($row = $this->db->sql_fetchrow($result)) |
||
436 | { |
||
437 | $this->template->assign_block_vars('category_lang', array( |
||
438 | 'CAT_LANG' => $row['lang_local_name'], |
||
439 | 'CAT_ISO' => $row['lang_iso'], |
||
440 | 'CAT_ORDER' => $cat_order, |
||
441 | 'CAT_ID' => $id, |
||
442 | 'CAT_TRANSLATE' => '', |
||
443 | 'CAT_SORT' => 'create', |
||
444 | )); |
||
445 | } |
||
446 | $this->db->sql_freeresult($result); |
||
447 | } |
||
448 | |||
449 | $this->template->assign_vars(array( |
||
450 | 'IN_CAT_ACTION' => true, |
||
451 | 'CAT_ORDER' => $cat_order, |
||
452 | 'CAT_TITLE' => $title, |
||
453 | 'U_BACK' => $u_action, |
||
454 | 'U_EDIT_CAT' => $u_action . '&action=edit_cat&id=' . $id, |
||
455 | )); |
||
456 | } |
||
457 | |||
458 | public function adm_list_cat($u_action) |
||
459 | { |
||
460 | $i = $cat = 0; |
||
461 | $max = $this->get_max_order(); |
||
462 | $sql = $this->db->sql_build_query('SELECT', array( |
||
463 | 'SELECT' => 'l.lang_iso, l.lang_local_name, c.*', |
||
464 | 'FROM' => array(LANG_TABLE => 'l'), |
||
465 | 'LEFT_JOIN' => array( |
||
466 | array( |
||
467 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
468 | 'ON' => 'cat_lang = lang_iso', |
||
469 | ), |
||
470 | ), |
||
471 | 'ORDER_BY' => 'cat_order ASC, cat_lang_id ASC', |
||
472 | )); |
||
473 | $result = $this->db->sql_query($sql); |
||
474 | while ($row = $this->db->sql_fetchrow($result)) |
||
475 | { |
||
476 | if (!$row) |
||
477 | { |
||
478 | $this->template->assign_vars(array( |
||
479 | 'EMPTY_ROW' => true, |
||
480 | )); |
||
481 | } |
||
482 | else |
||
483 | { |
||
484 | $this->template->assign_block_vars('categories', array( |
||
485 | 'CAT_NR' => $i + 1, |
||
486 | 'LANG_EMPTY' => !$row['cat_id'] && !$row['cat_order'] && !$row['cat_name'], |
||
487 | 'SPACER_CAT' => $this->language->lang('SC_CATEGORY_IN', $row['cat_title']), |
||
488 | 'CAT_LANG' => $row['lang_local_name'], |
||
489 | 'CAT_ISO' => $row['lang_iso'], |
||
490 | 'CAT_ID' => $row['cat_id'], |
||
491 | 'CAT_ORDER' => $row['cat_order'], |
||
492 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
493 | 'CAT_NB' => $row['cat_nb'], |
||
494 | 'ROW' => (int) $row['cat_id'] !== $cat, |
||
495 | 'ROW_MAX' => (int) $row['cat_order'] === $max, |
||
496 | 'U_EDIT' => $u_action . '&action=edit&id=' . $row['cat_id'], |
||
497 | 'U_DELETE' => $u_action . '&action=delete&id=' . $row['cat_id'], |
||
498 | )); |
||
499 | $i++; |
||
500 | // Keep this value in memory |
||
501 | $cat = (int) $row['cat_id']; |
||
502 | } |
||
503 | } |
||
504 | $this->db->sql_freeresult($result); |
||
505 | |||
506 | $this->template->assign_vars(array( |
||
507 | 'IN_LIST_CAT' => true, |
||
508 | 'U_ACTION' => $u_action, |
||
509 | 'U_MOVE_CATS' => $this->helper->route('sylver35_smiliescat_ajax_list_cat'), |
||
510 | )); |
||
511 | } |
||
512 | |||
513 | public function adm_edit_smiley($id, $u_action, $start) |
||
514 | { |
||
515 | $lang = $this->user->lang_name; |
||
516 | $sql = $this->db->sql_build_query('SELECT', array( |
||
517 | 'SELECT' => 's.*, c.*', |
||
518 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
519 | 'LEFT_JOIN' => array( |
||
520 | array( |
||
521 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
522 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
523 | ), |
||
524 | ), |
||
525 | 'WHERE' => 'smiley_id = ' . (int) $id, |
||
526 | )); |
||
527 | $result = $this->db->sql_query($sql); |
||
528 | $row = $this->db->sql_fetchrow($result); |
||
529 | |||
530 | $this->template->assign_vars(array( |
||
531 | 'WIDTH' => $row['smiley_width'], |
||
532 | 'HEIGHT' => $row['smiley_height'], |
||
533 | 'CODE' => $row['code'], |
||
534 | 'EMOTION' => $row['emotion'], |
||
535 | 'CATEGORY' => $row['cat_name'], |
||
536 | 'EX_CAT' => $row['cat_id'], |
||
537 | 'SELECT_CATEGORY' => $this->select_categories($row['cat_id'], false), |
||
538 | 'IMG_SRC' => $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'], |
||
539 | 'U_MODIFY' => $u_action . '&action=modify&id=' . $row['smiley_id'] . '&start=' . $start, |
||
540 | 'U_BACK' => $u_action, |
||
541 | )); |
||
542 | $this->db->sql_freeresult($result); |
||
543 | } |
||
544 | |||
545 | public function reset_first_cat($current_order, $switch_order_id) |
||
546 | { |
||
547 | $first = $this->get_first_order(); |
||
548 | if ($current_order === 1 || $switch_order_id === 1) |
||
549 | { |
||
550 | $this->config->set('smilies_category_nb', $first); |
||
551 | } |
||
552 | } |
||
553 | |||
554 | public function move_cat($action, $id) |
||
592 | } |
||
593 | } |
||
594 |