Cancelled
Push — 1.5.0 ( 5b3637...dfe7c0 )
by Sylver
03:52
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 12
dl 0
loc 14
rs 9.8666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 sylver35\smiliescat\core\smiley;
14
use phpbb\config\config;
15
use phpbb\db\driver\driver_interface as db;
16
use phpbb\pagination;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\language\language;
21
use phpbb\log\log;
22
23
class admin_controller
24
{
25
	/* @var \sylver35\smiliescat\core\category */
26
	protected $category;
27
28
	/* @var \sylver35\smiliescat\core\smiley */
29
	protected $smiley;
30
31
	/** @var \phpbb\config\config */
32
	protected $config;
33
34
	/** @var \phpbb\db\driver\driver_interface */
35
	protected $db;
36
37
	/** @var \phpbb\pagination */
38
	protected $pagination;
39
40
	/** @var \phpbb\request\request */
41
	protected $request;
42
43
	/** @var \phpbb\template\template */
44
	protected $template;
45
46
	/** @var \phpbb\user */
47
	protected $user;
48
49
	/** @var \phpbb\language\language */
50
	protected $language;
51
52
	/** @var \phpbb\log\log */
53
	protected $log;
54
55
	/** @var string phpBB root path */
56
	protected $root_path;
57
58
	/** @var string Custom form action */
59
	protected $u_action;
60
61
	/**
62
	 * The database tables
63
	 *
64
	 * @var string */
65
	protected $smilies_category_table;
66
67
	/**
68
	 * Constructor
69
	 */
70
	public function __construct(category $category, smiley $smiley, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table)
71
	{
72
		$this->category = $category;
73
		$this->smiley = $smiley;
74
		$this->config = $config;
75
		$this->db = $db;
76
		$this->pagination = $pagination;
77
		$this->request = $request;
78
		$this->template = $template;
79
		$this->user = $user;
80
		$this->language = $language;
81
		$this->log = $log;
82
		$this->root_path = $root_path;
83
		$this->smilies_category_table = $smilies_category_table;
84
	}
85
86
	public function acp_smilies_category($id, $action)
87
	{
88
		$this->language->add_lang('acp/posting');
89
		$start = (int) $this->request->variable('start', 0);
90
		$select = (int) $this->request->variable('select', -1);
91
		$cat_id = (int) $this->request->variable('cat_id', 0);
92
		$ex_cat = (int) $this->request->variable('ex_cat', 0);
93
		$list = $this->request->variable('list', [0]);
94
		$form_key = 'sylver35/smiliescat';
95
		add_form_key($form_key);
96
97
		if ($action)
98
		{
99
			switch ($action)
100
			{
101
				case 'edit':
102
					$this->smiley->edit_smiley($id, $start, $this->u_action);
103
				break;
104
105
				case 'edit_multi':
106
					$list = $this->request->variable('mark', [0]);
107
					$this->smiley->edit_multi_smiley($list, $start, $this->u_action);
108
				break;
109
110
				case 'modify':
111
					if (!check_form_key($form_key))
112
					{
113
						trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
114
					}
115
116
					$this->smiley->modify_smiley($id, $cat_id, $ex_cat);
117
					trigger_error($this->language->lang('SMILIES_EDITED', 1) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
118
				break;
119
120
				case 'modify_list':
121
					foreach ($list as $smiley)
122
					{
123
						$this->smiley->modify_smiley($smiley, $cat_id);
124
					}
125
					trigger_error($this->language->lang('SMILIES_EDITED', count($list)) . adm_back_link($this->u_action . '&amp;start=' . $start . '#acp_smilies_category'));
126
				break;
127
			}
128
129
			$this->template->assign_vars([
130
				'IN_ACTION' => true,
131
			]);
132
		}
133
		else
134
		{
135
			$this->smiley->extract_list_smilies($select, $start, $this->u_action);
136
137
			$this->template->assign_vars([
138
				'LIST_CATEGORY'		=> $this->smiley->select_categories($select, true, true),
139
				'U_SELECT_CAT'		=> $this->u_action . '&amp;select=' . $select,
140
				'U_BACK'			=> $this->u_action,
141
			]);
142
		}
143
144
		$this->template->assign_vars([
145
			'CATEGORIE_SMILIES'	=> true,
146
		]);
147
	}
148
149
	public function acp_categories_config($id, $action, $mode)
150
	{
151
		$this->language->add_lang('acp/language');
152
		$form_key = 'sylver35/smiliescat';
153
		add_form_key($form_key);
154
155
		if ($action)
156
		{
157
			if (in_array($action, ['config_cat', 'add_cat', 'edit_cat']) && !check_form_key($form_key))
158
			{
159
				trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
160
			}
161
162
			switch ($action)
163
			{
164
				case 'config_cat':
165
					$this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15));
166
167
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time());
168
					trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
169
				break;
170
171
				case 'add':
172
					$this->add_cat();
173
				break;
174
175
				case 'add_cat':
176
					$this->add_category();
177
				break;
178
179
				case 'edit':
180
					$this->edit_cat((int) $id);
181
				break;
182
183
				case 'edit_cat':
184
					$this->edit_category((int) $id);
185
				break;
186
187
				case 'delete':
188
					if (confirm_box(true))
189
					{
190
						$this->delete_cat((int) $id);
191
					}
192
					else
193
					{
194
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([
195
							'mode'		=> $mode,
196
							'id'		=> $id,
197
							'action'	=> $action,
198
						]));
199
					}
200
				break;
201
			}
