Completed
Pull Request — master (#114)
by Jakub
14:25
created

admin_controller::action_edit()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3.0005

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 24
cts 25
cp 0.96
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 0
crap 3.0005
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\ext;
14
15
/**
16
* Admin controller
17
*/
18
class admin_controller
19
{
20
	/** @var array Form data */
21
	protected $data = array();
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/** @var \phpbb\language\language */
27
	protected $language;
28
29
	/** @var \phpbb\request\request */
30
	protected $request;
31
32
	/** @var \phpbb\ads\ad\manager */
33
	protected $manager;
34
35
	/** @var \phpbb\config\db_text */
36
	protected $config_text;
37
38
	/** @var \phpbb\config\config */
39
	protected $config;
40
41
	/** @var \phpbb\group\helper */
42
	protected $group_helper;
43
44
	/** @var \phpbb\ads\controller\admin_input */
45
	protected $input;
46
47
	/** @var \phpbb\ads\controller\helper */
48
	protected $helper;
49
50
	/** @var \phpbb\ads\analyser\manager */
51
	protected $analyser;
52
53
	/** @var \phpbb\controller\helper */
54
	protected $controller_helper;
55
56
	/** @var string Custom form action */
57
	protected $u_action;
58
59
	/** @var \auth_admin Auth admin */
60
	protected $auth_admin;
61
62
	/**
63
	 * Constructor
64
	 *
65
	 * @param \phpbb\template\template          $template          Template object
66
	 * @param \phpbb\language\language          $language          Language object
67
	 * @param \phpbb\request\request            $request           Request object
68
	 * @param \phpbb\ads\ad\manager             $manager           Advertisement manager object
69
	 * @param \phpbb\config\db_text             $config_text       Config text object
70
	 * @param \phpbb\config\config              $config            Config object
71
	 * @param \phpbb\group\helper               $group_helper      Group helper object
72
	 * @param \phpbb\ads\controller\admin_input $input             Admin input object
73
	 * @param \phpbb\ads\controller\helper      $helper            Helper object
74
	 * @param \phpbb\ads\analyser\manager       $analyser          Ad code analyser object
75
	 * @param \phpbb\controller\helper          $controller_helper Controller helper object
76
	 * @param string                            $root_path         phpBB root path
77
	 * @param string                            $php_ext           PHP extension
78
	 */
79 37
	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\helper $helper, \phpbb\ads\analyser\manager $analyser, \phpbb\controller\helper $controller_helper, $root_path, $php_ext)
80
	{
81 37
		$this->template = $template;
82 37
		$this->language = $language;
83 37
		$this->request = $request;
84 37
		$this->manager = $manager;
85 37
		$this->config_text = $config_text;
86 37
		$this->config = $config;
87 37
		$this->group_helper = $group_helper;
88 37
		$this->input = $input;
89 37
		$this->helper = $helper;
90 37
		$this->analyser = $analyser;
91 37
		$this->controller_helper = $controller_helper;
92
93 37
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
94 37
		$this->language->add_lang('acp', 'phpbb/ads');
95
96 37
		$this->template->assign_var('S_PHPBB_ADS', true);
97
98 37
		if (!class_exists('auth_admin'))
99 37
		{
100 1
			include($root_path . 'includes/acp/auth.' . $php_ext);
101 1
		}
102 37
		$this->auth_admin = new \auth_admin();
103 37
	}
104
105
	/**
106
	 * Set page url
107
	 *
108
	 * @param	string	$u_action	Custom form action
109
	 * @return	void
110
	 */
111 31
	public function set_page_url($u_action)
112
	{
113 31
		$this->u_action = $u_action;
114 31
	}
115
116
	/**
117
	 * Get ACP page title for Ads module
118
	 *
119
	 * @return	string	Language string for Ads ACP module
120
	 */
121 1
	public function get_page_title()
122
	{
123 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
124
	}
125
126
	/**
127
	 * Process user request for settings mode
128
	 *
129
	 * @return	void
130
	 */
131 3
	public function mode_settings()
132
	{
133 3
		if ($this->request->is_set_post('submit'))
134 3
		{
135
			// Validate form key
136 2
			if (check_form_key('phpbb_ads'))
137 2
			{
138 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
139 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
140 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
141 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
142
143 1
				$this->success('ACP_AD_SETTINGS_SAVED');
144
			}
145
146 1
			$this->error('FORM_INVALID');
147
		}
148
149 1
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
150 1
		$groups = $this->manager->load_groups();
151 1
		foreach ($groups as $group)
152
		{
153 1
			$this->template->assign_block_vars('groups', array(
154 1
				'ID'         => $group['group_id'],
155 1
				'NAME'       => $this->group_helper->get_name($group['group_name']),
156 1
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
157 1
			));
158 1
		}
159
160 1
		$this->template->assign_vars(array(
161 1
			'U_ACTION'          => $this->u_action,
162 1
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
163 1
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
164 1
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
165 1
		));
166 1
	}
167
168
	/**
169
	 * Process user request for manage mode
170
	 *
171
	 * @return	void
172
	 */
173 33
	public function mode_manage()
174
	{
175
		// Trigger specific action
176 33
		$action = $this->request->variable('action', '');
177 33
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
178 33
		{
179 31
			$this->{'action_' . $action}();
180 16
		}
181
		else
182
		{
183
			// Otherwise default to this
184 2
			$this->list_ads();
185
		}
186 18
	}
187
188
	/**
189
	 * Add an advertisement
190
	 *
191
	 * @return	void
192
	 */
193 21
	protected function action_add()
194
	{
195 8
		$action = $this->get_submitted_action();
196 21
		if ($action !== false)
197 8
		{
198 7
			$this->data = $this->input->get_form_data();
199 7
			$this->{$action}();
200 5
			$this->helper->assign_data($this->data, $this->input->get_errors());
201 5
		}
202
		else
203
		{
204 1
			$this->helper->assign_locations();
205
		}
206
207
		// Set output vars for display in the template
208 6
		$this->template->assign_vars(array(
209 6
			'S_ADD_AD'				=> true,
210 6
			'U_BACK'				=> $this->u_action,
211 6
			'U_ACTION'				=> "{$this->u_action}&amp;action=add",
212 6
			'PICKER_DATE_FORMAT'	=> ext::DATE_FORMAT,
213 6
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
214 6
			'U_ENABLE_VISUAL_DEMO'	=> $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'enable', 'hash' => generate_link_hash('visual_demo'))),
215 6
		));
