Completed
Pull Request — master (#74)
by Jakub
09:44
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
		$no_ads = true;
383 1
		foreach ($this->manager->get_all_ads() as $row)
384
		{
385 1
			$no_ads = false;
386 1
			$ad_enabled = (int) $row['ad_enabled'];
387 1
			$ad_end_date = (int) $row['ad_end_date'];
388 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
389 1
			if ($ad_expired && $ad_enabled)
390 1
			{
391 1
				$ad_enabled = 0;
392 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
393 1
			}
394 1
			$ad_force_disabled = $ad_expired || ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit']) || ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit']);
395
396 1
			$this->template->assign_block_vars($ad_force_disabled ? 'force_disabled' : 'ads', array(
397 1
				'NAME'               => $row['ad_name'],
398 1
				'PRIORITY'			 => $row['ad_priority'],
399 1
				'END_DATE'           => $this->helper->prepare_end_date($ad_end_date),
400 1
				'VIEWS'              => $row['ad_views'],
401 1
				'CLICKS'             => $row['ad_clicks'],
402 1
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
403 1
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
404 1
				'S_END_DATE_EXPIRED' => $ad_expired,
405 1
				'S_ENABLED'          => $ad_enabled,
406 1
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
407 1
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
408 1
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
409 1
			));
410 1
		}
411
412
		// Set output vars for display in the template
413 1
		$this->template->assign_vars(array(
414 1
			'S_NO_ADS'		   => $no_ads,
415 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
416 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
417 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
418 1
		));
419 1
	}
420
421
	/**
422
	 * Perform general tasks
423
	 *
424
	 * @return void
425
	 */
426 9
	protected function setup()
427
	{
428 9
		if (!function_exists('user_get_id_name'))
429 9
		{
430
			include $this->root_path . 'includes/functions_user.' . $this->php_ext;
431
		}
432
433 9
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
434 9
		$this->language->add_lang('acp', 'phpbb/ads');
435
436 9
		$this->template->assign_var('S_PHPBB_ADS', true);
437 9
	}
438
439
	/**
440
	 * Enable/disable an advertisement
441
	 *
442
	 * @param    bool $enable Enable or disable the advertisement?
443
	 * @return void
444
	 */
445 6
	protected function ad_enable($enable)
446
	{
447 6
		$ad_id = $this->request->variable('id', 0);
448
449 6
		$success = $this->manager->update_ad($ad_id, array(
450 6
			'ad_enabled' => (int) $enable,
451 6
		));
452
453
		// If AJAX was used, show user a result message
454 6
		if ($this->request->is_ajax())
455 6
		{
456 2
			$json_response = new \phpbb\json_response;
457 2
			$json_response->send(array(
458 2
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
459 2
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
460 2
			));
461
		}
462
463
		// Otherwise, show traditional infobox
464
		if ($success)
465 4
		{
466 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
467
		}
468
		else
469
		{
470 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
471
		}
472
	}
473
474
	/**
475
	 * Prepare advertisement preview
476
	 *
477
	 * @param    string $code Ad code to preview
478
	 * @return    void
479
	 */
480 2
	protected function ad_preview($code)
481
	{
482 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
483 2
	}
484
485
	/**
486
	 * Print success message.
487
	 *
488
	 * It takes arguments in the form of a language key, followed by language substitution values.
489
	 */
490 6
	protected function success()
491
	{
492 6
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action));
493
	}
494
495
	/**
496
	 * Print error message.
497
	 *
498
	 * It takes arguments in the form of a language key, followed by language substitution values.
499
	 */
500 5
	protected function error()
501
	{
502 5
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
503
	}
504
}
505