Completed
Pull Request — master (#123)
by Jakub
12:38
created

admin_controller::submit_add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.004
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
				'END_DATE'     => $row['ad_end_date'],
333 1
				'VIEWS'        => $row['ad_views'],
334 1
				'CLICKS'       => $row['ad_clicks'],
335 1
				'VIEWS_LIMIT'  => $row['ad_views_limit'],
336 1
				'CLICKS_LIMIT' => $row['ad_clicks_limit'],
337 1
				'S_EXPIRED'    => $ad_expired,
338 1
				'S_ENABLED'    => $ad_enabled,
339 1
				'U_ENABLE'     => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
340 1
				'U_EDIT'       => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
341 1
				'U_DELETE'     => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
342 1
			));
343 2
		}
344
345
		// Set output vars for display in the template
346 2
		$this->template->assign_vars(array(
347 2
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
348 2
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
349 2
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
350 2
		));
351 2
	}
352
353
	/**
354
	 * Get what action user wants to do with the form.
355
	 * Possible options are:
356
	 *  - preview ad code
357
	 *  - upload banner to display in an ad code
358
	 *  - analyse ad code
359
	 *  - submit form (either add or edit an ad)
360
	 *
361
	 * @return	string|false	Action name or false when no action was submitted
362
	 */
363 16
	protected function get_submitted_action()
364
	{
365 16
		$actions = array('preview', 'upload_banner', 'analyse_ad_code', 'submit_add', 'submit_edit');
366 16
		foreach ($actions as $action)
367
		{
368 16
			if ($this->request->is_set_post($action))
369 16
			{
370 13
				return $action;
371
			}
372 14
		}
373
374 3
		return false;
375
	}
376
377
	/**
378
	 * Enable/disable an advertisement
379
	 *
380
	 * @param	bool	$enable	Enable or disable the advertisement?
381
	 * @return	void
382
	 */
383 6
	protected function ad_enable($enable)
384
	{
385 6
		$ad_id = $this->request->variable('id', 0);
386
387 6
		$success = $this->manager->update_ad($ad_id, array(
388 6
			'ad_enabled' => (int) $enable,
389 6
		));
390
391
		// If AJAX was used, show user a result message
392 6
		if ($this->request->is_ajax())
393 6
		{
394 2
			$json_response = new \phpbb\json_response;
395 2
			$json_response->send(array(
396 2
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
397 2
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
398 2
			));
399
		}
400
401
		// Otherwise, show traditional infobox
402
		if ($success)
403 4
		{
404 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
405
		}
406
		else
407
		{
408 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
409
		}
410
	}
411
412
	/**
413
	 * Submit action "preview".
414
	 * Prepare advertisement preview.
415
	 *
416
	 * @return	void
417
	 */
418 2
	protected function preview()
419
	{
420 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code']));
421 2
	}
422
423
	/**
424
	 * Submit action "upload_banner".
425
	 * Upload banner and append it to the ad code.
426
	 *
427
	 * @return	void
428
	 */
429 1
	protected function upload_banner()
430
	{
431 1
		$this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']);
432 1
	}
433
434
	/**
435
	 * Submit action "analyse_ad_code".
436
	 * Upload banner and append it to the ad code.
437
	 *
438
	 * @return	void
439
	 */
440 1
	protected function analyse_ad_code()
441
	{
442 1
		$this->analyser->run($this->data['ad_code']);
443 1
	}
444
445
	/**
446
	 * Submit action "submit_add".
447
	 * Add new ad.
448
	 *
449
	 * @return	void
450
	 */
451 4
	protected function submit_add()
452
	{
453 4
		if (!$this->input->has_errors())
454 4
		{
455 2
			$ad_id = $this->manager->insert_ad($this->data);
456 2
			$this->toggle_permission($this->data['ad_owner']);
457 2
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
458
459 2
			$this->helper->log('ADD', $this->data['ad_name']);
460
461 2
			$this->success('ACP_AD_ADD_SUCCESS');
462
		}
463 2
	}
464
465
	/**
466
	 * Submit action "submit_edit".
467
	 * Edit ad.
468
	 *
469
	 * @return	void
470
	 */
471 5
	protected function submit_edit()
472
	{
473 5
		$ad_id = $this->request->variable('id', 0);
474 5
		if ($ad_id && !$this->input->has_errors())
475 5
		{
476 3
			$old_data = $this->manager->get_ad($ad_id);
477 3
			$success = $this->manager->update_ad($ad_id, $this->data);
478
			if ($success)
479 3
			{
480
				// Only update permissions when update was successful
481 2
				$this->toggle_permission($old_data['ad_owner']);
482 2
				$this->toggle_permission($this->data['ad_owner']);
483
484
				// Only insert new ad locations to DB when ad exists
485 2
				$this->manager->delete_ad_locations($ad_id);
486 2
				$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
487
488 2
				$this->helper->log('EDIT', $this->data['ad_name']);
489
490 2
				$this->success('ACP_AD_EDIT_SUCCESS');
491
			}
492
493 1
			$this->error('ACP_AD_DOES_NOT_EXIST');
494
		}
495 2
	}
496
497
	/**
498
	 * Print success message.
499
	 *
500
	 * @param	string	$msg	Message lang key
501
	 */
502 9
	protected function success($msg)
503
	{
504 9
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action));
505
	}
506
507
	/**
508
	 * Print error message.
509
	 *
510
	 * @param	string	$msg	Message lang key
511
	 */
512 6
	protected function error($msg)
513
	{
514 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action), E_USER_WARNING);
515
	}
516
517
	/**
518
	 * Try to remove or add permission to see UCP module.
519
	 * Permission is only removed when user has no more ads.
520
	 * Permission is only added when user has at least one ad.
521
	 *
522
	 * @param	int	$user_id	User ID to try to remove permission
523
	 *
524
	 * @return	void
525
	 */
526 7
	protected function toggle_permission($user_id)
527
	{
528
		if ($user_id)
529 7
		{
530 3
			$has_ads = count($this->manager->get_ads_by_owner($user_id)) !== 0;
531
532 3
			$this->auth_admin->acl_set('user', 0, $user_id, array('u_phpbb_ads' => (int) $has_ads));
533 3
		}
534 7
	}
535
}
536