Completed
Pull Request — master (#28)
by Matt
35:33 queued 33:16
created

admin_controller::mode_settings()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.9295

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 22
cts 30
cp 0.7332
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 15
nop 0
crap 7.9295
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
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \phpbb\request\request */
28
	protected $request;
29
30
	/** @var \phpbb\ads\ad\manager */
31
	protected $manager;
32
33
	/** @var \phpbb\ads\location\manager */
34
	protected $location_manager;
35
36
	/** @var \phpbb\log\log */
37
	protected $log;
38
39
	/** @var \phpbb\config\db_text */
40
	protected $config_text;
41
42
	/** @var string php_ext */
43
	protected $php_ext;
44
45
	/** @var string ext_path */
46
	protected $ext_path;
47
48
	/** @var string Custom form action */
49
	protected $u_action;
50
51
	/** @var array Form validation errors */
52
	protected $errors = array();
53
54
	/**
55
	* Constructor
56
	*
57
	* @param \phpbb\template\template				$template			Template object
58
	* @param \phpbb\user							$user				User object
59
	* @param \phpbb\request\request					$request			Request object
60
	* @param \phpbb\ads\ad\manager			$manager			Advertisement manager object
61
	* @param \phpbb\ads\location\manager	$location_manager	Template location manager object
62
	* @param \phpbb\log\log							$log				The phpBB log system
63
	* @param \phpbb\config\db_text					$config_text		Config text object
64
	* @param string									$php_ext			PHP extension
65
	* @param string									$ext_path			Path to this extension
66
	*/
67 33
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, $php_ext, $ext_path)
68
	{
69 33
		$this->template = $template;
70 33
		$this->user = $user;
71 33
		$this->request = $request;
72 33
		$this->manager = $manager;
73 33
		$this->location_manager = $location_manager;
74 33
		$this->log = $log;
75 33
		$this->config_text = $config_text;
76 33
		$this->php_ext = $php_ext;
77 33
		$this->ext_path = $ext_path;
78 33
	}
79
80
	/**
81
	* Process user request for manage mode
82
	*
83
	* @return void
84
	*/
85 6
	public function mode_manage()
86
	{
87 6
		$this->setup();
88
89
		// Trigger specific action
90 6
		$action = $this->request->variable('action', '');
91 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
92 6
		{
93 5
			$this->{'action_' . $action}();
94 5
		}
95
96
		// Otherwise default to this
97 6
		$this->list_ads();
98 6
	}
99
100
	/**
101
	* Process user request for settings mode
102
	*
103
	* @return void
104
	*/
105 2
	public function mode_settings()
106
	{
107 2
		$this->setup();
108
109 2
		add_form_key('phpbb/ads/settings');
110 2
		if ($this->request->is_set_post('submit'))
111 2
		{
112
			// Validate form key
113 1
			if (!check_form_key('phpbb/ads/settings'))
114 1
			{
115
				$this->errors[] = $this->user->lang('FORM_INVALID');
116
			}
117
118 1
			if (empty($this->errors))
119 1
			{
120 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
121
122 1
				$this->success('ACP_AD_SETTINGS_SAVED');
123
			}
124
125
			$this->template->assign_vars(array(
126
				'S_ERROR'		=> (bool) count($this->errors),
127
				'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
128
			));
129
		}
130
131 1
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
132 1
		$groups = $this->manager->load_groups();
133 1
		foreach ($groups as $group)
134
		{
135 1
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
136
137 1
			$this->template->assign_block_vars('groups', array(
138 1
				'ID'			=> $group['group_id'],
139 1
				'NAME'			=> $group_name,
140 1
				'S_SELECTED'	=> in_array($group['group_id'], $hide_groups),
141 1
			));
142 1
		}
143 1
	}
144
145
	/**
146
	* Set page url
147
	*
148
	* @param string $u_action Custom form action
149
	* @return void
150
	*/
151 27
	public function set_page_url($u_action)
152
	{
153 27
		$this->u_action = $u_action;
154 27
	}
155
156
	/**
157
	* Get ACP page title for Ads module
158
	*
159
	* @return string	Language string for Ads ACP module
160
	*/