202
203
			$this->template->assign_vars([
204
				'IN_ACTION'	=> true,
205
			]);
206
		}
207
		else
208
		{
209
			$this->category->adm_list_cat($this->u_action);
210
		}
211
212
		$this->template->assign_vars([
213
			'CATEGORIE_CONFIG'		=> true,
214
			'SMILIES_PER_PAGE_CAT'	=> $this->config['smilies_per_page_cat'],
215
			'U_ACTION_CONFIG'		=> $this->u_action . '&amp;action=config_cat',
216
			'U_ADD'					=> $this->u_action . '&amp;action=add',
217
		]);
218
	}
219
220
	private function delete_cat($id)
221
	{
222
		$sql = 'SELECT cat_title, cat_order
223
			FROM ' . $this->smilies_category_table . '
224
				WHERE cat_id = ' . $id;
225
		$result = $this->db->sql_query($sql);
226
		$row = $this->db->sql_fetchrow($result);
227
		$title = $row['cat_title'];
228
		$order = (int) $row['cat_order'];
229
		$this->db->sql_freeresult($result);
230
231
		$this->db->sql_query('DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id);
232
233
		// Decrement orders if needed
234
		$sql_decrement = 'SELECT cat_id, cat_order
235
			FROM ' . $this->smilies_category_table . '
236
				WHERE cat_order > ' . $order;
237
		$result = $this->db->sql_query($sql_decrement);
238
		while ($row = $this->db->sql_fetchrow($result))
239
		{
240
			$new_order = (int) $row['cat_order'] - 1;
241
			$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']);
242
		}
243
		$this->db->sql_freeresult($result);
244
245
		// Reset appropriate smilies category id
246
		$this->db->sql_query('UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id);
247
248
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), [$title]);
249
		trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action));
250
	}
251
252
	private function add_category()
253
	{
254
		$sql_ary = [];
255
		$title = (string) $this->request->variable('name_' . $this->user->lang_name, '', true);
256
		$cat_order = (int) $this->request->variable('order', 0);
257
		$cat_id = (int) $this->category->get_max_id() + 1;
258
259
		$sql = 'SELECT lang_id, lang_iso
260
			FROM ' . LANG_TABLE . "
261
				ORDER BY lang_id ASC";
262
		$result = $this->db->sql_query($sql);
263
		while ($row = $this->db->sql_fetchrow($result))
264
		{
265
			$iso = strtolower($row['lang_iso']);
266
			$lang = (string) $this->request->variable("lang_$iso", '', true);
267
			$name = (string) $this->request->variable("name_$iso", '', true);
268
			if ($name === '')
269
			{
270
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=add'), E_USER_WARNING);
271
			}
272
			else
273
			{
274
				$sql_ary[] = [
275
					'cat_id'		=> $cat_id,
276
					'cat_order'		=> $cat_order,
277
					'cat_lang'		=> $lang,
278
					'cat_name'		=> $this->category->capitalize($name),
279
					'cat_title'		=> $this->category->capitalize($title),
280
					'cat_nb'		=> 0,
281
				];
282
			}
283
		}
284
		$this->db->sql_freeresult($result);
285
286
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_ary);
287
288
		if ($cat_order === 1)
