Completed
Pull Request — master (#32)
by Jakub
09:40 queued 01:34
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 10
crap 1

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
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \phpbb\request\request */
28
	protected $request;
29
30
	/** @var \phpbb\ads\ad\manager */
31
	protected $manager;
32
33
	/** @var \phpbb\ads\location\manager */
34
	protected $location_manager;
35
36
	/** @var \phpbb\log\log */
37
	protected $log;
38
39
	/** @var \phpbb\config\db_text */
40
	protected $config_text;
41
42
	/** @var \phpbb\config\config */
43
	protected $config;
44
45
	/** @var string php_ext */
46
	protected $php_ext;
47
48
	/** @var string ext_path */
49
	protected $ext_path;
50
51
	/** @var string Custom form action */
52
	protected $u_action;
53
54
	/** @var array Form validation errors */
55
	protected $errors = array();
56
57
	/**
58
	* Constructor
59
	*
60
	* @param \phpbb\template\template				$template			Template object
61
	* @param \phpbb\user							$user				User object
62
	* @param \phpbb\request\request					$request			Request object
63
	* @param \phpbb\ads\ad\manager					$manager			Advertisement manager object
64
	* @param \phpbb\ads\location\manager			$location_manager	Template location manager object
65
	* @param \phpbb\log\log							$log				The phpBB log system
66
	* @param \phpbb\config\db_text					$config_text		Config text object
67
	* @param \phpbb\config\config					$config				Config object
68
	* @param string									$php_ext			PHP extension
69
	* @param string									$ext_path			Path to this extension
70
	*/
71 38
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, $php_ext, $ext_path)
72
	{
73 38
		$this->template = $template;
74 38
		$this->user = $user;
75 38
		$this->request = $request;
76 38
		$this->manager = $manager;
77 38
		$this->location_manager = $location_manager;
78 38
		$this->log = $log;
79 38
		$this->config_text = $config_text;
80 38
		$this->config = $config;
81 38
		$this->php_ext = $php_ext;
82 38
		$this->ext_path = $ext_path;
83 38
	}
84
85
	/**
86
	* Process user request for manage mode
87
	*
88
	* @return void
89
	*/
90 6
	public function mode_manage()
91
	{
92 6
		$this->setup();
93
94
		// Trigger specific action
95 6
		$action = $this->request->variable('action', '');
96 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
97 6
		{
98 5
			$this->{'action_' . $action}();
99 5
		}
100
101
		// Otherwise default to this
102 6
		$this->list_ads();
103 6
	}
104
105
	/**
106
	* Process user request for settings mode
107
	*
108
	* @return void
109
	*/
110 3
	public function mode_settings()
111
	{
112 3
		$this->setup();
113
114 3
		add_form_key('phpbb/ads/settings');
115 3
		if ($this->request->is_set_post('submit'))
116 3
		{
117
			// Validate form key
118 2
			if (!check_form_key('phpbb/ads/settings'))
119 2
			{
120 1
				$this->errors[] = $this->user->lang('FORM_INVALID');
121 1
			}
122
123 2
			if (empty($this->errors))
124 2
			{
125 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
126 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
127
128 1
				$this->success('ACP_AD_SETTINGS_SAVED');
129
			}
130
131 1
			$this->template->assign_vars(array(
132 1
				'S_ERROR'		=> (bool) count($this->errors),
133 1
				'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
134 1
			));
135 1
		}
136
137 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
138 2
		$groups = $this->manager->load_groups();
139 2
		foreach ($groups as $group)
140
		{
141 2
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
142
143 2
			$this->template->assign_block_vars('groups', array(
144 2
				'ID'			=> $group['group_id'],
145 2
				'NAME'			=> $group_name,
146 2
				'S_SELECTED'	=> in_array($group['group_id'], $hide_groups),
147 2
			));
148 2
		}
149
150 2
		$this->template->assign_vars(array(
151 2
			'U_ACTION'			=> $this->u_action,
152 2
			'ADBLOCKER_MESSAGE'	=> $this->config['phpbb_ads_adblocker_message'],
153 2
		));
154 2
	}
155
156
	/**
157
	* Set page url
158
	*
159
	* @param string $u_action Custom form action
160
	* @return void
161
	*/
