Completed
Pull Request — master (#75)
by Jakub
11:05
created

admin_controller::upload_banner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
	private $data = array();
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\ad\manager */
32
	protected $manager;
33
34
	/** @var \phpbb\config\db_text */
35
	protected $config_text;
36
37
	/** @var \phpbb\config\config */
38
	protected $config;
39
40
	/** @var \phpbb\group\helper */
41
	protected $group_helper;
42
43
	/** @var \phpbb\ads\controller\admin_input */
44
	protected $input;
45
46
	/** @var \phpbb\ads\controller\admin_helper */
47
	protected $helper;
48
49
	/** @var \phpbb\ads\analyser\manager */
50
	protected $analyser;
51
52
	/** @var string root_path */
53
	protected $root_path;
54
55
	/** @var string php_ext */
56
	protected $php_ext;
57
58
	/** @var string Custom form action */
59
	protected $u_action;
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @param \phpbb\template\template           $template     Template object
65
	 * @param \phpbb\language\language           $language     Language object
66
	 * @param \phpbb\request\request             $request      Request object
67
	 * @param \phpbb\ads\ad\manager              $manager      Advertisement manager object
68
	 * @param \phpbb\config\db_text              $config_text  Config text object
69
	 * @param \phpbb\config\config               $config       Config object
70
	 * @param \phpbb\group\helper                $group_helper Group helper object
71
	 * @param \phpbb\ads\controller\admin_input  $input        Admin input object
72
	 * @param \phpbb\ads\controller\admin_helper $helper       Admin helper object
73
	 * @param \phpbb\ads\analyser\manager        $analyser     Ad code analyser object
74
	 * @param string                             $root_path    phpBB root path
75
	 * @param string                             $php_ext      PHP extension
76
	 */
77 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\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext)
78
	{
79 32
		$this->template = $template;
80 32
		$this->language = $language;
81 32
		$this->request = $request;
82 32
		$this->manager = $manager;
83 32
		$this->config_text = $config_text;
84 32
		$this->config = $config;
85 32
		$this->group_helper = $group_helper;
86 32
		$this->input = $input;
87 32
		$this->helper = $helper;
88 32
		$this->analyser = $analyser;
89 32
		$this->root_path = $root_path;
90 32
		$this->php_ext = $php_ext;
91 32
	}
92
93
	/**
94
	 * Process user request for manage mode
95
	 *
96
	 * @return void
97
	 */
98 6
	public function mode_manage()
99
	{
100 6
		$this->setup();
101
102
		// Trigger specific action
103 6
		$action = $this->request->variable('action', '');
104 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
105 6
		{
106 5
			$this->{'action_' . $action}();
107 5
		}
108
109
		// Otherwise default to this
110 6
		$this->list_ads();
111 6
	}
112
113
	/**
114
	 * Process user request for settings mode
115
	 *
116
	 * @return void
117
	 */
118 3
	public function mode_settings()
119
	{
120 3
		$this->setup();
121
122 3
		add_form_key('phpbb/ads/settings');
123 3
		if ($this->request->is_set_post('submit'))
124 3
		{
125
			// Validate form key
126 2
			if (check_form_key('phpbb/ads/settings'))
127 2
			{
128 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
129 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
130 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
131 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
132
133 1
				$this->success('ACP_AD_SETTINGS_SAVED');
134
			}
135
136 1
			$this->helper->assign_errors(array($this->language->lang('FORM_INVALID')));
137 1
		}
138
139 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
140 2
		$groups = $this->manager->load_groups();
141 2 View Code Duplication
		foreach ($groups as $group)
142
		{
143 2
			$this->template->assign_block_vars('groups', array(
144 2
				'ID'         => $group['group_id'],
145 2
				'NAME'       => $this->group_helper->get_name($group['group_name']),
146 2
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
147 2
			));
148 2
		}
149
150 2
		$this->template->assign_vars(array(
151 2
			'U_ACTION'          => $this->u_action,
152 2
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
153 2
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
154 2
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
155 2
		));
156 2
	}
157
158
	/**
159
	 * Set page url
160
	 *
161
	 * @param string $u_action Custom form action
162
	 * @return void
163
	 */
164 26
	public function set_page_url($u_action)
165
	{
166 26
		$this->u_action = $u_action;
167 26
	}
168
169
	/**
170
	 * Get ACP page title for Ads module
171
	 *
172
	 * @return string    Language string for Ads ACP module
173
	 */
174 1
	public function get_page_title()
175
	{
176 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
177
	}
178
179
	/**
180
	 * Add an advertisement
181
	 *
182
	 * @return void
183
	 */
