Completed
Pull Request — master (#45)
by Matt
53:10 queued 18:15
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
				$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
				$this->success('ACP_AD_SETTINGS_SAVED');
132 1
			}
133 1
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
			));
138 2
		}
139 2
140 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
141
		$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 2
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
			));
151 2
		}
152 2
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
			'ENABLE_VIEWS'		=> $this->config['phpbb_ads_enable_views'],
157
			'ENABLE_CLICKS'		=> $this->config['phpbb_ads_enable_clicks'],
158
		));
159
	}
160
161
	/**
162
	* Set page url
163 32
	*
164
	* @param string $u_action Custom form action
165 32
	* @return void
166 32
	*/
167
	public function set_page_url($u_action)
168
	{
169
		$this->u_action = $u_action;
170
	}
171
172
	/**
173 1
	* Get ACP page title for Ads module
174
	*
175 1
	* @return string	Language string for Ads ACP module
176
	*/
177
	public function get_page_title()
178
	{
179
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
180
	}
181
182
	/**
183 12
	* Add an advertisement
184
	*
185 9
	* @return void
186 9
	*/
187
	public function action_add()
188 9
	{
189 9
		$preview = $this->request->is_set_post('preview');
190 9
		$submit = $this->request->is_set_post('submit');
191 8
192
		add_form_key('phpbb/ads/add');
193
		if ($preview || $submit)
194 8
		{
195 1
			$data = $this->get_form_data('phpbb/ads/add');
196 12
197 View Code Duplication
			if ($preview)
198 7
			{
199 1
				$this->ad_preview($data['ad_code']);
200 1
			}
201
			else if (empty($this->errors))
202 1
			{
203
				$ad_id = $this->manager->insert_ad($data);
204 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
205
206
				$this->log('ADD', $data['ad_name']);
207 7
208 7
				$this->success('ACP_AD_ADD_SUCCESS');
209 7
			}
210
211
			$this->assign_locations($data);
212 1
			$this->assign_form_data($data);
213
		}
214
		else
215
		{
216 8
			$this->assign_locations();
217 8
		}
218 8
219 8
		// 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
			'U_ACTION'				=> "{$this->u_action}&amp;action=add",
224
			'PICKER_DATE_FORMAT'	=> self::DATE_FORMAT,
225
		));
226
	}
227
228
	/**
229 11
	* Edit an advertisement
230
	*
231 11
	* @return void
232 11
	*/
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 9
239
		add_form_key('phpbb/ads/edit/' . $ad_id);
240
		if ($preview || $submit)
241 9
		{
242 1
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
243 1
244
			if ($preview)
245 8
			{
246 2
				$this->ad_preview($data['ad_code']);
247
			}
248 View Code Duplication
			else if (empty($this->errors))
249 2
			{
250
				$success = $this->manager->update_ad($ad_id, $data);
251 1
252 1
				if ($success)
253
				{
254 1
					// Only insert new ad locations to DB when ad exists
255
					$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 7
					$this->success('ACP_AD_EDIT_SUCCESS');
261
				}
262
				$this->error('ACP_AD_DOES_NOT_EXIST');
263
			}
264 2
		}
265 2
		else
266 2
		{
267 1
			// Load ad data
268
			$data = $this->manager->get_ad($ad_id);
269
			if (empty($data))
270
			{
271 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
272
			}
273
274
			// Load ad template locations
275 8
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
276 8
		}
277 8
278 8
		// 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
		));
286
		$this->assign_locations($data);
287
		$this->assign_form_data($data);
288
	}
289
290
	/**
291 3
	* Enable an advertisement
292
	*
293 3
	* @return void
294 1
	*/
295
	public function action_enable()
296
	{
297
		$this->ad_enable(true);
298
	}
299
300
	/**
301 3
	* Disable an advertisement
302
	*
303 3
	* @return void
304 1
	*/
305
	public function action_disable()
