Completed
Pull Request — master (#30)
by Matt
12:31
created

admin_controller::mode_settings()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0016

Importance

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