Completed
Pull Request — master (#5)
by Jakub
32:58
created

admin_controller::ad_enable()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10.5

Importance

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