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

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 7
crap 2
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
	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
		$this->db = $db;
61
		$this->template = $template;
62
		$this->user = $user;
63
		$this->request = $request;
64
		$this->ads_table = $ads_table;
65
		$this->php_ext = $php_ext;
66
		$this->phpbb_admin_path = $phpbb_admin_path;
67
	}
68
69
	/**
70
	* Set page url
71
	*
72
	* @param string $u_action Custom form action
73
	* @return void
74
	*/
75
	public function set_page_url($u_action)
76
	{
77
		$this->u_action = $u_action;
78
	}
79
80
	/**
81
	* Load module-specific language
82
	*
83
	* @return void
84
	*/
85
	public function load_lang()
86
	{
87
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
88
	}
89
90
	/**
91
	* Get ACP page title for Ads module
92
	*
93
	* @return string	Language string for Ads ACP module
94
	*/
95
	public function get_page_title()
96
	{
97
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
98
	}
99
100
	/**
101
	* Get action
102
	*
103
	* @return string	Ads module action
104
	*/
105
	public function get_action()
106
	{
107
		return $this->request->variable('action', '');
108
	}
109
110
	/**
111
	* Add an advertisement
112
	*
113
	* @return void
114
	*/
115
	public function action_add()
116
	{
117
		$errors = array();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
119
		add_form_key('phpbb/admanagement/add');
120
		if ($this->request->is_set_post('submit'))
121
		{
122
			$this->check_form_key('phpbb/admanagement/add');
123
124
			$data = $this->get_form_data();
125
126
			$this->validate($data);
127
128 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...
129
			{
130
				// Insert the ad data to the database
131
				$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
132
				$this->db->sql_query($sql);
133
134
				$this->success('ACP_AD_ADD_SUCCESS');
135
			}
136
			else
137
			{
138
				$this->assign_errors();
139
				$this->assign_form_data($data);
140
			}
141
		}
142
143
		// Set output vars for display in the template
144
		$this->template->assign_vars(array(
145
			'S_ADD_AD'	=> true,
146
			'U_BACK'	=> $this->u_action,
147
		));
148
	}
149
150
	/**
151
	* Edit an advertisement
152
	*
153
	* @return void
154
	*/
155
	public function action_edit()
156
	{
157
		$ad_id = $this->request->variable('id', 0);
158
		$errors = array();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
160
		add_form_key('phpbb/admanagement/edit');
161
		if ($this->request->is_set_post('submit'))
162
		{
163
			$this->check_form_key('phpbb/admanagement/edit');
164
165
			$data = $this->get_form_data();
166
167
			$this->validate($data);
168
169 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...
170
			{
171
				// Insert the ad data to the database
172
				$sql = 'UPDATE ' . $this->ads_table . '
173
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
174
					WHERE ad_id = ' . (int) $ad_id;
175
				$this->db->sql_query($sql);
176
177
				$this->success('ACP_AD_EDIT_SUCCESS');
178
			}
179
			else
180
			{
181
				$this->assign_errors();
182
			}
183
		}
184
		else
185
		{
186
			$sql = 'SELECT *
187
				FROM ' . $this->ads_table . '
188
				WHERE ad_id = ' . (int) $ad_id;
189
			$result = $this->db->sql_query($sql);
190
			$data = $this->db->sql_fetchrow($result);
191
			$this->db->sql_freeresult($result);
192
193
			if (empty($data))
194
			{
195
				$this->error('ACP_AD_DOES_NOT_EXIST');
196
			}
197
		}
198
199
		// Set output vars for display in the template
200
		$this->template->assign_vars(array(
201
			'S_EDIT_AD'	=> true,
202
			'EDIT_ID'	=> $ad_id,
203
			'U_BACK'	=> $this->u_action,
204
		));
205
		$this->assign_form_data($data);
206
	}
207
208
	/**
209
	* Enable/disable an advertisement
210
	*
211
	* @param	bool	$enable	Enable or disable the advertisement?
212
	* @return void
213
	*/
214
	public function ad_enable($enable)
215
	{
216
		$sql = 'UPDATE ' . $this->ads_table . '
217
			SET ad_enabled = ' . (int) $enable . '
218
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
219
		$this->db->sql_query($sql);
220
		$success = (bool) $this->db->sql_affectedrows();
221
222
		// If AJAX was used, show user a result message
223
		if ($this->request->is_ajax())
224
		{
225
			$json_response = new \phpbb\json_response;
226
			$json_response->send(array(
227
				'text'	=> $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
228
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
229
			));
230
		}
231
232
		// Otherwise, show traditional infobox
233
		if ($success)
234
		{
235
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
236
		}
237
		else
238
		{
239
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
240
		}
241
	}
