Total Complexity | 47 |
Total Lines | 475 |
Duplicated Lines | 0 % |
Changes | 15 | ||
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 |
||
21 | class category |
||
22 | { |
||
23 | /** @var \phpbb\cache\driver\driver_interface */ |
||
24 | protected $cache; |
||
25 | |||
26 | /** @var \phpbb\db\driver\driver_interface */ |
||
27 | protected $db; |
||
28 | |||
29 | /** @var \phpbb\config\config */ |
||
30 | protected $config; |
||
31 | |||
32 | /** @var \phpbb\user */ |
||
33 | protected $user; |
||
34 | |||
35 | /** @var \phpbb\language\language */ |
||
36 | protected $language; |
||
37 | |||
38 | /** @var \phpbb\template\template */ |
||
39 | protected $template; |
||
40 | |||
41 | /** @var \phpbb\request\request */ |
||
42 | protected $request; |
||
43 | |||
44 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
45 | protected $ext_manager; |
||
46 | |||
47 | /** |
||
48 | * The database tables |
||
49 | * |
||
50 | * @var string */ |
||
51 | protected $smilies_category_table; |
||
52 | |||
53 | /** @var string phpBB root path */ |
||
54 | protected $root_path; |
||
55 | |||
56 | /** |
||
57 | * Constructor |
||
58 | */ |
||
59 | public function __construct(cache $cache, db $db, config $config, user $user, language $language, template $template, request $request, manager $ext_manager, $smilies_category_table, $root_path) |
||
60 | { |
||
61 | $this->cache = $cache; |
||
62 | $this->db = $db; |
||
63 | $this->config = $config; |
||
64 | $this->user = $user; |
||
65 | $this->language = $language; |
||
66 | $this->template = $template; |
||
67 | $this->request = $request; |
||
68 | $this->ext_manager = $ext_manager; |
||
69 | $this->smilies_category_table = $smilies_category_table; |
||
70 | $this->root_path = $root_path; |
||
71 | } |
||
72 | |||
73 | public function get_version() |
||
74 | { |
||
75 | if (($data = $this->cache->get('_smiliescat_version')) === false) |
||
76 | { |
||
77 | $md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat'); |
||
78 | $meta = $md_manager->get_metadata(); |
||
79 | |||
80 | $data = array( |
||
81 | 'version' => $meta['version'], |
||
82 | 'homepage' => $meta['homepage'], |
||
83 | ); |
||
84 | // cache for 7 days |
||
85 | $this->cache->put('_smiliescat_version', $data, 604800); |
||
86 | } |
||
87 | |||
88 | return $data; |
||
89 | } |
||
90 | |||
91 | public function smilies_count($cat) |
||
92 | { |
||
93 | $sql_where = ($cat == -1) ? "code <> ''" : "category = $cat"; |
||
94 | $sql = $this->db->sql_build_query('SELECT', array( |
||
95 | 'SELECT' => 'COUNT(DISTINCT smiley_url) AS smilies_count', |
||
96 | 'FROM' => array(SMILIES_TABLE => ''), |
||
97 | 'WHERE' => $sql_where, |
||
98 | )); |
||
99 | $result = $this->db->sql_query($sql); |
||
100 | $smilies_count = (int) $this->db->sql_fetchfield('smilies_count'); |
||
101 | $this->db->sql_freeresult($result); |
||
102 | |||
103 | return $smilies_count; |
||
104 | } |
||
105 | |||
106 | public function select_categories($cat, $modify = false) |
||
107 | { |
||
108 | $lang = $this->user->lang_name; |
||
109 | $select = '<option>' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>'; |
||
110 | if (!$modify) |
||
111 | { |
||
112 | $sel = ($cat == -1) ? ' selected="selected"' : ''; |
||
113 | $select .= '<option value="-1"' . $sel . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>'; |
||
114 | } |
||
115 | |||
116 | $sql = 'SELECT * |
||
117 | FROM ' . $this->smilies_category_table . " |
||
118 | WHERE cat_lang = '$lang' |
||
119 | ORDER BY cat_order ASC"; |
||
120 | $result = $this->db->sql_query($sql); |
||
121 | while ($row = $this->db->sql_fetchrow($result)) |
||
122 | { |
||
123 | $selected = ($cat == $row['cat_id']) ? ' selected="selected"' : ''; |
||
124 | $select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>'; |
||
125 | } |
||
126 | $this->db->sql_freeresult($result); |
||
127 | |||
128 | $selected_defaut = ($cat == 0) ? ' selected="selected"' : ''; |
||
129 | $select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_defaut . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>'; |
||
130 | |||
131 | return $select; |
||
132 | } |
||
133 | |||
134 | public function capitalize($var) |
||
135 | { |
||
136 | return $this->db->sql_escape(ucfirst(strtolower(trim($var)))); |
||
137 | } |
||
138 | |||
139 | public function get_max_order() |
||
140 | { |
||
141 | // Get max order id... |
||
142 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
143 | FROM ' . $this->smilies_category_table; |
||
144 | $result = $this->db->sql_query($sql); |
||
145 | $max = (int) $this->db->sql_fetchfield('maxi', $result); |
||
146 | $this->db->sql_freeresult($result); |
||
147 | |||
148 | return $max; |
||
149 | } |
||
150 | |||
151 | public function get_max_id() |
||
152 | { |
||
153 | // Get max id... |
||
154 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
155 | FROM ' . $this->smilies_category_table; |
||
156 | $result = $this->db->sql_query($sql); |
||
157 | $id_max = (int) $this->db->sql_fetchfield('id_max', $result); |
||
158 | $this->db->sql_freeresult($result); |
||
159 | |||
160 | return $id_max; |
||
161 | } |
||
162 | |||
163 | public function get_first_order() |
||
164 | { |
||
165 | // Get first order id... |
||
166 | $sql = 'SELECT cat_order, cat_id |
||
167 | FROM ' . $this->smilies_category_table . ' |
||
168 | ORDER BY cat_order ASC'; |
||
169 | $result = $this->db->sql_query_limit($sql, 1); |
||
170 | $row = $this->db->sql_fetchrow($result); |
||
171 | $mini = $row['cat_id']; |
||
172 | $this->db->sql_freeresult($result); |
||
173 | |||
174 | return $mini; |
||
175 | } |
||
176 | |||
177 | public function shoutbox_smilies($event) |
||
216 | )); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | public function shoutbox_smilies_popup($event) |
||
275 | )); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | public function adm_add_cat() |
||
280 | { |
||
281 | $max = $this->get_max_order(); |
||
282 | $sql = 'SELECT lang_local_name, lang_iso |
||
283 | FROM ' . LANG_TABLE . " |
||
284 | ORDER BY lang_id ASC"; |
||
285 | $result = $this->db->sql_query($sql); |
||
286 | while ($row = $this->db->sql_fetchrow($result)) |
||
287 | { |
||
288 | $this->template->assign_block_vars('categories', array( |
||
289 | 'CAT_LANG' => $row['lang_local_name'], |
||
290 | 'CAT_ISO' => $row['lang_iso'], |
||
291 | 'CAT_ORDER' => $max + 1, |
||
292 | )); |
||
293 | } |
||
294 | $this->db->sql_freeresult($result); |
||
295 | |||
296 | $this->template->assign_vars(array( |
||
297 | 'CAT_ORDER' => $max + 1, |
||
298 | )); |
||
299 | } |
||
300 | |||
301 | public function adm_edit_cat($id) |
||
302 | { |
||
303 | // Get total lang id... |
||
304 | $sql = 'SELECT COUNT(lang_id) as total |
||
305 | FROM ' . LANG_TABLE; |
||
306 | $result = $this->db->sql_query($sql); |
||
307 | $total = (int) $this->db->sql_fetchfield('total', $result); |
||
308 | $this->db->sql_freeresult($result); |
||
309 | |||
310 | $title = ''; |
||
311 | $i = $cat_order = $cat_id = 0; |
||
312 | $list_id = []; |
||
313 | $sql = $this->db->sql_build_query('SELECT', array( |
||
314 | 'SELECT' => 'l.*, c.*', |
||
315 | 'FROM' => array(LANG_TABLE => 'l'), |
||
316 | 'LEFT_JOIN' => array( |
||
317 | array( |
||
318 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
319 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
320 | ), |
||
321 | ), |
||
322 | 'WHERE' => "cat_id = $id", |
||
323 | 'ORDER_BY' => 'lang_id ASC', |
||
324 | )); |
||
325 | $result = $this->db->sql_query($sql); |
||
326 | while ($row = $this->db->sql_fetchrow($result)) |
||
327 | { |
||
328 | $this->template->assign_block_vars('category_lang', array( |
||
329 | 'CAT_LANG' => $row['lang_local_name'], |
||
330 | 'CAT_ISO' => $row['lang_iso'], |
||
331 | 'CAT_ORDER' => $row['cat_order'], |
||
332 | 'CAT_ID' => $row['cat_id'], |
||
333 | 'CAT_TRADUCT' => $row['cat_name'], |
||
334 | 'CAT_SORT' => 'edit', |
||
335 | )); |
||
336 | $i++; |
||
337 | $list_id[$i] = $row['lang_id']; |
||
338 | $cat_id = $row['cat_id']; |
||
339 | $cat_order = $row['cat_order']; |
||
340 | $title = $row['cat_title']; |
||
341 | } |
||
342 | $this->db->sql_freeresult($result); |
||
343 | |||
344 | // Add rows for empty langs in this category |
||
345 | if ($i !== $total) |
||
346 | { |
||
347 | $sql = $this->db->sql_build_query('SELECT', array( |
||
348 | 'SELECT' => '*', |
||
349 | 'FROM' => array(LANG_TABLE => 'l'), |
||
350 | 'WHERE' => $this->db->sql_in_set('lang_id', $list_id, true, true), |
||
351 | )); |
||
352 | $result = $this->db->sql_query($sql); |
||
353 | while ($row = $this->db->sql_fetchrow($result)) |
||
354 | { |
||
355 | $this->template->assign_block_vars('category_lang', array( |
||
356 | 'CAT_LANG' => $row['lang_local_name'], |
||
357 | 'CAT_ISO' => $row['lang_iso'], |
||
358 | 'CAT_ORDER' => $cat_order, |
||
359 | 'CAT_ID' => $cat_id, |
||
360 | 'CAT_TRADUCT' => '', |
||
361 | 'CAT_SORT' => 'create', |
||
362 | )); |
||
363 | } |
||
364 | $this->db->sql_freeresult($result); |
||
365 | } |
||
366 | |||
367 | $this->template->assign_vars(array( |
||
368 | 'CAT_ORDER' => $cat_order, |
||
369 | 'CAT_TITLE' => $title, |
||
370 | )); |
||
371 | } |
||
372 | |||
373 | public function adm_list_cat($u_action) |
||
374 | { |
||
375 | $i = 1; |
||
376 | $cat = 0; |
||
377 | $empty_row = false; |
||
378 | $max = $this->get_max_order(); |
||
379 | $sql = $this->db->sql_build_query('SELECT', array( |
||
380 | 'SELECT' => 'l.lang_iso, l.lang_local_name, c.*', |
||
381 | 'FROM' => array(LANG_TABLE => 'l'), |
||
382 | 'LEFT_JOIN' => array( |
||
383 | array( |
||
384 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
385 | 'ON' => 'c.cat_lang = l.lang_iso', |
||
386 | ), |
||
387 | ), |
||
388 | 'ORDER_BY' => 'cat_order ASC, c.cat_lang_id ASC', |
||
389 | )); |
||
390 | $result = $this->db->sql_query($sql); |
||
391 | if ($row = $this->db->sql_fetchrow($result)) |
||
392 | { |
||
393 | do |
||
394 | { |
||
395 | $this->template->assign_block_vars('categories', array( |
||
396 | 'CAT_NR' => $i, |
||
397 | 'CAT_LANG' => $row['lang_local_name'], |
||
398 | 'CAT_ISO' => $row['lang_iso'], |
||
399 | 'CAT_ID' => $row['cat_id'], |
||
400 | 'CAT_ORDER' => $row['cat_order'], |
||
401 | 'CAT_TRADUCT' => $row['cat_name'], |
||
402 | 'CAT_NB' => $row['cat_nb'], |
||
403 | 'ROW' => ($cat !== $row['cat_id']) ? true : false, |
||
404 | 'ROW_MAX' => ($row['cat_order'] == $max) ? true : false, |
||
405 | 'SPACER_CAT' => $this->language->lang('SC_CATEGORY_IN', $row['cat_title']), |
||
406 | 'U_EDIT' => $u_action . '&action=edit&id=' . $row['cat_id'], |
||
407 | 'U_DELETE' => $u_action . '&action=delete&id=' . $row['cat_id'], |
||
408 | 'U_MOVE_UP' => $u_action . '&action=move_up&id=' . $row['cat_id'] . '&hash=' . generate_link_hash('acp-main_module'), |
||
409 | 'U_MOVE_DOWN' => $u_action . '&action=move_down&id=' . $row['cat_id'] . '&hash=' . generate_link_hash('acp-main_module'), |
||
410 | )); |
||
411 | $i++; |
||
412 | $cat = $row['cat_id']; |
||
413 | $empty_row = (!$cat) ? true : false; |
||
414 | } while ($row = $this->db->sql_fetchrow($result)); |
||
415 | } |
||
416 | $this->db->sql_freeresult($result); |
||
417 | |||
418 | $this->template->assign_vars(array( |
||
419 | 'EMPTY_ROW' => $empty_row, |
||
420 | )); |
||
421 | } |
||
422 | |||
423 | public function extract_categories($cat) |
||
464 | } |
||
465 | |||
466 | public function adm_edit_smiley($id, $u_action, $start) |
||
467 | { |
||
468 | $lang = $this->user->lang_name; |
||
496 | } |
||
497 | } |
||
498 |