Passed
Push — 1.3.0 ( 7a4e4b...8f251a )
by Sylver
03:20
created

admin_controller::move_cat()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 27
nc 3
nop 2
dl 0
loc 40
rs 9.488
c 2
b 0
f 0
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
		$switch_order_id = $this->set_order($action, $current_order);
260
		if ($switch_order_id === 0)
261
		{
262
			return;
263
		}
264
265
		$sql = 'UPDATE ' . $this->smilies_category_table . "
266
			SET cat_order = $current_order
267
			WHERE cat_order = $switch_order_id
268
				AND cat_id <> $id";
269
		$this->db->sql_query($sql);
270
		$move_executed = (bool) $this->db->sql_affectedrows();
271
272
		// Only update the other entry too if the previous entry got updated
273
		if ($move_executed)
274
		{
275
			$sql = 'UPDATE ' . $this->smilies_category_table . "
276
				SET cat_order = $switch_order_id
277
				WHERE cat_order = $current_order
278
					AND cat_id = $id";
279
			$this->db->sql_query($sql);
280
		}
281
282
		$this->category->reset_first_cat((int) $current_order, (int) $switch_order_id);
283
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_' . strtoupper($action) . '_CAT', time(), array($title));
284
285
		trigger_error($this->language->lang('SC_MOVE_SUCCESS') . adm_back_link($this->u_action));
286
	}
287
288
	private function set_order($action, $current_order)
289
	{
290
		$switch_order_id = 0;
291
		if ($current_order === 1 && $action === 'move_up')
292
		{
293
			return $switch_order_id;
294
		}
295
296
		$max_order = (int) $this->category->get_max_order();
297
298
		if (($current_order === $max_order) && ($action === 'move_down'))
299
		{
300
			return $switch_order_id;
301
		}
302
303
		// on move_down, switch position with next order_id...
304
		// on move_up, switch position with previous order_id...
305
		$switch_order_id = ($action === 'move_down') ? $current_order + 1 : $current_order - 1;
306
307
		return $switch_order_id;
308
	}
309
310
	private function extract_list_smilies($select, $start)
311
	{
312
		$i = 0;
313
		$cat = -1;
314
		$lang = $this->user->lang_name;
315
		$smilies_count = (int) $this->category->smilies_count($select);
316
317
		if ($select === 0)
318
		{
319
			$sql = $this->db->sql_build_query('SELECT', array(
320
				'SELECT'	=> '*',
321
				'FROM'		=> array(SMILIES_TABLE => ''),
322
				'WHERE'		=> 'category = 0',
323
				'ORDER_BY'	=> 'smiley_order ASC',
324
			));
325
		}
326
		else
327
		{
328
			$sql = $this->db->sql_build_query('SELECT', array(
329
				'SELECT'	=> 's.*, c.*',
330
				'FROM'		=> array(SMILIES_TABLE => 's'),
331
				'LEFT_JOIN'	=> array(
332
					array(
333
						'FROM'	=> array($this->smilies_category_table => 'c'),
334
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
335
					),
336
				),
337
				'WHERE'		=> ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
338
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
339
			));
340
		}
341
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
342
		while ($row = $this->db->sql_fetchrow($result))
343
		{
344
			$row['category'] = isset($row['category']) ? $row['category'] : 0;
345
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
346
347
			$this->template->assign_block_vars('items', array(
348
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
349
				'IMG_SRC'		=> $row['smiley_url'],
350
				'WIDTH'			=> $row['smiley_width'],
351
				'HEIGHT'		=> $row['smiley_height'],
352
				'CODE'			=> $row['code'],
353
				'EMOTION'		=> $row['emotion'],
354
				'CATEGORY'		=> $row['cat_name'],
355
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
356
			));
357
			$i++;
358
359
			// Keep this value in memory
360
			$cat = (int) $row['category'];
361
		}
362
		$this->db->sql_freeresult($result);
363
364
		$this->template->assign_vars(array(
365
			'NB_SMILIES'	=> $smilies_count,
366
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
367
		));
368
369
		$this->pagination->generate_template_pagination($this->u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
370
	}
371
372
	private function delete_cat($id)
373
	{
374
		$id = (int) $id;
375
		$sql = 'SELECT cat_title, cat_order
376
			FROM ' . $this->smilies_category_table . '
377
				WHERE cat_id = ' . $id;
378
		$result = $this->db->sql_query($sql);
379
		$row = $this->db->sql_fetchrow($result);
380
		$title = $row['cat_title'];
381
		$order = $row['cat_order'];
382
		$this->db->sql_freeresult($result);
383
384
		$sql_delete = 'DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id;
385
		$this->db->sql_query($sql_delete);
386
387
		// Decrement orders if needed
388
		$sql_decrement = 'SELECT cat_id, cat_order
389
			FROM ' . $this->smilies_category_table . '
390
				WHERE cat_order > ' . (int) $order;
391
		$result = $this->db->sql_query($sql_decrement);
392
		while ($row = $this->db->sql_fetchrow($result))
393
		{
394
			$new_order = (int) $row['cat_order'] - 1;
395
			$sql_order = 'UPDATE ' . $this->smilies_category_table . '
396
				SET cat_order = ' . $new_order . '
397
					WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order'];
398
			$this->db->sql_query($sql_order);
399
		}
400
		$this->db->sql_freeresult($result);
401
402
		// Reset appropriate smilies category id
403
		$sql_update = 'UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id;
404
		$this->db->sql_query($sql_update);
405
406
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title));
407
408
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
409
	}
