Completed
Pull Request — master (#2)
by Jakub
05:41
created

admin_controller::get_page_title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
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 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 16
	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 16
		$this->db = $db;
61 16
		$this->template = $template;
62 16
		$this->user = $user;
63 16
		$this->request = $request;
64 16
		$this->ads_table = $ads_table;
65 16
		$this->php_ext = $php_ext;
66 16
		$this->ext_path = $ext_path;
67 16
	}
68
69
	/**
70
	* Process user request
71
	*
72
	* @return void
73
	*/
74 5
	public function main()
75
	{
76 5
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
77
78
		// Trigger specific action
79 5
		$action = $this->request->variable('action', '');
80 5
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
81 5
		{
82 5
			$this->{'action_' . $action}();
83 5
		}
84
85
		// Otherwise default to this
86 5
		$this->list_ads();
87 5
	}
88
89
	/**
90
	* Set page url
91
	*
92
	* @param string $u_action Custom form action
93
	* @return void
94
	*/
95 11
	public function set_page_url($u_action)
96
	{
97 11
		$this->u_action = $u_action;
98 11
	}
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 4
	public function action_add()
116
	{
117 4
		add_form_key('phpbb/admanagement/add');
118 4
		if ($this->request->is_set_post('submit'))
119 4
		{
120 3
			$this->check_form_key('phpbb/admanagement/add');
121
122 3
			$data = $this->get_form_data();
123
124 3
			$this->validate($data);
125
126 3 View Code Duplication
			if (empty($this->errors))
0 ignored issues
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...
127 3
			{
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 2
			$this->assign_form_data($data);
136 2
		}
137
138
		// Set output vars for display in the template
139 3
		$this->template->assign_vars(array(
140 3
			'S_ADD_AD'	=> true,
141 3
			'U_BACK'	=> $this->u_action,
142 3
		));
143 3
	}
144
145
	/**
146
	* Edit an advertisement
147
	*
148
	* @return void
149
	*/
150 6
	public function action_edit()
151
	{
152
		$ad_id = $this->request->variable('id', 0);
153
154
		add_form_key('phpbb/admanagement/edit');
155
		if ($this->request->is_set_post('submit'))
156
		{
157
			$this->check_form_key('phpbb/admanagement/edit');
158
159
			$data = $this->get_form_data();
160
161
			$this->validate($data);
162
163 View Code Duplication
			if (empty($this->errors))
0 ignored issues
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...
164
			{
165
				// Insert the ad data to the database
166
				$sql = 'UPDATE ' . $this->ads_table . '
167
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
168
					WHERE ad_id = ' . (int) $ad_id;
169
				$this->db->sql_query($sql);
170
171
				$this->success('ACP_AD_EDIT_SUCCESS');
172
			}
173
		}
174
		else
175
		{
176
			$sql = 'SELECT *
177
				FROM ' . $this->ads_table . '
178
				WHERE ad_id = ' . (int) $ad_id;
179
			$result = $this->db->sql_query($sql);
180
			$data = $this->db->sql_fetchrow($result);
181
			$this->db->sql_freeresult($result);
182
183
			if (empty($data))
184
			{
185
				$this->error('ACP_AD_DOES_NOT_EXIST');
186
			}
187
		}
188
189
		// Set output vars for display in the template
190
		$this->template->assign_vars(array(
191
			'S_EDIT_AD'	=> true,
192
			'EDIT_ID'	=> $ad_id,
193
			'U_BACK'	=> $this->u_action,
194
		));
195
		$this->assign_form_data($data);
196 6
	}
197
198
	/**
199
	* Enable an advertisement
200
	*
201
	* @return void
202
	*/
203 3
	public function action_enable()
204
	{
205 3
		$this->ad_enable(true);
206 1
	}
207
208
	/**
209
	* Disable an advertisement
210
	*
211
	* @return void
212
	*/
213 3
	public function action_disable()
214
	{
215 3
		$this->ad_enable(false);
216 1
	}
217
218
	/**
219
	* Delete an advertisement
220
	*
221
	* @return void
222
	*/
223 1
	public function action_delete()
224
	{
225 1
		$ad_id = $this->request->variable('id', 0);
226
		if ($ad_id)
227 1
		{
228 1
			if (confirm_box(true))
229 1
			{
230 1
				$sql = 'DELETE FROM ' . $this->ads_table . '
231 1
					WHERE ad_id = ' . (int) $ad_id;
232 1
				$this->db->sql_query($sql);
233
234
				// Only notify user on error or if not ajax
235 1
				if (!$this->db->sql_affectedrows())
236 1
				{
237
					$this->error('ACP_AD_DELETE_ERRORED');
238
				}
239 1
				else if (!$this->request->is_ajax())
240 1
				{
241 1
					$this->success('ACP_AD_DELETE_SUCCESS');
242
				}
243
			}
244
			else
245
			{
246
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
247
					'id'		=> $ad_id,
248
					'i'			=> $this->request->variable('i', ''),
249
					'mode'		=> $this->request->variable('mode', ''),
250
					'action'	=> 'delete'
251
				)));
252
			}
