category::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 3
b 0
f 0
nc 1
nop 11
dl 0
loc 13
rs 9.9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * @package		Breizh Smilies Categories Extension
5
 * @copyright	(c) 2020-2024 Sylver35  https://breizhcode.com
6
 * @license		https://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\controller\helper;
16
use phpbb\user;
17
use phpbb\language\language;
18
use phpbb\template\template;
19
use phpbb\extension\manager;
20
use phpbb\log\log;
21
22
class category
23
{
24
	private const DEFAULT_CAT = 9998;
25
	private const NOT_DISPLAY = 9999;
26
27
	/** @var \phpbb\cache\driver\driver_interface */
28
	protected $cache;
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\config\config */
34
	protected $config;
35
36
	/* @var \phpbb\controller\helper */
37
	protected $helper;
38
39
	/** @var \phpbb\user */
40
	protected $user;
41
42
	/** @var \phpbb\language\language */
43
	protected $language;
44
45
	/** @var \phpbb\template\template */
46
	protected $template;
47
48
	/** @var \phpbb\extension\manager "Extension Manager" */
49
	protected $ext_manager;
50
51
	/** @var \phpbb\log\log */
52
	protected $log;
53
54
	/**
55
	 * The database tables
56
	 *
57
	 * @var string */
58
	protected $smilies_category_table;
59
60
	/** @var string phpBB root path */
61
	protected $root_path;
62
63
	/**
64
	 * Constructor
65
	 */
66
	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)
67
	{
68
		$this->cache = $cache;
69
		$this->db = $db;
70
		$this->config = $config;
71
		$this->helper = $helper;
72
		$this->user = $user;
73
		$this->language = $language;
74
		$this->template = $template;
75
		$this->ext_manager = $ext_manager;
76
		$this->log = $log;
77
		$this->smilies_category_table = $smilies_category_table;
78
		$this->root_path = $root_path;
79
	}
80
81
	public function get_version()
82
	{
83
		if (($data = $this->cache->get('_smiliescat_version')) === false)
84
		{
85
			$md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat');
86
			$meta = $md_manager->get_metadata();
87
88
			$data = [
89
				'version'	=> $meta['version'],
90
				'homepage'	=> $meta['homepage'],
91
			];
92
			// cache for 7 days
93
			$this->cache->put('_smiliescat_version', $data, 604800);
94
		}
95
96
		return $data;
97
	}
98
99
	public function smilies_count($cat)
100
	{
101
		$sql = $this->db->sql_build_query('SELECT', [
102
			'SELECT'	=> 'COUNT(DISTINCT smiley_id) AS smilies_count',
103
			'FROM'		=> [SMILIES_TABLE => ''],
104
			'WHERE'		=> ($cat > 0) ? 'category = ' . $cat : "code <> ''",
105
		]);
106
		$result = $this->db->sql_query($sql);
107
		$nb = (int) $this->db->sql_fetchfield('smilies_count');
108
		$this->db->sql_freeresult($result);
109
110
		return $nb;
111
	}
112
113
	public function capitalize($var)
114
	{
115
		return ucfirst(strtolower(trim($var)));
116
	}
117
118
	public function get_langs()
119
	{
120
		$data = [];
121
		$sql = 'SELECT *
122
			FROM ' . LANG_TABLE;
123
		$result = $this->db->sql_query($sql);
124
		while ($row = $this->db->sql_fetchrow($result))
125
		{
126
			$data[$row['lang_id']] = $row['lang_iso'];
127
		}
128
		$this->db->sql_freeresult($result);
129
130
		return $data;
131
	}
132
133
	public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax)
134
	{
135
		$data = [];
136
		foreach ($langs as $id => $iso)
137
		{
138
			if (!isset($lang_cat[$cat][$id]))
139
			{
140
				$data[] = $iso;
141
			}
142
		}
143
144
		if ($ajax === false)
145
		{
146
			$this->cat_to_template($i, $data);
147
		}
148
		else
149
		{
150
			return $this->cat_to_ajax($i, $data);
151
		}
152
	}
153
154
	private function cat_to_template($i, $data)
155
	{
156
		if (($i !== 0) && !empty($data))
157
		{
158
			$this->template->assign_block_vars('categories', [
159
				'ERROR'			=> true,
160
				'LANG_EMPTY'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data),
161
			]);
162
		}
163
	}
164
165
	private function cat_to_ajax($i, $data)
166
	{
167
		$values = [
168
			'error'			=> false,
169
			'lang_empty'	=> '',
170
		];
171
172
		if (($i !== 0) && !empty($data))
173
		{
174
			$values = [
175
				'error'			=> true,
176
				'lang_empty'	=> $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data),
177
			];
178
		}
179
180
		return $values;
181
	}
182
183
	public function category_sort($category)
184
	{
185
		// Determine the type of categories
186
		switch ($category)
187
		{
188
			case self::DEFAULT_CAT:
189
				$sort = 2; // Unclassified category
190
			break;
191
			case self::NOT_DISPLAY:
192
				$sort = 3; // Undisplayed category
193
			break;
194
			default:
195
				$sort = 1; // user category
196
		}
197
198
		return $sort;
199
	}
200
201
	public function category_exist()
202
	{
203
		$sql = 'SELECT COUNT(cat_id) AS total
204
			FROM ' . $this->smilies_category_table;
205
		$result = $this->db->sql_query($sql);
206
		$total = (int) $this->db->sql_fetchfield('total');
207
		$this->db->sql_freeresult($result);
208
209
		return $total;
210
	}
211
212
	public function return_name($cat, $name = '', $all = false)
