Passed
Push — 1.4.0 ( 63e418...7b09fd )
by Sylver
04:18
created

admin_controller::add_cat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 0
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
*
4
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020-2022 Sylver35  https://breizhcode.com
6
* @license		http://opensource.org/licenses/gpl-license.php GNU Public License
7
*
8
*/
9
10
namespace sylver35\smiliescat\controller;
11
12
use sylver35\smiliescat\core\category;
13
use phpbb\config\config;
14
use phpbb\db\driver\driver_interface as db;
15
use phpbb\pagination;
16
use phpbb\request\request;
17
use phpbb\template\template;
18
use phpbb\user;
19
use phpbb\language\language;
20
use phpbb\log\log;
21
22
class admin_controller
23
{
24
	/* @var \sylver35\smiliescat\core\category */
25
	protected $category;
26
27
	/** @var \phpbb\config\config */
28
	protected $config;
29
30
	/** @var \phpbb\db\driver\driver_interface */
31
	protected $db;
32
33
	/** @var \phpbb\pagination */
34
	protected $pagination;
35
36
	/** @var \phpbb\request\request */
37
	protected $request;
38
39
	/** @var \phpbb\template\template */
40
	protected $template;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var \phpbb\language\language */
46
	protected $language;
47
48
	/** @var \phpbb\log\log */
49
	protected $log;
50
51
	/** @var string phpBB root path */
52
	protected $root_path;
53
54
	/** @var string Custom form action */
55
	protected $u_action;
56
57
	/**
58
	 * The database tables
59
	 *
60
	 * @var string */
61
	protected $smilies_category_table;
62
63
	/**
64
	 * Constructor
65
	 */
66
	public function __construct(category $category, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table)
67
	{
68
		$this->category = $category;
69
		$this->config = $config;
70
		$this->db = $db;
71
		$this->pagination = $pagination;
72
		$this->request = $request;
73
		$this->template = $template;
74
		$this->user = $user;
75
		$this->language = $language;
76
		$this->log = $log;
77
		$this->root_path = $root_path;
78
		$this->smilies_category_table = $smilies_category_table;
79
	}
80
81
	public function acp_smilies_category($id, $action)
82
	{
83
		$this->language->add_lang('acp/posting');
84
		$start = (int) $this->request->variable('start', 0);
85
		$select = (int) $this->request->variable('select', -1);
86
		$form_key = 'sylver35/smiliescat';
87
		add_form_key($form_key);
88
89
		if ($action)
90
		{
91
			switch ($action)
92
			{
93
				case 'edit':
94
					$this->edit_smiley($id, $start);
95
				break;
96
97
				case 'modify':
98
					if (!check_form_key($form_key))
99
					{
100
						trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
101
					}
102
103
					$this->modify_smiley($id, (int) $this->request->variable('cat_id', 0), (int) $this->request->variable('ex_cat', 0));
104
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
105
				break;
106
			}
107
108
			$this->template->assign_vars([
109
				'IN_ACTION'	=> true,
110
			]);
111
		}
112
		else
113
		{
114
			$this->extract_list_smilies($select, $start);
115
116
			$this->template->assign_vars([
117
				'LIST_CATEGORY'		=> $this->category->select_categories($select, true, true),
118
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
119
				'U_BACK'			=> ($select) ? $this->u_action : '',
120
			]);
121
		}
122
123
		$this->template->assign_vars([
124
			'CATEGORIE_SMILIES'	=> true,
125
		]);
126
	}
127
128
	public function acp_categories_config($id, $action, $mode)
129
	{
130
		$this->language->add_lang('acp/language');
131
		$form_key = 'sylver35/smiliescat';
132
		add_form_key($form_key);
133
134
		if ($action)
135
		{
136
			if (in_array($action, ['config_cat', 'add_cat', 'edit_cat']) && !check_form_key($form_key))
137
			{
138
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
139
			}
140
141
			switch ($action)
142
			{
143
				case 'config_cat':
144
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
145
146
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
147
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
148
				break;
149
150
				case 'add':
151
					$this->add_cat();
152
				break;
153
154
				case 'add_cat':
155
					$this->add_category();
156
				break;
157
158
				case 'edit':
159
					$this->edit_cat((int) $id);
160
				break;
161
162
				case 'edit_cat':
163
					$this->edit_category((int) $id);
164
				break;
165
166
				case 'delete':
167
					if (confirm_box(true))
168
					{
169
						$this->delete_cat((int) $id);
170
					}
171
					else
172
					{
173
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([
174
							'mode'		=> $mode,
175
							'id'		=> $id,
176
							'action'	=> $action,
177
						]));
178
					}
179
				break;
180
			}
181
182
			$this->template->assign_vars([
183
				'IN_ACTION'	=> true,
184
			]);
185
		}
186
		else
187
		{
188
			$this->category->adm_list_cat($this->u_action);
189
		}
190
191
		$this->template->assign_vars([
192
			'CATEGORIE_CONFIG'		=> true,
193
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
194
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
195
			'U_ADD'					=> $this->u_action . '&amp;action=add',
196
		]);
197
	}