216 6
	}
217
218
	/**
219
	 * Edit an advertisement
220
	 *
221
	 * @return	void
222
	 */
223 8
	protected function action_edit()
224
	{
225 8
		$ad_id = $this->request->variable('id', 0);
226 8
		$action = $this->get_submitted_action();
227 8
		if ($action !== false)
228 8
		{
229 6
			$this->data = $this->input->get_form_data();
230 6
			$this->{$action}();
231 3
		}
232
		else
233
		{
234 2
			$this->data = $this->manager->get_ad($ad_id);
235 2
			if (empty($this->data))
236 2
			{
237 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
238
			}
239
			// Load ad template locations
240 1
			$this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
241
		}
242
243
		// Set output vars for display in the template
244 4
		$this->template->assign_vars(array(
245 4
			'S_EDIT_AD'				=> true,
246 4
			'EDIT_ID'				=> $ad_id,
247 4
			'U_BACK'				=> $this->u_action,
248 4
			'U_ACTION'				=> "{$this->u_action}&amp;action=edit&amp;id=$ad_id",
249 4
			'PICKER_DATE_FORMAT'	=> ext::DATE_FORMAT,
250 4
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
251 4
			'U_ENABLE_VISUAL_DEMO'	=> $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'enable', 'hash' => generate_link_hash('visual_demo'))),
252 4
		));
253 4
		$this->helper->assign_data($this->data, $this->input->get_errors());
254 4
	}
255
256
	/**
257
	 * Enable an advertisement
258
	 *
259
	 * @return	void
260
	 */
261 4
	protected function action_enable()
262
	{
263 4
		$this->ad_enable(true);
264 1
	}
265
266
	/**
267
	 * Disable an advertisement
268
	 *
269
	 * @return	void
270
	 */
271 4
	protected function action_disable()
272
	{
273 4
		$this->ad_enable(false);
274 1
	}
275
276
	/**
277
	 * Delete an advertisement
278
	 *
279
	 * @return	void
280
	 */