306
	{
307
		$this->ad_enable(false);
308
	}
309
310
	/**
311 3
	* Delete an advertisement
312
	*
313 3
	* @return void
314
	*/
315 3
	public function action_delete()
316 3
	{
317 3
		$ad_id = $this->request->variable('id', 0);
318
		if ($ad_id)
319 2
		{
320
			if (confirm_box(true))
321
			{
322 2
				// 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 1
329
				// Only notify user on error or if not ajax
330
				if (!$success)
331
				{
332 1
					$this->error('ACP_AD_DELETE_ERRORED');
333
				}
334 1
				else
335 1
				{
336 1
					$this->log('DELETE', $ad_data['ad_name']);
337
338
					if (!$this->request->is_ajax())
339
					{
340
						$this->success('ACP_AD_DELETE_SUCCESS');
341
					}
342 1
				}
343 1
			}
344 1
			else
345 1
			{
346
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
347 1
					'id'		=> $ad_id,
348
					'i'			=> $this->request->variable('i', ''),
349 1
					'mode'		=> $this->request->variable('mode', ''),
350 1
					'action'	=> 'delete'
351
				)));
352
			}
353
		}
354
	}
355
356
	/**
357 1
	* Display the ads
358
	*
359 1
	* @return void
360
	*/
361 1
	public function list_ads()
362 1
	{
363 1
		foreach ($this->manager->get_all_ads() as $row)
364 1
		{
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
			{
370 1
				$ad_enabled = 0;
371 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
372 1
			}
373 1
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
				'CLICKS_LIMIT'			=> $row['ad_clicks_limit'],
381
				'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
				'U_EDIT'				=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
385
				'U_DELETE'				=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
386
			));
387
		}
388
389
		// Set output vars for display in the template
390 9
		$this->template->assign_vars(array(
391
			'U_ACTION_ADD'		=> $this->u_action . '&amp;action=add',
392 9
			'S_VIEWS_ENABLED'	=> $this->config['phpbb_ads_enable_views'],
393
			'S_CLICKS_ENABLED'	=> $this->config['phpbb_ads_enable_clicks'],
394 9
		));
395 9
	}
396
397
	/**
398
	* Perform general tasks
399
	*
400
	* @return void
401
	*/
402
	protected function setup()
403 4
	{
404
		$this->user->add_lang_ext('phpbb/ads', 'acp');
405 4
406
		$this->template->assign_var('S_PHPBB_ADS', true);
407 4
	}
408 4
409 4
	/**
410
	* Enable/disable an advertisement
411
	*
412 4
	* @param	bool	$enable	Enable or disable the advertisement?
413 4
	* @return void
414
	*/
415
	protected function ad_enable($enable)
