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

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 12
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
use phpbb\ads\controller\admin_input as input;
14
15
/**
16
* Admin controller
17
*/
18
class admin_controller
19
{
20
	private $data = array();
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\ad\manager */
32
	protected $manager;
33
34
	/** @var \phpbb\config\db_text */
35
	protected $config_text;
36
37
	/** @var \phpbb\config\config */
38
	protected $config;
39
40
	/** @var \phpbb\group\helper */
41
	protected $group_helper;
42
43
	/** @var \phpbb\ads\controller\admin_input */
44
	protected $input;
45
46
	/** @var \phpbb\ads\controller\admin_helper */
47
	protected $helper;
48
49
	/** @var \phpbb\ads\analyser\manager */
50
	protected $analyser;
51
52
	/** @var string root_path */
53
	protected $root_path;
54
55
	/** @var string php_ext */
56
	protected $php_ext;
57
58
	/** @var string Custom form action */
59
	protected $u_action;
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @param \phpbb\template\template           $template     Template object
65
	 * @param \phpbb\language\language           $language     Language object
66
	 * @param \phpbb\request\request             $request      Request object
67
	 * @param \phpbb\ads\ad\manager              $manager      Advertisement manager object
68
	 * @param \phpbb\config\db_text              $config_text  Config text object
69
	 * @param \phpbb\config\config               $config       Config object
70
	 * @param \phpbb\group\helper                $group_helper Group helper object
71
	 * @param \phpbb\ads\controller\admin_input  $input        Admin input object
72
	 * @param \phpbb\ads\controller\admin_helper $helper       Admin helper object
73
	 * @param \phpbb\ads\analyser\manager        $analyser     Ad code analyser object
74
	 * @param string                             $root_path    phpBB root path
75
	 * @param string                             $php_ext      PHP extension
76
	 */
77 32
	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)
78
	{
79 32
		$this->template = $template;
80 32
		$this->language = $language;
81 32
		$this->request = $request;
82 32
		$this->manager = $manager;
83 32
		$this->config_text = $config_text;
84 32
		$this->config = $config;
85 32
		$this->group_helper = $group_helper;
86 32
		$this->input = $input;
87 32
		$this->helper = $helper;
88 32
		$this->analyser = $analyser;
89 32
		$this->root_path = $root_path;
90 32
		$this->php_ext = $php_ext;
91 32
	}
92
93
	/**
94
	 * Process user request for manage mode
95
	 *
96
	 * @return void
97
	 */
98 6
	public function mode_manage()
99
	{
100 6
		$this->setup();
101
102
		// Trigger specific action
103 6
		$action = $this->request->variable('action', '');
104 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
105 6
		{
106 5
			$this->{'action_' . $action}();
107 5
		}
108
109
		// Otherwise default to this
110 6
		$this->list_ads();
111 6
	}
112
113
	/**
114
	 * Process user request for settings mode
115
	 *
116
	 * @return void
117
	 */
118 3
	public function mode_settings()