253
		}
254
	}
255
256
	/**
257
	* Display the ads
258
	*
259
	* @return void
260
	*/
261 6
	public function list_ads()
262
	{
263
		$sql = 'SELECT ad_id, ad_name, ad_enabled
264 6
			FROM ' . $this->ads_table;
265 6
		$result = $this->db->sql_query($sql);
266 6
		while ($row = $this->db->sql_fetchrow($result))
267
		{
268 6
			$ad_enabled = (int) $row['ad_enabled'];
269
270 6
			$this->template->assign_block_vars('ads', array(
271 6
				'NAME'		=> $row['ad_name'],
272 6
				'S_ENABLED'	=> $ad_enabled,
273 6
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
274 6
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
275 6
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
276 6
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
277 6
			));
278 6
		}
279 6
		$this->db->sql_freeresult($result);
280
281
		// Set output vars for display in the template
282 6
		$this->template->assign_vars(array(
283 6
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
284 6
			'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 6
		));
286 6
	}
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
		$sql = 'UPDATE ' . $this->ads_table . '
297 4
			SET ad_enabled = ' . (int) $enable . '
298 4
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
299 4
		$this->db->sql_query($sql);
300 4
		$success = (bool) $this->db->sql_affectedrows();
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
	* Check the form key.
325
	*
326
	* @param	string	$form_name	The name of the form.
327
	* @return void
328
	*/
329 3
	protected function check_form_key($form_name)
330
	{
331 3
		if (!check_form_key($form_name))
332 3
		{
333
			$this->errors[] = $this->user->lang('FORM_INVALID');
334
		}
335 3
	}
336
337
	/**
338
	* Get admin form data.
339
	*
340
	* @return	array	Form data
341
	*/
342 3
	protected function get_form_data()
343
	{
344
		return array(
345 3
			'ad_name'		=> $this->request->variable('ad_name', '', true),
346 3
			'ad_note'		=> $this->request->variable('ad_note', '', true),
347 3
			'ad_code'		=> $this->request->variable('ad_code', '', true),
348 3
			'ad_enabled'	=> $this->request->variable('ad_enabled', false),
349 3
		);
350
	}
351
352
	/**
353
	* Validate form data.
354
	*
355
	* @param	array	$data	The form data.
356
	* @return void
357
	*/
358 3
	protected function validate($data)
359
	{
360 3
		if ($data['ad_name'] === '')
361 3
		{
362 1
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
363 1
		}
364 3
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
365 3
		{
366 1
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
367 1
		}
368 3
	}
369
370
	/**
371
	* Assign form data to the template.
372
	*
373
	* @param	array	$data	The form data.
374
	* @return void
375
	*/
376 2
	protected function assign_form_data($data)
377
	{
378 2
		$this->template->assign_vars(array(
379 2
			'S_ERROR'			=> (bool) count($this->errors),
380 2
			'ERROR_MSG'			=> count($this->errors) ? implode('<br />', $this->errors) : '',
381
382 2
			'AD_NAME'		=> $data['ad_name'],
383 2
			'AD_NOTE'		=> $data['ad_note'],
384 2
			'AD_CODE'		=> $data['ad_code'],
385 2
			'AD_ENABLED'	=> $data['ad_enabled'],
386 2
		));
387 2
	}
388
389
	/**
390
	* Print success message.
391
	*
392
	* It takes arguments in the form of a language key, followed by language substitution values.
393
	*/
394 4
	protected function success()
395
	{
396 4
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
397
	}
398
399
	/**
400
	* Print error message.
401
	*
402
	* It takes arguments in the form of a language key, followed by language substitution values.
403
	*/
404 2
	protected function error()
405
	{
406 2
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
407
	}
408
}
409