Completed
Pull Request — master (#75)
by Jakub
10:46
created

admin_controller::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2.0046

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 17
cts 19
cp 0.8947
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 12
crap 2.0046

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
use phpbb\ads\controller\admin_input as input;
14
15
/**
16
* Admin controller
17
*/
18
class admin_controller
19
{
20
	/** @var array Form data */
21
	protected $data = array();
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/** @var \phpbb\language\language */
27
	protected $language;
28
29
	/** @var \phpbb\request\request */
30
	protected $request;
31
32
	/** @var \phpbb\ads\ad\manager */
33
	protected $manager;
34
35
	/** @var \phpbb\config\db_text */
36
	protected $config_text;
37
38
	/** @var \phpbb\config\config */
39
	protected $config;
40
41
	/** @var \phpbb\group\helper */
42
	protected $group_helper;
43
44
	/** @var \phpbb\ads\controller\admin_input */
45
	protected $input;
46
47
	/** @var \phpbb\ads\controller\admin_helper */
48
	protected $helper;
49
50
	/** @var \phpbb\ads\analyser\manager */
51
	protected $analyser;
52
53
	/** @var string Custom form action */
54
	protected $u_action;
55
56
	/**
57
	 * Constructor
58
	 *
59
	 * @param \phpbb\template\template           $template     Template object
60
	 * @param \phpbb\language\language           $language     Language object
61
	 * @param \phpbb\request\request             $request      Request object
62
	 * @param \phpbb\ads\ad\manager              $manager      Advertisement manager object
63
	 * @param \phpbb\config\db_text              $config_text  Config text object
64
	 * @param \phpbb\config\config               $config       Config object
65
	 * @param \phpbb\group\helper                $group_helper Group helper object
66
	 * @param \phpbb\ads\controller\admin_input  $input        Admin input object
67
	 * @param \phpbb\ads\controller\admin_helper $helper       Admin helper object
68
	 * @param \phpbb\ads\analyser\manager        $analyser     Ad code analyser object
69
	 * @param string                             $root_path    phpBB root path
70
	 * @param string                             $php_ext      PHP extension
71
	 */
72 33
	public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext)
73
	{
74 33
		$this->template = $template;
75 33
		$this->language = $language;
76 33
		$this->request = $request;
77 33
		$this->manager = $manager;
78 33
		$this->config_text = $config_text;
79 33
		$this->config = $config;
80 33
		$this->group_helper = $group_helper;
81 33
		$this->input = $input;
82 33
		$this->helper = $helper;
83 33
		$this->analyser = $analyser;
84
85
		// VSE doesn't like this here, but it's really the best placement:
86
		//    1. It is only called once
87
		//    2. We need all this everywhere in ACP
88 33
		if (!function_exists('user_get_id_name'))
89 33
		{
90
			include $root_path . 'includes/functions_user.' . $php_ext;
91
		}
92
93 33
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
94 33
		$this->language->add_lang('acp', 'phpbb/ads');
95
96 33
		$this->template->assign_var('S_PHPBB_ADS', true);
97 33
	}
98
99
	/**
100
	 * Set page url
101
	 *
102
	 * @param	string	$u_action	Custom form action
103
	 * @return	void
104
	 */
105 27
	public function set_page_url($u_action)
106
	{
107 27
		$this->u_action = $u_action;
108 27
	}
109
110
	/**
111
	 * Get ACP page title for Ads module
112
	 *
113
	 * @return	string	Language string for Ads ACP module
114
	 */
115 1
	public function get_page_title()
116
	{
117 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
118
	}
119
120
	/**
121
	 * Process user request for settings mode
122
	 *
123
	 * @return	void
124
	 */
125 3
	public function mode_settings()
126
	{
127 3
		add_form_key('phpbb/ads/settings');
128 3
		if ($this->request->is_set_post('submit'))
129 3
		{
130
			// Validate form key
131 2
			if (check_form_key('phpbb/ads/settings'))
132 2
			{
133 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
134 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
135 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
136 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
137
138 1
				$this->success('ACP_AD_SETTINGS_SAVED');
139
			}
140
141 1
			$this->error('FORM_INVALID');
142
		}
143
144 1
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
145 1
		$groups = $this->manager->load_groups();
146 1 View Code Duplication
		foreach ($groups as $group)
147
		{
148 1
			$this->template->assign_block_vars('groups', array(
149 1
				'ID'         => $group['group_id'],
150 1
				'NAME'       => $this->group_helper->get_name($group['group_name']),
151 1
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
152 1
			));
153 1
		}
154
155 1
		$this->template->assign_vars(array(
156 1
			'U_ACTION'          => $this->u_action,
157 1
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
158 1
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
159 1
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
160 1
		));
161 1
	}
162
163
	/**
164
	 * Process user request for manage mode
165
	 *
166
	 * @return	void
167
	 */
168 29
	public function mode_manage()
169
	{
170
		// Trigger specific action
171 29
		$action = $this->request->variable('action', '');
172 29
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
173 29
		{
174 27
			$this->{'action_' . $action}();
175 15
		}
176
		else
177
		{
178
			// Otherwise default to this
179 2
			$this->list_ads();
180
		}
181 17
	}
182
183
	/**
184
	 * Add an advertisement
185
	 *
186
	 * @return	void
187
	 */
188 16
	protected function action_add()
189
	{
190 6
		add_form_key('phpbb/ads/add');
191
192 6
		$action = $this->get_submitted_action();
193
		if ($action)
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
194 6
		{
195 5
			$this->data = $this->input->get_form_data('phpbb/ads/add');
196 16
			$this->{$action}();
197 4
			$this->helper->assign_data($this->data, $this->input->get_errors());
198 4
		}
199
		else
200
		{
201 1
			$this->helper->assign_locations();
202
		}
203
204
		// Set output vars for display in the template
205 5
		$this->template->assign_vars(array(
206 5
			'S_ADD_AD'				=> true,
207 5
			'U_BACK'				=> $this->u_action,
208 5
			'U_ACTION'				=> "{$this->u_action}&amp;action=add",
209 5
			'PICKER_DATE_FORMAT'	=> input::DATE_FORMAT,
210 5
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
211 5
		));
212 5
	}
213
214
	/**
215
	 * Edit an advertisement
216
	 *
217
	 * @return	void
218
	 */
219 7
	protected function action_edit()
220
	{
221 7
		$ad_id = $this->request->variable('id', 0);
222 7
		add_form_key('phpbb/ads/edit/' . $ad_id);
223
224 7
		$action = $this->get_submitted_action();
225
		if ($action)
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
226 7
		{
227 5
			$this->data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
228 5
			$this->{$action}();
229 3
		}
230
		else
231
		{
232 2
			$this->data = $this->manager->get_ad($ad_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->manager->get_ad($ad_id) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
233 2
			if (empty($this->data))
234 2
			{
235 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
236
			}
237
238
			// Load ad template locations
239 1
			$this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
240
		}
241
242
		// Set output vars for display in the template
243 4
		$this->template->assign_vars(array(
244 4
			'S_EDIT_AD'				=> true,
245 4
			'EDIT_ID'				=> $ad_id,
246 4
			'U_BACK'				=> $this->u_action,
247 4
			'U_ACTION'				=> "{$this->u_action}&amp;action=edit&amp;id=$ad_id",
248 4
			'PICKER_DATE_FORMAT'	=> input::DATE_FORMAT,
249 4
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
250 4
		));
251 4
		$this->helper->assign_data($this->data, $this->input->get_errors());
252 4
	}
253
254
	/**
255
	 * Enable an advertisement
256
	 *
257
	 * @return	void
258
	 */
259 4
	protected function action_enable()
260
	{
261 4
		$this->ad_enable(true);
262 1
	}
263
264
	/**
265
	 * Disable an advertisement
266
	 *
267
	 * @return	void
268
	 */
269 4
	protected function action_disable()
270
	{
271 4
		$this->ad_enable(false);
272 1
	}
273
274
	/**
275
	 * Delete an advertisement
276
	 *
277
	 * @return	void
278
	 */
279 3
	protected function action_delete()
280
	{
281 3
		$ad_id = $this->request->variable('id', 0);
282
		if ($ad_id)
283 3
		{
284 3
			if (confirm_box(true))
285 3
			{
286
				// Get ad data so that we can log ad name
287 2
				$ad_data = $this->manager->get_ad($ad_id);
288
289
				// Delete ad and it's template locations
290 2
				$this->manager->delete_ad_locations($ad_id);
291 2
				$success = $this->manager->delete_ad($ad_id);
292
293
				// Only notify user on error or if not ajax
294 2
				if (!$success)
295 2
				{
296 1
					$this->error('ACP_AD_DELETE_ERRORED');
297
				}
298
				else
299
				{
300 1
					$this->helper->log('DELETE', $ad_data['ad_name']);
301
302 1
					if (!$this->request->is_ajax())
303 1
					{
304 1
						$this->success('ACP_AD_DELETE_SUCCESS');
305
					}
306
				}
307
			}
308
			else
309
			{
310 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
311 1
					'id'     => $ad_id,
312 1
					'i'      => $this->request->variable('i', ''),
313 1
					'mode'   => $this->request->variable('mode', ''),
314 1
					'action' => 'delete',
315 1
				)));
316
			}
317 1
		}
