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

admin_controller::action_edit()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 75
Code Lines 43

Duplication

Lines 4
Ratio 5.33 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 75
ccs 0
cts 64
cp 0
rs 6.2413
cc 8
eloc 43
nc 18
nop 0
crap 72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	/**
45
	* Constructor
46
	*
47
	* @param \phpbb\db\driver\driver_interface	$db					DB driver interface
48
	* @param \phpbb\template\template			$template			Template object
49
	* @param \phpbb\user						$user				User object
50
	* @param \phpbb\request\request				$request			Request object
51
	* @param string								$ads_table			Ads table
52
	* @param string								$php_ext			PHP extension
53
	* @param string								$phpbb_admin_path	Path to admin
54
	*/
55
	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)
56
	{
57
		$this->db = $db;
58
		$this->template = $template;
59
		$this->user = $user;
60
		$this->request = $request;
61
		$this->ads_table = $ads_table;
62
		$this->php_ext = $php_ext;
63
		$this->phpbb_admin_path = $phpbb_admin_path;
64
	}
65
66
	/**
67
	* Set page url
68
	*
69
	* @param string $u_action Custom form action
70
	* @return void
71
	*/
72
	public function set_page_url($u_action)
73
	{
74
		$this->u_action = $u_action;
75
	}
76
77
	/**
78
	* Load module-specific language
79
	*
80
	* @return void
81
	*/
82
	public function load_lang()
83
	{
84
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
85
	}
86
87
	/**
88
	* Get ACP page title for Ads module
89
	*
90
	* @return string	Language string for Ads ACP module
91
	*/
92
	public function get_page_title()
93
	{
94
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
95
	}
96
97
	/**
98
	* Get action
99
	*
100
	* @return string	Ads module action
101
	*/
102
	public function get_action()
103
	{
104
		return $this->request->variable('action', '');
105
	}
106
107
	/**
108
	* Add an advertisement
109
	*
110
	* @return void
111
	*/
112
	public function action_add()
113
	{
114
		$errors = array();
115
116
		add_form_key('phpbb/admanagement/add');
117
		if ($this->request->is_set_post('submit'))
118
		{
119
			if (!check_form_key('phpbb/admanagement/add'))
120
			{
121
				$errors[] = $this->user->lang('FORM_INVALID');
122
			}
123
124
			$data = array(
125
				'ad_name'		=> $this->request->variable('ad_name', '', true),
126
				'ad_note'		=> $this->request->variable('ad_note', '', true),
127
				'ad_code'		=> $this->request->variable('ad_code', '', true),
128
				'ad_enabled'	=> $this->request->variable('ad_enabled', false),
129
			);
130
131
			// Validate data
132
			if ($data['ad_name'] === '')
133
			{
134
				$errors[] = $this->user->lang('AD_NAME_REQUIRED');
135
			}
136 View Code Duplication
			if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
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...
137
			{
138
				$errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
139
			}
140
141
			if (empty($errors))
142
			{
143
				// Insert the ad data to the database
144
				$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
145
				$this->db->sql_query($sql);
146
147
				trigger_error($this->user->lang('ACP_AD_ADD_SUCCESS') . adm_back_link($this->u_action));
148
			}
149
			else
150
			{
151
				$this->template->assign_vars(array(
152
					'S_ERROR'			=> (bool) count($errors),
153
					'ERROR_MSG'			=> count($errors) ? implode('<br />', $errors) : '',
154
155
					'AD_NAME'		=> $data['ad_name'],
156
					'AD_NOTE'		=> $data['ad_note'],
157
					'AD_CODE'		=> $data['ad_code'],
158
					'AD_ENABLED'	=> $data['ad_enabled'],
159
				));
160
			}
161
		}
162
163
		// Set output vars for display in the template
164
		$this->template->assign_vars(array(
165
			'S_ADD_AD'	=> true,
166
			'U_BACK'	=> $this->u_action,
167
		));
168
	}
169
170
	/**
171
	* Edit an advertisement
172
	*
173
	* @return void
174
	*/
175
	public function action_edit()