198
199
	private function modify_smiley($id, $cat_id, $ex_cat)
200
	{
201
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = ' . $cat_id . ' WHERE smiley_id = ' . $id);
202
203
		// Increment nb value if wanted
204
		if ($cat_id)
205
		{
206
			if ($this->category->get_first_order() === $cat_id)
207
			{
208
				if ($this->category->get_cat_nb($cat_id) === 0)
209
				{
210
					$this->config->set('smilies_category_nb', $cat_id);
211
				}
212
			}
213
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb + 1 WHERE cat_id = ' . $cat_id);
214
		}
215
216
		// Decrement nb value if wanted
217
		if ($ex_cat)
218
		{
219
			$this->db->sql_query('UPDATE ' . $this->smilies_category_table . ' SET cat_nb = cat_nb - 1 WHERE cat_id = ' . $ex_cat);
220
		}
221
	}
222
223
	private function extract_list_smilies($select, $start)
224
	{
225
		$i = 0;
226
		$cat = -1;
227
		$lang = $this->user->lang_name;
228
		$smilies_count = (int) $this->category->smilies_count($select);
229
230
		if ($select === 0)
231
		{
232
			$sql = 'SELECT *
233
				FROM ' . SMILIES_TABLE . '
234
					WHERE category = 0
235
				ORDER BY smiley_order ASC';
236
		}
237
		else
238
		{
239
			$sql = $this->db->sql_build_query('SELECT', [
240
				'SELECT'	=> 's.*, c.*',
241
				'FROM'		=> [SMILIES_TABLE => 's'],
242
				'LEFT_JOIN'	=> [
243
					[
244
						'FROM'	=> [$this->smilies_category_table => 'c'],
245
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
246
					],
247
				],
248
				'WHERE'		=> ($select === -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
249
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
250
			]);
251
		}
252
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
253
		while ($row = $this->db->sql_fetchrow($result))
254
		{
255
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
256
			$this->template->assign_block_vars('items', [
257
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
258
				'IMG_SRC'		=> $row['smiley_url'],
259
				'WIDTH'			=> $row['smiley_width'],
260
				'HEIGHT'		=> $row['smiley_height'],
261
				'CODE'			=> $row['code'],
262
				'EMOTION'		=> $row['emotion'],
263
				'CATEGORY'		=> $row['cat_name'],
264
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
265
			]);
266
			$i++;
267
268
			// Keep this value in memory
269
			$cat = (int) $row['category'];
270
		}
271
		$this->db->sql_freeresult($result);
272
273
		$this->template->assign_vars([
274
			'NB_SMILIES'	=> $this->language->lang('SC_SMILIES', ($smilies_count > 1) ? 2 : 1, $smilies_count),
275
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
276
		]);
277
278
		$this->pagination->generate_template_pagination($this->u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
279
	}
280
281
	private function delete_cat($id)
282
	{
283
		$sql = 'SELECT cat_title, cat_order
284
			FROM ' . $this->smilies_category_table . '
285
				WHERE cat_id = ' . $id;
286
		$result = $this->db->sql_query($sql);
287
		$row = $this->db->sql_fetchrow($result);
288
		$title = $row['cat_title'];
289
		$order = (int) $row['cat_order'];
290
		$this->db->sql_freeresult($result);
291
292
		$this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id);
293
294
		// Decrement orders if needed
295
		$sql_decrement = 'SELECT cat_id, cat_order
296
			FROM ' . $this->smilies_category_table . '
297
				WHERE cat_order > ' . $order;
298
		$result = $this->db->sql_query($sql_decrement);
299
		while ($row = $this->db->sql_fetchrow($result))
300
		{
301
			$new_order = (int) $row['cat_order'] - 1;
302
			$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']);
303
		}
304
		$this->db->sql_freeresult($result);
305
306
		// Reset appropriate smilies category id
307
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id);
308
309
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]);
310
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
311
	}
312
313
	private function add_category()