161 1
	public function get_page_title()
162
	{
163 1
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
164
	}
165
166
	/**
167
	* Add an advertisement
168
	*
169
	* @return void
170
	*/
171 16
	public function action_add()
172
	{
173 7
		$preview = $this->request->is_set_post('preview');
174 7
		$submit = $this->request->is_set_post('submit');
175
176 7
		add_form_key('phpbb/ads/add');
177 7
		if ($preview || $submit)
178 7
		{
179 6
			$data = $this->get_form_data('phpbb/ads/add');
180
181 View Code Duplication
			if ($preview)
182 6
			{
183 1
				$this->ad_preview($data['ad_code']);
184 1
			}
185
			else if (empty($this->errors))
186 5
			{
187 1
				$ad_id = $this->manager->insert_ad($data);
188 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
189
190 1
				$this->log('ADD', $data['ad_name']);
191
192 1
				$this->success('ACP_AD_ADD_SUCCESS');
193
			}
194
195 5
			$this->assign_locations($data);
196 16
			$this->assign_form_data($data);
197 5
		}
198
		else
199
		{
200 1
			$this->assign_locations();
201
		}
202
203
		// Set output vars for display in the template
204 6
		$this->template->assign_vars(array(
205 6
			'S_ADD_AD'				=> true,
206 6
			'U_BACK'				=> $this->u_action,
207 6
			'PICKER_DATE_FORMAT'	=> self::DATE_FORMAT,
208 6
		));
209 6
	}
210
211
	/**
212
	* Edit an advertisement
213
	*
214
	* @return void
215
	*/
216 9
	public function action_edit()
217
	{
218 9
		$ad_id = $this->request->variable('id', 0);
219 9
		$preview = $this->request->is_set_post('preview');
220 9
		$submit = $this->request->is_set_post('submit');
221
222 9
		add_form_key('phpbb/ads/edit/' . $ad_id);
223 9
		if ($preview || $submit)
224 9
		{
225 7
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
226
227
			if ($preview)
228 7
			{
229 1
				$this->ad_preview($data['ad_code']);
230 1
			}
231 View Code Duplication
			else if (empty($this->errors))
232 6
			{
233 2
				$success = $this->manager->update_ad($ad_id, $data);
234
235
				if ($success)
236 2
				{
237
					// Only insert new ad locations to DB when ad exists
238 1
					$this->manager->delete_ad_locations($ad_id);
239 1
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
240
241 1
					$this->log('EDIT', $data['ad_name']);
242
243 1
					$this->success('ACP_AD_EDIT_SUCCESS');
244
				}
245 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
246
			}
247 5
		}
248
		else
249
		{
250
			// Load ad data
251 2
			$data = $this->manager->get_ad($ad_id);
252 2
			if (empty($data))
253 2
			{
254 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
255
			}
256
257
			// Load ad template locations
258 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
259
		}
260
261
		// Set output vars for display in the template
262 6
		$this->template->assign_vars(array(
263 6
			'S_EDIT_AD'				=> true,
264 6
			'EDIT_ID'				=> $ad_id,
265 6
			'U_BACK'				=> $this->u_action,
266 6
			'PICKER_DATE_FORMAT'	=> self::DATE_FORMAT,
267 6
		));
268 6
		$this->assign_locations($data);
269 6
		$this->assign_form_data($data);
270 6
	}
271
272
	/**
273
	* Enable an advertisement
274
	*
275
	* @return void
276
	*/
277 3
	public function action_enable()
278
	{
279 3
		$this->ad_enable(true);
280 1
	}
281
282
	/**
283
	* Disable an advertisement
284
	*
285
	* @return void
286
	*/
287 3
	public function action_disable()
288
	{
289 3
		$this->ad_enable(false);
290 1
	}
291
292
	/**
293
	* Delete an advertisement
294
	*
295
	* @return void
296
	*/
297 3
	public function action_delete()
