Completed
Pull Request — master (#86)
by Matt
35:08 queued 32:59
created

admin_controller::action_delete()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0384

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 23
cts 26
cp 0.8846
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 5
nop 0
crap 5.0384
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\controller\admin_input as input;
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)
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
		if (!function_exists('user_get_id_name'))
86 33
		{
87
			include $root_path . 'includes/functions_user.' . $php_ext;
88
		}
89
90 33
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
91 33
		$this->language->add_lang('acp', 'phpbb/ads');
92
93 33
		$this->template->assign_var('S_PHPBB_ADS', true);
94 33
	}
95
96
	/**
97
	 * Set page url
98
	 *
99
	 * @param	string	$u_action	Custom form action
100
	 * @return	void
101
	 */
102 27
	public function set_page_url($u_action)
103
	{
104 27
		$this->u_action = $u_action;
105 27
	}
106
107
	/**
108
	 * Get ACP page title for Ads module
109
	 *
110
	 * @return	string	Language string for Ads ACP module
111
	 */
112 1
	public function get_page_title()
113
	{
114 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
115
	}
116
117
	/**
118
	 * Process user request for settings mode
119
	 *
120
	 * @return	void
121
	 */
122 3
	public function mode_settings()
123
	{
124 3
		if ($this->request->is_set_post('submit'))
125 3
		{
126
			// Validate form key
127 2
			if (check_form_key('phpbb_ads'))
128 2
			{
129 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
130 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
131 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
132 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
133
134 1
				$this->success('ACP_AD_SETTINGS_SAVED');
135
			}
136
137 1
			$this->error('FORM_INVALID');
138
		}
139
140 1
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
141 1
		$groups = $this->manager->load_groups();
142 1 View Code Duplication
		foreach ($groups as $group)
143
		{
144 1
			$this->template->assign_block_vars('groups', array(
145 1
				'ID'         => $group['group_id'],
146 1
				'NAME'       => $this->group_helper->get_name($group['group_name']),
147 1
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
148 1
			));
149 1
		}
150
151 1
		$this->template->assign_vars(array(
152 1
			'U_ACTION'          => $this->u_action,
153 1
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
154 1
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
155 1
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
156 1
		));
157 1
	}
158
159
	/**
160
	 * Process user request for manage mode
161
	 *
162
	 * @return	void
163
	 */
164 29
	public function mode_manage()
165
	{
166
		// Trigger specific action
167 29
		$action = $this->request->variable('action', '');
168 29
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
169 29
		{
170 27
			$this->{'action_' . $action}();
171 15
		}
172
		else
173
		{
174
			// Otherwise default to this
175 2
			$this->list_ads();
176
		}
177 17
	}
178
179
	/**
180
	 * Add an advertisement
181
	 *
182
	 * @return	void
183
	 */
184 12
	protected function action_add()
185
	{
186 6
		$action = $this->get_submitted_action();
187 6
		if ($action !== false)
188 6
		{
189 5
			$this->data = $this->input->get_form_data();
190 5
			$this->{$action}();
191 4
			$this->helper->assign_data($this->data, $this->input->get_errors());
192 4
		}
193
		else
194
		{
195 1
			$this->helper->assign_locations();
196 12
		}
197
198
		// Set output vars for display in the template
199 5
		$this->template->assign_vars(array(
200 5
			'S_ADD_AD'				=> true,
201 5
			'U_BACK'				=> $this->u_action,
202 5
			'U_ACTION'				=> "{$this->u_action}&amp;action=add",
203 5
			'PICKER_DATE_FORMAT'	=> input::DATE_FORMAT,
204 5
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
205 5
		));
206 5
	}
207
208
	/**
209
	 * Edit an advertisement
210
	 *
211
	 * @return	void
212
	 */
213 7
	protected function action_edit()
214
	{
215 7
		$ad_id = $this->request->variable('id', 0);
216 7
		$action = $this->get_submitted_action();
217 7
		if ($action !== false)
218 7
		{
219 5
			$this->data = $this->input->get_form_data();
220 5
			$this->{$action}();
221 3
		}
222
		else
223
		{
224 2
			$this->data = $this->manager->get_ad($ad_id);
225 2
			if (empty($this->data))
226 2
			{
227 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
228
			}
229
			// Load ad template locations
230 1
			$this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
231
		}
232
233
		// Set output vars for display in the template
234 4
		$this->template->assign_vars(array(
235 4
			'S_EDIT_AD'				=> true,
236 4
			'EDIT_ID'				=> $ad_id,
237 4
			'U_BACK'				=> $this->u_action,
238 4
			'U_ACTION'				=> "{$this->u_action}&amp;action=edit&amp;id=$ad_id",
239 4
			'PICKER_DATE_FORMAT'	=> input::DATE_FORMAT,
240 4
			'U_FIND_USERNAME'		=> $this->helper->get_find_username_link(),
241 4
		));
242 4
		$this->helper->assign_data($this->data, $this->input->get_errors());
243 4
	}