184 9
	public function action_add()
185
	{
186 5
		add_form_key('phpbb/ads/add');
187
188 5
		$action = $this->get_submitted_action();
189
		if ($action)
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
190 5
		{
191 5
			$this->data = $this->input->get_form_data('phpbb/ads/add');
192 5
			$this->{$action}();
193 4
			$this->assign_data();
194 4
		}
195
		else
196 9
		{
197
			$this->helper->assign_locations();
198
		}
199
200 4
		$this->set_output_vars('add', 0);
201 4
	}
202
203
	/**
204
	 * Edit an advertisement
205
	 *
206
	 * @return void
207
	 */
208 7
	public function action_edit()
209
	{
210 7
		$ad_id = $this->request->variable('id', 0);
211 7
		add_form_key('phpbb/ads/edit/' . $ad_id);
212
213 7
		$action = $this->get_submitted_action();
214
		if ($action)
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
215 7
		{
216 5
			$this->data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
217 5
			$this->{$action}();
218 3
		}
219
		else
220
		{
221 2
			$this->data = $this->manager->get_ad($ad_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->manager->get_ad($ad_id) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
222 2
			if (empty($this->data))
223 2
			{
224 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
225
			}
226
227
			// Load ad template locations
228 1
			$this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
229
		}
230
231 4
		$this->set_output_vars('edit', $ad_id);
232 4
		$this->assign_data();
233 4
	}
234
235
	/**
236
	 * Enable an advertisement
237
	 *
238
	 * @return void
239
	 */
240 4
	public function action_enable()
241
	{
242 4
		$this->ad_enable(true);
243 1
	}
244
245
	/**
246
	 * Disable an advertisement
247
	 *
248
	 * @return void
249
	 */
250 4
	public function action_disable()
251
	{
252 4
		$this->ad_enable(false);
253 1
	}
254
255
	/**
256
	 * Delete an advertisement
257
	 *
258
	 * @return void
259
	 */
260 3
	public function action_delete()
261
	{
262 3
		$ad_id = $this->request->variable('id', 0);
263
		if ($ad_id)
264 3
		{
265 3
			if (confirm_box(true))
266 3
			{
267
				// Get ad data so that we can log ad name
268 2
				$ad_data = $this->manager->get_ad($ad_id);
269
270
				// Delete ad and it's template locations
271 2
				$this->manager->delete_ad_locations($ad_id);
272 2
				$success = $this->manager->delete_ad($ad_id);
273
274
				// Only notify user on error or if not ajax
275 2
				if (!$success)
276 2
				{
277 1
					$this->error('ACP_AD_DELETE_ERRORED');
278
				}
279
				else
280
				{
281 1
					$this->helper->log('DELETE', $ad_data['ad_name']);
282
283 1
					if (!$this->request->is_ajax())
284 1
					{
285 1
						$this->success('ACP_AD_DELETE_SUCCESS');
286
					}
287
				}
288
			}
289
			else
290
			{
291 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
292 1
					'id'     => $ad_id,
293 1
					'i'      => $this->request->variable('i', ''),
294 1
					'mode'   => $this->request->variable('mode', ''),
295
					'action' => 'delete'
296 1
				)));
297
			}
298 1
		}
299 1
	}
300
301
	/**
302
	 * Display the ads
303
	 *
304
	 * @return void
305
	 */
306 1
	public function list_ads()
307
	{
308 1
		foreach ($this->manager->get_all_ads() as $row)
309
		{
310 1
			$ad_enabled = (int) $row['ad_enabled'];
311 1
			$ad_end_date = (int) $row['ad_end_date'];
312 1
			$ad_expired = ($ad_end_date > 0 && $ad_end_date < time()) || ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit']) || ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit']);
313 1
			if ($ad_expired && $ad_enabled)
314 1
			{
315 1
				$ad_enabled = 0;
316 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
317 1
			}
318
319 1
			$this->assign_ad_template_vars($row, $ad_expired, $ad_end_date, $ad_enabled);
320 1
		}
321
322
		// Set output vars for display in the template
323 1
		$this->template->assign_vars(array(
324 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
325 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
326 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
327 1
		));
328 1
	}
329
330 1
	protected function assign_ad_template_vars($row, $ad_expired, $ad_end_date, $ad_enabled)
