Completed
Pull Request — master (#89)
by Matt
11:05
created

admin_controller::get_submitted_action()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3
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\admin_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
	/**
57
	 * Constructor
58
	 *
59
	 * @param \phpbb\template\template           $template     Template object
60
	 * @param \phpbb\language\language           $language     Language object
61
	 * @param \phpbb\request\request             $request      Request object
62
	 * @param \phpbb\ads\ad\manager              $manager      Advertisement manager object
63
	 * @param \phpbb\config\db_text              $config_text  Config text object
64
	 * @param \phpbb\config\config               $config       Config object
65
	 * @param \phpbb\group\helper                $group_helper Group helper object
66
	 * @param \phpbb\ads\controller\admin_input  $input        Admin input object
67
	 * @param \phpbb\ads\controller\admin_helper $helper       Admin helper object
68
	 * @param \phpbb\ads\analyser\manager        $analyser     Ad code analyser object
69
	 * @param string                             $root_path    phpBB root path
70
	 * @param string                             $php_ext      PHP extension
71
	 */
72 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\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext)
0 ignored issues
show
Unused Code introduced by
The parameter $root_path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $php_ext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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