Completed
Pull Request — master (#112)
by Jakub
11:14
created

admin_controller::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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