Completed
Push — master ( 7d29c5...8ac802 )
by Jakub
20s
created

admin_controller::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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