119
	{
120 3
		$this->setup();
121
122 3
		add_form_key('phpbb/ads/settings');
123 3
		if ($this->request->is_set_post('submit'))
124 3
		{
125
			// Validate form key
126 2
			if (check_form_key('phpbb/ads/settings'))
127 2
			{
128 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
129 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
130 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
131 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
132
133 1
				$this->success('ACP_AD_SETTINGS_SAVED');
134
			}
135
136 1
			$this->helper->assign_errors(array($this->language->lang('FORM_INVALID')));
137 1
		}
138
139 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
140 2
		$groups = $this->manager->load_groups();
141 2 View Code Duplication
		foreach ($groups as $group)
142
		{
143 2
			$this->template->assign_block_vars('groups', array(
144 2
				'ID'         => $group['group_id'],
145 2
				'NAME'       => $this->group_helper->get_name($group['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
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
154 2
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
155 2
		));
156 2
	}
157
158
	/**
159
	 * Set page url
160
	 *
161
	 * @param string $u_action Custom form action
162
	 * @return void
163
	 */
164 26
	public function set_page_url($u_action)
165
	{
166 26
		$this->u_action = $u_action;
167 26
	}
168
169
	/**
170
	 * Get ACP page title for Ads module
171
	 *
172
	 * @return string    Language string for Ads ACP module
173
	 */
174 1
	public function get_page_title()
175
	{
176 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
177
	}
178
179
	/**
180
	 * Add an advertisement
181
	 *
182
	 * @return void
183
	 */
184 9
	public function action_add()
185
	{
186 5
		add_form_key('phpbb/ads/add');
187
188 5
		$action = $this->get_submitted_action();
189
		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...
190 5
		{
191 5
			$this->data = $this->input->get_form_data('phpbb/ads/add');
192 5
			$this->{$action}();
193 4
			$this->assign_data();
194 4
		}
195
		else
196 9
		{
197
			$this->helper->assign_locations();
198
		}
199
200 4
		$this->set_output_vars('add', 0);
201 4
	}
202
203
	/**
204
	 * Edit an advertisement
205
	 *
206
	 * @return void
207
	 */
208 7
	public function action_edit()
209
	{
210 7
		$ad_id = $this->request->variable('id', 0);
211 7
		add_form_key('phpbb/ads/edit/' . $ad_id);
212
213 7
		$action = $this->get_submitted_action();
214
		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...
215 7
		{
216 5
			$this->data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
217 5
			$this->{$action}();
218 3
		}
219
		else
220
		{
221 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...
222 2
			if (empty($this->data))
223 2
			{
224 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
225
			}
226
227
			// Load ad template locations
228 1
			$this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
229
		}
230
231 4
		$this->set_output_vars('edit', $ad_id);
232 4
		$this->assign_data();
233 4
	}
234
235
	/**
236
	 * Enable an advertisement
237
	 *
238
	 * @return void
239
	 */
240 4
	public function action_enable()
241
	{
242 4
		$this->ad_enable(true);
243 1
	}
244
245
	/**
246
	 * Disable an advertisement
247
	 *
248
	 * @return void
249
	 */
250 4
	public function action_disable()
251
	{
252 4
		$this->ad_enable(false);
253 1
	}
254
255
	/**
256
	 * Delete an advertisement
257
	 *
258
	 * @return void
259
	 */
260 3
	public function action_delete()
261
	{
262 3
		$ad_id = $this->request->variable('id', 0);
263
		if ($ad_id)
264 3
		{
265 3
			if (confirm_box(true))
266 3
			{
267
				// Get ad data so that we can log ad name
268 2
				$ad_data = $this->manager->get_ad($ad_id);
269
270
				// Delete ad and it's template locations
271 2
				$this->manager->delete_ad_locations($ad_id);
272 2
				$success = $this->manager->delete_ad($ad_id);
273
274
				// Only notify user on error or if not ajax
275 2
				if (!$success)
276 2
				{
277 1
					$this->error('ACP_AD_DELETE_ERRORED');
278
				}
279
				else
280
				{
281 1
					$this->helper->log('DELETE', $ad_data['ad_name']);
282
283 1
					if (!$this->request->is_ajax())
284 1
					{
285 1
						$this->success('ACP_AD_DELETE_SUCCESS');
286
					}
287
				}
288
			}
289
			else
290
			{
291 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
292 1
					'id'     => $ad_id,
293 1
					'i'      => $this->request->variable('i', ''),
294 1
					'mode'   => $this->request->variable('mode', ''),
295
					'action' => 'delete'
296 1
				)));
297
			}
298 1
		}
299 1
	}
300
301
	/**
302
	 * Display the ads
303
	 *
304
	 * @return void
305
	 */
306 1
	public function list_ads()
307
	{
308 1
		foreach ($this->manager->get_all_ads() as $row)
309
		{
310 1
			$ad_enabled = (int) $row['ad_enabled'];
311 1
			$ad_end_date = (int) $row['ad_end_date'];
312 1
			$ad_expired = ($ad_end_date > 0 && $ad_end_date < time()) || ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit']) || ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit']);
313 1
			if ($ad_expired && $ad_enabled)
314 1
			{
315 1
				$ad_enabled = 0;
316 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
317 1
			}
318
319 1
			$this->template->assign_block_vars($ad_expired ? 'expired' : 'ads', array(
320 1
				'NAME'         => $row['ad_name'],
321 1
				'PRIORITY'     => $row['ad_priority'],
322 1
				'END_DATE'     => $this->helper->prepare_end_date($ad_end_date),
323 1
				'VIEWS'        => $row['ad_views'],
324 1
				'CLICKS'       => $row['ad_clicks'],
325 1
				'VIEWS_LIMIT'  => $row['ad_views_limit'],
326 1
				'CLICKS_LIMIT' => $row['ad_clicks_limit'],
327 1
				'S_EXPIRED'    => $ad_expired,
328 1
				'S_ENABLED'    => $ad_enabled,
329 1
				'U_ENABLE'     => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
330 1
				'U_EDIT'       => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
331 1
				'U_DELETE'     => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
332 1
			));
333 1
		}
334
335
		// Set output vars for display in the template
336 1
		$this->template->assign_vars(array(
337 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
338 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
339 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
340 1
		));
341 1
	}
342
343
	/**
344
	 * Perform general tasks
345
	 *
346
	 * @return void
347
	 */
348 9
	protected function setup()
