work::delete_cat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 20
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 30
rs 9.6
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\log\log;
19
use phpbb\request\request;
20
use phpbb\controller\helper;
21
22
class work
23
{
24
	private const DEFAULT_CAT = 9998;
25
	private const NOT_DISPLAY = 9999;
26
27
	/* @var \sylver35\smiliescat\core\category */
28
	protected $category;
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\config\config */
34
	protected $config;
35
36
	/** @var \phpbb\user */
37
	protected $user;
38
39
	/** @var \phpbb\language\language */
40
	protected $language;
41
42
	/** @var \phpbb\template\template */
43
	protected $template;
44
45
	/** @var \phpbb\log\log */
46
	protected $log;
47
48
	/** @var \phpbb\request\request */
49
	protected $request;
50
51
	/* @var \phpbb\controller\helper */
52
	protected $helper;
53
54
	/**
55
	 * The database tables
56
	 *
57
	 * @var string */
58
	protected $smilies_category_table;
59
60
	/**
61
	 * Constructor
62
	 */
63
	public function __construct(category $category, db $db, config $config, user $user, language $language, template $template, log $log, request $request, helper $helper, $smilies_category_table)
64
	{
65
		$this->category = $category;
66
		$this->db = $db;
67
		$this->config = $config;
68
		$this->user = $user;
69
		$this->language = $language;
70
		$this->template = $template;
71
		$this->log = $log;
72
		$this->request = $request;
73
		$this->helper = $helper;
74
		$this->smilies_category_table = $smilies_category_table;
75
	}
76
77
	public function delete_cat($id, $u_action)
78
	{
79
		$sql = 'SELECT cat_title, cat_order
80
			FROM ' . $this->smilies_category_table . '
81
				WHERE cat_id = ' . $id;
82
		$result = $this->db->sql_query($sql);
83
		$row = $this->db->sql_fetchrow($result);
84
		$title = $row['cat_title'];
85
		$order = (int) $row['cat_order'];
86
		$this->db->sql_freeresult($result);
87
88
		$this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id);
89
90
		// Decrement orders if needed
91
		$sql_decrement = 'SELECT cat_id, cat_order
92
			FROM ' . $this->smilies_category_table . '
93
				WHERE cat_order > ' . $order;
94
		$result = $this->db->sql_query($sql_decrement);
95
		while ($row = $this->db->sql_fetchrow($result))
96
		{
97
			$new_order = (int) $row['cat_order'] - 1;
98
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_order = ' . $new_order . ' WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order']);
99
		}
100
		$this->db->sql_freeresult($result);
101
102
		// Reset appropriate smilies category id
103
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id);
104
105
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]);
106
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($u_action));
107
	}
108
109
	public function add_category($u_action)
110
	{
111
		$sql_ary = [];
112
		$title = (string) $this->request->variable('name_' . $this->user->lang_name, '', true);
113
		$cat_order = (int) $this->request->variable('order', 0);
114
		$cat_id = (int) $this->category->get_max_id() + 1;
115
116
		$sql = 'SELECT lang_id, lang_iso
117
			FROM ' . LANG_TABLE . "
118
				ORDER BY lang_id ASC";
119
		$result = $this->db->sql_query($sql);
120
		while ($row = $this->db->sql_fetchrow($result))
121
		{
122
			$iso = strtolower($row['lang_iso']);
123
			$lang = (string) $this->request->variable("lang_$iso", '', true);
124
			$name = (string) $this->request->variable("name_$iso", '', true);
125
			if ($name === '')
126
			{
127
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($u_action . '&amp;action=add'), E_USER_WARNING);
128
			}
129
			else
130
			{
131
				$sql_ary[] = [
132
					'cat_id'		=> $cat_id,
133
					'cat_order'		=> $cat_order,
134
					'cat_lang'		=> $lang,
135
					'cat_name'		=> $this->category->capitalize($name),
136
					'cat_title'		=> $this->category->capitalize($title),
137
					'cat_nb'		=> 0,
138
				];
139
			}
140
		}
141
		$this->db->sql_freeresult($result);
142
143
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_ary);
144
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), [$title]);
145
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($u_action));
146
	}
147
148
	public function edit_category($id, $u_action)
