Passed
Push — 1.4.0 ( 26901a...ea1ce7 )
by Sylver
02:59
created

admin_controller::adm_edit_cat()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 71
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 51
nc 4
nop 1
dl 0
loc 71
rs 9.069
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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