Passed
Push — master ( a62c96...39b502 )
by Sylver
03:39
created

smiley::display_cat_smiley()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 10
rs 10
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($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($smiley, $new_cat, $ex_cat, $sort_new, $sort_ex)
0 ignored issues
show
Unused Code introduced by
The parameter $smiley is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

97
	private function update_cat_smiley(/** @scrutinizer ignore-unused */ $smiley, $new_cat, $ex_cat, $sort_new, $sort_ex)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
		if ($select == self::DEFAULT_CAT)
135
		{
136
			$sql = 'SELECT *
137
				FROM ' . SMILIES_TABLE . '
138
					WHERE category = ' . self::DEFAULT_CAT . '
139
				ORDER BY smiley_order ASC';
140
		}
141
		else if ($select == self::NOT_DISPLAY)
142
		{
143
			$sql = 'SELECT *
144
				FROM ' . SMILIES_TABLE . '
145
					WHERE display_on_cat = 0
146
				ORDER BY smiley_order ASC';
147
		}
148
		else
149
		{
150
			$sql = $this->db->sql_build_query('SELECT', [
151
				'SELECT'	=> 's.*, c.*',
152
				'FROM'		=> [SMILIES_TABLE => 's'],
153
				'LEFT_JOIN'	=> [
154
					[
155
						'FROM'	=> [$this->smilies_category_table => 'c'],
156
						'ON'	=> "s.category = c.cat_id AND c.cat_lang = '$lang'",
157
					],
158
				],
159
				'WHERE'		=> ($select == 0) ? "s.code <> ''" : "c.cat_id = $select",
160
				'ORDER_BY'	=> 's.display_on_cat DESC, s.category ASC, c.cat_order ASC, s.smiley_order ASC',
161
			]);
162
		}
163
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_acp'], $start);
164
		while ($row = $this->db->sql_fetchrow($result))
165
		{
166
			$row['cat_name'] = $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']);
167
			$this->template->assign_block_vars('items', [
168
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
169
				'IMG_SRC'		=> $row['smiley_url'],
170
				'WIDTH'			=> $row['smiley_width'],
171
				'HEIGHT'		=> $row['smiley_height'],
172
				'ID'			=> $row['smiley_id'],
173
				'CAT_ID'		=> $row['category'],
174
				'CODE'			=> $row['code'],
175
				'EMOTION'		=> $row['emotion'],
176
				'CATEGORY'		=> $row['cat_name'],
177
				'U_EDIT'		=> $u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;ex_cat=' . $row['category'] . '&amp;start=' . $start,
178
			]);
179
180
			// Keep this value in memory
181
			$cat = (int) $row['category'];
182
		}
183
		$this->db->sql_freeresult($result);
184
185
		$this->template->assign_vars([
186
			'NB_SMILIES'	=> $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count),
187
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
188
		]);
189
190
		$this->pagination->generate_template_pagination($u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
191
	}
192
193
	public function edit_smiley($smiley, $start, $ex_cat, $u_action)
194
	{
195
		$lang = $this->user->lang_name;
196
		$sql = $this->db->sql_build_query('SELECT', [
197
			'SELECT'	=> 's.*, c.*',
198
			'FROM'		=> [SMILIES_TABLE => 's'],
199
			'LEFT_JOIN'	=> [
200
				[
201
					'FROM'	=> [$this->smilies_category_table => 'c'],
202
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
203
				],
204
			],
205
			'WHERE'	=> 's.smiley_id = ' . (int) $smiley,
206
		]);
207
		$result = $this->db->sql_query($sql);
208
		$row = $this->db->sql_fetchrow($result);
209
210
		$this->template->assign_vars([
211
			'WIDTH'				=> $row['smiley_width'],
212
			'HEIGHT'			=> $row['smiley_height'],
213
			'CODE'				=> $row['code'],
214
			'EMOTION'			=> $row['emotion'],
215
			'CATEGORY'			=> $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']),
216
			'EX_CAT'			=> $ex_cat,
217
			'SELECT_CATEGORY'	=> $this->select_categories($row['category'], false, false),
218
			'IMG_SRC'			=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
219
			'U_MODIFY'			=> $u_action . '&amp;action=modify&amp;id=' . $row['smiley_id'] . '&amp;ex_cat=' . $ex_cat . '&amp;start=' . $start,
220
			'U_BACK'			=> $u_action,
221
			'S_IN_LIST'			=> false,
222
		]);
223
		$this->db->sql_freeresult($result);
224
	}
