Passed
Push — master ( 7a05a5...807ba8 )
by Sylver
03:18
created

smiley::get_max_order()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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