Total Complexity | 64 |
Total Lines | 558 |
Duplicated Lines | 0 % |
Changes | 14 | ||
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\user */ |
||
34 | protected $user; |
||
35 | |||
36 | /** @var \phpbb\language\language */ |
||
37 | protected $language; |
||
38 | |||
39 | /** @var \phpbb\template\template */ |
||
40 | protected $template; |
||
41 | |||
42 | /** @var \phpbb\request\request */ |
||
43 | protected $request; |
||
44 | |||
45 | /** @var \phpbb\pagination */ |
||
46 | protected $pagination; |
||
47 | |||
48 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
49 | protected $ext_manager; |
||
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, user $user, language $language, template $template, request $request, pagination $pagination, manager $ext_manager, $smilies_category_table, $root_path) |
||
64 | { |
||
65 | $this->cache = $cache; |
||
66 | $this->db = $db; |
||
67 | $this->config = $config; |
||
68 | $this->user = $user; |
||
69 | $this->language = $language; |
||
70 | $this->template = $template; |
||
71 | $this->request = $request; |
||
72 | $this->pagination = $pagination; |
||
73 | $this->ext_manager = $ext_manager; |
||
74 | $this->smilies_category_table = $smilies_category_table; |
||
75 | $this->root_path = $root_path; |
||
76 | } |
||
77 | |||
78 | public function get_version() |
||
79 | { |
||
80 | if (($data = $this->cache->get('_smiliescat_version')) === false) |
||
81 | { |
||
82 | $md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat'); |
||
83 | $meta = $md_manager->get_metadata(); |
||
84 | |||
85 | $data = array( |
||
86 | 'version' => $meta['version'], |
||
87 | 'homepage' => $meta['homepage'], |
||
88 | ); |
||
89 | // cache for 7 days |
||
90 | $this->cache->put('_smiliescat_version', $data, 604800); |
||
91 | } |
||
92 | |||
93 | return $data; |
||
94 | } |
||
95 | |||
96 | public function smilies_count($cat) |
||
97 | { |
||
98 | $sql_where = ($cat == -1) ? "code <> ''" : "category = $cat"; |
||
99 | $sql = $this->db->sql_build_query('SELECT', array( |
||
100 | 'SELECT' => 'COUNT(DISTINCT smiley_url) AS smilies_count', |
||
101 | 'FROM' => array(SMILIES_TABLE => ''), |
||
102 | 'WHERE' => $sql_where, |
||
103 | )); |
||
104 | $result = $this->db->sql_query($sql); |
||
105 | $smilies_count = (int) $this->db->sql_fetchfield('smilies_count'); |
||
106 | $this->db->sql_freeresult($result); |
||
107 | |||
108 | return $smilies_count; |
||
109 | } |
||
110 | |||
111 | public function select_categories($cat, $modify = false) |
||
112 | { |
||
113 | $lang = $this->user->lang_name; |
||
114 | $select = '<option>' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>'; |
||
115 | if (!$modify) |
||
116 | { |
||
117 | $sel = ($cat == -1) ? ' selected="selected"' : ''; |
||
118 | $select .= '<option value="-1"' . $sel . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>'; |
||
119 | } |
||
120 | |||
121 | $sql = 'SELECT * |
||
122 | FROM ' . $this->smilies_category_table . " |
||
123 | WHERE cat_lang = '$lang' |
||
124 | ORDER BY cat_order ASC"; |
||
125 | $result = $this->db->sql_query($sql); |
||
126 | while ($row = $this->db->sql_fetchrow($result)) |
||
127 | { |
||
128 | $selected = ($cat == $row['cat_id']) ? ' selected="selected"' : ''; |
||
129 | $select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>'; |
||
130 | } |
||
131 | $this->db->sql_freeresult($result); |
||
132 | |||
133 | $selected_defaut = ($cat == 0) ? ' selected="selected"' : ''; |
||
134 | $select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_defaut . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>'; |
||
135 | |||
136 | return $select; |
||
137 | } |
||
138 | |||
139 | public function capitalize($var) |
||
140 | { |
||
141 | return $this->db->sql_escape(ucfirst(strtolower(trim($var)))); |
||
142 | } |
||
143 | |||
144 | public function get_max_order() |
||
145 | { |
||
146 | // Get max order id... |
||
147 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
148 | FROM ' . $this->smilies_category_table; |
||
149 | $result = $this->db->sql_query($sql); |
||
150 | $max = (int) $this->db->sql_fetchfield('maxi', $result); |
||
151 | $this->db->sql_freeresult($result); |
||
152 | |||
153 | return $max; |
||
154 | } |
||
155 | |||
156 | public function get_max_id() |
||
157 | { |
||
158 | // Get max id... |
||
159 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
160 | FROM ' . $this->smilies_category_table; |
||
161 | $result = $this->db->sql_query($sql); |
||
162 | $id_max = (int) $this->db->sql_fetchfield('id_max', $result); |
||
163 | $this->db->sql_freeresult($result); |
||
164 | |||
165 | return $id_max; |
||
166 | } |
||
167 | |||
168 | public function get_first_order() |
||
169 | { |
||
170 | // Get first order id... |
||
171 | $sql = 'SELECT cat_order, cat_id |
||
172 | FROM ' . $this->smilies_category_table . ' |
||
173 | ORDER BY cat_order ASC'; |
||
174 | $result = $this->db->sql_query_limit($sql, 1); |
||
175 | $row = $this->db->sql_fetchrow($result); |
||
176 | $mini = $row['cat_id']; |
||
177 | $this->db->sql_freeresult($result); |
||
178 | |||
179 | return $mini; |
||
180 | } |
||
181 | |||
182 | public function shoutbox_smilies($event) |
||
221 | )); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | public function shoutbox_smilies_popup($event) |
||
280 | )); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | public function adm_add_cat() |
||
285 | { |
||
286 | $max = $this->get_max_order(); |
||
287 | $sql = 'SELECT lang_local_name, lang_iso |
||
288 | FROM ' . LANG_TABLE . " |
||
289 | ORDER BY lang_id ASC"; |
||
290 | $result = $this->db->sql_query($sql); |
||
291 | while ($row = $this->db->sql_fetchrow($result)) |
||
292 | { |
||
293 | $this->template->assign_block_vars('categories', array( |
||
294 | 'CAT_LANG' => $row['lang_local_name'], |
||
295 | 'CAT_ISO' => $row['lang_iso'], |
||
296 | 'CAT_ORDER' => $max + 1, |
||
297 | )); |
||
298 | } |
||
299 | $this->db->sql_freeresult($result); |
||
300 | |||
301 | $this->template->assign_vars(array( |
||
302 | 'CAT_ORDER' => $max + 1, |
||
303 | )); |
||
304 | } |
||
305 | |||
306 | public function adm_edit_cat($id) |
||
307 | { |
||
308 | // Get total lang id... |
||
309 | $sql = 'SELECT COUNT(lang_id) as total |
||
310 | FROM ' . LANG_TABLE; |
||
311 | $result = $this->db->sql_query($sql); |
||
312 | $total = (int) $this->db->sql_fetchfield('total', $result); |
||
313 | $this->db->sql_freeresult($result); |
||
314 | |||
315 | $title = ''; |
||
316 | $i = $cat_order = $cat_id = 0; |
||
317 | $list_id = []; |
||
318 | $sql = $this->db->sql_build_query('SELECT', array( |
||
319 | 'SELECT' => 'l.*, c.*', |
||
320 | 'FROM' => array(LANG_TABLE => 'l'), |
||
321 | 'LEFT_JOIN' => array( |
||
322 | array( |
||
323 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
324 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
325 | ), |
||
326 | ), |
||
327 | 'WHERE' => "cat_id = $id", |
||
328 | 'ORDER_BY' => 'lang_id ASC', |
||
329 | )); |
||
330 | $result = $this->db->sql_query($sql); |
||
331 | while ($row = $this->db->sql_fetchrow($result)) |
||
332 | { |
||
333 | $this->template->assign_block_vars('category_lang', array( |
||
334 | 'CAT_LANG' => $row['lang_local_name'], |
||
335 | 'CAT_ISO' => $row['lang_iso'], |
||
336 | 'CAT_ORDER' => $row['cat_order'], |
||
337 | 'CAT_ID' => $row['cat_id'], |
||
338 | 'CAT_TRADUCT' => $row['cat_name'], |
||
339 | 'CAT_SORT' => 'edit', |
||
340 | )); |
||
341 | $i++; |
||
342 | $list_id[$i] = $row['lang_id']; |
||
343 | $cat_id = $row['cat_id']; |
||
344 | $cat_order = $row['cat_order']; |
||
345 | $title = $row['cat_title']; |
||
346 | } |
||
347 | $this->db->sql_freeresult($result); |
||
348 | |||
349 | // Add rows for empty langs in this category |
||
350 | if ($i !== $total) |
||
351 | { |
||
352 | $sql = $this->db->sql_build_query('SELECT', array( |
||
353 | 'SELECT' => '*', |
||
354 | 'FROM' => array(LANG_TABLE => 'l'), |
||
355 | 'WHERE' => $this->db->sql_in_set('lang_id', $list_id, true, true), |
||
356 | )); |
||
357 | $result = $this->db->sql_query($sql); |
||
358 | while ($row = $this->db->sql_fetchrow($result)) |
||
359 | { |
||
360 | $this->template->assign_block_vars('category_lang', array( |
||
361 | 'CAT_LANG' => $row['lang_local_name'], |
||
362 | 'CAT_ISO' => $row['lang_iso'], |
||
363 | 'CAT_ORDER' => $cat_order, |
||
364 | 'CAT_ID' => $cat_id, |
||
365 | 'CAT_TRADUCT' => '', |
||
366 | 'CAT_SORT' => 'create', |
||
367 | )); |
||
368 | } |
||
369 | $this->db->sql_freeresult($result); |
||
370 | } |
||
371 | |||
372 | $this->template->assign_vars(array( |
||
373 | 'CAT_ORDER' => $cat_order, |
||
374 | 'CAT_TITLE' => $title, |
||
375 | )); |
||
376 | } |
||
377 | |||
378 | public function adm_list_cat($u_action) |
||
379 | { |
||
380 | $i = 1; |
||
381 | $cat = 0; |
||
382 | $empty_row = false; |
||
383 | $max = $this->get_max_order(); |
||
384 | $sql = $this->db->sql_build_query('SELECT', array( |
||
385 | 'SELECT' => 'l.lang_iso, l.lang_local_name, c.*', |
||
386 | 'FROM' => array(LANG_TABLE => 'l'), |
||
387 | 'LEFT_JOIN' => array( |
||
388 | array( |
||
389 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
390 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
391 | ), |
||
392 | ), |
||
393 | 'ORDER_BY' => 'cat_order ASC, c.cat_lang_id ASC', |
||
394 | )); |
||
395 | $result = $this->db->sql_query($sql); |
||
396 | if ($row = $this->db->sql_fetchrow($result)) |
||
397 | { |
||
398 | do |
||
399 | { |
||
400 | $this->template->assign_block_vars('categories', array( |
||
401 | 'CAT_NR' => $i, |
||
402 | 'CAT_LANG' => $row['lang_local_name'], |
||
403 | 'CAT_ISO' => $row['lang_iso'], |
||
404 | 'CAT_ID' => $row['cat_id'], |
||
405 | 'CAT_ORDER' => $row['cat_order'], |
||
406 | 'CAT_TRADUCT' => $row['cat_name'], |
||
407 | 'CAT_NB' => $row['cat_nb'], |
||
408 | 'ROW' => ($cat !== $row['cat_id']) ? true : false, |
||
409 | 'ROW_MAX' => ($row['cat_order'] == $max) ? true : false, |
||
410 | 'SPACER_CAT' => $this->language->lang('SC_CATEGORY_IN', $row['cat_title']), |
||
411 | 'U_EDIT' => $u_action . '&action=edit&id=' . $row['cat_id'], |
||
412 | 'U_DELETE' => $u_action . '&action=delete&id=' . $row['cat_id'], |
||
413 | 'U_MOVE_UP' => $u_action . '&action=move_up&id=' . $row['cat_id'] . '&hash=' . generate_link_hash('acp-main_module'), |
||
414 | 'U_MOVE_DOWN' => $u_action . '&action=move_down&id=' . $row['cat_id'] . '&hash=' . generate_link_hash('acp-main_module'), |
||
415 | )); |
||
416 | $i++; |
||
417 | $cat = $row['cat_id']; |
||
418 | $empty_row = (!$cat) ? true : false; |
||
419 | } while ($row = $this->db->sql_fetchrow($result)); |
||
420 | } |
||
421 | $this->db->sql_freeresult($result); |
||
422 | |||
423 | $this->template->assign_vars(array( |
||
424 | 'EMPTY_ROW' => $empty_row, |
||
425 | )); |
||
426 | } |
||
427 | |||
428 | public function extract_categories($cat) |
||
469 | } |
||
470 | |||
471 | public function adm_edit_smiley($id, $u_action, $start) |
||
472 | { |
||
473 | $lang = $this->user->lang_name; |
||
474 | $sql = $this->db->sql_build_query('SELECT', array( |
||
475 | 'SELECT' => 's.*, c.*', |
||
476 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
477 | 'LEFT_JOIN' => array( |
||
478 | array( |
||
479 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
480 | 'ON' => "c.cat_id = s.category AND c.cat_lang = '$lang'", |
||
481 | ), |
||
482 | ), |
||
483 | 'WHERE' => "smiley_id = $id", |
||
484 | )); |
||
485 | $result = $this->db->sql_query($sql); |
||
486 | $row = $this->db->sql_fetchrow($result); |
||
487 | |||
488 | $this->template->assign_vars(array( |
||
489 | 'WIDTH' => $row['smiley_width'], |
||
490 | 'HEIGHT' => $row['smiley_height'], |
||
491 | 'CODE' => $row['code'], |
||
492 | 'EMOTION' => $row['emotion'], |
||
493 | 'CATEGORY' => $row['cat_name'], |
||
494 | 'EX_CAT' => ($row['cat_id']) ? $row['cat_id'] : 0, |
||
495 | 'SELECT_CATEGORY' => $this->select_categories($row['cat_id'], true), |
||
496 | 'IMG_SRC' => $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'], |
||
497 | 'U_MODIFY' => $u_action . '&action=modify&id=' . $row['smiley_id'] . '&start=' . $start, |
||
498 | 'U_BACK' => $u_action, |
||
499 | )); |
||
500 | $this->db->sql_freeresult($result); |
||
501 | } |
||
502 | |||
503 | public function extract_list_smilies($select, $start, $u_action) |
||
580 | } |
||
581 | } |
||
582 |