149
	{
150
		$sql_in = [];
151
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
152
		$order = (int) $this->request->variable('order', 0);
153
		$cat_nb = (int) $this->request->variable('cat_nb', 0);
154
155
		$sql = 'SELECT lang_id, lang_iso
156
			FROM ' . LANG_TABLE . "
157
				ORDER BY lang_id ASC";
158
		$result = $this->db->sql_query($sql);
159
		while ($row = $this->db->sql_fetchrow($result))
160
		{
161
			$iso = strtolower($row['lang_iso']);
162
			$lang = (string) $this->request->variable('lang_' . $iso, '', true);
163
			$action = (string) $this->request->variable('sort_' . $iso, '');
164
			$name = $this->category->capitalize($this->request->variable('name_' . $iso, '', true));
165
166
			if ($name === '')
167
			{
168
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
169
			}
170
			else if ($action === 'edit')
171
			{
172
				$this->db->sql_query('UPDATE ' . $this->smilies_category_table . " SET cat_name = '" . $this->db->sql_escape($name) . "', cat_title = '" . $this->db->sql_escape($title) . "', cat_nb = $cat_nb WHERE cat_lang = '" . $this->db->sql_escape($lang) . "' AND cat_id = $id");
173
			}
174
			else if ($action === 'create')
175
			{
176
				$sql_in[] = [
177
					'cat_id'		=> $id,
178
					'cat_order'		=> $order,
179
					'cat_lang'		=> $lang,
180
					'cat_name'		=> $name,
181
					'cat_title'		=> $title,
182
					'cat_nb'		=> $cat_nb,
183
				];
184
			}
185
		}
186
		$this->db->sql_freeresult($result);
187
188
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_in);
189
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]);
190
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($u_action));
191
	}
192
193
	public function add_cat($u_action)
194
	{
195
		$max = (int) $this->category->get_max_order() + 1;
196
		$sql = 'SELECT lang_local_name, lang_iso
197
			FROM ' . LANG_TABLE . '
198
				ORDER BY lang_id ASC';
199
		$result = $this->db->sql_query($sql);
200
		while ($row = $this->db->sql_fetchrow($result))
201
		{
202
			$this->template->assign_block_vars('categories', [
203
				'CAT_LANG'		=> $row['lang_local_name'],
204
				'CAT_ISO'		=> $row['lang_iso'],
205
				'CAT_ORDER'		=> $max,
206
			]);
207
		}
208
		$this->db->sql_freeresult($result);
209
210
		$this->template->assign_vars([
211
			'IN_CAT_ACTION'		=> true,
212
			'IN_ADD_ACTION'		=> true,
213
			'CAT_ORDER'			=> $max,
214
			'U_BACK'			=> $u_action,
215
			'U_ADD_CAT'			=> $u_action . '&amp;action=add_cat',
216
		]);
217
	}
218
219
	public function edit_cat($id, $u_action)
220
	{
221
		// Get total lang id...
222
		$sql = 'SELECT COUNT(lang_id) as total
223
			FROM ' . LANG_TABLE;
224
		$result = $this->db->sql_query($sql);
225
		$total = (int) $this->db->sql_fetchfield('total');
226
		$this->db->sql_freeresult($result);
227
228
		$title = '';
229
		$list_id = [];
230
		$i = $cat_order = $cat_nb = 0;
231
		$sql = $this->db->sql_build_query('SELECT', [
232
			'SELECT'	=> 'l.*, c.*',
233
			'FROM'		=> [LANG_TABLE => 'l'],
234
			'LEFT_JOIN'	=> [
235
				[
236
					'FROM'	=> [$this->smilies_category_table => 'c'],
237
					'ON'	=> 'c.cat_lang = l.lang_iso',
238
				],
239
			],
240
			'WHERE'		=> 'cat_id = ' . $id,
241
			'ORDER_BY'	=> 'lang_id ASC',
242
		]);
243
		$result = $this->db->sql_query($sql);
244
		while ($row = $this->db->sql_fetchrow($result))
245
		{
246
			$this->template->assign_block_vars('category_lang', [
247
				'CAT_LANG'			=> $row['lang_local_name'],
248
				'CAT_ISO'			=> $row['lang_iso'],
249
				'CAT_ORDER'			=> $row['cat_order'],
250
				'CAT_ID'			=> $row['cat_id'],
251
				'CAT_TRANSLATE'		=> $row['cat_name'],
252
				'CAT_SORT'			=> 'edit',
253
			]);
254
			$i++;
255
			$list_id[$i] = $row['lang_id'];
256
			$cat_order = $row['cat_order'];
257
			$title = $row['cat_title'];
258
			$cat_nb = $row['cat_nb'];
259
		}
260
		$this->db->sql_freeresult($result);
261
262
		// Add rows for empty langs in this category
263
		if ($i !== $total)
264
		{
265
			$sql = $this->db->sql_build_query('SELECT', [
266
				'SELECT'	=> '*',
267
				'FROM'		=> [LANG_TABLE => 'l'],
268
				'WHERE'		=> $this->db->sql_in_set('lang_id', $list_id, true, true),
269
			]);
270
			$result = $this->db->sql_query($sql);
271
			while ($row = $this->db->sql_fetchrow($result))
272
			{
273
				$this->template->assign_block_vars('category_lang', [
274
					'CAT_LANG'			=> $row['lang_local_name'],
275
					'CAT_ISO'			=> $row['lang_iso'],
276
					'CAT_ORDER'			=> $cat_order,
277
					'CAT_ID'			=> $id,
278
					'CAT_TRANSLATE'		=> '',
279
					'CAT_SORT'			=> 'create',
280
				]);
281
			}
282
			$this->db->sql_freeresult($result);
283
		}
284
285
		$this->template->assign_vars([
286
			'IN_CAT_ACTION'	=> true,
287
			'CAT_ORDER'		=> $cat_order,
288
			'CAT_NB'		=> $cat_nb,
289
			'CAT_TITLE'		=> $title,
290
			'U_BACK'		=> $u_action,
291
			'U_EDIT_CAT'	=> $u_action . '&amp;action=edit_cat&amp;id=' . $id,
292
		]);
293
	}
