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