Passed
Push — 1.6.0 ( b45b13...aab18c )
by Sylver
03:18
created

smiley::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 11
rs 9.9666

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