Completed
Pull Request — master (#113)
by Jakub
18:13
created

admin_controller::toggle_permission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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