Passed
Push — 1.1.0 ( 72422e...74b104 )
by Sylver
02:41
created

category   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 475
Duplicated Lines 0 %

Importance

Changes 15
Bugs 0 Features 0
Metric Value
eloc 294
c 15
b 0
f 0
dl 0
loc 475
rs 8.64
wmc 47

15 Methods

Rating   Name   Duplication   Size   Complexity  
A shoutbox_smilies() 0 39 3
A select_categories() 0 26 6
B shoutbox_smilies_popup() 0 55 5
A get_max_id() 0 10 1
A capitalize() 0 3 1
B extract_categories() 0 41 10
A get_version() 0 16 2
A smilies_count() 0 13 2
A __construct() 0 12 1
B adm_list_cat() 0 47 6
A adm_edit_smiley() 0 30 2
A get_first_order() 0 12 1
A adm_add_cat() 0 19 2
A get_max_order() 0 10 1
A adm_edit_cat() 0 69 4

How to fix   Complexity   

Complex Class

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
2
/**
3
*
4
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020 Sylver35  https://breizhcode.com
6
* @license		http://opensource.org/licenses/gpl-license.php GNU Public License
7
*
8
*/
9
10
namespace sylver35\smiliescat\core;
11
12
use phpbb\cache\driver\driver_interface as cache;
13
use phpbb\db\driver\driver_interface as db;
14
use phpbb\config\config;
15
use phpbb\user;
16
use phpbb\language\language;
17
use phpbb\template\template;
18
use phpbb\request\request;
19
use phpbb\extension\manager;
20
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)
178
	{
179
		$i = $cat_order = $cat_id = 0;
180
		$list_cat = [];
181
		$lang = $this->user->lang_name;
182
183
		$sql = $this->db->sql_build_query('SELECT', array(
184
			'SELECT'	=> '*',
185
			'FROM'		=> array($this->smilies_category_table => ''),
186
			'WHERE'		=> "cat_lang = '$lang'",
187
			'ORDER_BY'	=> 'cat_order ASC',
188
		));
189
		$result = $this->db->sql_query($sql, 3600);
190
		while ($row = $this->db->sql_fetchrow($result))
191
		{
192
			$list_cat[$i] = array(
193
				'cat_id'		=> $row['cat_id'],
194
				'cat_order'		=> $row['cat_order'],
195
				'cat_name'		=> $row['cat_name'],
196
				'cat_nb'		=> $row['cat_nb'],
197
			);
198
			$i++;
199
			$cat_order = $row['cat_order'];
200
		}
201
		$this->db->sql_freeresult($result);
202
203
		if ($i > 0)
204
		{
205
			// Add the Unclassified category
206
			$list_cat[$i] = array(
207
				'cat_id'		=> $cat_id,
208
				'cat_order'		=> $cat_order + 1,
209
				'cat_name'		=> $this->language->lang('SC_CATEGORY_DEFAUT'),
210
				'cat_nb'		=> $this->smilies_count($cat_id),
211
			);
212
213
			$event['content'] = array_merge($event['content'], array(
214
				'title_cat'		=> $this->language->lang('ACP_SC_SMILIES'),
215
				'categories'	=> $list_cat,
216
			));
217
		}
218
	}
219
220
	public function shoutbox_smilies_popup($event)
