Completed
Push — master ( eea803...458169 )
by Matt
15s
created

admin_controller::list_ads()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

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