Completed
Pull Request — master (#4)
by Jakub
05:56
created

admin_controller::get_page_title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 27 View Code Duplication
	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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
	{
60 27
		$this->template = $template;
61 27
		$this->user = $user;
62 27
		$this->request = $request;
63 27
		$this->manager = $manager;
64 27
		$this->location_manager = $location_manager;
65 27
		$this->php_ext = $php_ext;
66 27
		$this->ext_path = $ext_path;
67 27
	}
68
69
	/**
70
	* Process user request
71
	*
72
	* @return void
73
	*/
74 6
	public function main()
75
	{
76 6
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
77
78
		// Trigger specific action
79 6
		$action = $this->request->variable('action', '');
80 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
81 6
		{
82 5
			$this->{'action_' . $action}();
83 5
		}
84
85
		// Otherwise default to this
86 6
		$this->list_ads();
87 6
	}
88
89
	/**
90
	* Set page url
91
	*
92
	* @param string $u_action Custom form action
93
	* @return void
94
	*/
95 21
	public function set_page_url($u_action)
96
	{
97 21
		$this->u_action = $u_action;
98 21
	}
99
100
	/**
101
	* Get ACP page title for Ads module
102
	*
103
	* @return string	Language string for Ads ACP module
104
	*/
105 1
	public function get_page_title()
106
	{
107 1
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
108
	}
109
110
	/**
111
	* Add an advertisement
112
	*
113
	* @return void
114
	*/
115 5
	public function action_add()
116
	{
117 5
		add_form_key('phpbb/admanagement/add');
118 5
		if ($this->request->is_set_post('submit'))
119 5
		{
120 4
			$data = $this->get_form_data();
121
122 4
			$this->validate($data, 'phpbb/admanagement/add');
123
124 4
			if (empty($this->errors))
125 4
			{
126 1
				$ad_id = $this->manager->insert_ad($data);
127 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
128
129 1
				$this->success('ACP_AD_ADD_SUCCESS');
130
			}
131
132 3
			$this->assign_locations($data);
133 3
			$this->assign_form_data($data);
134 3
		}
135
136
		// Set output vars for display in the template
137 4
		$this->template->assign_vars(array(
138 4
			'S_ADD_AD'	=> true,
139 4
			'U_BACK'	=> $this->u_action,
140 4
		));
141 4
		$this->assign_locations();
142 4
	}
143
144
	/**
145
	* Edit an advertisement
146
	*
147
	* @return void
148
	*/
149 14
	public function action_edit()
150
	{
151 7
		$ad_id = $this->request->variable('id', 0);
152
153 7
		add_form_key('phpbb/admanagement/edit/' . $ad_id);
154 7
		if ($this->request->is_set_post('submit'))
155 7
		{
156 5
			$data = $this->get_form_data();
157
158 5
			$this->validate($data, 'phpbb/admanagement/edit/' . $ad_id);
159
160 5
			if (empty($this->errors))
161 5
			{
162 2
				$success = $this->manager->update_ad($ad_id, $data);
163
164
				if ($success)
165 2
				{
166
					// Only insert new ad locations to DB when ad exists
167 1
					$this->manager->delete_ad_locations($ad_id);
168 1
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
169
170 1
					$this->success('ACP_AD_EDIT_SUCCESS');
171
				}
172 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
173
			}
174 3
		}
175
		else
176
		{
177
			// Load ad data
178 2
			$data = $this->manager->get_ad($ad_id);
179 2
			if (empty($data))
180 2
			{
181 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
182
			}
183
184
			// Load ad template locations
185 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
186
		}
187
188
		// Set output vars for display in the template
189 4
		$this->template->assign_vars(array(
190 4
			'S_EDIT_AD'	=> true,
191 4
			'EDIT_ID'	=> $ad_id,
192 4
			'U_BACK'	=> $this->u_action,
193 4
		));
194 4
		$this->assign_locations($data);
195 4
		$this->assign_form_data($data);
196 14
	}
197
198
	/**
199
	* Enable an advertisement
200
	*
201
	* @return void
202
	*/
203 3
	public function action_enable()
204
	{
205 3
		$this->ad_enable(true);
206 1
	}
207
208
	/**
209
	* Disable an advertisement
210
	*
211
	* @return void
212
	*/
213 3
	public function action_disable()
214
	{
215 3
		$this->ad_enable(false);
216 1
	}
217
218
	/**
219
	* Delete an advertisement
220
	*
221
	* @return void
222
	*/
223 3
	public function action_delete()
224
	{
225 3
		$ad_id = $this->request->variable('id', 0);
226
		if ($ad_id)
227 3
		{
228 3
			if (confirm_box(true))
229 3
			{
230 2
				$this->manager->delete_ad_locations($ad_id);
231 2
				$success = $this->manager->delete_ad($ad_id);
232
233
				// Only notify user on error or if not ajax
234 2
				if (!$success)
235 2
				{
236 1
					$this->error('ACP_AD_DELETE_ERRORED');
237
				}
238 1
				else if (!$this->request->is_ajax())
239 1
				{
240 1
					$this->success('ACP_AD_DELETE_SUCCESS');
241
				}
242
			}
243
			else
244
			{
245 1
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
246 1
					'id'		=> $ad_id,
247 1
					'i'			=> $this->request->variable('i', ''),
248 1
					'mode'		=> $this->request->variable('mode', ''),
249
					'action'	=> 'delete'
250 1
				)));
251
			}
