Completed
Pull Request — master (#114)
by Jakub
13:20
created

admin_controller::set_page_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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