225
226
	public function edit_multi_smiley($list, $start, $u_action)
227
	{
228
		$lang = $this->user->lang_name;
229
		$sql = $this->db->sql_build_query('SELECT', [
230
			'SELECT'	=> 's.*, c.*',
231
			'FROM'		=> [SMILIES_TABLE => 's'],
232
			'LEFT_JOIN'	=> [
233
				[
234
					'FROM'	=> [$this->smilies_category_table => 'c'],
235
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
236
				],
237
			],
238
			'WHERE'		=> $this->db->sql_in_set('smiley_id', $list),
239
			'ORDER_BY'	=> 'c.cat_order ASC, s.smiley_order ASC',
240
		]);
241
		$result = $this->db->sql_query($sql);
242
		while ($row = $this->db->sql_fetchrow($result))
243
		{
244
			$this->template->assign_block_vars('items', [
245
				'IMG_SRC'		=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
246
				'WIDTH'			=> $row['smiley_width'],
247
				'HEIGHT'		=> $row['smiley_height'],
248
				'ID'			=> $row['smiley_id'],
249
				'CODE'			=> $row['code'],
250
				'EMOTION'		=> $row['emotion'],
251
				'CATEGORY'		=> $this->category->return_name($row['category'], !isset($row['cat_name']) ?: $row['cat_name']),
252
			]);
253
		}
254
		$this->db->sql_freeresult($result);
255
256
		$this->template->assign_vars([
257
			'SELECT_CATEGORY'	=> $this->select_categories(0),
258
			'U_MODIFY'			=> $u_action . '&amp;action=modify_list&amp;start=' . $start,
259
			'S_IN_LIST'			=> true,
260
		]);
261
	}
262
263
	public function select_categories($cat, $modify = false, $empty = false)
264
	{
265
		$lang = $this->user->lang_name;
266
		$select = '<option disabled="disabled">' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>';
267
		if ($modify)
268
		{
269
			$selected = (!$cat) ? ' selected="selected" class="in-red"' : '';
270
			$select .= '<option value="0"' . $selected . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>';
271
		}
272
273
		$sql = 'SELECT *
274
			FROM ' . $this->smilies_category_table . "
275
				WHERE cat_lang = '$lang'
276
				ORDER BY cat_order ASC";
277
		$result = $this->db->sql_query($sql);
278
		while ($row = $this->db->sql_fetchrow($result))
279
		{
280
			if (!$row['cat_nb'] && $empty)
281
			{
282
				continue;
283
			}
284
			$selected = ((int) $cat === (int) $row['cat_id']) ? ' selected="selected" class="in-red"' : '';
285
			$select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>';
286
		}
287
		$this->db->sql_freeresult($result);
288
289
		// Add the default category
290
		$selected_default = ((int) $cat === self::DEFAULT_CAT) ? ' selected="selected" class="in-red"' : '';
291
		$select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="' . self::DEFAULT_CAT . '"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>';
292
293
		// Add the not displayed category
294
		$selected_not = ((int) $cat === self::NOT_DISPLAY) ? ' selected="selected" class="in-red"' : '';
295
		$select .= '<option title="' . $this->language->lang('SC_CATEGORY_NOT') . '" value="' . self::NOT_DISPLAY . '"' . $selected_not . '> ' . $this->language->lang('SC_CATEGORY_NOT') . '</option>';
296
297
		return $select;
298
	}
299
}
300