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

admin_controller::assign_form_data()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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