318 1
	}
319
320
	/**
321
	 * Display the list of all ads
322
	 *
323
	 * @return	void
324
	 */
325 1
	protected function list_ads()
326
	{
327 1
		foreach ($this->manager->get_all_ads() as $row)
328
		{
329 1
			$ad_enabled  = (int) $row['ad_enabled'];
330 1
			$ad_expired  = $this->helper->is_expired($row);
331
332 1
			if ($ad_expired && $ad_enabled)
333 1
			{
334
				$ad_enabled = 0;
335
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
336
			}
337
338 1
			$this->template->assign_block_vars($ad_expired ? 'expired' : 'ads', array(
339 1
				'NAME'         => $row['ad_name'],
340 1
				'PRIORITY'     => $row['ad_priority'],
341 1
				'END_DATE'     => $this->helper->prepare_end_date($row['ad_end_date']),
342 1
				'VIEWS'        => $row['ad_views'],
343 1
				'CLICKS'       => $row['ad_clicks'],
344 1
				'VIEWS_LIMIT'  => $row['ad_views_limit'],
345 1
				'CLICKS_LIMIT' => $row['ad_clicks_limit'],
346 1
				'S_EXPIRED'    => $ad_expired,
347 1
				'S_ENABLED'    => $ad_enabled,
348 1
				'U_ENABLE'     => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
349 1
				'U_EDIT'       => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
350 1
				'U_DELETE'     => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
351 1
			));
352 1
		}
353
354
		// Set output vars for display in the template
355 1
		$this->template->assign_vars(array(
356 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
357 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
358 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
359 1
		));
360 1
	}