242
243
	/**
244
	* Delete an advertisement
245
	*
246
	* @return void
247
	*/
248
	public function action_delete()
249
	{
250
		$ad_id = $this->request->variable('id', 0);
251
		if ($ad_id)
252
		{
253
			if (confirm_box(true))
254
			{
255
				$sql = 'DELETE FROM ' . $this->ads_table . '
256
					WHERE ad_id = ' . (int) $ad_id;
257
				$this->db->sql_query($sql);
258
259
				// Only notify user on error or if not ajax
260
				if (!$this->db->sql_affectedrows())
261
				{
262
					$this->error('ACP_AD_DELETE_ERRORED');
263
				}
264
				else if (!$this->request->is_ajax())
265
				{
266
					$this->success('ACP_AD_DELETE_SUCCESS');
267
				}
268
			}
269
			else
270
			{
271
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
272
					'id'		=> $ad_id,
273
					'i'			=> $this->request->variable('i', ''),
274
					'mode'		=> $this->request->variable('mode', ''),
275
					'action'	=> 'delete'
276
				)));
277
			}
278
		}
279
	}
280
281
282
	/**
283
	* Display the ads
284
	*
285
	* @return void
286
	*/
287
	public function list_ads()
288
	{
289
		$sql = 'SELECT ad_id, ad_name, ad_enabled
290
			FROM ' . $this->ads_table;
291
		$result = $this->db->sql_query($sql);
292
		while ($row = $this->db->sql_fetchrow($result))
293
		{
294
			$ad_enabled = (bool) $row['ad_enabled'];
295
296
			$this->template->assign_block_vars('ads', array(
297
				'NAME'		=> $row['ad_name'],
298
				'S_ENABLED'	=> (int) $ad_enabled,
299
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
300
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
301
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
302
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
303
			));
304
		}
305
		$this->db->sql_freeresult($result);
306
307
		// Set output vars for display in the template
308
		$this->template->assign_vars(array(
309
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
310
			'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') . '" />',
311
		));
312
	}
313
314
	/**
315
	* Check the form key.
316
	*
317
	* @param	string	$form_name	The name of the form.
318
	* @return void
319
	*/
320
	protected function check_form_key($form_name)
321
	{
322
		if (!check_form_key($form_name))
323
		{
324
			$this->errors[] = $this->user->lang('FORM_INVALID');
325
		}
326
	}
327
328
	/**
329
	* Get admin form data.
330
	*
331
	* @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
332
	*/
333
	protected function get_form_data()
334
	{
335
		return array(
336
			'ad_name'		=> $this->request->variable('ad_name', '', true),
337
			'ad_note'		=> $this->request->variable('ad_note', '', true),
338
			'ad_code'		=> $this->request->variable('ad_code', '', true),
339
			'ad_enabled'	=> $this->request->variable('ad_enabled', false),
340
		);
341
	}
342
343
	/**
344
	* Validate form data.
345
	*
346
	* @param	array	$data	The form data.
347
	* @return void
348
	*/
349
	protected function validate($data)
350
	{
351
		if ($data['ad_name'] === '')
352
		{
353
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
354
		}
355
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
356
		{
357
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
358
		}
359
	}
360
361
	/**
362
	* Assign errors to the template.
363
	*
364
	* @return void
365
	*/
366
	protected function assign_errors()
367
	{
368
		$this->template->assign_vars(array(
369
			'S_ERROR'			=> (bool) count($this->errors),
370
			'ERROR_MSG'			=> count($this->errors) ? implode('<br />', $this->errors) : '',
371
		));
372
	}
373
374
	/**
375
	* Assign form data to the template.
376
	*
377
	* @param	array	$data	The form data.
378
	* @return void
379
	*/
380
	protected function assign_form_data($data)
381
	{
382
		$this->template->assign_vars(array(
383
			'AD_NAME'		=> $data['ad_name'],
384
			'AD_NOTE'		=> $data['ad_note'],
385
			'AD_CODE'		=> $data['ad_code'],
386
			'AD_ENABLED'	=> $data['ad_enabled'],
387
		));
388
	}
389
390
	/**
391
	* Print success message.
392
	*
393
	* It takes arguments in the form of a language key, followed by language substitution values.
394
	*/
395
	protected function success()
396
	{
397
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
398
	}
399
400
	/**
401
	* Print error message.
402
	*
403
	* It takes arguments in the form of a language key, followed by language substitution values.
404
	*/
405
	protected function error()
406
	{
407
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
408
	}
409
}
410