298
	{
299 3
		$ad_id = $this->request->variable('id', 0);
300
		if ($ad_id)
301 3
		{
302 3
			if (confirm_box(true))
303 3
			{
304
				// Get ad data so that we can log ad name
305 2
				$ad_data = $this->manager->get_ad($ad_id);
306
307
				// Delete ad and it's template locations
308 2
				$this->manager->delete_ad_locations($ad_id);
309 2
				$success = $this->manager->delete_ad($ad_id);
310
311
				// Only notify user on error or if not ajax
312 2
				if (!$success)
313 2
				{
314 1
					$this->error('ACP_AD_DELETE_ERRORED');
315
				}
316
				else
317
				{
318 1
					$this->log('DELETE', $ad_data['ad_name']);
319
320 1
					if (!$this->request->is_ajax())
321 1
					{
322 1
						$this->success('ACP_AD_DELETE_SUCCESS');
323
					}
324
				}
325
			}
326
			else
327
			{
328 1
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
329 1
					'id'		=> $ad_id,
330 1
					'i'			=> $this->request->variable('i', ''),
331 1
					'mode'		=> $this->request->variable('mode', ''),
332
					'action'	=> 'delete'
333 1
				)));
334
			}
335 1
		}
336 1
	}
337
338
	/**
339
	* Display the ads
340
	*
341
	* @return void
342
	*/
343 1
	public function list_ads()
344
	{
345 1
		foreach ($this->manager->get_all_ads() as $row)
346
		{
347 1
			$ad_enabled = (int) $row['ad_enabled'];
348 1
			$ad_end_date = (int) $row['ad_end_date'];
349 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
350 1
			if ($ad_expired && $ad_enabled)
351 1
			{
352 1
				$ad_enabled = 0;
353 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
354 1
			}
355
356 1
			$this->template->assign_block_vars('ads', array(
357 1
				'NAME'					=> $row['ad_name'],
358 1
				'END_DATE'				=> $ad_end_date ? $this->user->format_date($ad_end_date, self::DATE_FORMAT) : '',
359 1
				'S_END_DATE_EXPIRED'	=> $ad_expired,
360 1
				'S_ENABLED'				=> $ad_enabled,
361 1
				'U_ENABLE'				=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
362 1
				'U_EDIT'				=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
363 1
				'U_DELETE'				=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
364 1
			));
365 1
		}
366
367
		// Set output vars for display in the template
368 1
		$this->template->assign_var('U_ACTION_ADD', $this->u_action . '&amp;action=add');
369 1
	}
370
371
	/**
372
	* Perform general tasks
373
	*
374
	* @return void
375
	*/
376 8
	protected function setup()
377
	{
378 8
		$this->user->add_lang_ext('phpbb/ads', 'acp');
379
380 8
		$this->template->assign_vars(array(
381 8
			'S_PHPBB_ADS'	=> true,
382 8
			'U_ACTION'		=> $this->u_action,
383 8
		));
384 8
	}
385
386
	/**
387
	* Enable/disable an advertisement
388
	*
389
	* @param	bool	$enable	Enable or disable the advertisement?
390
	* @return void
391
	*/
392 4
	protected function ad_enable($enable)
393
	{
394 4
		$ad_id = $this->request->variable('id', 0);
395
396 4
		$success = $this->manager->update_ad($ad_id, array(
397 4
			'ad_enabled'	=> (int) $enable,
398 4
		));
399
400
		// If AJAX was used, show user a result message
401 4
		if ($this->request->is_ajax())
402 4
		{
403
			$json_response = new \phpbb\json_response;
404
			$json_response->send(array(
405
				'text'	=> $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
406
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
407
			));
408
		}
409
410
		// Otherwise, show traditional infobox
411
		if ($success)
412 4
		{
413 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
414
		}
415
		else
416
		{
417 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
418
		}
419
	}
420
421
	/**
422
	* Get admin form data.
423
	*
424
	* @param	string	$form_name	The form name.
425
	* @return	array	Form data
426
	*/
427 13
	protected function get_form_data($form_name)
