Passed
Push — 1.5.0 ( 8846b6...5b3637 )
by Sylver
03:05
created

admin_controller   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 539
Duplicated Lines 0 %

Importance

Changes 21
Bugs 0 Features 0
Metric Value
eloc 310
c 21
b 0
f 0
dl 0
loc 539
rs 6.4799
wmc 54

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A modify_smiley() 0 15 2
A add_category() 0 52 5
A update_cat_smiley() 0 19 5
B acp_categories_config() 0 68 11
A delete_cat() 0 30 2
A edit_cat() 0 73 4
A add_cat() 0 23 2
B acp_smilies_category() 0 60 9
A edit_category() 0 47 5
B extract_list_smilies() 0 58 7
A set_page_url() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like admin_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use admin_controller, and based on these observations, apply Extract Interface, too.

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