Completed
Push — master ( 8b656c...88d06e )
by Matt
14s
created

admin_controller   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 399
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.96%

Importance

Changes 18
Bugs 0 Features 1
Metric Value
wmc 39
c 18
b 0
f 1
lcom 1
cbo 0
dl 0
loc 399
ccs 171
cts 188
cp 0.9096
rs 8.2857

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A main() 0 14 2
A set_page_url() 0 4 1
A get_page_title() 0 4 1
A action_enable() 0 4 1
A action_disable() 0 4 1
B action_add() 0 29 3
B action_edit() 0 51 5
B action_delete() 0 32 5
B list_ads() 0 26 3
B ad_enable() 0 30 6
A check_form_key() 0 7 2
A get_form_data() 0 9 1
A validate() 0 11 3
A assign_form_data() 0 12 2
A success() 0 4 1
A error() 0 4 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 string ads_table */
33
	protected $ads_table;
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\db\driver\driver_interface	$db					DB driver interface
51
	* @param \phpbb\template\template			$template			Template object
52
	* @param \phpbb\user						$user				User object
53
	* @param \phpbb\request\request				$request			Request object
54
	* @param string								$ads_table			Ads table
55
	* @param string								$php_ext			PHP extension
56
	* @param string								$ext_path			Path to this extension
57
	*/
58 27
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table, $php_ext, $ext_path)
59
	{
60 27
		$this->db = $db;
61 27
		$this->template = $template;
62 27
		$this->user = $user;
63 27
		$this->request = $request;
64 27
		$this->ads_table = $ads_table;
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
		add_form_key('phpbb/admanagement/add');
118 5
		if ($this->request->is_set_post('submit'))
119 5
		{
120 4
			$this->check_form_key('phpbb/admanagement/add');
121
122 4
			$data = $this->get_form_data();
123
124 4
			$this->validate($data);
125
126 4
			if (empty($this->errors))
127 4
			{
128
				// Insert the ad data to the database
129 1
				$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
130 1
				$this->db->sql_query($sql);
131
132 1
				$this->success('ACP_AD_ADD_SUCCESS');
133
			}
134
135 3
			$this->assign_form_data($data);
136 3
		}
137
138
		// Set output vars for display in the template
139 4
		$this->template->assign_vars(array(
140 4
			'S_ADD_AD'	=> true,
141 4
			'U_BACK'	=> $this->u_action,
142 4
		));
143 4
	}
144
145
	/**
146
	* Edit an advertisement
147
	*
148
	* @return void
149
	*/
150 14
	public function action_edit()
