Passed
Push — 1.6.0 ( 2cff17...f65516 )
by Sylver
03:21
created

smiley::get_cat_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
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 smilies_count($cat, $compact = false)
69
	{
70
		$sql = $this->db->sql_build_query('SELECT', [
71
			'SELECT'	=> (!$compact) ? 'COUNT(DISTINCT smiley_id) AS smilies_count' : 'COUNT(DISTINCT smiley_url) AS smilies_count',
72
			'FROM'		=> [SMILIES_TABLE => ''],
73
			'WHERE'		=> ($cat > -1) ? 'category = ' . (int) $cat : "code <> ''",
74
		]);
75
		$result = $this->db->sql_query($sql);
76
		$nb = (int) $this->db->sql_fetchfield('smilies_count');
77
		$this->db->sql_freeresult($result);
78
79
		return $nb;
80
	}
81
82
	public function get_max_order()
83
	{
84
		// Get max order id...
85
		$sql = 'SELECT MAX(cat_order) AS maxi
86
			FROM ' . $this->smilies_category_table;
87
		$result = $this->db->sql_query($sql);
88
		$max = (int) $this->db->sql_fetchfield('maxi');
89
		$this->db->sql_freeresult($result);
90
91
		return $max;
92
	}
93
94
	public function modify_smiley($id, $cat_id, $ex_cat = -1)
95
	{
96
		$ex_cat = ($ex_cat == -1) ? $this->get_cat_id($id) : $ex_cat;
97
98
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = ' . $cat_id . ' WHERE smiley_id = ' . $id);
99
		$this->update_cat_smiley($cat_id, $ex_cat);
100
	}
101
102
	private function update_cat_smiley($cat_id, $ex_cat)
103
	{
104
		// Increment nb value if wanted
105
		if ($cat_id)
106
		{
107
			if ($this->category->get_first_order() === $cat_id)
108
			{
109
				if ($this->category->get_cat_nb($cat_id) === 0)
110
				{
111
					$this->config->set('smilies_category_nb', $cat_id);
112
				}
113
			}
114
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb + 1 WHERE cat_id = ' . $cat_id);
115
		}
116
117
		// Decrement nb value if wanted
118
		if ($ex_cat)
119
		{
120
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb - 1 WHERE cat_id = ' . $ex_cat);
121
		}
122
	}
123
124
	private function get_cat_id($id)
125
	{
126
		$sql = 'SELECT category
127
			FROM ' . SMILIES_TABLE . '
128
				WHERE smiley_id = ' . $id;
129
		$result = $this->db->sql_query($sql);
130
		$cat = (int) $this->db->sql_fetchfield('category');
131
		$this->db->sql_freeresult($result);
132
133
		return $cat;
134
	}
135
136
	public function extract_list_smilies($select, $start, $u_action)
