Completed
Pull Request — master (#61)
by Matt
09:57
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

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