smiley::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 12
rs 9.9332

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 sylver35\smiliescat\core\category;
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\controller\helper;
19
use phpbb\pagination;
20
21
class smiley
22
{
23
	private const DEFAULT_CAT = 9998;
24
	private const NOT_DISPLAY = 9999;
25
26
	/* @var \sylver35\smiliescat\core\category */
27
	protected $category;
28
29
	/** @var \phpbb\db\driver\driver_interface */
30
	protected $db;
31
32
	/** @var \phpbb\config\config */
33
	protected $config;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	/** @var \phpbb\language\language */
39
	protected $language;
40
41
	/** @var \phpbb\template\template */
42
	protected $template;
43
44
	/** @var \phpbb\pagination */
45
	protected $pagination;
46
47
	/* @var \phpbb\controller\helper */
48
	protected $helper;
49
50
	/**
51
	 * The database tables
52
	 *
53
	 * @var string */
54
	protected $smilies_category_table;
55
56
	/** @var string phpBB root path */
57
	protected $root_path;
58
59
	/**
60
	 * Constructor
61
	 */
62
	public function __construct(category $category, db $db, config $config, user $user, language $language, template $template, pagination $pagination, helper $helper, $smilies_category_table, $root_path)
63
	{
64
		$this->category = $category;
65
		$this->db = $db;
66
		$this->config = $config;
67
		$this->user = $user;
68
		$this->language = $language;
69
		$this->template = $template;
70
		$this->pagination = $pagination;
71
		$this->helper = $helper;
72
		$this->smilies_category_table = $smilies_category_table;
73
		$this->root_path = $root_path;
74
	}
75
76
	public function modify_smiley($smiley, $new_cat, $ex_cat = 0)
77
	{
78
		$ex_cat = (!$ex_cat) ? $this->category->get_cat_id($smiley) : $ex_cat;
79
80
		// Change the category
81
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = ' . $new_cat . ' WHERE smiley_id = ' . $smiley);
82
83
		// Determine the type of categories 1/user category 2/Unclassified category 3/Undisplayed category
84
		$sort_new_cat = $this->category->category_sort($new_cat);
85
		$sort_ex_cat = $this->category->category_sort($ex_cat);
86
87
		// Increment or Decrement in user categories
88
		$this->update_cat_smiley($new_cat, $ex_cat, $sort_new_cat, $sort_ex_cat);
89
90
		// Change the display if wanted
91
		if ($sort_ex_cat == 3 || $sort_new_cat == 3)
92
		{
93
			$this->display_cat_smiley($smiley, $sort_new_cat, $sort_ex_cat);
94
		}
95
	}
96
97
	private function update_cat_smiley($new_cat, $ex_cat, $sort_new, $sort_ex)
98
	{
99
		// Decrement nb value if wanted
100
		if ($sort_ex == 1)
101
		{
102
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb - 1 WHERE cat_id = ' . $ex_cat);
103
		}
104
105
		// Increment nb value if wanted
106
		if ($sort_new == 1)
107
		{
108
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb + 1 WHERE cat_id = ' . $new_cat);
109
			
110
		}
111
112
		$this->config->set('smilies_first_cat', $this->category->get_first_order());
113
	}
114
115
	private function display_cat_smiley($smiley, $sort_new, $sort_ex)
116
	{
117
		if ($sort_ex == 3)
118
		{
119
			$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET display_on_cat = 1 WHERE smiley_id = ' . $smiley);
120
		}
121
122
		if ($sort_new == 3)
123
		{
124
			$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET display_on_cat = 0 WHERE smiley_id = ' . $smiley);
125
		}
126
	}
127
128
	public function extract_list_smilies($select, $start, $u_action)
129
	{
130
		$cat = 0;
131
		$lang = $this->user->lang_name;
132
		$smilies_count = (int) $this->category->smilies_count($select);
133
134
		switch ($select)
135
		{
136
			case self::DEFAULT_CAT:
137
				$sql = 'SELECT * FROM ' . SMILIES_TABLE . ' WHERE category = ' . self::DEFAULT_CAT . ' ORDER BY smiley_order ASC';
138
			break;
139
			case self::NOT_DISPLAY:
140
				$sql = 'SELECT * FROM ' . SMILIES_TABLE . ' WHERE display_on_cat = 0 ORDER BY smiley_order ASC';
141
			break;
142
			default :
143
				$sql = $this->db->sql_build_query('SELECT', [
144
					'SELECT'	=> 's.*, c.*',
145
					'FROM'		=> [SMILIES_TABLE => 's'],
146
					'LEFT_JOIN'	=> [
147
						[
148
							'FROM'	=> [$this->smilies_category_table => 'c'],
149
							'ON'	=> "s.category = c.cat_id AND c.cat_lang = '$lang'",
150
						],
151
					],
152
					'WHERE'		=> ($select == 0) ? "s.code <> ''" : "c.cat_id = $select",
153
					'ORDER_BY'	=> 's.display_on_cat DESC, s.category ASC, c.cat_order ASC, s.smiley_order ASC',
154
				]);
155
		}
156
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_acp'], $start);
157
		while ($row = $this->db->sql_fetchrow($result))
158
		{
159
			$row['cat_name'] = $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']);
160
			$this->template->assign_block_vars('items', [
161
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
162
				'IMG_SRC'		=> $row['smiley_url'],
163
				'WIDTH'			=> $row['smiley_width'],
164
				'HEIGHT'		=> $row['smiley_height'],
165
				'ID'			=> $row['smiley_id'],
166
				'CAT_ID'		=> $row['category'],
167
				'CODE'			=> $row['code'],
168
				'EMOTION'		=> $row['emotion'],
169
				'CATEGORY'		=> $row['cat_name'],
170
				'U_EDIT'		=> $u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;ex_cat=' . $row['category'] . '&amp;start=' . $start,
171
			]);
172
173
			// Keep this value in memory
174
			$cat = (int) $row['category'];
175
		}
176
		$this->db->sql_freeresult($result);
177
178
		$this->template->assign_vars([
179
			'NB_SMILIES'	=> $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count),
180
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
181
		]);
182
183
		$this->pagination->generate_template_pagination($u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
184
	}
185
186
	public function edit_smiley($smiley, $start, $ex_cat, $u_action)
187
	{
188
		$lang = $this->user->lang_name;
189
		$sql = $this->db->sql_build_query('SELECT', [
190
			'SELECT'	=> 's.*, c.*',
191
			'FROM'		=> [SMILIES_TABLE => 's'],
192
			'LEFT_JOIN'	=> [
193
				[
194
					'FROM'	=> [$this->smilies_category_table => 'c'],
195
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
196
				],
197
			],
198
			'WHERE'	=> 's.smiley_id = ' . (int) $smiley,
199
		]);
200
		$result = $this->db->sql_query($sql);
201
		$row = $this->db->sql_fetchrow($result);
202
203
		$this->template->assign_vars([
204
			'WIDTH'				=> $row['smiley_width'],
205
			'HEIGHT'			=> $row['smiley_height'],
206
			'CODE'				=> $row['code'],
207
			'EMOTION'			=> $row['emotion'],
208
			'CATEGORY'			=> $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']),
209
			'EX_CAT'			=> $ex_cat,
210
			'SELECT_CATEGORY'	=> $this->select_categories($row['category'], false, false),
211
			'IMG_SRC'			=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
212
			'U_MODIFY'			=> $u_action . '&amp;action=modify&amp;id=' . $row['smiley_id'] . '&amp;ex_cat=' . $ex_cat . '&amp;start=' . $start,
213
			'U_BACK'			=> $u_action,
214
			'S_IN_LIST'			=> false,
215
		]);
216
		$this->db->sql_freeresult($result);
217
	}
218
219
	public function edit_multi_smiley($list, $start, $u_action)
220
	{
221
		$lang = $this->user->lang_name;
222
		$sql = $this->db->sql_build_query('SELECT', [
223
			'SELECT'	=> 's.*, c.*',
224
			'FROM'		=> [SMILIES_TABLE => 's'],
225
			'LEFT_JOIN'	=> [
226
				[
227
					'FROM'	=> [$this->smilies_category_table => 'c'],
228
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
229
				],
230
			],
231
			'WHERE'		=> $this->db->sql_in_set('smiley_id', $list),
232
			'ORDER_BY'	=> 'c.cat_order ASC, s.smiley_order ASC',
233
		]);
234
		$result = $this->db->sql_query($sql);
235
		while ($row = $this->db->sql_fetchrow($result))
236
		{
237
			$this->template->assign_block_vars('items', [
238
				'IMG_SRC'		=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
239
				'WIDTH'			=> $row['smiley_width'],
240
				'HEIGHT'		=> $row['smiley_height'],
241
				'ID'			=> $row['smiley_id'],
242
				'CODE'			=> $row['code'],
243
				'EMOTION'		=> $row['emotion'],
244
				'CATEGORY'		=> $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']),
245
			]);
246
		}
247
		$this->db->sql_freeresult($result);
248
249
		$this->template->assign_vars([
250
			'SELECT_CATEGORY'	=> $this->select_categories(0),
251
			'U_MODIFY'			=> $u_action . '&amp;action=modify_list&amp;start=' . $start,
252
			'S_IN_LIST'			=> true,
253
		]);
254
	}
255
256
	public function select_categories($cat, $modify = false, $empty = false)
257
	{
258
		$lang = $this->user->lang_name;
259
		$select = '<option disabled="disabled">' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>';
260
		if ($modify)
261
		{
262
			$selected = (!$cat) ? ' selected="selected" class="in-red"' : '';
263
			$select .= '<option value="0"' . $selected . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>';
264
		}
265
266
		$sql = 'SELECT *
267
			FROM ' . $this->smilies_category_table . "
268
				WHERE cat_lang = '$lang'
269
				ORDER BY cat_order ASC";
270
		$result = $this->db->sql_query($sql);
271
		while ($row = $this->db->sql_fetchrow($result))
272
		{
273
			if (!$row['cat_nb'] && $empty)
274
			{
275
				continue;
276
			}
277
			$selected = ((int) $cat === (int) $row['cat_id']) ? ' selected="selected" class="in-red"' : '';
278
			$select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>';
279
		}
280
		$this->db->sql_freeresult($result);
281
282
		// Add the default category
283
		$selected_default = ((int) $cat === self::DEFAULT_CAT) ? ' selected="selected" class="in-red"' : '';
284
		$select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="' . self::DEFAULT_CAT . '"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>';
285
286
		// Add the not displayed category
287
		$selected_not = ((int) $cat === self::NOT_DISPLAY) ? ' selected="selected" class="in-red"' : '';
288
		$select .= '<option title="' . $this->language->lang('SC_CATEGORY_NOT') . '" value="' . self::NOT_DISPLAY . '"' . $selected_not . '> ' . $this->language->lang('SC_CATEGORY_NOT') . '</option>';
289
290
		return $select;
291
	}
292
}
293