252 1
		}
253 1
	}
254
255
	/**
256
	* Display the ads
257
	*
258
	* @return void
259
	*/
260 1
	public function list_ads()
261
	{
262 1
		foreach ($this->manager->get_all_ads() as $row)
263
		{
264 1
			$ad_enabled = (int) $row['ad_enabled'];
265
266 1
			$this->template->assign_block_vars('ads', array(
267 1
				'NAME'		=> $row['ad_name'],
268 1
				'S_ENABLED'	=> $ad_enabled,
269 1
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
270 1
				'U_PREVIEW'	=> append_sid(generate_board_url() . '/index.' . $this->php_ext, 'ad_preview=' . $row['ad_id']),
271 1
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
272 1
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
273 1
			));
274 1
		}
275
276
		// Set output vars for display in the template
277 1
		$this->template->assign_vars(array(
278 1
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
279 1
			'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 1
		));
281 1
	}
282
283
	/**
284
	* Enable/disable an advertisement
285
	*
286
	* @param	bool	$enable	Enable or disable the advertisement?
287
	* @return void
288
	*/
289 4
	protected function ad_enable($enable)
290
	{
291 4
		$ad_id = $this->request->variable('id', 0);
292
293 4
		$success = $this->manager->update_ad($ad_id, array(
294 4
			'ad_enabled'	=> (int) $enable,
295 4
		));
296
297
		// If AJAX was used, show user a result message
298 4
		if ($this->request->is_ajax())
299 4
		{
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 4
		{
310 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
311
		}
312
		else
313
		{
314 2
			$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 9
	protected function get_form_data()
324
	{
325
		return array(
326 9
			'ad_name'		=> $this->request->variable('ad_name', '', true),
327 9
			'ad_note'		=> $this->request->variable('ad_note', '', true),
328 9
			'ad_code'		=> $this->request->variable('ad_code', '', true),
329 9
			'ad_enabled'	=> $this->request->variable('ad_enabled', 0),
330 9
			'ad_locations'	=> $this->request->variable('ad_locations', array('')),
331 9
		);
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 9
	protected function validate($data, $form_name)
342
	{
343 9
		if (!check_form_key($form_name))
344 9
		{
345 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
346 2
		}
347
348 9
		if ($data['ad_name'] === '')
349 9
		{
350 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
351 2
		}
352 9
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
353 9
		{
354 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
355 2
		}
356 9
	}
357
358
	/**
359
	* Assign form data to the template.
360
	*
361
	* @param	array	$data	The form data.
362
	* @return void
363
	*/
364 7
	protected function assign_form_data($data)
365
	{
366 7
		$this->template->assign_vars(array(
367 7
			'S_ERROR'		=> (bool) count($this->errors),
368 7
			'ERROR_MSG'		=> count($this->errors) ? implode('<br />', $this->errors) : '',
369
370 7
			'AD_NAME'		=> $data['ad_name'],
371 7
			'AD_NOTE'		=> $data['ad_note'],
372 7
			'AD_CODE'		=> $data['ad_code'],
373 7
			'AD_ENABLED'	=> $data['ad_enabled'],
374 7
		));
375 7
	}
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 8
	protected function assign_locations($data = false)
384
	{
385 8
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
386
		{
387 8
			$this->template->assign_block_vars('ad_locations', array(
388 8
				'LOCATION_ID'	=> $location_id,
389 8
				'LOCATION_DESC'	=> $location_data['desc'],
390 8
				'LOCATION_NAME'	=> $location_data['name'],
391 8
				'S_SELECTED'	=> $data ? in_array($location_id, $data['ad_locations']) : false,
392 8
			));
393 8
		}
394 8
	}
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 5
	protected function success()
402
	{
403 5
		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 5
	protected function error()
412
	{
413 5
		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