289
		{
290
			$sql = 'SELECT cat_id, cat_nb
291
				FROM ' . $this->smilies_category_table . '
292
					WHERE cat_order = 1';
293
			$result = $this->db->sql_query_limit($sql, 1);
294
			$row = $this->db->sql_fetchrow($result);
295
			if ($row['cat_nb'] > 0)
296
			{
297
				$this->config->set('smilies_category_nb', $row['cat_id']);
298
			}
299
			$this->db->sql_freeresult($result);
300
		}
301
302
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_ADD_CAT', time(), [$title]);
303
		trigger_error($this->language->lang('SC_CREATE_SUCCESS') . adm_back_link($this->u_action));
304
	}
305
306
	private function edit_category($id)
307
	{
308
		$sql_in = [];
309
		$title = $this->category->capitalize($this->request->variable('name_' . $this->user->lang_name, '', true));
310
		$order = (int) $this->request->variable('order', 0);
311
		$cat_nb = (int) $this->request->variable('cat_nb', 0);
312
313
		$sql = 'SELECT lang_id, lang_iso
314
			FROM ' . LANG_TABLE . "
315
				ORDER BY lang_id ASC";
316
		$result = $this->db->sql_query($sql);
317
		while ($row = $this->db->sql_fetchrow($result))
318
		{
319
			$iso = strtolower($row['lang_iso']);
320
			$lang = (string) $this->request->variable("lang_$iso", '', true);
321
			$sort = (string) $this->request->variable("sort_$iso", '');
322
			$name = $this->category->capitalize($this->request->variable("name_$iso", '', true));
323
324
			if ($name === '')
325
			{
326
				trigger_error($this->language->lang('SC_CATEGORY_ERROR') . adm_back_link($this->u_action . '&amp;action=edit&amp;id=' . $id), E_USER_WARNING);
327
			}
328
			else
329
			{
330
				if ($sort === 'edit')
331
				{
332
					$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");
333
				}
334
				else if ($sort === 'create')
335
				{
336
					$sql_in[] = [
337
						'cat_id'		=> $id,
338
						'cat_order'		=> $order,
339
						'cat_lang'		=> $lang,
340
						'cat_name'		=> $name,
341
						'cat_title'		=> $title,
342
						'cat_nb'		=> $cat_nb,
343
					];
344
				}
345
			}
346
		}
347
		$this->db->sql_freeresult($result);
348
349
		$this->db->sql_multi_insert($this->smilies_category_table, $sql_in);
350
351
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_EDIT_CAT', time(), [$title]);
352
		trigger_error($this->language->lang('SC_EDIT_SUCCESS') . adm_back_link($this->u_action));
353
	}
354
355
	private function add_cat()
356
	{
357
		$max = (int) $this->category->get_max_order() + 1;
358
		$sql = 'SELECT lang_local_name, lang_iso
359
			FROM ' . LANG_TABLE . '
360
				ORDER BY lang_id ASC';
361
		$result = $this->db->sql_query($sql);
362
		while ($row = $this->db->sql_fetchrow($result))
363
		{
364
			$this->template->assign_block_vars('categories', [
365
				'CAT_LANG'		=> $row['lang_local_name'],
366
				'CAT_ISO'		=> $row['lang_iso'],
367
				'CAT_ORDER'		=> $max,
368
			]);
369
		}
370
		$this->db->sql_freeresult($result);
371
372
		$this->template->assign_vars([
373
			'IN_CAT_ACTION'		=> true,
374
			'IN_ADD_ACTION'		=> true,
375
			'CAT_ORDER'			=> $max,
376
			'U_BACK'			=> $this->u_action,
377
			'U_ADD_CAT'			=> $this->u_action . '&amp;action=add_cat',
378
		]);
379
	}
