Completed
Pull Request — master (#56)
by Jakub
34:52 queued 16s
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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