Passed
Push — 1.3.0 ( 8f251a...b261a4 )
by Sylver
03:15
created

admin_controller   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 412
Duplicated Lines 0 %

Importance

Changes 18
Bugs 0 Features 0
Metric Value
eloc 235
dl 0
loc 412
rs 9.0399
c 18
b 0
f 0
wmc 42

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A add_category() 0 46 5
C acp_categories_config() 0 77 13
A delete_cat() 0 36 2
B acp_smilies_category() 0 64 8
A edit_category() 0 46 5
A set_page_url() 0 3 1
B extract_list_smilies() 0 60 7

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 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->category->adm_edit_smiley($id, $this->u_action, $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
					$cat_id = (int) $this->request->variable('cat_id', 0);
106
					$ex_cat = (int) $this->request->variable('ex_cat', 0);
107
108
					$sql = 'UPDATE ' . SMILIES_TABLE . " SET category = $cat_id WHERE smiley_id = $id";
109
					$this->db->sql_query($sql);
110
111
					// Decrement nb value if wanted
112
					if ($ex_cat !== 0)
113
					{
114
						$sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb - 1 WHERE cat_id = $ex_cat";
115
						$this->db->sql_query($sql);
116
					}
117
					// Increment nb value if wanted
118
					if ($cat_id !== 0)
119
					{
120
						$sql = 'UPDATE ' . $this->smilies_category_table . " SET cat_nb = cat_nb + 1 WHERE cat_id = $cat_id";
121
						$this->db->sql_query($sql);
122
					}
123
124
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
125
				break;
126
			}
127
128
			$this->template->assign_vars(array(
129
				'IN_ACTION'			=> true,
130
			));
131
		}
132
		else
133
		{
134
			$this->extract_list_smilies($select, $start);
135
136
			$this->template->assign_vars(array(
137
				'LIST_CATEGORY'		=> $this->category->select_categories($select, true),
138
				'U_BACK'			=> ($select) ? $this->u_action : '',
139
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
140
			));
141
		}
142
143
		$this->template->assign_vars(array(
144
			'CATEGORIE_SMILIES'		=> true,
145
		));
146
	}
147
148
	public function acp_categories_config()
149
	{
150
		$this->language->add_lang('acp/language');
151
		$mode = (string) $this->request->variable('mode', '');
152
		$action = (string) $this->request->variable('action', '');
153
		$id = (int) $this->request->variable('id', 0);
154
		$form_key = 'sylver35/smiliescat';
155
		add_form_key($form_key);
156
157
		if ($action)
158
		{
159
			if (in_array($action, array('config_cat', 'add_cat', 'edit_cat')) && !check_form_key($form_key))
160
			{
161
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
162
			}
163
164
			switch ($action)
165
			{
166
				case 'config_cat':
167
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
168
169
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
170
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
171
				break;
172
173
				case 'add':
174
					$this->category->adm_add_cat($this->u_action);
175
				break;
176
177
				case 'add_cat':
178
					$this->add_category();
179
				break;
180
181
				case 'edit':
182
					$this->category->adm_edit_cat($id, $this->u_action);
183
				break;
184
185
				case 'edit_cat':
186
					$this->edit_category($id);
187
				break;
188
189
				case 'move_up':
190
				case 'move_down':
191
					$this->move_cat($action, $id);
0 ignored issues
show
Bug introduced by
The method move_cat() does not exist on sylver35\smiliescat\controller\admin_controller. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
					$this->/** @scrutinizer ignore-call */ 
192
            move_cat($action, $id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
				break;
193
194
				case 'delete':
195
					if (confirm_box(true))
196
					{
197
						$this->delete_cat($id);
198
					}
199
					else
200
					{
201
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
202
							'mode'		=> $mode,
203
							'id'		=> $id,
204
							'action'	=> $action,
205
						)));
206
					}
207
				break;
208
				
209
			}