410
411
	private function add_category()
412
	{
413
		$title = $this->request->variable('name_' . $this->user->lang_name, '', true);
414
		$cat_order = (int) $this->request->variable('order', 0);
415
		$cat_id = (int) $this->category->get_max_id() + 1;
416
		$sql_in = array();
417
		$i = 0;
418
419
		$sql = 'SELECT lang_id, 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
			$iso = $row['lang_iso'];
426
			$lang = (string) $this->request->variable("lang_$iso", '', true);
427
			$name = (string) $this->request->variable("name_$iso", '', true);
428
			if ($name === '')
429
			{
430
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
431
			}
432
			else
433
			{
434
				$sql_in[$i] = array(
435
					'cat_id'		=> $cat_id,
436
					'cat_order'		=> $cat_order,
437
					'cat_lang'		=> $lang,
438
					'cat_name'		=> $this->category->capitalize($name),
439
					'cat_title'		=> $this->category->capitalize($title),
440
				);
441
			}
442
			$i++;
443
		}
444
445
		for ($j = 0; $j < $i; $j++)
446
		{
447
			$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in[$j]));
448
		}
449
450
		if ($cat_order === 1)
451
		{
452
			$this->config->set('smilies_category_nb', $cat_id);
453
		}
454
455
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), array($title));
456
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
457
	}
458
459
	private function edit_category($id)
460
	{
461
		$id = (int) $id;
462
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
463
464
		$sql = 'SELECT lang_id, lang_iso
465
			FROM ' . LANG_TABLE . "
466
				ORDER BY lang_id ASC";
467
		$result = $this->db->sql_query($sql);
468
		while ($row = $this->db->sql_fetchrow($result))
469
		{
470
			$iso = $row['lang_iso'];
471
			$lang = (string) $this->request->variable("lang_$iso", '', true);
472
			$sort = (string) $this->request->variable("sort_$iso", '');
473
			$order = (int) $this->request->variable('order', 0);
474
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
475
			if ($name === '')
476
			{
477
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
478
			}
479
			else
480
			{
481
				if ($sort === 'edit')
482
				{
483
					$sql = 'UPDATE ' . $this->smilies_category_table . "
484
						SET cat_name = '" . $name . "', cat_title = '" . $title . "'
485
							WHERE cat_lang = '" . $this->db->sql_escape($lang) . "'
486
							AND cat_id = $id";
487
					$this->db->sql_query($sql);
488
				}
489
				else if ($sort === 'create')
490
				{
491
					$sql_in = array(
492
						'cat_id'		=> $id,
493
						'cat_order'		=> $order,
494
						'cat_lang'		=> $lang,
495
						'cat_name'		=> $name,
496
						'cat_title'		=> $title,
497
					);
498
					$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in));
499
				}
500
			}
501
		}
502
503
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), array($title));
504
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
505
	}
506
507
	/**
508
	 * Set page url
509
	 *
510
	 * @param string $u_action Custom form action
511
	 * @return null
512
	 * @access public
513
	 */
514
	public function set_page_url($u_action)
515
	{
516
		$this->u_action = $u_action;
517
	}
518
}
519