281 4
	protected function action_delete()
282
	{
283 4
		$ad_id = $this->request->variable('id', 0);
284
		if ($ad_id)
285 4
		{
286 4
			if (confirm_box(true))
287 4
			{
288
				// Get ad data so that we can log ad name
289 3
				$ad_data = $this->manager->get_ad($ad_id);
290
291
				// Delete ad and it's template locations
292 3
				$this->manager->delete_ad_locations($ad_id);
293 3
				$success = $this->manager->delete_ad($ad_id);
294
295 3
				$this->toggle_permission($ad_data['ad_owner']);
296
297
				// Only notify user on error or if not ajax
298 3
				if (!$success)
299 3
				{
300 1
					$this->error('ACP_AD_DELETE_ERRORED');
301
				}
302
				else
303
				{
304 2
					$this->helper->log('DELETE', $ad_data['ad_name']);
305
306 2
					if (!$this->request->is_ajax())
307 2
					{
308 2
						$this->success('ACP_AD_DELETE_SUCCESS');
309
					}
310
				}
311
			}
312
			else
313
			{
314 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
315 1
					'id'     => $ad_id,
316 1
					'i'      => $this->request->variable('i', ''),
317 1
					'mode'   => $this->request->variable('mode', ''),
318 1
					'action' => 'delete',
319 1
				)));
320
321
				// When you don't confirm deleting ad
322 1
				$this->list_ads();
323
			}
324 1
		}
325 1
	}
326
327
	/**
328
	 * Display the list of all ads
329
	 *
330
	 * @return	void
331
	 */
332 2
	protected function list_ads()
333
	{
334 2
		foreach ($this->manager->get_all_ads() as $row)
335
		{
336 1
			$ad_enabled = (int) $row['ad_enabled'];
337 1
			$ad_expired = $this->helper->is_expired($row);
338
339 1 View Code Duplication
			if ($ad_expired && $ad_enabled)
0 ignored issues
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...
340 1
			{
341 1
				$ad_enabled = 0;
342 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
343 1
			}
344
345 1
			$this->template->assign_block_vars($ad_expired ? 'expired' : 'ads', array(
346 1
				'NAME'         => $row['ad_name'],
347 1
				'PRIORITY'     => $row['ad_priority'],
348 1
				'END_DATE'     => $row['ad_end_date'],
349 1
				'VIEWS'        => $row['ad_views'],
350 1
				'CLICKS'       => $row['ad_clicks'],
351 1
				'VIEWS_LIMIT'  => $row['ad_views_limit'],
352 1
				'CLICKS_LIMIT' => $row['ad_clicks_limit'],
353 1
				'S_EXPIRED'    => $ad_expired,
354 1
				'S_ENABLED'    => $ad_enabled,
355 1
				'U_ENABLE'     => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
356 1
				'U_EDIT'       => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
357 1
				'U_DELETE'     => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
358 1
			));
359 2
		}
360
361
		// Set output vars for display in the template
362 2
		$this->template->assign_vars(array(
363 2
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
364 2
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
365 2
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
366 2
		));
367 2
	}
368
369
	/**
370
	 * Get what action user wants to do with the form.
371
	 * Possible options are:
372
	 *  - preview ad code
373
	 *  - upload banner to display in an ad code
374
	 *  - analyse ad code
375
	 *  - submit form (either add or edit an ad)
376
	 *
377
	 * @return	string|false	Action name or false when no action was submitted
378
	 */
379 16
	protected function get_submitted_action()
380
	{
381 16
		$actions = array('preview', 'upload_banner', 'analyse_ad_code', 'submit_add', 'submit_edit');
382 16
		foreach ($actions as $action)
383
		{
384 16
			if ($this->request->is_set_post($action))
385 16
			{
386 13
				return $action;
387
			}
388 14
		}
389
390 3
		return false;
391
	}
392
393
	/**
394
	 * Enable/disable an advertisement
395
	 *
396
	 * @param	bool	$enable	Enable or disable the advertisement?
397
	 * @return	void
398
	 */
399 6
	protected function ad_enable($enable)
