Completed
Push — 1.1.0 ( c7508f...88ee47 )
by Sylver
08:30
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 13
rs 9.9

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