Completed
Pull Request — master (#56)
by Jakub
10:57
created

admin_controller::action_delete()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5.0432

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 22
cts 25
cp 0.88
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 5
nop 0
crap 5.0432
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\ads\controller;
12
13
use phpbb\ads\controller\admin_input as input;
14
15
/**
16
* Admin controller
17
*/
18
class admin_controller
19
{
20
	/** @var \phpbb\template\template */
21
	protected $template;
22
23
	/** @var \phpbb\language\language */
24
	protected $language;
25
26
	/** @var \phpbb\request\request */
27
	protected $request;
28
29
	/** @var \phpbb\ads\ad\manager */
30
	protected $manager;
31
32
	/** @var \phpbb\config\db_text */
33
	protected $config_text;
34
35
	/** @var \phpbb\config\config */
36
	protected $config;
37
38
	/** @var \phpbb\group\helper */
39
	protected $group_helper;
40
41
	/** @var \phpbb\ads\controller\admin_input */
42
	protected $input;
43
44
	/** @var \phpbb\ads\controller\admin_helper */
45
	protected $helper;
46
47
	/** @var string root_path */
48
	protected $root_path;
49
50
	/** @var string php_ext */
51
	protected $php_ext;
52
53
	/** @var string Custom form action */
54
	protected $u_action;
55
56
	/**
57
	* Constructor
58
	*
59
	* @param \phpbb\template\template		    $template		  Template object
60
	* @param \phpbb\language\language           $language         Language object
61
	* @param \phpbb\request\request             $request          Request object
62
	* @param \phpbb\ads\ad\manager              $manager          Advertisement manager object
63
	* @param \phpbb\config\db_text              $config_text      Config text object
64
	* @param \phpbb\config\config               $config           Config object
65
	* @param \phpbb\group\helper                $group_helper     Group helper object
66
	* @param \phpbb\ads\controller\admin_input 	$input			  Admin input object
67
	* @param \phpbb\ads\controller\admin_helper $helper			  Admin helper object
68
	* @param string								$root_path		  phpBB root path
69
	* @param string								$php_ext		  PHP extension
70
	*/
71 30
	public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, $root_path, $php_ext)
72
	{
73 30
		$this->template = $template;
74 30
		$this->language = $language;
75 30
		$this->request = $request;
76 30
		$this->manager = $manager;
77 30
		$this->config_text = $config_text;
78 30
		$this->config = $config;
79 30
		$this->group_helper = $group_helper;
80 30
		$this->input = $input;
81 30
		$this->helper = $helper;
82 30
		$this->root_path = $root_path;
83 30
		$this->php_ext = $php_ext;
84 30
	}
85
86
	/**
87
	 * Process user request for manage mode
88
	 *
89
	 * @return void
90
	 */
91 6
	public function mode_manage()
92
	{
93 6
		$this->setup();
94
95
		// Trigger specific action
96 6
		$action = $this->request->variable('action', '');
97 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
98 6
		{
99 5
			$this->{'action_' . $action}();
100 5
		}
101
102
		// Otherwise default to this
103 6
		$this->list_ads();
104 6
	}
105
106
	/**
107
	 * Process user request for settings mode
108
	 *
109
	 * @return void
110
	 */
111 3
	public function mode_settings()
112
	{
113 3
		$this->setup();
114
115 3
		add_form_key('phpbb/ads/settings');
116 3
		if ($this->request->is_set_post('submit'))
117 3
		{
118
			// Validate form key
119 2
			if (check_form_key('phpbb/ads/settings'))
120 2
			{
121 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
122 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
123 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
124 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
125
126 1
				$this->success('ACP_AD_SETTINGS_SAVED');
127
			}
128
129 1
			$this->helper->assign_errors(array($this->language->lang('FORM_INVALID')));
130 1
		}
131
132 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
133 2
		$groups = $this->manager->load_groups();
134 2 View Code Duplication
		foreach ($groups as $group)
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...
135
		{
136 2
			$this->template->assign_block_vars('groups', array(
137 2
				'ID'         => $group['group_id'],
138 2
				'NAME'       => $this->group_helper->get_name($group['group_name']),
139 2
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
140 2
			));
141 2
		}
142
143 2
		$this->template->assign_vars(array(
144 2
			'U_ACTION'          => $this->u_action,
145 2
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
146 2
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
147 2
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
148 2
		));
149 2
	}
150
151
	/**
152
	 * Set page url
153
	 *
154
	 * @param string $u_action Custom form action
155
	 * @return void
156
	 */
157 24
	public function set_page_url($u_action)
158
	{
159 24
		$this->u_action = $u_action;
160 24
	}
161
162
	/**
163
	 * Get ACP page title for Ads module
164
	 *
165
	 * @return string    Language string for Ads ACP module
166
	 */
167 1
	public function get_page_title()
168
	{
169 1
		return $this->language->lang('ACP_PHPBB_ADS_TITLE');
170
	}
171
172
	/**
173
	 * Add an advertisement
174
	 *
175
	 * @return void
176
	 */
177 12
	public function action_add()
178
	{
179 5
		$preview = $this->request->is_set_post('preview');
180 5
		$submit = $this->request->is_set_post('submit');
181 5
		$upload_banner = $this->request->is_set_post('upload_banner');
182
183 5
		add_form_key('phpbb/ads/add');
184 5
		if ($preview || $submit || $upload_banner)
185 5
		{
186 4
			$data = $this->input->get_form_data('phpbb/ads/add');
187
188 View Code Duplication
			if ($preview)
189 4
			{
190 1
				$this->ad_preview($data['ad_code']);
191 1
			}
192
			else if ($upload_banner)
193 3
			{
194 1
				$data['ad_code'] = $this->input->banner_upload($data['ad_code']);
195 1
			}
196 12
			else if (!$this->input->has_errors())
197 2
			{
198 1
				$ad_id = $this->manager->insert_ad($data);
199 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
200
201 1
				$this->helper->log('ADD', $data['ad_name']);
202
203 1
				$this->success('ACP_AD_ADD_SUCCESS');
204
			}
205
206 3
			$this->helper->assign_locations($data['ad_locations']);
207 3
			$this->helper->assign_form_data($data);
208 3
			$this->helper->assign_errors($this->input->get_errors());
209 3
		}
210
		else
211
		{
212 1
			$this->helper->assign_locations();
213
		}
214
215
		// Set output vars for display in the template
216 4
		$this->template->assign_vars(array(
217 4
			'S_ADD_AD'           => true,
218 4
			'U_BACK'             => $this->u_action,
219 4
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
220 4
			'PICKER_DATE_FORMAT' => input::DATE_FORMAT,
221 4
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
222 4
		));
223 4
	}
224
225
	/**
226
	 * Edit an advertisement
227
	 *
228
	 * @return void
229
	 */
230 7
	public function action_edit()
231
	{
232 7
		$ad_id = $this->request->variable('id', 0);
233 7
		$preview = $this->request->is_set_post('preview');
234 7
		$submit = $this->request->is_set_post('submit');
235 7
		$upload_banner = $this->request->is_set_post('upload_banner');
236
237 7
		add_form_key('phpbb/ads/edit/' . $ad_id);
238 7
		if ($preview || $submit || $upload_banner)
239 7
		{
240 5
			$data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
241
242
			if ($preview)
243 5
			{
244 1
				$this->ad_preview($data['ad_code']);
245 1
			}
246 View Code Duplication
			else if ($upload_banner)
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...
247 4
			{
248
				$data['ad_code'] = $this->input->banner_upload($data['ad_code']);
249
			}
250 4
			else if (!$this->input->has_errors())
251 4
			{
252 2
				$success = $this->manager->update_ad($ad_id, $data);
253
254
				if ($success)
255 2
				{
256
					// Only insert new ad locations to DB when ad exists
257 1
					$this->manager->delete_ad_locations($ad_id);
258 1
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
259
260 1
					$this->helper->log('EDIT', $data['ad_name']);
261
262 1
					$this->success('ACP_AD_EDIT_SUCCESS');
263
				}
264
265 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
266
			}
267 3
		}
268
		else
269
		{
270 2
			$data = $this->manager->get_ad($ad_id);
271 2
			if (empty($data))
272 2
			{
273 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
274
			}
275
276
			// Load ad template locations
277 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
278
		}
279
280
		// Set output vars for display in the template
281 4
		$this->template->assign_vars(array(
282 4
			'S_EDIT_AD'          => true,
283 4
			'EDIT_ID'            => $ad_id,
284 4
			'U_BACK'             => $this->u_action,
285 4
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
286 4
			'PICKER_DATE_FORMAT' => input::DATE_FORMAT,
287 4
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
288 4
		));
289 4
		$this->helper->assign_locations($data['ad_locations']);
290 4
		$this->helper->assign_form_data($data);
291 4
		$this->helper->assign_errors($this->input->get_errors());
292 4
	}
293
294
	/**
295
	 * Enable an advertisement
296
	 *
297
	 * @return void
298
	 */
299 3
	public function action_enable()
300
	{
301 3
		$this->ad_enable(true);
302 1
	}
303
304
	/**
305
	 * Disable an advertisement
306
	 *
307
	 * @return void
308
	 */
309 3
	public function action_disable()
310
	{
311 3
		$this->ad_enable(false);
312 1
	}
313
314
	/**
315
	 * Delete an advertisement
316
	 *
317
	 * @return void
318
	 */
319 3
	public function action_delete()
320
	{
321 3
		$ad_id = $this->request->variable('id', 0);
322
		if ($ad_id)
323 3
		{
324 3
			if (confirm_box(true))
325 3
			{
326
				// Get ad data so that we can log ad name
327 2
				$ad_data = $this->manager->get_ad($ad_id);
328
329
				// Delete ad and it's template locations
330 2
				$this->manager->delete_ad_locations($ad_id);
331 2
				$success = $this->manager->delete_ad($ad_id);
332
333
				// Only notify user on error or if not ajax
334 2
				if (!$success)
335 2
				{
336 1
					$this->error('ACP_AD_DELETE_ERRORED');
337
				}
338
				else
339
				{
340 1
					$this->helper->log('DELETE', $ad_data['ad_name']);
341
342 1
					if (!$this->request->is_ajax())
343 1
					{
344 1
						$this->success('ACP_AD_DELETE_SUCCESS');
345
					}
346
				}
347
			}
348
			else
349
			{
350 1
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
351 1
					'id'     => $ad_id,
352 1
					'i'      => $this->request->variable('i', ''),
353 1
					'mode'   => $this->request->variable('mode', ''),
354
					'action' => 'delete'
355 1
				)));
356
			}