137
	{
138
		$i = 0;
139
		$cat = -1;
140
		$lang = $this->user->lang_name;
141
		$smilies_count = (int) $this->category->smilies_count($select);
142
143
		if ($select === 0)
144
		{
145
			$sql = 'SELECT *
146
				FROM ' . SMILIES_TABLE . '
147
					WHERE category = 0
148
				ORDER BY smiley_order ASC';
149
		}
150
		else
151
		{
152
			$sql = $this->db->sql_build_query('SELECT', [
153
				'SELECT'	=> 's.*, c.*',
154
				'FROM'		=> [SMILIES_TABLE => 's'],
155
				'LEFT_JOIN'	=> [
156
					[
157
						'FROM'	=> [$this->smilies_category_table => 'c'],
158
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
159
					],
160
				],
161
				'WHERE'		=> ($select === -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
162
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
163
			]);
164
		}
165
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_acp'], $start);
166
		while ($row = $this->db->sql_fetchrow($result))
167
		{
168
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
169
			$this->template->assign_block_vars('items', [
170
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
171
				'IMG_SRC'		=> $row['smiley_url'],
172
				'WIDTH'			=> $row['smiley_width'],
173
				'HEIGHT'		=> $row['smiley_height'],
174
				'ID'			=> $row['smiley_id'],
175
				'CODE'			=> $row['code'],
176
				'EMOTION'		=> $row['emotion'],
177
				'CATEGORY'		=> $row['cat_name'],
178
				'U_EDIT'		=> $u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
179
			]);
180
			$i++;
181
182
			// Keep this value in memory
183
			$cat = (int) $row['category'];
184
		}
185
		$this->db->sql_freeresult($result);
186
187
		$this->template->assign_vars([
188
			'NB_SMILIES'	=> $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count),
189
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
190
		]);
191
192
		$this->pagination->generate_template_pagination($u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
193
	}
194
195
	public function edit_smiley($id, $start, $u_action)
196
	{
197
		$lang = $this->user->lang_name;
198
		$sql = $this->db->sql_build_query('SELECT', [
199
			'SELECT'	=> 's.*, c.*',
200
			'FROM'		=> [SMILIES_TABLE => 's'],
201
			'LEFT_JOIN'	=> [
202
				[
203
					'FROM'	=> [$this->smilies_category_table => 'c'],
204
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
205
				],
206
			],
207
			'WHERE'	=> 's.smiley_id = ' . (int) $id,
208
		]);
209
		$result = $this->db->sql_query($sql);
210
		$row = $this->db->sql_fetchrow($result);
211
212
		$this->template->assign_vars([
213
			'WIDTH'				=> $row['smiley_width'],
214
			'HEIGHT'			=> $row['smiley_height'],
215
			'CODE'				=> $row['code'],
216
			'EMOTION'			=> $row['emotion'],
217
			'CATEGORY'			=> $row['cat_name'],
218
			'EX_CAT'			=> $row['cat_id'],
219
			'SELECT_CATEGORY'	=> $this->select_categories($row['cat_id'], false, false),
220
			'IMG_SRC'			=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
221
			'U_MODIFY'			=> $u_action . '&amp;action=modify&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
222
			'U_BACK'			=> $u_action,
223
			'S_IN_LIST'			=> false,
224
		]);
225
		$this->db->sql_freeresult($result);
226
	}
227
228
	public function edit_multi_smiley($list, $start, $u_action)
229
	{
230
		$i = 0;
231
		$lang = $this->user->lang_name;
232
		$sql = $this->db->sql_build_query('SELECT', [
233
			'SELECT'	=> 's.*, c.*',
234
			'FROM'		=> [SMILIES_TABLE => 's'],
235
			'LEFT_JOIN'	=> [
236
				[
237
					'FROM'	=> [$this->smilies_category_table => 'c'],
238
					'ON'	=> "c.cat_id = s.category AND cat_lang = '$lang'",
239
				],
240
			],
241
			'WHERE'		=> $this->db->sql_in_set('smiley_id', $list),
242
			'ORDER_BY'	=> 'cat_order ASC, s.smiley_order ASC',
243
		]);
244
		$result = $this->db->sql_query($sql);
245
		while ($row = $this->db->sql_fetchrow($result))
246
		{
247
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
248
			$this->template->assign_block_vars('items', [
249
				'IMG_SRC'		=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
250
				'WIDTH'			=> $row['smiley_width'],
251
				'HEIGHT'		=> $row['smiley_height'],
252
				'ID'			=> $row['smiley_id'],
253
				'CODE'			=> $row['code'],
254
				'EMOTION'		=> $row['emotion'],
255
				'CATEGORY'		=> $row['cat_name'],
256
			]);
257
			$i++;
258
		}
259
		$this->db->sql_freeresult($result);
260
261
		$this->template->assign_vars([
262
			'SELECT_CATEGORY'	=> $this->select_categories(-1),
263
			'U_MODIFY'			=> $u_action . '&amp;action=modify_list&amp;start=' . $start,
264
			'S_IN_LIST'			=> true,
265
		]);
266
	}
267
268
	public function select_categories($cat, $modify = false, $empty = false)
269
	{
270
		$lang = $this->user->lang_name;
271
		$select = '<option disabled="disabled">' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>';
272
		if ($modify)
273
		{
274
			$selected = ((int) $cat === -1) ? ' selected="selected"' : '';
275
			$select .= '<option value="-1"' . $selected . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>';
276
		}
277
278
		$sql = 'SELECT *
279
			FROM ' . $this->smilies_category_table . "
280
				WHERE cat_lang = '$lang'
281
				ORDER BY cat_order ASC";
282
		$result = $this->db->sql_query($sql);
283
		while ($row = $this->db->sql_fetchrow($result))
284
		{
285
			if (!$row['cat_nb'] && $empty)
286
			{
287
				continue;
288
			}
289
			$selected = ((int) $cat === (int) $row['cat_id']) ? ' selected="selected"' : '';
290
			$select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>';
291
		}
292
		$this->db->sql_freeresult($result);
293
294
		$selected_default = (!$cat) ? ' selected="selected"' : '';
295
		$select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>';
296
297
		return $select;
298
	}
299
}
300