Completed
Pull Request — master (#54)
by Jakub
05:19
created

admin_controller::action_edit()   C

Complexity

Conditions 9
Paths 7

Size

Total Lines 63
Code Lines 35

Duplication

Lines 21
Ratio 33.33 %

Code Coverage

Tests 36
CRAP Score 9.081

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 21
loc 63
ccs 36
cts 40
cp 0.9
rs 6.6149
cc 9
eloc 35
nc 7
nop 0
crap 9.081

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\user */
24
	protected $user;
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\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\user                        $user             User object
58
	* @param \phpbb\request\request             $request          Request object
59
	* @param \phpbb\ads\ad\manager              $manager          Advertisement manager object
60
	* @param \phpbb\config\db_text              $config_text      Config text object
61
	* @param \phpbb\config\config               $config           Config 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
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, $root_path, $php_ext)
68
	{
69
		$this->template = $template;
70
		$this->user = $user;
71
		$this->request = $request;
72
		$this->manager = $manager;
73
		$this->config_text = $config_text;
74
		$this->config = $config;
75
		$this->input = $input;
76
		$this->helper = $helper;
77
		$this->root_path = $root_path;
78
		$this->php_ext = $php_ext;
79
	}
80
81
	/**
82
	 * Process user request for manage mode
83
	 *
84 48
	 * @return void
85
	 */
86 48
	public function mode_manage()
87 48
	{
88 48
		$this->setup();
89 48
90 48
		// Trigger specific action
91 48
		$action = $this->request->variable('action', '');
92 48
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
93 48
		{
94 48
			$this->{'action_' . $action}();
95 48
		}
96 48
97 48
		// Otherwise default to this
98 48
		$this->list_ads();
99 48
	}
100
101
	/**
102
	 * Process user request for settings mode
103
	 *
104
	 * @return void
105
	 */
106 6
	public function mode_settings()
107
	{
108 6
		$this->setup();
109
110 6
		add_form_key('phpbb/ads/settings');
111 6
		if ($this->request->is_set_post('submit'))
112
		{
113
			// Validate form key
114
			if (check_form_key('phpbb/ads/settings'))
115
			{
116 6
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
117 6
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
118 6
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
119 5
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
120 5
121
				$this->success('ACP_AD_SETTINGS_SAVED');
122
			}
123 6
124 6
			$this->helper->assign_errors(array($this->user->lang('FORM_INVALID')));
125
		}
126
127
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
128
		$groups = $this->manager->load_groups();
129
		foreach ($groups as $group)
130
		{
131 3
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
132
133 3
			$this->template->assign_block_vars('groups', array(
134
				'ID'         => $group['group_id'],
135 3
				'NAME'       => $group_name,
136 3
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
137 3
			));
138
		}
139 2
140 2
		$this->template->assign_vars(array(
141 1
			'U_ACTION'          => $this->u_action,
142 1
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
143
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
144 2
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
145 2
		));
146 1
	}
147 1
148 1
	/**
149 1
	 * Set page url
150
	 *
151 1
	 * @param string $u_action Custom form action
152
	 * @return void
153
	 */
154 1
	public function set_page_url($u_action)
155 1
	{
156 1
		$this->u_action = $u_action;
157 1
	}
158 1
159
	/**
160 2
	 * Get ACP page title for Ads module
161 2
	 *
162 2
	 * @return string    Language string for Ads ACP module
163
	 */
164 2
	public function get_page_title()
165
	{
166 2
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
167 2
	}
168 2
169 2
	/**
170 2
	 * Add an advertisement
171 2
	 *
172
	 * @return void
173 2
	 */
174 2
	public function action_add()