221
	{
222
		$cat = $event['cat'];
223
		if ($cat > -1)
224
		{
225
			$i = 0;
226
			$smilies = array();
227
			$lang = $this->user->lang_name;
228
229
			if ($cat > 0)
230
			{
231
				$sql = 'SELECT cat_name
232
					FROM ' . $this->smilies_category_table . "
233
						WHERE cat_lang = '$lang'
234
						AND cat_id = $cat";
235
				$result = $this->db->sql_query_limit($sql, 1);
236
				$row = $this->db->sql_fetchrow($result);
237
				$cat_name = $row['cat_name'];
238
				$this->db->sql_freeresult($result);
239
			}
240
			else
241
			{
242
				$cat_name = $this->language->lang('SC_CATEGORY_DEFAUT');
243
			}
244
245
			$sql = $this->db->sql_build_query('SELECT', array(
246
				'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, MIN(display_on_shout) AS display_on_shout',
247
				'FROM'		=> array(SMILIES_TABLE => ''),
248
				'WHERE'		=> "category = $cat",
249
				'GROUP_BY'	=> 'smiley_url',
250
				'ORDER_BY'	=> 'min_smiley_order ASC',
251
			));
252
			$result = $this->db->sql_query($sql);
253
			while ($row = $this->db->sql_fetchrow($result))
254
			{
255
				$smilies[$i] = array(
256
					'nb'		=> $i,
257
					'code'		=> $row['code'],
258
					'emotion'	=> $row['emotion'],
259
					'width'		=> $row['smiley_width'],
260
					'height'	=> $row['smiley_height'],
261
					'image'		=> $row['smiley_url'],
262
				);
263
				$i++;
264
			}
265
266
			$empty_row = ($i == 0) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : false;
267
268
			$event['content'] = array_merge($event['content'], array(
269
				'in_cat'		=> true,
270
				'cat'			=> $cat,
271
				'total'			=> $i,
272
				'smilies'		=> $smilies,
273
				'emptyRow'		=> $empty_row,
274
				'title'			=> $this->language->lang('SC_CATEGORY_IN', $cat_name),
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 . '&amp;action=edit&amp;id=' . $row['cat_id'],
407
					'U_DELETE'			=> $u_action . '&amp;action=delete&amp;id=' . $row['cat_id'],
408
					'U_MOVE_UP'			=> $u_action . '&amp;action=move_up&amp;id=' . $row['cat_id'] . '&amp;hash=' . generate_link_hash('acp-main_module'),
409
					'U_MOVE_DOWN'		=> $u_action . '&amp;action=move_down&amp;id=' . $row['cat_id'] . '&amp;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)
424
	{
425
		$title = '';
426
		$cat_order = $i = 0;
427
		$sql = $this->db->sql_build_query('SELECT', array(
428
			'SELECT'	=> '*',
429
			'FROM'		=> array($this->smilies_category_table => ''),
430
			'WHERE'		=> "cat_lang = '$this->user->lang_name'",
431
			'ORDER_BY'	=> 'cat_order ASC',
432
		));
433
		$result = $this->db->sql_query($sql);
434
		while ($row = $this->db->sql_fetchrow($result))
435
		{
436
			$active = ($row['cat_id'] == $cat) ? true : false;
437
			$this->template->assign_block_vars('categories', array(
438
				'CLASS'			=> ($active) ? 'cat-active' : 'cat-inactive',
439
				'SEPARATE'		=> ($i > 0) ? ' - ' : '',
440
				'CAT_ID'		=> $row['cat_id'],
441
				'CAT_ORDER'		=> $row['cat_order'],
442
				'CAT_NAME'		=> $row['cat_name'],
443
				'CAT_NB'		=> $row['cat_nb'],
444
			));
445
			$i++;
446
			$title = ($active) ? $row['cat_name'] : $title;
447
			$cat_order = $row['cat_order'];
448
		}
449
		$this->db->sql_freeresult($result);
450
451
		// Add the Unclassified category
452
		$unclassified = $this->language->lang('SC_CATEGORY_DEFAUT');
453
		$un_active = ($cat == 0) ? true : false;
454
		$this->template->assign_block_vars('categories', array(
455
			'CLASS'			=> ($un_active) ? 'cat-active' : 'cat-inactive',
456
			'SEPARATE'		=> ($i > 0) ? ' - ' : '',
457
			'CAT_ID'		=> 0,
458
			'CAT_ORDER'		=> $cat_order + 1,
459
			'CAT_NAME'		=> $unclassified,
460
			'CAT_NB'		=> $this->smilies_count(0),
461
		));
462
463
		return ($un_active) ? $unclassified : $title;
464
	}
465
466
	public function adm_edit_smiley($id, $u_action, $start)
467
	{
468
		$lang = $this->user->lang_name;
469
		$sql = $this->db->sql_build_query('SELECT', array(
470
			'SELECT'	=> 's.*, c.*',
471
			'FROM'		=> array(SMILIES_TABLE => 's'),
472
			'LEFT_JOIN'	=> array(
473
				array(
474
					'FROM'	=> array($this->smilies_category_table => 'c'),
475
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
476
				),
477
			),
478
			'WHERE'	=> "smiley_id = $id",
479
		));
480
		$result = $this->db->sql_query($sql);
481
		$row = $this->db->sql_fetchrow($result);
482
483
		$this->template->assign_vars(array(
484
			'WIDTH'				=> $row['smiley_width'],
485
			'HEIGHT'			=> $row['smiley_height'],
486
			'CODE'				=> $row['code'],
487
			'EMOTION'			=> $row['emotion'],
488
			'CATEGORY'			=> $row['cat_name'],
489
			'EX_CAT'			=> ($row['cat_id']) ? $row['cat_id'] : 0,
490
			'SELECT_CATEGORY'	=> $this->select_categories($row['cat_id'], true),
491
			'IMG_SRC'			=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
492
			'U_MODIFY'			=> $u_action . '&amp;action=modify&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
493
			'U_BACK'			=> $u_action,
494
		));
495
		$this->db->sql_freeresult($result);
496
	}
497
}
498