176
	{
177
		$ad_id = $this->request->variable('id', 0);
178
		$errors = array();
179
180
		add_form_key('phpbb/admanagement/edit');
181
		if ($this->request->is_set_post('submit'))
182
		{
183
			if (!check_form_key('phpbb/admanagement/edit'))
184
			{
185
				$errors[] = $this->user->lang('FORM_INVALID');
186
			}
187
188
			$data = array(
189
				'ad_name'		=> $this->request->variable('ad_name', '', true),
190
				'ad_note'		=> $this->request->variable('ad_note', '', true),
191
				'ad_code'		=> $this->request->variable('ad_code', '', true),
192
				'ad_enabled'	=> $this->request->variable('ad_enabled', false),
193
			);
194
195
			// Validate data
196
			if ($data['ad_name'] === '')
197
			{
198
				$errors[] = $this->user->lang('AD_NAME_REQUIRED');
199
			}
200 View Code Duplication
			if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
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...
201
			{
202
				$errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
203
			}
204
205
			if (empty($errors))
206
			{
207
				// Insert the ad data to the database
208
				$sql = 'UPDATE ' . $this->ads_table . '
209
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
210
					WHERE ad_id = ' . (int) $ad_id;
211
				$this->db->sql_query($sql);
212
213
				trigger_error($this->user->lang('ACP_AD_EDIT_SUCCESS') . adm_back_link($this->u_action));
214
			}
215
			else
216
			{
217
				$this->template->assign_vars(array(
218
					'S_ERROR'			=> (bool) count($errors),
219
					'ERROR_MSG'			=> count($errors) ? implode('<br />', $errors) : '',
220
				));
221
			}
222
		}
223
		else
224
		{
225
			$sql = 'SELECT *
226
				FROM ' . $this->ads_table . '
227
				WHERE ad_id = ' . (int) $ad_id;
228
			$result = $this->db->sql_query($sql);
229
			$data = $this->db->sql_fetchrow($result);
230
			$this->db->sql_freeresult($result);
231
232
			if (empty($data))
233
			{
234
				trigger_error($this->user->lang('ACP_AD_DOES_NOT_EXIST') . adm_back_link($this->u_action), E_USER_WARNING);
235
			}
236
		}
237
238
		// Set output vars for display in the template
239
		$this->template->assign_vars(array(
240
			'S_EDIT_AD'	=> true,
241
			'EDIT_ID'	=> $ad_id,
242
			'U_BACK'	=> $this->u_action,
243
244
			'AD_NAME'		=> $data['ad_name'],
245
			'AD_NOTE'		=> $data['ad_note'],
246
			'AD_CODE'		=> $data['ad_code'],
247
			'AD_ENABLED'	=> $data['ad_enabled'],
248
		));
249
	}
250
251
	/**
252
	* Enable/disable an advertisement
253
	*
254
	* @param	bool	$enable	Enable or disable the advertisement?
255
	* @return void
256
	*/
257
	public function ad_enable($enable)
258
	{
259
		$sql = 'UPDATE ' . $this->ads_table . '
260
			SET ad_enabled = ' . (int) $enable . '
261
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
262
		$this->db->sql_query($sql);
263
		$success = (bool) $this->db->sql_affectedrows();
264
265
		// If AJAX was used, show user a result message
266
		if ($this->request->is_ajax())
267
		{
268
			$json_response = new \phpbb\json_response;
269
			$json_response->send(array(
270
				'text'	=> $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
271
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
272
			));
273
		}
274
275
		// Otherwise, show traditional infobox
276
		if ($success)
277
		{
278
			trigger_error($this->user->lang($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS') . adm_back_link($this->u_action));
279
		}
280
		else
281
		{
282
			trigger_error($this->user->lang($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED') . adm_back_link($this->u_action), E_USER_WARNING);
283
		}
284
	}
285
286
	/**
287
	* Delete an advertisement
288
	*
289
	* @return void
290
	*/
291
	public function action_delete()
292
	{
293
		$ad_id = $this->request->variable('id', 0);
294
		if ($ad_id)
295
		{
296
			if (confirm_box(true))
297
			{
298
				$sql = 'DELETE FROM ' . $this->ads_table . '
299
					WHERE ad_id = ' . (int) $ad_id;
300
				$this->db->sql_query($sql);
301
302
				// Only notify user on error
303
				if (!$this->db->sql_affectedrows())
304
				{
305
					trigger_error($this->user->lang('ACP_AD_DELETE_ERRORED') . adm_back_link($this->u_action), E_USER_WARNING);
306
				}
307
			}
308
			else
309
			{
310
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
311
					'id'		=> $ad_id,
312
					'i'			=> $this->request->variable('i', ''),
313
					'mode'		=> $this->request->variable('mode', ''),
314
					'action'	=> 'delete'))
315
				);
316
			}
317
		}
318
	}
319
320
321
	/**
322
	* Display the ads
323
	*
324
	* @return void
325
	*/
326
	public function list_ads()
327
	{
328
		$sql = 'SELECT ad_id, ad_name, ad_enabled
329
			FROM ' . $this->ads_table;
330
		$result = $this->db->sql_query($sql);
331
		while ($row = $this->db->sql_fetchrow($result))
332
		{
333
			$ad_enabled = (bool) $row['ad_enabled'];
334
335
			$this->template->assign_block_vars('ads', array(
336
				'NAME'		=> $row['ad_name'],
337
				'S_ENABLED'	=> (int) $ad_enabled,
338
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
339
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
340
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
341
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
342
			));
343
		}
344
		$this->db->sql_freeresult($result);
345
346
		// Set output vars for display in the template
347
		$this->template->assign_vars(array(
348
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
349
			'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') . '" />',
350
		));
351
	}
352
}
353