Completed
Pull Request — master (#4)
by Jakub
06:46
created

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