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