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