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