416
	{
417
		$ad_id = $this->request->variable('id', 0);
418
419
		$success = $this->manager->update_ad($ad_id, array(
420
			'ad_enabled'	=> (int) $enable,
421
		));
422
423 4
		// If AJAX was used, show user a result message
424 2
		if ($this->request->is_ajax())
425
		{
426
			$json_response = new \phpbb\json_response;
427
			$json_response->send(array(
428 2
				'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
		{
436
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
437
		}
438 17
		else
439
		{
440
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
441 17
		}
442 17
	}
443 17
444 17
	/**
445 17
	* Get admin form data.
446 17
	*
447 17
	* @param	string	$form_name	The form name.
448 17
	* @return	array	Form data
449
	*/
450
	protected function get_form_data($form_name)
451 17
	{
452 17
		$data = array(
453 2
			'ad_name'			=> $this->request->variable('ad_name', '', true),
454 2
			'ad_note'			=> $this->request->variable('ad_note', '', true),
455
			'ad_code'			=> $this->request->variable('ad_code', '', true),
456
			'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 2
			'ad_priority'		=> $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
460 2
			'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 2
464 2
		// Validate form key
465
		if (!check_form_key($form_name))
466
		{
467 17
			$this->errors[] = $this->user->lang('FORM_INVALID');
468 17
		}
469 4
470
		// Validate ad name
471 4
		if ($data['ad_name'] === '')
472 4
		{
473 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
474 2
		}
475 4
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
476 13
		{
477 13
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
478
		}
479
480
		// Validate ad end date
481
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
482 13
		{
483
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
484
485
			if ($data['ad_end_date'] < time())
486 17
			{
487 17
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
488 6
			}
489 6
		}
490
		else if ($data['ad_end_date'] !== '')
491 17
		{
492
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
493
		}
494
		else
495
		{
496
			$data['ad_end_date'] = 0;
497
		}
498
499
		// Validate ad priority
500 15
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
501
		{
502 15
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
503 15
		}
504 15
505
		// Validate ad views limit
506 15
		if ($data['ad_views_limit'] < 0)
507 15
		{
508 15
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
509 15
		}
510 15
511 15
		// Validate ad clicks limit
512 15
		if ($data['ad_clicks_limit'] < 0)
513 15
		{
514
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
515
		}
516
517
		return $data;
518
	}
519
520
	/**
521 15
	* Assign form data to the template.
522
	*
523 15
	* @param	array	$data	The form data.
524 15
	* @return void
525 12
	*/
526
	protected function assign_form_data($data)
527
	{
528 3
		$this->template->assign_vars(array(
529 3
			'S_ERROR'		=> (bool) count($this->errors),
530 3
			'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
531
532
			'AD_NAME'			=> $data['ad_name'],
533
			'AD_NOTE'			=> $data['ad_note'],
534
			'AD_CODE'			=> $data['ad_code'],
535
			'AD_ENABLED'		=> $data['ad_enabled'],
536
			'AD_END_DATE'		=> $this->prepare_end_date($data['ad_end_date']),
537
			'AD_PRIORITY'		=> $data['ad_priority'],
538
			'AD_VIEWS_LIMIT'	=> $data['ad_views_limit'],
539
			'AD_CLICKS_LIMIT'	=> $data['ad_clicks_limit'],
540
		));
541
	}
542 16
543
	/**
544 16
	* Prepare end date for display
545
	*
546 16
	* @param	mixed	$end_date	End date.
547 16
	* @return	string	End date prepared for display.
548 16
	*/
549 16
	protected function prepare_end_date($end_date)
550 16
	{
551 16
		if (empty($end_date))
552 16
		{
553 16
			return '';
554
		}
555
556
		if (is_numeric($end_date))
557
		{
558
			return $this->user->format_date($end_date, self::DATE_FORMAT);
559
		}
560
561 2
		return (string) $end_date;
562
	}
563 2
564 2
	/**
565
	* Assign template locations data to the template.
566
	*
567
	* @param	mixed	$data	The form data or nothing.
568
	* @return	void
569
	*/
570
	protected function assign_locations($data = false)
571 6
	{
572
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
573 6
		{
574
			$this->template->assign_block_vars('ad_locations', array(
575
				'LOCATION_ID'	=> $location_id,
576
				'LOCATION_DESC'	=> $location_data['desc'],
577
				'LOCATION_NAME'	=> $location_data['name'],
578
				'S_SELECTED'	=> $data ? in_array($location_id, $data['ad_locations']) : false,
579
			));
580
		}
581 5
	}
582
583 5
	/**
584
	* Prepare advertisement preview
585
	*
586
	* @param	string	$code	Ad code to preview
587
	* @return	void
588
	*/
589
	protected function ad_preview($code)
590
	{
591
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
592
	}
593 3
594
	/**
595 3
	* Print success message.
596 3
	*
597
	* It takes arguments in the form of a language key, followed by language substitution values.
598
	*/
599
	protected function success()
600
	{
601
		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
	protected function error()
610
	{
611
		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
	protected function log($action, $ad_name)
622
	{
623
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
624
	}
625
}
626