Completed
Pull Request — master (#4)
by Jakub
06:22 queued 55s
created

admin_controller::action_disable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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