Completed
Pull Request — master (#2)
by Jakub
06:02
created

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