357 1
		}
358 1
	}
359
360
	/**
361
	 * Display the ads
362
	 *
363
	 * @return void
364
	 */
365 1
	public function list_ads()
366
	{
367 1
		foreach ($this->manager->get_all_ads() as $row)
368
		{
369 1
			$ad_enabled = (int) $row['ad_enabled'];
370 1
			$ad_end_date = (int) $row['ad_end_date'];
371 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
372 1
			if ($ad_expired && $ad_enabled)
373 1
			{
374 1
				$ad_enabled = 0;
375 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
376 1
			}
377
378 1
			$this->template->assign_block_vars('ads', array(
379 1
				'NAME'               => $row['ad_name'],
380 1
				'END_DATE'           => $this->helper->prepare_end_date($ad_end_date),
381 1
				'VIEWS'              => $row['ad_views'],
382 1
				'CLICKS'             => $row['ad_clicks'],
383 1
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
384 1
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
385 1
				'S_END_DATE_EXPIRED' => $ad_expired,
386 1
				'S_ENABLED'          => $ad_enabled,
387 1
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
388 1
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
389 1
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
390 1
			));
391 1
		}
392
393
		// Set output vars for display in the template
394 1
		$this->template->assign_vars(array(
395 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
396 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
397 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
398 1
		));
399 1
	}