314
	{
315
		$sql_ary = [];
316
		$title = (string) $this->request->variable('name_' . $this->user->lang_name, '', true);
317
		$cat_order = (int) $this->request->variable('order', 0);
318
		$cat_id = (int) $this->category->get_max_id() + 1;
319
320
		$sql = 'SELECT lang_id, lang_iso
321
			FROM ' . LANG_TABLE . "
322
				ORDER BY lang_id ASC";
323
		$result = $this->db->sql_query($sql);
324
		while ($row = $this->db->sql_fetchrow($result))
325
		{
326
			$iso = strtolower($row['lang_iso']);
327
			$lang = (string) $this->request->variable("lang_$iso", '', true);
328
			$name = (string) $this->request->variable("name_$iso", '', true);
329
			if ($name === '')
330
			{
331
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
332
			}
333
			else
334
			{
335
				$sql_ary[] = [
336
					'cat_id'		=> $cat_id,
337
					'cat_order'		=> $cat_order,
338
					'cat_lang'		=> $lang,
339
					'cat_name'		=> $this->category->capitalize($name),
340
					'cat_title'		=> $this->category->capitalize($title),
341
					'cat_nb'		=> 0,
342
				];
343
			}
344
		}
345
		$this->db->sql_freeresult($result);
346
347
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_ary);
348
349
		if ($cat_order === 1)
350
		{
351
			$sql = 'SELECT cat_id, cat_nb
352
				FROM ' . $this->smilies_category_table . '
353
					WHERE cat_order = 1';
354
			$result = $this->db->sql_query_limit($sql, 1);
355
			$row = $this->db->sql_fetchrow($result);
356
			if ($row['cat_nb'] > 0)
357
			{
358
				$this->config->set('smilies_category_nb', $row['cat_id']);
359
			}
360
			$this->db->sql_freeresult($result);
361
		}
362
363
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), [$title]);
364
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
365
	}
366
367
	private function edit_category($id)
368
	{
369
		$sql_in = [];
370
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
371
		$order = (int) $this->request->variable('order', 0);
372
		$cat_nb = (int) $this->request->variable('cat_nb', 0);
373
374
		$sql = 'SELECT lang_id, lang_iso
375
			FROM ' . LANG_TABLE . "
376
				ORDER BY lang_id ASC";
377
		$result = $this->db->sql_query($sql);
378
		while ($row = $this->db->sql_fetchrow($result))
379
		{
380
			$iso = strtolower($row['lang_iso']);
381
			$lang = (string) $this->request->variable("lang_$iso", '', true);
382
			$sort = (string) $this->request->variable("sort_$iso", '');
383
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
384
385
			if ($name === '')
386
			{
387
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
388
			}
389
			else
390
			{
391
				if ($sort === 'edit')
392
				{
393
					$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");
394
				}
395
				else if ($sort === 'create')
396
				{
397
					$sql_in[] = [
398
						'cat_id'		=> $id,
399
						'cat_order'		=> $order,
400
						'cat_lang'		=> $lang,
401
						'cat_name'		=> $name,
402
						'cat_title'		=> $title,
403
						'cat_nb'		=> $cat_nb,
404
					];
405
				}
406
			}
407
		}
408
		$this->db->sql_freeresult($result);
409
410
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_in);
411
412
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]);
413
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
414
	}
415
416
	private function add_cat()
417
	{
418
		$max = (int) $this->category->get_max_order() + 1;
419
		$sql = 'SELECT lang_local_name, lang_iso
420
			FROM ' . LANG_TABLE . '
421
				ORDER BY lang_id ASC';
422
		$result = $this->db->sql_query($sql);
423
		while ($row = $this->db->sql_fetchrow($result))
424
		{
425
			$this->template->assign_block_vars('categories', [
426
				'CAT_LANG'		=> $row['lang_local_name'],
427
				'CAT_ISO'		=> $row['lang_iso'],
428
				'CAT_ORDER'		=> $max,
429
			]);
430
		}
431
		$this->db->sql_freeresult($result);
432
433
		$this->template->assign_vars([
434
			'IN_CAT_ACTION'		=> true,
435
			'IN_ADD_ACTION'		=> true,
436
			'CAT_ORDER'			=> $max,
437
			'U_BACK'			=> $this->u_action,
438
			'U_ADD_CAT'			=> $this->u_action . '&amp;action=add_cat',
439
		]);
440
	}
441
442
	private function edit_cat($id)