162 32
	public function set_page_url($u_action)
163
	{
164 32
		$this->u_action = $u_action;
165 32
	}
166
167
	/**
168
	* Get ACP page title for Ads module
169
	*
170
	* @return string	Language string for Ads ACP module
171
	*/
172 1
	public function get_page_title()
173
	{
174 1
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
175
	}
176
177
	/**
178
	* Add an advertisement
179
	*
180
	* @return void
181
	*/
182 11
	public function action_add()
183
	{
184 9
		$preview = $this->request->is_set_post('preview');
185 9
		$submit = $this->request->is_set_post('submit');
186
187 9
		add_form_key('phpbb/ads/add');
188 9
		if ($preview || $submit)
189 9
		{
190 8
			$data = $this->get_form_data('phpbb/ads/add');
191
192 View Code Duplication
			if ($preview)
193 8
			{
194 1
				$this->ad_preview($data['ad_code']);
195 1
			}
196 11
			else if (empty($this->errors))
197 7
			{
198 1
				$ad_id = $this->manager->insert_ad($data);
199 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
200
201 1
				$this->log('ADD', $data['ad_name']);
202
203 1
				$this->success('ACP_AD_ADD_SUCCESS');
204
			}
205
206 7
			$this->assign_locations($data);
207 7
			$this->assign_form_data($data);
208 7
		}
209
		else
210
		{
211 1
			$this->assign_locations();
212
		}
213
214
		// Set output vars for display in the template
215 8
		$this->template->assign_vars(array(
216 8
			'S_ADD_AD'				=> true,
217 8
			'U_BACK'				=> $this->u_action,
218 8
			'U_ACTION'				=> "{$this->u_action}&amp;action=add",
219 8
			'PICKER_DATE_FORMAT'	=> self::DATE_FORMAT,
220 8
		));
221 8
	}
222
223
	/**
224
	* Edit an advertisement
225
	*
226
	* @return void
227
	*/
228 11
	public function action_edit()
229
	{
230 11
		$ad_id = $this->request->variable('id', 0);
231 11
		$preview = $this->request->is_set_post('preview');
232 11
		$submit = $this->request->is_set_post('submit');
233
234 11
		add_form_key('phpbb/ads/edit/' . $ad_id);
235 11
		if ($preview || $submit)
236 11
		{
237 9
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
238
239
			if ($preview)
240 9
			{
241 1
				$this->ad_preview($data['ad_code']);
242 1
			}
243 View Code Duplication
			else if (empty($this->errors))
244 8
			{
245 2
				$success = $this->manager->update_ad($ad_id, $data);
246
247
				if ($success)
248 2
				{
249
					// Only insert new ad locations to DB when ad exists
250 1
					$this->manager->delete_ad_locations($ad_id);
251 1
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
252
253 1
					$this->log('EDIT', $data['ad_name']);
254
255 1
					$this->success('ACP_AD_EDIT_SUCCESS');
256
				}
257 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
258
			}
259 7
		}
260
		else
261
		{
262
			// Load ad data
263 2
			$data = $this->manager->get_ad($ad_id);
264 2
			if (empty($data))
265 2
			{
266 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
267
			}
268
269
			// Load ad template locations
270 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
271
		}
272
273
		// Set output vars for display in the template
274 8
		$this->template->assign_vars(array(
275 8
			'S_EDIT_AD'				=> true,
276 8
			'EDIT_ID'				=> $ad_id,
277 8
			'U_BACK'				=> $this->u_action,
278 8
			'U_ACTION'				=> "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
279 8
			'PICKER_DATE_FORMAT'	=> self::DATE_FORMAT,
280 8
		));
281 8
		$this->assign_locations($data);
282 8
		$this->assign_form_data($data);
283 8
	}
284
285
	/**
286
	* Enable an advertisement
287
	*
288
	* @return void
289
	*/
290 3
	public function action_enable()
291
	{
292 3
		$this->ad_enable(true);
293 1
	}
294
295
	/**
296
	* Disable an advertisement
297
	*
298
	* @return void
299
	*/
300 3
	public function action_disable()
301
	{
302 3
		$this->ad_enable(false);
303 1
	}
304
305
	/**
306
	* Delete an advertisement
307
	*
308
	* @return void
309
	*/