428
	{
429
		$data = array(
430 13
			'ad_name'		=> $this->request->variable('ad_name', '', true),
431 13
			'ad_note'		=> $this->request->variable('ad_note', '', true),
432 13
			'ad_code'		=> $this->request->variable('ad_code', '', true),
433 13
			'ad_enabled'	=> $this->request->variable('ad_enabled', 0),
434 13
			'ad_locations'	=> $this->request->variable('ad_locations', array('')),
435 13
			'ad_end_date'	=> $this->request->variable('ad_end_date', ''),
436 13
		);
437
438
		// Validate form key
439 13
		if (!check_form_key($form_name))
440 13
		{
441 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
442 2
		}
443
444
		// Validate ad name
445 13
		if ($data['ad_name'] === '')
446 13
		{
447 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
448 2
		}
449 13
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
450 13
		{
451 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
452 2
		}
453
454
		// Validate ad end date
455 13
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
456 13
		{
457 4
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
458
459 4
			if ($data['ad_end_date'] < time())
460 4
			{
461 2
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
462 2
			}
463 4
		}
464 9
		else if ($data['ad_end_date'] !== '')
465 9
		{
466
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
467
		}
468
		else
469
		{
470 9
			$data['ad_end_date'] = 0;
471
		}
472
473 13
		return $data;
474
	}
475
476
	/**
477
	* Assign form data to the template.
478
	*
479
	* @param	array	$data	The form data.
480
	* @return void
481
	*/
482 11
	protected function assign_form_data($data)
483
	{
484 11
		$this->template->assign_vars(array(
485 11
			'S_ERROR'		=> (bool) count($this->errors),
486 11
			'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
487
488 11
			'AD_NAME'		=> $data['ad_name'],
489 11
			'AD_NOTE'		=> $data['ad_note'],
490 11
			'AD_CODE'		=> $data['ad_code'],
491 11
			'AD_ENABLED'	=> $data['ad_enabled'],
492 11
			'AD_END_DATE'	=> $this->prepare_end_date($data['ad_end_date']),
493 11
		));
494 11
	}
495
496
	/**
497
	* Prepare end date for display
498
	*
499
	* @param	mixed	$end_date	End date.
500
	* @return	string	End date prepared for display.
501
	*/
502 11
	protected function prepare_end_date($end_date)
503
	{
504 11
		if (empty($end_date))
505 11
		{
506 8
			return '';
507
		}
508
509 3
		if (is_numeric($end_date))
510 3
		{
511 3
			return $this->user->format_date($end_date, self::DATE_FORMAT);
512
		}
513
514
		return (string) $end_date;
515
	}
516
517
	/**
518
	* Assign template locations data to the template.
519
	*
520
	* @param	mixed	$data	The form data or nothing.
521
	* @return	void
522
	*/
523 12
	protected function assign_locations($data = false)
524
	{
525 12
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
526
		{
527 12
			$this->template->assign_block_vars('ad_locations', array(
528 12
				'LOCATION_ID'	=> $location_id,
529 12
				'LOCATION_DESC'	=> $location_data['desc'],
530 12
				'LOCATION_NAME'	=> $location_data['name'],
531 12
				'S_SELECTED'	=> $data ? in_array($location_id, $data['ad_locations']) : false,
532 12
			));
533 12
		}
534 12
	}
535
536
	/**
537
	* Prepare advertisement preview
538
	*
539
	* @param	string	$code	Ad code to preview
540
	* @return	void
541
	*/
542 2
	protected function ad_preview($code)
543
	{
544 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
545 2
	}
546
547
	/**
548
	* Print success message.
549
	*
550
	* It takes arguments in the form of a language key, followed by language substitution values.
551
	*/
552 6
	protected function success()
553
	{
554 6
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
555
	}
556
557
	/**
558
	* Print error message.
559
	*
560
	* It takes arguments in the form of a language key, followed by language substitution values.
561
	*/
562 5
	protected function error()
563
	{
564 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
565
	}
566
567
	/**
568
	* Log action
569
	*
570
	* @param	string	$action		Performed action in uppercase
571
	* @param	string	$ad_name	Advertisement name
572
	* @return	void
573
	*/
574 3
	protected function log($action, $ad_name)
575
	{
576 3
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
577 3
	}
578
}
579