443
	{
444
		// Get total lang id...
445
		$sql = 'SELECT COUNT(lang_id) as total
446
			FROM ' . LANG_TABLE;
447
		$result = $this->db->sql_query($sql);
448
		$total = (int) $this->db->sql_fetchfield('total');
449
		$this->db->sql_freeresult($result);
450
451
		$title = '';
452
		$list_id = [];
453
		$i = $cat_order = $cat_nb = 0;
454
		$sql = $this->db->sql_build_query('SELECT', [
455
			'SELECT'	=> 'l.*, c.*',
456
			'FROM'		=> [LANG_TABLE => 'l'],
457
			'LEFT_JOIN'	=> [
458
				[
459
					'FROM'	=> [$this->smilies_category_table => 'c'],
460
					'ON'	=> 'c.cat_lang = l.lang_iso',
461
				],
462
			],
463
			'WHERE'		=> 'cat_id = ' . $id,
464
			'ORDER_BY'	=> 'lang_id ASC',
465
		]);
466
		$result = $this->db->sql_query($sql);
467
		while ($row = $this->db->sql_fetchrow($result))
468
		{
469
			$this->template->assign_block_vars('category_lang', [
470
				'CAT_LANG'			=> $row['lang_local_name'],
471
				'CAT_ISO'			=> $row['lang_iso'],
472
				'CAT_ORDER'			=> $row['cat_order'],
473
				'CAT_ID'			=> $row['cat_id'],
474
				'CAT_TRANSLATE'		=> $row['cat_name'],
475
				'CAT_SORT'			=> 'edit',
476
			]);
477
			$i++;
478
			$list_id[$i] = $row['lang_id'];
479
			$cat_order = $row['cat_order'];
480
			$title = $row['cat_title'];
481
			$cat_nb = $row['cat_nb'];
482
		}
483
		$this->db->sql_freeresult($result);
484
485
		// Add rows for empty langs in this category
486
		if ($i !== $total)
487
		{
488
			$sql = $this->db->sql_build_query('SELECT', [
489
				'SELECT'	=> '*',
490
				'FROM'		=> [LANG_TABLE => 'l'],
491
				'WHERE'		=> $this->db->sql_in_set('lang_id', $list_id, true, true),
492
			]);
493
			$result = $this->db->sql_query($sql);
494
			while ($row = $this->db->sql_fetchrow($result))
495
			{
496
				$this->template->assign_block_vars('category_lang', [
497
					'CAT_LANG'			=> $row['lang_local_name'],
498
					'CAT_ISO'			=> $row['lang_iso'],
499
					'CAT_ORDER'			=> $cat_order,
500
					'CAT_ID'			=> $id,
501
					'CAT_TRANSLATE'		=> '',
502
					'CAT_SORT'			=> 'create',
503
				]);
504
			}
505
			$this->db->sql_freeresult($result);
506
		}
507
508
		$this->template->assign_vars([
509
			'IN_CAT_ACTION'	=> true,
510
			'CAT_ORDER'		=> $cat_order,
511
			'CAT_NB'		=> $cat_nb,
512
			'CAT_TITLE'		=> $title,
513
			'U_BACK'		=> $this->u_action,
514
			'U_EDIT_CAT'	=> $this->u_action . '&amp;action=edit_cat&amp;id=' . $id,
515
		]);
516
	}
517
518
	private function edit_smiley($id, $start)
519
	{
520
		$lang = $this->user->lang_name;
521
		$sql = $this->db->sql_build_query('SELECT', [
522
			'SELECT'	=> 's.*, c.*',
523
			'FROM'		=> [SMILIES_TABLE => 's'],
524
			'LEFT_JOIN'	=> [
525
				[
526
					'FROM'	=> [$this->smilies_category_table => 'c'],
527
					'ON'	=> "c.cat_id = s.category AND c.cat_lang = '$lang'",
528
				],
529
			],
530
			'WHERE'	=> 's.smiley_id = ' . (int) $id,
531
		]);
532
		$result = $this->db->sql_query($sql);
533
		$row = $this->db->sql_fetchrow($result);
534
535
		$this->template->assign_vars([
536
			'WIDTH'				=> $row['smiley_width'],
537
			'HEIGHT'			=> $row['smiley_height'],
538
			'CODE'				=> $row['code'],
539
			'EMOTION'			=> $row['emotion'],
540
			'CATEGORY'			=> $row['cat_name'],
541
			'EX_CAT'			=> $row['cat_id'],
542
			'SELECT_CATEGORY'	=> $this->category->select_categories($row['cat_id'], false, false),
543
			'IMG_SRC'			=> $this->root_path . $this->config['smilies_path'] . '/' . $row['smiley_url'],
544
			'U_MODIFY'			=> $this->u_action . '&amp;action=modify&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
545
			'U_BACK'			=> $this->u_action,
546
		]);
547
		$this->db->sql_freeresult($result);
548
	}
549
550
	/**
551
	 * Set page url
552
	 *
553
	 * @param string $u_action Custom form action
554
	 * @return null
555
	 * @access public
556
	 */
557
	public function set_page_url($u_action)
558
	{
559
		$this->u_action = $u_action;
560
	}
561
}
562