210
211
			$this->template->assign_vars(array(
212
				'IN_ACTION'		=> true,
213
			));
214
		}
215
		else
216
		{
217
			$this->category->adm_list_cat($this->u_action);
218
		}
219
220
		$this->template->assign_vars(array(
221
			'CATEGORIE_CONFIG'		=> true,
222
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
223
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
224
			'U_ADD'					=> $this->u_action . '&amp;action=add',
225
		));
226
	}
227
228
	private function extract_list_smilies($select, $start)
229
	{
230
		$i = 0;
231
		$cat = -1;
232
		$lang = $this->user->lang_name;
233
		$smilies_count = (int) $this->category->smilies_count($select);
234
235
		if ($select === 0)
236
		{
237
			$sql = $this->db->sql_build_query('SELECT', array(
238
				'SELECT'	=> '*',
239
				'FROM'		=> array(SMILIES_TABLE => ''),
240
				'WHERE'		=> 'category = 0',
241
				'ORDER_BY'	=> 'smiley_order ASC',
242
			));
243
		}
244
		else
245
		{
246
			$sql = $this->db->sql_build_query('SELECT', array(
247
				'SELECT'	=> 's.*, c.*',
248
				'FROM'		=> array(SMILIES_TABLE => 's'),
249
				'LEFT_JOIN'	=> array(
250
					array(
251
						'FROM'	=> array($this->smilies_category_table => 'c'),
252
						'ON'	=> "cat_id = category AND cat_lang = '$lang'",
253
					),
254
				),
255
				'WHERE'		=> ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''",
256
				'ORDER_BY'	=> 'cat_order ASC, smiley_order ASC',
257
			));
258
		}
259
		$result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start);
260
		while ($row = $this->db->sql_fetchrow($result))
261
		{
262
			$row['category'] = isset($row['category']) ? $row['category'] : 0;
263
			$row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT');
264
265
			$this->template->assign_block_vars('items', array(
266
				'SPACER_CAT'	=> ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '',
267
				'IMG_SRC'		=> $row['smiley_url'],
268
				'WIDTH'			=> $row['smiley_width'],
269
				'HEIGHT'		=> $row['smiley_height'],
270
				'CODE'			=> $row['code'],
271
				'EMOTION'		=> $row['emotion'],
272
				'CATEGORY'		=> $row['cat_name'],
273
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['smiley_id'] . '&amp;start=' . $start,
274
			));
275
			$i++;
276
277
			// Keep this value in memory
278
			$cat = (int) $row['category'];
279
		}
280
		$this->db->sql_freeresult($result);
281
282
		$this->template->assign_vars(array(
283
			'NB_SMILIES'	=> $smilies_count,
284
			'U_SMILIES'		=> $this->root_path . $this->config['smilies_path'] . '/',
285
		));
286
287
		$this->pagination->generate_template_pagination($this->u_action . '&amp;select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start);
288
	}
289
290
	private function delete_cat($id)
291
	{
292
		$id = (int) $id;
293
		$sql = 'SELECT cat_title, cat_order
294
			FROM ' . $this->smilies_category_table . '
295
				WHERE cat_id = ' . $id;
296
		$result = $this->db->sql_query($sql);
297
		$row = $this->db->sql_fetchrow($result);
298
		$title = $row['cat_title'];
299
		$order = $row['cat_order'];
300
		$this->db->sql_freeresult($result);
301
302
		$sql_delete = 'DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id;
303
		$this->db->sql_query($sql_delete);
304
305
		// Decrement orders if needed
306
		$sql_decrement = 'SELECT cat_id, cat_order
307
			FROM ' . $this->smilies_category_table . '
308
				WHERE cat_order > ' . (int) $order;
309
		$result = $this->db->sql_query($sql_decrement);
310
		while ($row = $this->db->sql_fetchrow($result))
311
		{
312
			$new_order = (int) $row['cat_order'] - 1;
313
			$sql_order = 'UPDATE ' . $this->smilies_category_table . '
314
				SET cat_order = ' . $new_order . '
315
					WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order'];
316
			$this->db->sql_query($sql_order);
317
		}
318
		$this->db->sql_freeresult($result);
319
320
		// Reset appropriate smilies category id
321
		$sql_update = 'UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id;
322
		$this->db->sql_query($sql_update);
323
324
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title));
325
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
326
	}