349
	{
350 9
		if (!function_exists('user_get_id_name'))
351 9
		{
352
			include $this->root_path . 'includes/functions_user.' . $this->php_ext;
353
		}
354
355 9
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
356 9
		$this->language->add_lang('acp', 'phpbb/ads');
357
358 9
		$this->template->assign_var('S_PHPBB_ADS', true);
359 9
	}
360
361 12
	protected function get_submitted_action()
362
	{
363 12
		$actions = array('preview', 'submit', 'upload_banner', 'analyse_ad_code');
364 12
		foreach ($actions as $action)
365
		{
366 12
			if ($this->request->is_set_post($action))
367 12
			{
368 10
				return $action;
369
			}
370 10
		}
371
372 2
		return false;
373
	}
374
375 8
	protected function assign_data()
376
	{
377 8
		$this->helper->assign_locations($this->data['ad_locations']);
378 8
		$this->helper->assign_form_data($this->data);
379 8
		$this->helper->assign_errors($this->input->get_errors());
380 8
	}
381
382
	/**
383
	 * Enable/disable an advertisement
384
	 *
385
	 * @param    bool $enable Enable or disable the advertisement?
386
	 * @return void
387
	 */
388 6
	protected function ad_enable($enable)
389
	{
390 6
		$ad_id = $this->request->variable('id', 0);
391
392 6
		$success = $this->manager->update_ad($ad_id, array(
393 6
			'ad_enabled' => (int) $enable,
394 6
		));
395
396
		// If AJAX was used, show user a result message
397 6
		if ($this->request->is_ajax())
398 6
		{
399 2
			$json_response = new \phpbb\json_response;
400 2
			$json_response->send(array(
401 2
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
402 2
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
403 2
			));
404
		}
405
406
		// Otherwise, show traditional infobox
407
		if ($success)
408 4
		{
409 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
410
		}
411
		else
412
		{
413 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
414
		}
415
	}
416
417
	/**
418
	 * Prepare advertisement preview
419
	 *
420
	 * @return    void
421
	 */
422 2
	protected function preview()
423
	{
424 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code']));
425 2
	}
426
427 1
	protected function upload_banner()
428
	{
429 1
		$this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']);
430 1
	}
431
432 1
	protected function analyse_ad_code()
433
	{
434 1
		$this->analyser->run($this->data['ad_code']);
435 1
	}
436
437 6
	protected function submit()
438
	{
439 6
		if (!$this->input->has_errors())
440 6
		{
441 3
			$ad_id = $this->request->variable('id', 0);
442
			if ($ad_id)
443 1
			{
444
				$this->edit_ad($ad_id);
445
			}
446 1
			$this->add_ad();
447
		}
448 3
	}
449
450 8
	protected function set_output_vars($action, $ad_id)
451
	{
452
		// Set output vars for display in the template
453 8
		$this->template->assign_vars(array(
454 8
			'S_' . strtoupper($action) . '_AD'	=> true,
455 8
			'EDIT_ID'							=> $ad_id,
456 8
			'U_BACK'							=> $this->u_action,
457 8
			'U_ACTION'							=> "{$this->u_action}&amp;action=$action" . ($ad_id ? '&amp;id=' . $ad_id : ''),
458 8
			'PICKER_DATE_FORMAT'				=> input::DATE_FORMAT,
459 8
			'U_FIND_USERNAME'					=> $this->helper->get_find_username_link(),
460 8
		));
461 8
	}
462
463 1
	protected function add_ad()
464
	{
465 1
		$ad_id = $this->manager->insert_ad($this->data);
466 1
		$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
467
468 1
		$this->helper->log('ADD', $this->data['ad_name']);
469
470 1
		$this->success('ACP_AD_ADD_SUCCESS');
471
	}
472
473
	protected function edit_ad($ad_id)
474
	{
475
		$success = $this->manager->update_ad($ad_id, $this->data);
476
477
		if ($success)
478
		{
479
			// Only insert new ad locations to DB when ad exists
480
			$this->manager->delete_ad_locations($ad_id);
481
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
482
483
			$this->helper->log('EDIT', $this->data['ad_name']);
484
485
			$this->success('ACP_AD_EDIT_SUCCESS');
486
		}
487
488
		$this->error('ACP_AD_DOES_NOT_EXIST');
489
	}
490
491
	/**
492
	 * Print success message.
493
	 *
494
	 * It takes arguments in the form of a language key, followed by language substitution values.
495
	 */
496 5
	protected function success()
497
	{
498 5
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action));
499
	}
500
501
	/**
502
	 * Print error message.
503
	 *
504
	 * It takes arguments in the form of a language key, followed by language substitution values.
505
	 */
506 4
	protected function error()
507
	{
508 4
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
509
	}
510
}
511