Completed
Pull Request — master (#4)
by Jakub
06:03
created

admin_controller::action_edit()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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