327
328
	private function add_category()
329
	{
330
		$title = $this->request->variable('name_' . $this->user->lang_name, '', true);
331
		$cat_order = (int) $this->request->variable('order', 0);
332
		$cat_id = (int) $this->category->get_max_id() + 1;
333
		$sql_in = array();
334
		$i = 0;
335
336
		$sql = 'SELECT lang_id, lang_iso
337
			FROM ' . LANG_TABLE . "
338
				ORDER BY lang_id ASC";
339
		$result = $this->db->sql_query($sql);
340
		while ($row = $this->db->sql_fetchrow($result))
341
		{
342
			$iso = $row['lang_iso'];
343
			$lang = (string) $this->request->variable("lang_$iso", '', true);
344
			$name = (string) $this->request->variable("name_$iso", '', true);
345
			if ($name === '')
346
			{
347
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
348
			}
349
			else
350
			{
351
				$sql_in[$i] = array(
352
					'cat_id'		=> $cat_id,
353
					'cat_order'		=> $cat_order,
354
					'cat_lang'		=> $lang,
355
					'cat_name'		=> $this->category->capitalize($name),
356
					'cat_title'		=> $this->category->capitalize($title),
357
				);
358
			}
359
			$i++;
360
		}
361
362
		for ($j = 0; $j < $i; $j++)
363
		{
364
			$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in[$j]));
365
		}
366
367
		if ($cat_order === 1)
368
		{
369
			$this->config->set('smilies_category_nb', $cat_id);
370
		}
371
372
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), array($title));
373
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
374
	}
375
376
	private function edit_category($id)
377
	{
378
		$id = (int) $id;
379
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
380
381
		$sql = 'SELECT lang_id, lang_iso
382
			FROM ' . LANG_TABLE . "
383
				ORDER BY lang_id ASC";
384
		$result = $this->db->sql_query($sql);
385
		while ($row = $this->db->sql_fetchrow($result))
386
		{
387
			$iso = $row['lang_iso'];
388
			$lang = (string) $this->request->variable("lang_$iso", '', true);
389
			$sort = (string) $this->request->variable("sort_$iso", '');
390
			$order = (int) $this->request->variable('order', 0);
391
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
392
			if ($name === '')
393
			{
394
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
395
			}
396
			else
397
			{
398
				if ($sort === 'edit')
399
				{
400
					$sql = 'UPDATE ' . $this->smilies_category_table . "
401
						SET cat_name = '" . $name . "', cat_title = '" . $title . "'
402
							WHERE cat_lang = '" . $this->db->sql_escape($lang) . "'
403
							AND cat_id = $id";
404
					$this->db->sql_query($sql);
405
				}
406
				else if ($sort === 'create')
407
				{
408
					$sql_in = array(
409
						'cat_id'		=> $id,
410
						'cat_order'		=> $order,
411
						'cat_lang'		=> $lang,
412
						'cat_name'		=> $name,
413
						'cat_title'		=> $title,
414
					);
415
					$this->db->sql_query('INSERT INTO ' . $this->smilies_category_table . $this->db->sql_build_array('INSERT', $sql_in));
416
				}
417
			}
418
		}
419
420
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), array($title));
421
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
422
	}
423
424
	/**
425
	 * Set page url
426
	 *
427
	 * @param string $u_action Custom form action
428
	 * @return null
429
	 * @access public
430
	 */
431
	public function set_page_url($u_action)
432
	{
433
		$this->u_action = $u_action;
434
	}
435
}
436