294
295
	public function adm_list_cat($u_action)
296
	{
297
		$i = 0;
298
		$cat = 0;
299
		$total = $this->category->category_exist();
300
		if ($total === 0)
301
		{
302
			$this->template->assign_vars([
303
				'EMPTY_ROW' =>	true,
304
			]);
305
		}
306
		else
307
		{
308
			$lang_cat = [];
309
			$langs = $this->category->get_langs();
310
			$max = $this->category->get_max_order();
311
			$sql = $this->db->sql_build_query('SELECT', [
312
				'SELECT'	=> 'l.lang_id, l.lang_iso, l.lang_local_name, c.*',
313
				'FROM'		=> [LANG_TABLE => 'l'],
314
				'LEFT_JOIN'	=> [
315
					[
316
						'FROM'	=> [$this->smilies_category_table => 'c'],
317
						'ON'	=> 'c.cat_lang = l.lang_iso',
318
					],
319
				],
320
				'WHERE'		=> 'c.cat_id NOT IN (0, 9999)',
321
				'ORDER_BY'	=> 'c.cat_order ASC, l.lang_id ASC',
322
			]);
323
			$result = $this->db->sql_query($sql);
324
			while ($row = $this->db->sql_fetchrow($result))
325
			{
326
				$title = '';
327
				if ((int) $row['cat_id'] !== $cat)
328
				{
329
					$title = $this->language->lang('SC_CATEGORY_IN', $this->category->return_name($row['cat_id'], $row['cat_name'], true));
330
					$this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, false);
331
				}
332
				$lang_cat[$row['cat_id']][$row['lang_id']] = $row['lang_iso'];
333
				$this->template->assign_block_vars('categories', [
334
					'CAT_ORDER'		=> (int) $row['cat_order'],
335
					'CAT_ID'		=> $row['cat_id'],
336
					'CAT_LANG'		=> $row['lang_local_name'],
337
					'CAT_ISO'		=> $row['lang_iso'],
338
					'CAT_TRANSLATE'	=> $row['cat_name'],
339
					'CAT_NB'		=> $row['cat_nb'],
340
					'NB_CSS'		=> $row['cat_nb'] ? 'green' : 'orange',
341
					'ROW_MAX'		=> (int) $row['cat_order'] === $max,
342
					'TITLE_CAT'		=> $title,
343
					'U_EDIT'		=> $u_action . '&amp;action=edit&amp;id=' . $row['cat_id'],
344
					'U_DELETE'		=> $u_action . '&amp;action=delete&amp;id=' . $row['cat_id'],
345
				]);
346
				$i++;
347
				// Keep this value in memory
348
				$cat = (int) $row['cat_id'];
349
350
				if ((int) $row['cat_order'] === $max && ($i === $total))
351
				{
352
					$this->category->verify_cat_langs($langs, $cat, $i, $lang_cat, false);
353
				}
354
			}
355
			$this->db->sql_freeresult($result);
356
		}
357
358
		$this->template->assign_vars([
359
			'IN_LIST_CAT'	=> true,
360
			'U_ACTION'		=> $u_action,
361
			'U_MOVE_CATS'	=> $this->helper->route('sylver35_smiliescat_ajax_list_cat'),
362
		]);
363
	}
364
}
365