Completed
Branch 1.3.0 (40ec5c)
by Sylver
04:31
created

admin_controller::edit_category()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 46
rs 9.1288
1
<?php
2
/**
3
*
4
* @package		Breizh Smilies Categories Extension
5
* @copyright	(c) 2020 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()
82
	{
83
		$this->language->add_lang('acp/posting');
84
		$action = (string) $this->request->variable('action', '');
85
		$start = (int) $this->request->variable('start', 0);
86
		$select = (int) $this->request->variable('select', -1);
87
		$id = (int) $this->request->variable('id', -1);
88
		$form_key = 'sylver35/smiliescat';
89
		add_form_key($form_key);
90
91
		if ($action)
92
		{
93
			switch ($action)
94
			{
95
				case 'edit':
96
97
					$this->category->adm_edit_smiley($id, $this->u_action, $start);
98
99
				break;
100
101
				case 'modify':
102
103
					if (!check_form_key($form_key))
104
					{
105
						trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
106
					}
107
108
					$cat_id = (int) $this->request->variable('cat_id', 0);
109
					$ex_cat = (int) $this->request->variable('ex_cat', 0);
110
111
					$sql = 'UPDATE ' . SMILIES_TABLE . " SET category = $cat_id WHERE smiley_id = $id";
112
					$this->db->sql_query($sql);
113
114
					// Decrement nb value if wanted
115
					if ($ex_cat !== 0)
116
					{
117
						$sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb - 1 WHERE cat_id = $ex_cat";
118
						$this->db->sql_query($sql);
119
					}
120
					// Increment nb value if wanted
121
					if ($cat_id !== 0)
122
					{
123
						$sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb + 1 WHERE cat_id = $cat_id";
124
						$this->db->sql_query($sql);
125
					}
126
127
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
128
129
				break;
130
			}
131
132
			$this->template->assign_vars(array(
133
				'IN_ACTION'			=> true,
134
			));
135
		}
136
		else
137
		{
138
			$this->extract_list_smilies($select, $start);
139
140
			$this->template->assign_vars(array(
141
				'LIST_CATEGORY'		=> $this->category->select_categories($select, true),
142
				'U_BACK'			=> ($select) ? $this->u_action : '',
143
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
144
			));
145
		}
146
147
		$this->template->assign_vars(array(
148
			'CATEGORIE_SMILIES'		=> true,
149
		));
150
	}
151
152
	public function acp_categories_config()
153
	{
154
		$this->language->add_lang('acp/language');
155
		$mode = (string) $this->request->variable('mode', '');
156
		$action = (string) $this->request->variable('action', '');
157
		$id = (int) $this->request->variable('id', 0);
158
		$form_key = 'sylver35/smiliescat';
159
		add_form_key($form_key);
160
161
		if ($action)
162
		{
163
			if (in_array($action, array('config_cat', 'add_cat', 'edit_cat')) && !check_form_key($form_key))
164
			{
165
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
166
			}
167
168
			switch ($action)
169
			{
170
				case 'config_cat':
171
172
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
173
174
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
175
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
176
177
				break;
178
179
				case 'add':
180
181
					$this->category->adm_add_cat($this->u_action);
182
183
				break;
184
185
				case 'add_cat':
186
187
					$this->add_category();
188
189
				break;
190
191
				case 'edit':
192
193
					$this->category->adm_edit_cat($id, $this->u_action);
194
195
				break;
196
197
				case 'edit_cat':
198
199
					$this->edit_category($id);
200
201
				break;
202
203
				case 'move_up':
204
				case 'move_down':
205
					
206
					$this->move_cat($action, $id);
207
208
				break;
209
210
				case 'delete':
211
212
					if (confirm_box(true))
213
					{
214
						$this->delete_cat($id);
215
					}
216
					else
217
					{
218
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
219
							'mode'		=> $mode,
220
							'id'		=> $id,
221
							'action'	=> 'delete',
222
						)));
223
					}
224
225
				break;
226
				
227
			}
228
229
			$this->template->assign_vars(array(
230
				'IN_ACTION'		=> true,
231
			));
232
		}
233
		else
234
		{
235
			$this->category->adm_list_cat($this->u_action);
236
		}
237
238
		$this->template->assign_vars(array(
239
			'CATEGORIE_CONFIG'		=> true,
240
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
241
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
242
			'U_ADD'					=> $this->u_action . '&amp;action=add',
243
		));
244
	}
245
246
	private function move_cat($action, $id)
247
	{
248
		$id = (int) $id;
249
		// Get current order id and title...
250
		$sql = 'SELECT cat_order, cat_title
251
			FROM ' . $this->smilies_category_table . "
252
				WHERE cat_id = $id";
253
		$result = $this->db->sql_query($sql);
254
		$row = $this->db->sql_fetchrow($result);
255
		$current_order = (int) $row['cat_order'];
256
		$title = $row['cat_title'];
257
		$this->db->sql_freeresult($result);
258
259
		if ($current_order === 1 && $action === 'move_up')
260
		{
261
			return;
262
		}
263
264
		$max_order = (int) $this->category->get_max_order();
265
266
		if (($current_order === $max_order) && ($action === 'move_down'))
267
		{
268
			return;
269
		}
270
271
		// on move_down, switch position with next order_id...
272
		// on move_up, switch position with previous order_id...
273
		$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1;
274
275
		$sql = 'UPDATE ' . $this->smilies_category_table . "
276
			SET cat_order = $current_order
277
			WHERE cat_order = $switch_order_id
278
				AND cat_id <> $id";
279
		$this->db->sql_query($sql);
280
		$move_executed = (bool) $this->db->sql_affectedrows();
281
282
		// Only update the other entry too if the previous entry got updated
283
		if ($move_executed)
284
		{
285
			$sql = 'UPDATE ' . $this->smilies_category_table . "
286
				SET cat_order = $switch_order_id
287
				WHERE cat_order = $current_order
288
					AND cat_id = $id";
289
			$this->db->sql_query($sql);
290
		}
291
292
		$this->category->reset_first_cat((int) $current_order, (int) $switch_order_id);
293
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), array($title));
294
295
		if ($this->request->is_ajax())
296
		{
297
			trigger_error($this->language->lang('SC_MOVE_SUCCESS_AJAX') . adm_back_link($this->u_action));
298
			$json_response = new \phpbb\json_response;
299
			$json_response->send(array(
300
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
301
				'MESSAGE_TEXT'	=> $this->language->lang('SC_MOVE_SUCCESS_AJAX'),
302
				'REFRESH_DATA'	=> array('time' => 1),
303
			));
304
		}
305
		else
306
		{
307
			trigger_error($this->language->lang('SC_MOVE_SUCCESS') . adm_back_link($this->u_action));
308
		}
309
	}
310
311
	private function extract_list_smilies($select, $start)
312
	{
313
		$i = 0;
314
		$cat = -1;
315
		$lang = $this->user->lang_name;
316
		$smilies_count = (int) $this->category->smilies_count($select);
317
318
		if ($select === 0)
319
		{
320
			$sql = $this->db->sql_build_query('SELECT', array(
321
				'SELECT'	=> '*',
322
				'FROM'		=> array(SMILIES_TABLE => ''),
323
				'WHERE'		=> 'category = 0',
324
				'ORDER_BY'	=> 'smiley_order ASC',
325
			));
326
		}
327
		else
328
		{
329
			$sql = $this->db->sql_build_query('SELECT', array(
330
				'SELECT'	=> 's.*, c.*',
331
				'FROM'		=> array(SMILIES_TABLE => 's'),
332
				'LEFT_JOIN'	=> array(
333
					array(
334
						'FROM'	=> array($this->smilies_category_table => 'c'),
335
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
336
					),
337
				),
338
				'WHERE'		=> ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
339
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
340
			));
341
		}
342
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
343
		while ($row = $this->db->sql_fetchrow($result))
344
		{
345
			$row['category'] = isset($row['category']) ? $row['category'] : 0;
346
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
347
348
			$this->template->assign_block_vars('items', array(
349
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
350
				'IMG_SRC'		=> $row['smiley_url'],
351
				'WIDTH'			=> $row['smiley_width'],
352
				'HEIGHT'		=> $row['smiley_height'],
353
				'CODE'			=> $row['code'],
354
				'EMOTION'		=> $row['emotion'],
355
				'CATEGORY'		=> $row['cat_name'],
356
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
357
			));
358
			$i++;
359
360
			// Keep this value in memory
361
			$cat = (int) $row['category'];
362
		}
363
		$this->db->sql_freeresult($result);
364
365
		$this->template->assign_vars(array(
366
			'NB_SMILIES'	=> $smilies_count,
367
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
368
		));
369
370
		$this->pagination->generate_template_pagination($this->u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
371
	}
372
373
	private function delete_cat($id)
374
	{
375
		$id = (int) $id;
376
		$sql = 'SELECT cat_title, cat_order
377
			FROM ' . $this->smilies_category_table . "
378
				WHERE cat_id = $id";
379
		$result = $this->db->sql_query($sql);
380
		$row = $this->db->sql_fetchrow($result);
381
		$title = $row['cat_title'];
382
		$order = $row['cat_order'];
383
		$this->db->sql_freeresult($result);
384
385
		$sql_delete = 'DELETE FROM ' . $this->smilies_category_table . " WHERE cat_id = $id";
386
		$this->db->sql_query($sql_delete);
387
388
		// Decrement orders if needed
389
		$sql_decrement = 'SELECT cat_id, cat_order
390
			FROM ' . $this->smilies_category_table . "
391
				WHERE cat_order > $order";
392
		$result = $this->db->sql_query($sql_decrement);
393
		while ($row = $this->db->sql_fetchrow($result))
394
		{
395
			$new_order = $row['cat_order'] - 1;
396
			$sql_order = 'UPDATE ' . $this->smilies_category_table . '
397
				SET cat_order = ' . $new_order . '
398
					WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order'];
399
			$this->db->sql_query($sql_order);
400
		}
401
		$this->db->sql_freeresult($result);
402
403
		// Reset appropriate smilies category id
404
		$sql_update = 'UPDATE ' . SMILIES_TABLE . " SET category = 0 WHERE category = $id";
405
		$this->db->sql_query($sql_update);
406
407
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title));
408
409
		if ($this->request->is_ajax())
410
		{
411
			trigger_error($this->language->lang('SC_DELETE_SUCCESS_AJAX') . adm_back_link($this->u_action));
412
			$json_response = new \phpbb\json_response;
413
			$json_response->send(array(
414
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
415
				'MESSAGE_TEXT'	=> $this->language->lang('SC_DELETE_SUCCESS_AJAX'),
416
				'REFRESH_DATA'	=> array('time' => 2),
417
			));
418
		}
419
		else
420
		{
421
			trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
422
		}
423
	}
424
425
	private function add_category()
426
	{
427
		$title = $this->request->variable('name_' . $this->user->lang_name, '', true);
428
		$cat_order = (int) $this->request->variable('order', 0);
429
		$cat_id = (int) $this->category->get_max_id() + 1;
430
		$sql_in = array();
431
		$i = 0;
432
433
		$sql = 'SELECT lang_id, lang_iso
434
			FROM ' . LANG_TABLE . "
435
				ORDER BY lang_id ASC";
436
		$result = $this->db->sql_query($sql);
437
		while ($row = $this->db->sql_fetchrow($result))
438
		{
439
			$iso = $row['lang_iso'];
440
			$lang = (string) $this->request->variable("lang_$iso", '', true);
441
			$name = (string) $this->request->variable("name_$iso", '', true);
442
			if ($name === '')
443
			{
444
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
445
			}
446
			else
447
			{
448
				$sql_in[$i] = array(
449
					'cat_id'		=> $cat_id,
450
					'cat_order'		=> $cat_order,
451
					'cat_lang'		=> $lang,
452
					'cat_name'		=> $this->category->capitalize($name),
453
					'cat_title'		=> $this->category->capitalize($title),
454
				);
455
			}
456
			$i++;
457
		}
458
459
		for ($j = 0; $j < $i; $j++)
460
		{
461
			$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in[$j]));
462
		}
463
464
		if ($cat_order === 1)
465
		{
466
			$this->config->set('smilies_category_nb', $cat_id);
467
		}
468
469
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), array($title));
470
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
471
	}
472
473
	private function edit_category($id)
474
	{
475
		$id = (int) $id;
476
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
477
478
		$sql = 'SELECT lang_id, lang_iso
479
			FROM ' . LANG_TABLE . "
480
				ORDER BY lang_id ASC";
481
		$result = $this->db->sql_query($sql);
482
		while ($row = $this->db->sql_fetchrow($result))
483
		{
484
			$iso = $row['lang_iso'];
485
			$lang = (string) $this->request->variable("lang_$iso", '', true);
486
			$sort = (string) $this->request->variable("sort_$iso", '');
487
			$order = (int) $this->request->variable('order', 0);
488
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
489
			if ($name === '')
490
			{
491
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
492
			}
493
			else
494
			{
495
				if ($sort === 'edit')
496
				{
497
					$sql = 'UPDATE ' . $this->smilies_category_table . "
498
						SET cat_name = '" . $name . "', cat_title = '" . $title . "'
499
							WHERE cat_lang = '" . $this->db->sql_escape($lang) . "'
500
							AND cat_id = $id";
501
					$this->db->sql_query($sql);
502
				}
503
				else if ($sort === 'create')
504
				{
505
					$sql_in = array(
506
						'cat_id'		=> $id,
507
						'cat_order'		=> $order,
508
						'cat_lang'		=> $lang,
509
						'cat_name'		=> $name,
510
						'cat_title'		=> $title,
511
					);
512
					$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in));
513
				}
514
			}
515
		}
516
517
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), array($title));
518
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
519
	}
520
521
	/**
522
	 * Set page url
523
	 *
524
	 * @param string $u_action Custom form action
525
	 * @return null
526
	 * @access public
527
	 */
528
	public function set_page_url($u_action)
529
	{
530
		$this->u_action = $u_action;
531
	}
532
}
533