213
	{
214
		if ($cat == self::DEFAULT_CAT)
215
		{
216
			return $this->language->lang('SC_CATEGORY_DEFAUT');
217
		}
218
		if ($cat == self::NOT_DISPLAY)
219
		{
220
			return $this->language->lang('SC_CATEGORY_NOT');
221
		}
222
		else if ($all)
223
		{
224
			return $this->cat_name($cat);
225
		}
226
		else if ($name)
227
		{
228
			return $name;
229
		}
230
	}
231
232
	private function cat_name($cat)
233
	{
234
		$lang = (string) $this->user->lang_name;
235
		$sql = 'SELECT cat_name
236
			FROM ' . $this->smilies_category_table . "
237
				WHERE cat_id = $cat
238
				AND cat_lang = '$lang'";
239
		$result = $this->db->sql_query_limit($sql, 1);
240
		$cat_name = (string) $this->db->sql_fetchfield('cat_name');
241
		$this->db->sql_freeresult($result);
242
		if ($cat_name)
243
		{
244
			return $cat_name;
245
		}
246
		else
247
		{
248
			$sql2 = 'SELECT cat_name
249
				FROM ' . $this->smilies_category_table . "
250
					WHERE cat_id = $cat
251
					AND cat_lang = 'en'";
252
			$result2 = $this->db->sql_query_limit($sql2, 1);
253
			$cat_name = (string) $this->db->sql_fetchfield('cat_name');
254
			$this->db->sql_freeresult($result2);
255
		}
256
257
		return $cat_name;
258
	}
259
260
	public function get_max_order()
261
	{
262
		// Get max order id...
263
		$sql = 'SELECT MAX(cat_order) AS maxi
264
			FROM ' . $this->smilies_category_table;
265
		$result = $this->db->sql_query($sql);
266
		$max = (int) $this->db->sql_fetchfield('maxi');
267
		$this->db->sql_freeresult($result);
268
269
		return $max;
270
	}
271
272
	public function get_max_id()
273
	{
274
		// Get max id...
275
		$sql = 'SELECT MAX(cat_id) AS id_max
276
			FROM ' . $this->smilies_category_table;
277
		$result = $this->db->sql_query($sql);
278
		$id_max = (int) $this->db->sql_fetchfield('id_max');
279
		$this->db->sql_freeresult($result);
280
281
		return $id_max;
282
	}
283
284
	public function get_first_order()
285
	{
286
		// Get first order id non empty...
287
		$sql = 'SELECT cat_id, cat_order, cat_nb
288
			FROM ' . $this->smilies_category_table . '
289
				WHERE cat_nb > 0
290
				ORDER BY cat_order ASC';
291
		$result = $this->db->sql_query_limit($sql, 1);
292
		$first = (int) $this->db->sql_fetchfield('cat_id');
293
		$this->db->sql_freeresult($result);
294
		$first = !$first ? self::DEFAULT_CAT : $first;
295
296
		return $first;
297
	}
298
299
	public function get_cat_id($id)
300
	{
301
		$sql = 'SELECT category
302
			FROM ' . SMILIES_TABLE . '
303
				WHERE smiley_id = ' . $id;
304
		$result = $this->db->sql_query($sql);
305
		$category = (int) $this->db->sql_fetchfield('category');
306
		$this->db->sql_freeresult($result);
307
308
		return $category;
309
	}
310
311
	public function get_cat_nb($id)
312
	{
313
		$sql = 'SELECT cat_nb
314
			FROM ' . $this->smilies_category_table . '
315
				WHERE cat_id = ' . $id;
316
		$result = $this->db->sql_query($sql);
317
		$cat_nb = (int) $this->db->sql_fetchfield('cat_nb');
318
		$this->db->sql_freeresult($result);
319
320
		return $cat_nb;
321
	}
322
323
	public function set_order($action, $current_order)
324
	{
325
		// Never move up the first
326
		if ($current_order === 1 && $action === 'move_up')
327
		{
328
			return 0;
329
		}
330
331
		// Never move down the last
332
		$max_order = $this->get_max_order();
333
		if (($current_order === $max_order) && ($action === 'move_down'))
334
		{
335
			return 0;
336
		}
337
338
		// on move_down, switch position with next order_id...
339
		// on move_up, switch position with previous order_id...
340
		$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1;
341
342
		// Return the new position
343
		return $switch_order_id;
344
	}
345
346
	public function move_cat($id, $action)
347
	{
348
		// Get current order id and title...
349
		$sql = 'SELECT cat_order, cat_title
350
			FROM ' . $this->smilies_category_table . '
351
				WHERE cat_id = ' . $id;
352
		$result = $this->db->sql_query($sql);
353
		$row = $this->db->sql_fetchrow($result);
354
		$current_order = (int) $row['cat_order'];
355
		$title = $row['cat_title'];
356
		$this->db->sql_freeresult($result);
357
358
		$switch_order_id = (int) $this->set_order($action, $current_order);
359
		if ($switch_order_id === 0)
360
		{
361
			return;
362
		}
363
364
		$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $current_order WHERE cat_order = $switch_order_id AND cat_id <> $id");
365
		$move_executed = (bool) $this->db->sql_affectedrows();
366
367
		// Only update the other entry too if the previous entry got updated
368
		if ($move_executed)
369
		{
370
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_order = $switch_order_id WHERE cat_order = $current_order AND cat_id = $id");
371
		}
372
		
373
		$this->config->set('smilies_first_cat', $this->get_first_order());
374
375
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), [$title]);
376
	}
377
}
378