331
	{
332 1
		$this->template->assign_block_vars($ad_expired ? 'expired' : 'ads', array(
333 1
			'NAME'         => $row['ad_name'],
334 1
			'PRIORITY'     => $row['ad_priority'],
335 1
			'END_DATE'     => $this->helper->prepare_end_date($ad_end_date),
336 1
			'VIEWS'        => $row['ad_views'],
337 1
			'CLICKS'       => $row['ad_clicks'],
338 1
			'VIEWS_LIMIT'  => $row['ad_views_limit'],
339 1
			'CLICKS_LIMIT' => $row['ad_clicks_limit'],
340 1
			'S_EXPIRED'    => $ad_expired,
341 1
			'S_ENABLED'    => $ad_enabled,
342 1
			'U_ENABLE'     => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
343 1
			'U_EDIT'       => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
344 1
			'U_DELETE'     => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
345 1
		));
346 1
	}
347
348
	/**
349
	 * Perform general tasks
350
	 *
351
	 * @return void
352
	 */
353 9
	protected function setup()
354
	{
355 9
		if (!function_exists('user_get_id_name'))
356 9
		{
357
			include $this->root_path . 'includes/functions_user.' . $this->php_ext;
358
		}
359
360 9
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
361 9
		$this->language->add_lang('acp', 'phpbb/ads');
362
363 9
		$this->template->assign_var('S_PHPBB_ADS', true);
364 9
	}
365
366 12
	protected function get_submitted_action()
367
	{
368 12
		$actions = array('preview', 'submit', 'upload_banner', 'analyse_ad_code');
369 12
		foreach ($actions as $action)
370
		{
371 12
			if ($this->request->is_set_post($action))
372 12
			{
373 10
				return $action;
374
			}
375 10
		}
376
377 2
		return false;
378
	}
379
380 8
	protected function assign_data()
381
	{
382 8
		$this->helper->assign_locations($this->data['ad_locations']);
383 8
		$this->helper->assign_form_data($this->data);
384 8
		$this->helper->assign_errors($this->input->get_errors());
385 8
	}
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
	 * Prepare advertisement preview
424
	 *
425
	 * @return    void
426
	 */
427 2
	protected function preview()
428
	{
429 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code']));
430 2
	}
431
432 1
	protected function upload_banner()
433
	{
434 1
		$this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']);
435 1
	}
436
437 1
	protected function analyse_ad_code()
438
	{
439 1
		$this->analyser->run($this->data['ad_code']);
440 1
	}
441
442 6
	protected function submit()
443
	{
444 6
		if (!$this->input->has_errors())
445 6
		{
446 3
			$ad_id = $this->request->variable('id', 0);
447
			if ($ad_id)
448 1
			{
449
				$this->edit_ad($ad_id);
450
			}
451 1
			$this->add_ad();
452
		}
453 3
	}
454
455 8
	protected function set_output_vars($action, $ad_id)
456
	{
457
		// Set output vars for display in the template
458 8
		$this->template->assign_vars(array(
459 8
			'S_' . strtoupper($action) . '_AD'	=> true,
460 8
			'EDIT_ID'							=> $ad_id,
461 8
			'U_BACK'							=> $this->u_action,
462 8
			'U_ACTION'							=> "{$this->u_action}&amp;action=$action" . ($ad_id ? '&amp;id=' . $ad_id : ''),
463 8
			'PICKER_DATE_FORMAT'				=> input::DATE_FORMAT,
464 8
			'U_FIND_USERNAME'					=> $this->helper->get_find_username_link(),
465 8
		));
466 8
	}
467
468 1
	protected function add_ad()
469
	{
470 1
		$ad_id = $this->manager->insert_ad($this->data);
471 1
		$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
472
473 1
		$this->helper->log('ADD', $this->data['ad_name']);
474
475 1
		$this->success('ACP_AD_ADD_SUCCESS');
476
	}
477
478
	protected function edit_ad($ad_id)
479
	{
480
		$success = $this->manager->update_ad($ad_id, $this->data);
481
482
		if ($success)
483
		{
484
			// Only insert new ad locations to DB when ad exists
485
			$this->manager->delete_ad_locations($ad_id);
486
			$this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']);
487
488
			$this->helper->log('EDIT', $this->data['ad_name']);
489
490
			$this->success('ACP_AD_EDIT_SUCCESS');
491
		}
492
493
		$this->error('ACP_AD_DOES_NOT_EXIST');
494
	}
495
496
	/**
497
	 * Print success message.
498
	 *
499
	 * It takes arguments in the form of a language key, followed by language substitution values.
500
	 */
501 5
	protected function success()
502
	{
503 5
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action));
504
	}
505
506
	/**
507
	 * Print error message.
508
	 *
509
	 * It takes arguments in the form of a language key, followed by language substitution values.
510
	 */
511 4
	protected function error()
512
	{
513 4
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
514
	}
515
}
516