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