310 3
	public function action_delete()
311
	{
312 3
		$ad_id = $this->request->variable('id', 0);
313
		if ($ad_id)
314 3
		{
315 3
			if (confirm_box(true))
316 3
			{
317
				// Get ad data so that we can log ad name
318 2
				$ad_data = $this->manager->get_ad($ad_id);
319
320
				// Delete ad and it's template locations
321 2
				$this->manager->delete_ad_locations($ad_id);
322 2
				$success = $this->manager->delete_ad($ad_id);
323
324
				// Only notify user on error or if not ajax
325 2
				if (!$success)
326 2
				{
327 1
					$this->error('ACP_AD_DELETE_ERRORED');
328
				}
329
				else
330
				{
331 1
					$this->log('DELETE', $ad_data['ad_name']);
332
333 1
					if (!$this->request->is_ajax())
334 1
					{
335 1
						$this->success('ACP_AD_DELETE_SUCCESS');
336
					}
337
				}
338
			}
339
			else
340
			{
341 1
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
342 1
					'id'		=> $ad_id,
343 1
					'i'			=> $this->request->variable('i', ''),
344 1
					'mode'		=> $this->request->variable('mode', ''),
345
					'action'	=> 'delete'
346 1
				)));
347
			}
348 1
		}
349 1
	}
350
351
	/**
352
	* Display the ads
353
	*
354
	* @return void
355
	*/
356 1
	public function list_ads()
357
	{
358 1
		foreach ($this->manager->get_all_ads() as $row)
359
		{
360 1
			$ad_enabled = (int) $row['ad_enabled'];
361 1
			$ad_end_date = (int) $row['ad_end_date'];
362 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
363 1
			if ($ad_expired && $ad_enabled)
364 1
			{
365 1
				$ad_enabled = 0;
366 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
367 1
			}
368
369 1
			$this->template->assign_block_vars('ads', array(
370 1
				'NAME'					=> $row['ad_name'],
371 1
				'END_DATE'				=> $ad_end_date ? $this->user->format_date($ad_end_date, self::DATE_FORMAT) : '',
372 1
				'S_END_DATE_EXPIRED'	=> $ad_expired,
373 1
				'S_ENABLED'				=> $ad_enabled,
374 1
				'U_ENABLE'				=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
375 1
				'U_EDIT'				=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
376 1
				'U_DELETE'				=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
377 1
			));
378 1
		}
379
380
		// Set output vars for display in the template
381 1
		$this->template->assign_var('U_ACTION_ADD', $this->u_action . '&amp;action=add');
382 1
	}
383
384
	/**
385
	* Perform general tasks
386
	*
387
	* @return void
388
	*/
389 9
	protected function setup()
390
	{
391 9
		$this->user->add_lang_ext('phpbb/ads', 'acp');
392
393 9
		$this->template->assign_var('S_PHPBB_ADS', true);
394 9
	}
395
396
	/**
397
	* Enable/disable an advertisement
398
	*
399
	* @param	bool	$enable	Enable or disable the advertisement?
400
	* @return void
401
	*/
402 4
	protected function ad_enable($enable)
403
	{
404 4
		$ad_id = $this->request->variable('id', 0);
405
406 4
		$success = $this->manager->update_ad($ad_id, array(
407 4
			'ad_enabled'	=> (int) $enable,
408 4
		));
409
410
		// If AJAX was used, show user a result message
411 4
		if ($this->request->is_ajax())
412 4
		{
413
			$json_response = new \phpbb\json_response;
414
			$json_response->send(array(
415
				'text'	=> $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
416
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
417
			));
418
		}
419
420
		// Otherwise, show traditional infobox
421
		if ($success)
422 4
		{
423 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
424
		}
425
		else
426
		{
427 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
428
		}
429
	}
430
431
	/**
432
	* Get admin form data.
433
	*
434
	* @param	string	$form_name	The form name.
435
	* @return	array	Form data
436
	*/
437 17
	protected function get_form_data($form_name)