175 2
	{
176 2
		$preview = $this->request->is_set_post('preview');
177 2
		$submit = $this->request->is_set_post('submit');
178 2
		$upload_banner = $this->request->is_set_post('upload_banner');
179 2
180
		add_form_key('phpbb/ads/add');
181
		if ($preview || $submit || $upload_banner)
182
		{
183
			$data = $this->input->get_form_data('phpbb/ads/add');
184
185 View Code Duplication
			if ($preview)
186
			{
187 42
				$this->ad_preview($data['ad_code']);
188
			}
189 42
			else if ($upload_banner)
190 42
			{
191
				$data['ad_code'] = $this->input->banner_upload($data['ad_code']);
192
			}
193
			else if (!$this->input->has_errors())
194
			{
195
				$ad_id = $this->manager->insert_ad($data);
196
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
197 1
198
				$this->helper->log('ADD', $data['ad_name']);
199 1
200
				$this->success('ACP_AD_ADD_SUCCESS');
201
			}
202
203
			$this->helper->assign_locations($data['ad_locations']);
204
			$this->helper->assign_form_data($data);
205
			$this->helper->assign_errors($this->input->get_errors());
206
		}
207 15
		else
208
		{
209 15
			$this->helper->assign_locations();
210 15
		}
211 15
212
		// Set output vars for display in the template
213 15
		$this->template->assign_vars(array(
214 15
			'S_ADD_AD'           => true,
215 15
			'U_BACK'             => $this->u_action,
216 14
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
217
			'PICKER_DATE_FORMAT' => input::DATE_FORMAT,
218
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
219 14
		));
220 1
	}
221 1
222
	/**
223 13
	 * Edit an advertisement
224 2
	 *
225 2
	 * @return void
226
	 */
227 11
	public function action_edit()
228 1
	{
229 1
		$ad_id = $this->request->variable('id', 0);
230
		$preview = $this->request->is_set_post('preview');
231 1
		$submit = $this->request->is_set_post('submit');
232
		$upload_banner = $this->request->is_set_post('upload_banner');
233 1
234
		add_form_key('phpbb/ads/edit/' . $ad_id);
235
		if ($preview || $submit || $upload_banner)
236 13
		{
237 13
			$data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
238 13
239
			if ($preview)
240
			{
241 1
				$this->ad_preview($data['ad_code']);
242
			}
243 View Code Duplication
			else if ($upload_banner)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
			{
245 14
				$data['ad_code'] = $this->input->banner_upload($data['ad_code']);
246 14
			}
247 14
			else if (!$this->input->has_errors())
248 14
			{
249 14
				$success = $this->manager->update_ad($ad_id, $data);
250 14
251 14
				if ($success)
252 14
				{
253
					// Only insert new ad locations to DB when ad exists
254
					$this->manager->delete_ad_locations($ad_id);
255
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
256
257
					$this->helper->log('EDIT', $data['ad_name']);
258
259 15
					$this->success('ACP_AD_EDIT_SUCCESS');
260
				}
261 15
262 15
				$this->error('ACP_AD_DOES_NOT_EXIST');
263 15
			}
264 15
		}
265
		else
266 15
		{
267 15
			$data = $this->manager->get_ad($ad_id);
268 15
			if (empty($data))
269 13
			{
270
				$this->error('ACP_AD_DOES_NOT_EXIST');
271
			}
272 13
273 1
			// Load ad template locations
274 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
275
		}
276 12
277
		// Set output vars for display in the template
278
		$this->template->assign_vars(array(
279
			'S_EDIT_AD'          => true,
280 12
			'EDIT_ID'            => $ad_id,
281 2
			'U_BACK'             => $this->u_action,
282
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
283
			'PICKER_DATE_FORMAT' => input::DATE_FORMAT,
284 2
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
285
		));
286 1
		$this->helper->assign_locations($data['ad_locations']);
287 1
		$this->helper->assign_form_data($data);
288
		$this->helper->assign_errors($this->input->get_errors());
289 1
	}
290
291 1
	/**
292
	 * Enable an advertisement
293
	 *
294 1
	 * @return void
295
	 */
296 11
	public function action_enable()
297
	{
298
		$this->ad_enable(true);
299 2
	}
300 2
301 2
	/**
302 1
	 * Disable an advertisement
303
	 *
304
	 * @return void
305
	 */
306 1
	public function action_disable()
307
	{
308
		$this->ad_enable(false);
309
	}
310 12
311 12
	/**
312 12
	 * Delete an advertisement
313 12
	 *
314 12
	 * @return void
315 12
	 */
316 12
	public function action_delete()