400
401
	/**
402
	 * Perform general tasks
403
	 *
404
	 * @return void
405
	 */
406 9
	protected function setup()
407
	{
408 9
		if (!function_exists('user_get_id_name'))
409 9
		{
410
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
411
		}
412
413 9
		$this->language->add_lang('posting'); // Used by banner_upload() file errors
414 9
		$this->language->add_lang('acp', 'phpbb/ads');
415
416 9
		$this->template->assign_var('S_PHPBB_ADS', true);
417 9
	}
418
419
	/**
420
	 * Enable/disable an advertisement
421
	 *
422
	 * @param    bool $enable Enable or disable the advertisement?
423
	 * @return void
424
	 */
425 4
	protected function ad_enable($enable)
426
	{
427 4
		$ad_id = $this->request->variable('id', 0);
428
429 4
		$success = $this->manager->update_ad($ad_id, array(
430 4
			'ad_enabled' => (int) $enable,
431 4
		));
432
433
		// If AJAX was used, show user a result message
434 4
		if ($this->request->is_ajax())
435 4
		{
436
			$json_response = new \phpbb\json_response;
437
			$json_response->send(array(
438
				'text'  => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'),
439
				'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable),
440
			));
441
		}
442
443
		// Otherwise, show traditional infobox
444
		if ($success)
445 4
		{
446 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
447
		}
448
		else
449
		{
450 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
451
		}
452
	}
453
454
	/**
455
	 * Prepare advertisement preview
456
	 *
457
	 * @param    string $code Ad code to preview
458
	 * @return    void
459
	 */
460 2
	protected function ad_preview($code)
461
	{
462 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
463 2
	}
464
465
	/**
466
	 * Print success message.
467
	 *
468
	 * It takes arguments in the form of a language key, followed by language substitution values.
469
	 */
470 6
	protected function success()
471
	{
472 6
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action));
473
	}
474
475
	/**
476
	 * Print error message.
477
	 *
478
	 * It takes arguments in the form of a language key, followed by language substitution values.
479
	 */
480 5
	protected function error()
481
	{
482 5
		trigger_error(call_user_func_array(array($this->language, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
483
	}
484
}
485