438
	{
439
		$data = array(
440 17
			'ad_name'		=> $this->request->variable('ad_name', '', true),
441 17
			'ad_note'		=> $this->request->variable('ad_note', '', true),
442 17
			'ad_code'		=> $this->request->variable('ad_code', '', true),
443 17
			'ad_enabled'	=> $this->request->variable('ad_enabled', 0),
444 17
			'ad_locations'	=> $this->request->variable('ad_locations', array('')),
445 17
			'ad_end_date'	=> $this->request->variable('ad_end_date', ''),
446 17
			'ad_priority'	=> $this->request->variable('ad_priority', 5),
447 17
		);
448
449
		// Validate form key
450 17
		if (!check_form_key($form_name))
451 17
		{
452 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
453 2
		}
454
455
		// Validate ad name
456 17
		if ($data['ad_name'] === '')
457 17
		{
458 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
459 2
		}
460 17
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
461 17
		{
462 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
463 2
		}
464
465
		// Validate ad end date
466 17
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
467 17
		{
468 4
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
469
470 4
			if ($data['ad_end_date'] < time())
471 4
			{
472 2
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
473 2
			}
474 4
		}
475 13
		else if ($data['ad_end_date'] !== '')
476 13
		{
477
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
478
		}
479
		else
480
		{
481 13
			$data['ad_end_date'] = 0;
482
		}
483
484
		// Validate ad priority
485 17
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
486 17
		{
487 6
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
488 6
		}
489
490 17
		return $data;
491
	}
492
493
	/**
494
	* Assign form data to the template.
495
	*
496
	* @param	array	$data	The form data.
497
	* @return void
498
	*/
499 15
	protected function assign_form_data($data)
500
	{
501 15
		$this->template->assign_vars(array(
502 15
			'S_ERROR'		=> (bool) count($this->errors),
503 15
			'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
504
505 15
			'AD_NAME'		=> $data['ad_name'],
506 15
			'AD_NOTE'		=> $data['ad_note'],
507 15
			'AD_CODE'		=> $data['ad_code'],
508 15
			'AD_ENABLED'	=> $data['ad_enabled'],
509 15
			'AD_END_DATE'	=> $this->prepare_end_date($data['ad_end_date']),
510 15
			'AD_PRIORITY'	=> $data['ad_priority'],
511 15
		));
512 15
	}
513
514
	/**
515
	* Prepare end date for display
516
	*
517
	* @param	mixed	$end_date	End date.
518
	* @return	string	End date prepared for display.
519
	*/
520 15
	protected function prepare_end_date($end_date)
521
	{
522 15
		if (empty($end_date))
523 15
		{
524 12
			return '';
525
		}
526
527 3
		if (is_numeric($end_date))
528 3
		{
529 3
			return $this->user->format_date($end_date, self::DATE_FORMAT);
530
		}
531
532
		return (string) $end_date;
533
	}
534
535
	/**
536
	* Assign template locations data to the template.
537
	*
538
	* @param	mixed	$data	The form data or nothing.
539
	* @return	void
540
	*/
541 16
	protected function assign_locations($data = false)
542
	{
543 16
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
544
		{
545 16
			$this->template->assign_block_vars('ad_locations', array(
546 16
				'LOCATION_ID'	=> $location_id,
547 16
				'LOCATION_DESC'	=> $location_data['desc'],
548 16
				'LOCATION_NAME'	=> $location_data['name'],
549 16
				'S_SELECTED'	=> $data ? in_array($location_id, $data['ad_locations']) : false,
550 16
			));
551 16
		}
552 16
	}
553
554
	/**
555
	* Prepare advertisement preview
556
	*
557
	* @param	string	$code	Ad code to preview
558
	* @return	void
559
	*/
560 2
	protected function ad_preview($code)
561
	{
562 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
563 2
	}
564
565
	/**
566
	* Print success message.
567
	*
568
	* It takes arguments in the form of a language key, followed by language substitution values.
569
	*/
570 6
	protected function success()
571
	{
572 6
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
573
	}
574
575
	/**
576
	* Print error message.
577
	*
578
	* It takes arguments in the form of a language key, followed by language substitution values.
579
	*/
580 5
	protected function error()
581
	{
582 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
583
	}
584
585
	/**
586
	* Log action
587
	*
588
	* @param	string	$action		Performed action in uppercase
589
	* @param	string	$ad_name	Advertisement name
590
	* @return	void
591
	*/
592 3
	protected function log($action, $ad_name)
593
	{
594 3
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
595 3
	}
596
}
597