400
	{
401 6
		$ad_id = $this->request->variable('id', 0);
402
403 6
		$success = $this->manager->update_ad($ad_id, array(
404 6
			'ad_enabled' => (int) $enable,
405 6
		));
406
407
		// If AJAX was used, show user a result message
408 6
		if ($this->request->is_ajax())
409 6
		{
410 2
			$json_response = new \phpbb\json_response;
411 2
			$json_response->send(array(
412 2
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
413 2
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
414 2
			));
415
		}
416
417
		// Otherwise, show traditional infobox
418
		if ($success)
419 4
		{
420 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
421
		}
422
		else
423
		{
424 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
425
		}
426
	}
427
428
	/**
429
	 * Submit action "preview".
430
	 * Prepare advertisement preview.
431
	 *
432
	 * @return	void
433
	 */
434 2
	protected function preview()
435
	{
436 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code']));
437 2
	}
438
439
	/**
440
	 * Submit action "upload_banner".
441
	 * Upload banner and append it to the ad code.
442
	 *
443
	 * @return	void
444
	 */
445 1
	protected function upload_banner()
446
	{
447 1
		$this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']);
448 1
	}
449
450
	/**
451
	 * Submit action "analyse_ad_code".
452
	 * Upload banner and append it to the ad code.
453
	 *
454
	 * @return	void
455
	 */
456 1
	protected function analyse_ad_code()
457
	{
458 1
		$this->analyser->run($this->data['ad_code']);
459 1
	}
460
461
	/**
462
	 * Submit action "submit_add".
463
	 * Add new ad.
464
	 *
465
	 * @return	void
466
	 */
467 4
	protected function submit_add()
468
	{
469 4
		if (!$this->input->has_errors())
470 4
		{
471 2
			$ad_id = $this->manager->insert_ad($this->data);
472 2
			$this->toggle_permission($this->data['ad_owner']);
473 2
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
474
475 2
			$this->helper->log('ADD', $this->data['ad_name']);
476
477 2
			$this->success('ACP_AD_ADD_SUCCESS');
478
		}
479 2
	}
480
481
	/**
482
	 * Submit action "submit_edit".
483
	 * Edit ad.
484
	 *
485
	 * @return	void
486
	 */
487 5
	protected function submit_edit()
488
	{
489 5
		$ad_id = $this->request->variable('id', 0);
490 5
		if ($ad_id && !$this->input->has_errors())
491 5
		{
492 3
			$old_data = $this->manager->get_ad($ad_id);
493 3
			$success = $this->manager->update_ad($ad_id, $this->data);
494
			if ($success)
495 3
			{
496
				// Only update permissions when update was successful
497 2
				$this->toggle_permission($old_data['ad_owner']);
498 2
				$this->toggle_permission($this->data['ad_owner']);
499
500
				// Only insert new ad locations to DB when ad exists
501 2
				$this->manager->delete_ad_locations($ad_id);
502 2
				$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
503
504 2
				$this->helper->log('EDIT', $this->data['ad_name']);
505
506 2
				$this->success('ACP_AD_EDIT_SUCCESS');
507
			}
508
509 1
			$this->error('ACP_AD_DOES_NOT_EXIST');
510
		}
511 2
	}
512
513
	/**
514
	 * Print success message.
515
	 *
516
	 * @param	string	$msg	Message lang key
517
	 */
518 9
	protected function success($msg)
519
	{
520 9
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action));
521
	}
522
523
	/**
524
	 * Print error message.
525
	 *
526
	 * @param	string	$msg	Message lang key
527
	 */
528 6
	protected function error($msg)
529
	{
530 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action), E_USER_WARNING);
531
	}
532
533
	/**
534
	 * Try to remove or add permission to see UCP module.
535
	 * Permission is only removed when user has no more ads.
536
	 * Permission is only added when user has at least one ad.
537
	 *
538
	 * @param	int	$user_id	User ID to try to remove permission
539
	 *
540
	 * @return	void
541
	 */
542 7
	protected function toggle_permission($user_id)
543
	{
544
		if ($user_id)
545 7
		{
546 3
			$has_ads = count($this->manager->get_ads_by_owner($user_id)) !== 0;
547
548 3
			$this->auth_admin->acl_set('user', 0, $user_id, array('u_phpbb_ads' => (int) $has_ads));
549 3
		}
550 7
	}
551
}
552