317 12
	{
318 12
		$ad_id = $this->request->variable('id', 0);
319 12
		if ($ad_id)
320 12
		{
321
			if (confirm_box(true))
322
			{
323
				// Get ad data so that we can log ad name
324
				$ad_data = $this->manager->get_ad($ad_id);
325
326
				// Delete ad and it's template locations
327 3
				$this->manager->delete_ad_locations($ad_id);
328
				$success = $this->manager->delete_ad($ad_id);
329 3
330 1
				// Only notify user on error or if not ajax
331
				if (!$success)
332
				{
333
					$this->error('ACP_AD_DELETE_ERRORED');
334
				}
335
				else
336
				{
337 3
					$this->helper->log('DELETE', $ad_data['ad_name']);
338
339 3
					if (!$this->request->is_ajax())
340 1
					{
341
						$this->success('ACP_AD_DELETE_SUCCESS');
342
					}
343
				}
344
			}
345
			else
346
			{
347 3
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
348
					'id'     => $ad_id,
349 3
					'i'      => $this->request->variable('i', ''),
350
					'mode'   => $this->request->variable('mode', ''),
351 3
					'action' => 'delete'
352 3
				)));
353 3
			}
354
		}
355 2
	}
356
357
	/**
358 2
	 * Display the ads
359 2
	 *
360
	 * @return void
361
	 */
362 2
	public function list_ads()
363 2
	{
364 1
		foreach ($this->manager->get_all_ads() as $row)
365
		{
366
			$ad_enabled = (int) $row['ad_enabled'];
367
			$ad_end_date = (int) $row['ad_end_date'];
368 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
369
			if ($ad_expired && $ad_enabled)
370 1
			{
371 1
				$ad_enabled = 0;
372 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
373
			}
374
375
			$this->template->assign_block_vars('ads', array(
376
				'NAME'               => $row['ad_name'],
377
				'END_DATE'           => $ad_end_date ? $this->user->format_date($ad_end_date, input::DATE_FORMAT) : '',
378 1
				'VIEWS'              => $row['ad_views'],
379 1
				'CLICKS'             => $row['ad_clicks'],
380 1
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
381 1
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
382
				'S_END_DATE_EXPIRED' => $ad_expired,
383 1
				'S_ENABLED'          => $ad_enabled,
384
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
385 1
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
386 1
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
387
			));
388
		}
389
390
		// Set output vars for display in the template
391
		$this->template->assign_vars(array(
392
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
393 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
394
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
395 1
		));
396
	}
397 1
398 1
	/**
399 1
	 * Perform general tasks
400 1
	 *
401 1
	 * @return void
402 1
	 */
403 1
	protected function setup()
404 1
	{
405
		if (!function_exists('user_get_id_name'))
406 1
		{
407 1
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
408 1
		}
409 1
410 1
		$this->user->add_lang('posting'); // Used by banner_upload() file errors
411 1
		$this->user->add_lang_ext('phpbb/ads', 'acp');
412 1
413 1
		$this->template->assign_var('S_PHPBB_ADS', true);
414 1
	}
415 1
416 1
	/**
417 1
	 * Enable/disable an advertisement
418 1
	 *
419 1
	 * @param    bool $enable Enable or disable the advertisement?
420
	 * @return void
421
	 */
422 1
	protected function ad_enable($enable)
423 1
	{
424 1
		$ad_id = $this->request->variable('id', 0);
425 1
426 1
		$success = $this->manager->update_ad($ad_id, array(
427 1
			'ad_enabled' => (int) $enable,
428
		));
429
430
		// If AJAX was used, show user a result message
431
		if ($this->request->is_ajax())
432
		{
433
			$json_response = new \phpbb\json_response;
434 9
			$json_response->send(array(
435
				'text'  => $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
436 9
				'title' => $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
437 9
			));
438
		}
439 9
440 9
		// Otherwise, show traditional infobox
441
		if ($success)
442
		{
443
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
444
		}
445
		else
446
		{
447
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
448 4
		}
449
	}
450 4
451
	/**
452 4
	 * Prepare advertisement preview
453 4
	 *
454 4
	 * @param    string $code Ad code to preview
455
	 * @return    void
456
	 */
457 4
	protected function ad_preview($code)
458 4
	{
459
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
460
	}
461
462
	/**
463
	 * Print success message.
464
	 *
465
	 * It takes arguments in the form of a language key, followed by language substitution values.
466
	 */
467
	protected function success()
468 4
	{
469 2
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
470
	}
471
472
	/**
473 2
	 * Print error message.
474
	 *
475
	 * It takes arguments in the form of a language key, followed by language substitution values.
476
	 */
477
	protected function error()
478
	{
479
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
480
	}
481
}
482