380
381
	private function edit_cat($id)
382
	{
383
		// Get total lang id...
384
		$sql = 'SELECT COUNT(lang_id) as total
385
			FROM ' . LANG_TABLE;
386
		$result = $this->db->sql_query($sql);
387
		$total = (int) $this->db->sql_fetchfield('total');
388
		$this->db->sql_freeresult($result);
389
390
		$title = '';
391
		$list_id = [];
392
		$i = $cat_order = $cat_nb = 0;
393
		$sql = $this->db->sql_build_query('SELECT', [
394
			'SELECT'	=> 'l.*, c.*',
395
			'FROM'		=> [LANG_TABLE => 'l'],
396
			'LEFT_JOIN'	=> [
397
				[
398
					'FROM'	=> [$this->smilies_category_table => 'c'],
399
					'ON'	=> 'c.cat_lang = l.lang_iso',
400
				],
401
			],
402
			'WHERE'		=> 'cat_id = ' . $id,
403
			'ORDER_BY'	=> 'lang_id ASC',
404
		]);
405
		$result = $this->db->sql_query($sql);
406
		while ($row = $this->db->sql_fetchrow($result))
407
		{
408
			$this->template->assign_block_vars('category_lang', [
409
				'CAT_LANG'			=> $row['lang_local_name'],
410
				'CAT_ISO'			=> $row['lang_iso'],
411
				'CAT_ORDER'			=> $row['cat_order'],
412
				'CAT_ID'			=> $row['cat_id'],
413
				'CAT_TRANSLATE'		=> $row['cat_name'],
414
				'CAT_SORT'			=> 'edit',
415
			]);
416
			$i++;
417
			$list_id[$i] = $row['lang_id'];
418
			$cat_order = $row['cat_order'];
419
			$title = $row['cat_title'];
420
			$cat_nb = $row['cat_nb'];
421
		}
422
		$this->db->sql_freeresult($result);
423
424
		// Add rows for empty langs in this category
425
		if ($i !== $total)
426
		{
427
			$sql = $this->db->sql_build_query('SELECT', [
428
				'SELECT'	=> '*',
429
				'FROM'		=> [LANG_TABLE => 'l'],
430
				'WHERE'		=> $this->db->sql_in_set('lang_id', $list_id, true, true),
431
			]);
432
			$result = $this->db->sql_query($sql);
433
			while ($row = $this->db->sql_fetchrow($result))
434
			{
435
				$this->template->assign_block_vars('category_lang', [
436
					'CAT_LANG'			=> $row['lang_local_name'],
437
					'CAT_ISO'			=> $row['lang_iso'],
438
					'CAT_ORDER'			=> $cat_order,
439
					'CAT_ID'			=> $id,
440
					'CAT_TRANSLATE'		=> '',
441
					'CAT_SORT'			=> 'create',
442
				]);
443
			}
444
			$this->db->sql_freeresult($result);
445
		}
446
447
		$this->template->assign_vars([
448
			'IN_CAT_ACTION'	=> true,
449
			'CAT_ORDER'		=> $cat_order,
450
			'CAT_NB'		=> $cat_nb,
451
			'CAT_TITLE'		=> $title,
452
			'U_BACK'		=> $this->u_action,
453
			'U_EDIT_CAT'	=> $this->u_action . '&amp;action=edit_cat&amp;id=' . $id,
454
		]);
455
	}
456
457
	/**
458
	 * Set page url
459
	 *
460
	 * @param string $u_action Custom form action
461
	 * @return null
462
	 * @access public
463
	 */
464
	public function set_page_url($u_action)
465
	{
466
		$this->u_action = $u_action;
467
	}
468
}
469