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

admin_controller::action_delete()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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