244
245
	/**
246
	 * Enable an advertisement
247
	 *
248
	 * @return	void
249
	 */
250 4
	protected function action_enable()
251
	{
252 4
		$this->ad_enable(true);
253 1
	}
254
255
	/**
256
	 * Disable an advertisement
257
	 *
258
	 * @return	void
259
	 */
260 4
	protected function action_disable()
261
	{
262 4
		$this->ad_enable(false);
263 1
	}
264
265
	/**
266
	 * Delete an advertisement
267
	 *
268
	 * @return	void
269
	 */
270 3
	protected function action_delete()
271
	{
272 3
		$ad_id = $this->request->variable('id', 0);
273
		if ($ad_id)
274 3
		{
275 3
			if (confirm_box(true))
276 3
			{
277
				// Get ad data so that we can log ad name
278 2
				$ad_data = $this->manager->get_ad($ad_id);
279
280
				// Delete ad and it's template locations
281 2
				$this->manager->delete_ad_locations($ad_id);
282 2
				$success = $this->manager->delete_ad($ad_id);
283
284
				// Only notify user on error or if not ajax
285 2
				if (!$success)
286 2
				{
287 1
					$this->error('ACP_AD_DELETE_ERRORED');
288
				}
289
				else
290
				{
291 1
					$this->helper->log('DELETE', $ad_data['ad_name']);
292
293 1
					if (!$this->request->is_ajax())
294 1
					{
295 1
						$this->success('ACP_AD_DELETE_SUCCESS');
296
					}
297
				}
298
			}
299
			else
300
			{
301 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
302 1
					'id'     => $ad_id,
303 1
					'i'      => $this->request->variable('i', ''),
304 1
					'mode'   => $this->request->variable('mode', ''),
305 1
					'action' => 'delete',
306 1
				)));
307
			}
308 1
		}
309 1
	}
310
311
	/**
312
	 * Display the list of all ads
313
	 *
314
	 * @return	void
315
	 */
316 1
	protected function list_ads()
317
	{
318 1
		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
			if ($ad_expired && $ad_enabled)
324 1
			{
325
				$ad_enabled = 0;
326
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
327
			}
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 1
		}
344
345
		// Set output vars for display in the template
346 1
		$this->template->assign_vars(array(
347 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
348 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
349 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
350 1
		));
351 1
	}
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	mixed	Action name or false when no action was submitted
362
	 */
363 13
	protected function get_submitted_action()
364
	{
365 13
		$actions = array('preview', 'upload_banner', 'analyse_ad_code', 'submit_add', 'submit_edit');
366 13
		foreach ($actions as $action)
367
		{
368 13
			if ($this->request->is_set_post($action))
369 13
			{
370 10
				return $action;
371
			}
372 11
		}
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 2
	protected function submit_add()
452
	{
453 2
		if (!$this->input->has_errors())
454 2
		{
455 1
			$ad_id = $this->manager->insert_ad($this->data);
456 1
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
457
458 1
			$this->helper->log('ADD', $this->data['ad_name']);
459
460 1
			$this->success('ACP_AD_ADD_SUCCESS');
461
		}
462 1
	}
463
464
	/**
465
	 * Submit action "submit_edit".
466
	 * Edit ad.
467
	 *
468
	 * @return	void
469
	 */
470 4
	protected function submit_edit()
471
	{
472 4
		$ad_id = $this->request->variable('id', 0);
473 4
		if ($ad_id && !$this->input->has_errors())
474 4
		{
475 2
			$success = $this->manager->update_ad($ad_id, $this->data);
476
			if ($success)
477 2
			{
478
				// Only insert new ad locations to DB when ad exists
479 1
				$this->manager->delete_ad_locations($ad_id);
480 1
				$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
481
482 1
				$this->helper->log('EDIT', $this->data['ad_name']);
483
484 1
				$this->success('ACP_AD_EDIT_SUCCESS');
485
			}
486
487 1
			$this->error('ACP_AD_DOES_NOT_EXIST');
488
		}
489 2
	}
490
491
	/**
492
	 * Print success message.
493
	 *
494
	 * @param	string	$msg	Message lang key
495
	 */
496 6
	protected function success($msg)
497
	{
498 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action));
499
	}
500
501
	/**
502
	 * Print error message.
503
	 *
504
	 * @param	string	$msg	Message lang key
505
	 */
506 6
	protected function error($msg)
507
	{
508 6
		trigger_error($this->language->lang($msg) . adm_back_link($this->u_action), E_USER_WARNING);
509
	}
510
}
511