Completed
Push — master ( 435892...45f10c )
by Matt
20:55
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

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