151
	{
152 7
		$ad_id = $this->request->variable('id', 0);
153
154 7
		add_form_key('phpbb/admanagement/edit/' . $ad_id);
155 7
		if ($this->request->is_set_post('submit'))
156 7
		{
157 5
			$this->check_form_key('phpbb/admanagement/edit/' . $ad_id);
158
159 5
			$data = $this->get_form_data();
160
161 5
			$this->validate($data);
162
163 5
			if (empty($this->errors))
164 5
			{
165
				// Insert the ad data to the database
166 2
				$sql = 'UPDATE ' . $this->ads_table . '
167 2
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
168 2
					WHERE ad_id = ' . (int) $ad_id;
169 2
				$this->db->sql_query($sql);
170
171 2
				if ($this->db->sql_affectedrows())
172 2
				{
173 1
					$this->success('ACP_AD_EDIT_SUCCESS');
174
				}
175 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
176
			}
177 3
		}
178
		else
179
		{
180
			$sql = 'SELECT *
181 2
				FROM ' . $this->ads_table . '
182 2
				WHERE ad_id = ' . (int) $ad_id;
183 2
			$result = $this->db->sql_query($sql);
184 2
			$data = $this->db->sql_fetchrow($result);
185 2
			$this->db->sql_freeresult($result);
186
187 2
			if (empty($data))
188 2
			{
189 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
190
			}
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_form_data($data);
200 4
	}
201
202
	/**
203
	* Enable an advertisement
204
	*
205
	* @return void
206
	*/
207 3
	public function action_enable()
208
	{
209 3
		$this->ad_enable(true);
210 1
	}
211
212
	/**
213
	* Disable an advertisement
214
	*
215
	* @return void
216
	*/
217 3
	public function action_disable()
218
	{
219 3
		$this->ad_enable(false);
220 1
	}
221
222
	/**
223
	* Delete an advertisement
224
	*
225
	* @return void
226
	*/
227 3
	public function action_delete()
228
	{
229 3
		$ad_id = $this->request->variable('id', 0);
230
		if ($ad_id)
231 3
		{
232 3
			if (confirm_box(true))
233 3
			{
234 2
				$sql = 'DELETE FROM ' . $this->ads_table . '
235 2
					WHERE ad_id = ' . (int) $ad_id;
236 2
				$this->db->sql_query($sql);
237
238
				// Only notify user on error or if not ajax
239 2
				if (!$this->db->sql_affectedrows())
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
		$sql = 'SELECT ad_id, ad_name, ad_enabled
268 1
			FROM ' . $this->ads_table;
269 1
		$result = $this->db->sql_query($sql);
270 1
		while ($row = $this->db->sql_fetchrow($result))
271
		{
272 1
			$ad_enabled = (int) $row['ad_enabled'];
273
274 1
			$this->template->assign_block_vars('ads', array(
275 1
				'NAME'		=> $row['ad_name'],
276 1
				'S_ENABLED'	=> $ad_enabled,
277 1
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
278 1
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
279 1
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
280 1
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
281 1
			));
282 1
		}
283 1
		$this->db->sql_freeresult($result);
284
285
		// Set output vars for display in the template
286 1
		$this->template->assign_vars(array(
287 1
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
288 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') . '" />',
289 1
		));
290 1
	}
291
292
	/**
293
	* Enable/disable an advertisement
294
	*
295
	* @param	bool	$enable	Enable or disable the advertisement?
296
	* @return void
297
	*/
298 4
	protected function ad_enable($enable)
299
	{
300 4
		$ad_id = $this->request->variable('id', 0);
301
302 4
		$sql = 'UPDATE ' . $this->ads_table . '
303 4
			SET ad_enabled = ' . (int) $enable . '
304 4
			WHERE ad_id = ' . (int) $ad_id;
305 4
		$this->db->sql_query($sql);
306 4
		$success = (bool) $this->db->sql_affectedrows();
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
	* Check the form key.
331
	*
332
	* @param	string	$form_name	The name of the form.
333
	* @return void
334
	*/
335 9
	protected function check_form_key($form_name)
336
	{
337 9
		if (!check_form_key($form_name))
338 9
		{
339 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
340 2
		}
341 9
	}
342
343
	/**
344
	* Get admin form data.
345
	*
346
	* @return	array	Form data
347
	*/
348 9
	protected function get_form_data()
349
	{
350
		return array(
351 9
			'ad_name'		=> $this->request->variable('ad_name', '', true),
352 9
			'ad_note'		=> $this->request->variable('ad_note', '', true),
353 9
			'ad_code'		=> $this->request->variable('ad_code', '', true),
354 9
			'ad_enabled'	=> $this->request->variable('ad_enabled', 0),
355 9
		);
356
	}
357
358
	/**
359
	* Validate form data.
360
	*
361
	* @param	array	$data	The form data.
362
	* @return void
363
	*/
364 9
	protected function validate($data)
365
	{
366 9
		if ($data['ad_name'] === '')
367 9
		{
368 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
369 2
		}
370 9
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
371 9
		{
372 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
373 2
		}
374 9
	}
375
376
	/**
377
	* Assign form data to the template.
378
	*
379
	* @param	array	$data	The form data.
380
	* @return void
381
	*/
382 7
	protected function assign_form_data($data)
383
	{
384 7
		$this->template->assign_vars(array(
385 7
			'S_ERROR'		=> (bool) count($this->errors),
386 7
			'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
387
388 7
			'AD_NAME'		=> $data['ad_name'],
389 7
			'AD_NOTE'		=> $data['ad_note'],
390 7
			'AD_CODE'		=> $data['ad_code'],
391 7
			'AD_ENABLED'	=> $data['ad_enabled'],
392 7
		));
393 7
	}
394
395
	/**
396
	* Print success message.
397
	*
398
	* It takes arguments in the form of a language key, followed by language substitution values.
399
	*/
400 5
	protected function success()
401
	{
402 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
403
	}
404
405
	/**
406
	* Print error message.
407
	*
408
	* It takes arguments in the form of a language key, followed by language substitution values.
409
	*/
410 5
	protected function error()
411
	{
412 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
413
	}
414
}
415