Completed
Pull Request — master (#60)
by Matt
31:15 queued 29:16
created

admin_controller::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

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