361
362
	/**
363
	 * Get what action user wants to do with the form.
364
	 * Possible options are:
365
	 *  - preview ad code
366
	 *  - upload banner to display in an ad code
367
	 *  - analyse ad code
368
	 *  - submit form (either add or edit an ad)
369
	 *
370
	 * @return	mixed	Action name or false when no action was submitted
371
	 */
372 13
	protected function get_submitted_action()
373
	{
374 13
		$actions = array('preview', 'upload_banner', 'analyse_ad_code', 'submit_add', 'submit_edit');
375 13
		foreach ($actions as $action)
376
		{
377 13
			if ($this->request->is_set_post($action))
378 13
			{
379 10
				return $action;
380
			}
381 11
		}
382
383 3
		return false;
384
	}
385
386
	/**
387
	 * Enable/disable an advertisement
388
	 *
389
	 * @param	bool	$enable	Enable or disable the advertisement?
390
	 * @return	void
391
	 */
392 6
	protected function ad_enable($enable)
393
	{
394 6
		$ad_id = $this->request->variable('id', 0);
395
396 6
		$success = $this->manager->update_ad($ad_id, array(
397 6
			'ad_enabled' => (int) $enable,
398 6
		));
399
400
		// If AJAX was used, show user a result message
401 6
		if ($this->request->is_ajax())
402 6
		{
403 2
			$json_response = new \phpbb\json_response;
404 2
			$json_response->send(array(
405 2
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
406 2
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
407 2
			));
408
		}
409
410
		// Otherwise, show traditional infobox
411
		if ($success)
412 4
		{
413 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
414
		}
415
		else
416
		{
417 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
418
		}
419
	}
420
421
	/**
422
	 * Submit action "preview".
423
	 * Prepare advertisement preview.
424
	 *
425
	 * @return	void
426
	 */
427 2
	protected function preview()
428
	{
429 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code']));
430 2
	}
431
432
	/**
433
	 * Submit action "upload_banner".
434
	 * Upload banner and append it to the ad code.
435
	 *
436
	 * @return	void
437
	 */
438 1
	protected function upload_banner()
439
	{
440 1
		$this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']);
441 1
	}
442
443
	/**
444
	 * Submit action "analyse_ad_code".
445
	 * Upload banner and append it to the ad code.
446
	 *
447
	 * @return	void
448
	 */
449 1
	protected function analyse_ad_code()
450
	{
451 1
		$this->analyser->run($this->data['ad_code']);
452 1
	}
453
454
	/**
455
	 * Submit action "submit_add".
456
	 * Add new ad.
457
	 *
458
	 * @return	void
459
	 */
460 2
	protected function submit_add()
461
	{
462 2
		if (!$this->input->has_errors())
463 2
		{
464 1
			$ad_id = $this->manager->insert_ad($this->data);
465 1
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
466
467 1
			$this->helper->log('ADD', $this->data['ad_name']);
468
469 1
			$this->success('ACP_AD_ADD_SUCCESS');
470
		}
471 1
	}
472
473
	/**
474
	 * Submit action "submit_edit".
475
	 * Edit ad.
476
	 *
477
	 * @return	void
478
	 */
479 4
	protected function submit_edit()
480
	{
481 4
		$ad_id = $this->request->variable('id', 0);
482 4
		if ($ad_id && !$this->input->has_errors())
483 4
		{
484 2
			$success = $this->manager->update_ad($ad_id, $this->data);
485
			if ($success)
486 2
			{
487
				// Only insert new ad locations to DB when ad exists
488 1
				$this->manager->delete_ad_locations($ad_id);
489 1
				$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
490
491 1
				$this->helper->log('EDIT', $this->data['ad_name']);
492
493 1
				$this->success('ACP_AD_EDIT_SUCCESS');
494
			}
495
496 1
			$this->error('ACP_AD_DOES_NOT_EXIST');
497
		}
498 2
	}
499
500
	/**
501
	 * Print success message.
502
	 *
503
	 * @param	string	$msg	Message lang key
504
	 */
505 6
	protected function success($msg)
506
	{
507 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action));
508
	}
509
510
	/**
511
	 * Print error message.
512
	 *
513
	 * @param	string	$msg	Message lang key
514
	 */
515 6
	protected function error($msg)
516
	{
517 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action), E_USER_WARNING);
518
	}
519
}
520