Completed
Pull Request — master (#48)
by Jakub
08:30
created

admin_controller   C

Complexity

Total Complexity 73

Size/Duplication

Total Lines 667
Duplicated Lines 4.95 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 73
c 1
b 0
f 0
lcom 1
cbo 2
dl 33
loc 667
ccs 0
cts 336
cp 0
rs 5.1939

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A mode_manage() 0 14 2
C mode_settings() 0 49 7
A set_page_url() 0 4 1
A get_page_title() 0 4 1
B action_add() 12 44 5
C action_edit() 17 68 8
A action_enable() 0 4 1
A action_disable() 0 4 1
B action_delete() 4 46 6
C list_ads() 0 35 7
A setup() 0 6 1
B ad_enable() 0 28 6
F get_form_data() 0 79 12
A assign_form_data() 0 17 2
A prepare_end_date() 0 14 3
A assign_locations() 0 12 3
A ad_preview() 0 4 1
A success() 0 4 1
A error() 0 4 1
A log() 0 4 1
A get_auth_admin() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like admin_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use admin_controller, and based on these observations, apply Extract Interface, too.

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
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
	const DEFAULT_PRIORITY = 5;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\user */
26
	protected $user;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\ad\manager */
32
	protected $manager;
33
34
	/** @var \phpbb\ads\location\manager */
35
	protected $location_manager;
36
37
	/** @var \phpbb\log\log */
38
	protected $log;
39
40
	/** @var \phpbb\config\db_text */
41
	protected $config_text;
42
43
	/** @var \phpbb\config\config */
44
	protected $config;
45
46
	/** @var string root_path */
47
	protected $root_path;
48
49
	/** @var string php_ext */
50
	protected $php_ext;
51
52
	/** @var string ext_path */
53
	protected $ext_path;
54
55
	/** @var \auth_admin Auth admin */
56
	protected $auth_admin;
57
58
	/** @var string Custom form action */
59
	protected $u_action;
60
61
	/** @var array Form validation errors */
62
	protected $errors = array();
63
64
	/**
65
	 * Constructor
66
	 *
67
	 * @param \phpbb\template\template    $template         Template object
68
	 * @param \phpbb\user                 $user             User object
69
	 * @param \phpbb\request\request      $request          Request object
70
	 * @param \phpbb\ads\ad\manager       $manager          Advertisement manager object
71
	 * @param \phpbb\ads\location\manager $location_manager Template location manager object
72
	 * @param \phpbb\log\log              $log              The phpBB log system
73
	 * @param \phpbb\config\db_text       $config_text      Config text object
74
	 * @param \phpbb\config\config        $config           Config object
75
	 * @param string                      $root_path        phpBB root path
76
	 * @param string                      $php_ext          PHP extension
77
	 * @param string                      $ext_path         Path to this extension
78
	 */
79
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, $root_path, $php_ext, $ext_path)
80
	{
81
		$this->template = $template;
82
		$this->user = $user;
83
		$this->request = $request;
84
		$this->manager = $manager;
85
		$this->location_manager = $location_manager;
86
		$this->log = $log;
87
		$this->config_text = $config_text;
88
		$this->config = $config;
89
		$this->root_path = $root_path;
90
		$this->php_ext = $php_ext;
91
		$this->ext_path = $ext_path;
92
93
		$this->auth_admin = $this->get_auth_admin();
94
	}
95
96
	/**
97
	 * Process user request for manage mode
98
	 *
99
	 * @return void
100
	 */
101
	public function mode_manage()
102
	{
103
		$this->setup();
104
105
		// Trigger specific action
106
		$action = $this->request->variable('action', '');
107
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
108
		{
109
			$this->{'action_' . $action}();
110
		}
111
112
		// Otherwise default to this
113
		$this->list_ads();
114
	}
115
116
	/**
117
	 * Process user request for settings mode
118
	 *
119
	 * @return void
120
	 */
121
	public function mode_settings()
122
	{
123
		$this->setup();
124
125
		add_form_key('phpbb/ads/settings');
126
		if ($this->request->is_set_post('submit'))
127
		{
128
			// Validate form key
129
			if (!check_form_key('phpbb/ads/settings'))
130
			{
131
				$this->errors[] = $this->user->lang('FORM_INVALID');
132
			}
133
134
			if (empty($this->errors))
135
			{
136
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
137
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
138
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
139
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
140
141
				$this->success('ACP_AD_SETTINGS_SAVED');
142
			}
143
144
			$this->template->assign_vars(array(
145
				'S_ERROR'   => (bool) count($this->errors),
146
				'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
147
			));
148
		}
149
150
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
151
		$groups = $this->manager->load_groups();
152
		foreach ($groups as $group)
153
		{
154
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
155
156
			$this->template->assign_block_vars('groups', array(
157
				'ID'         => $group['group_id'],
158
				'NAME'       => $group_name,
159
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
160
			));
161
		}
162
163
		$this->template->assign_vars(array(
164
			'U_ACTION'          => $this->u_action,
165
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
166
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
167
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
168
		));
169
	}
170
171
	/**
172
	 * Set page url
173
	 *
174
	 * @param string $u_action Custom form action
175
	 * @return void
176
	 */
177
	public function set_page_url($u_action)
178
	{
179
		$this->u_action = $u_action;
180
	}
181
182
	/**
183
	 * Get ACP page title for Ads module
184
	 *
185
	 * @return string    Language string for Ads ACP module
186
	 */
187
	public function get_page_title()
188
	{
189
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
190
	}
191
192
	/**
193
	 * Add an advertisement
194
	 *
195
	 * @return void
196
	 */
197
	public function action_add()
198
	{
199
		$preview = $this->request->is_set_post('preview');
200
		$submit = $this->request->is_set_post('submit');
201
202
		add_form_key('phpbb/ads/add');
203
		if ($preview || $submit)
204
		{
205
			$data = $this->get_form_data('phpbb/ads/add');
206
207
			if ($preview)
208
			{
209
				$this->ad_preview($data['ad_code']);
210
			}
211 View Code Duplication
			else if (empty($this->errors))
212
			{
213
				$ad_id = $this->manager->insert_ad($data);
214
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
215
216
				// Add u_phpbb_ads_owner permission
217
				$this->auth_admin->acl_set('user', 0, $data['ad_owner'], array('u_phpbb_ads_owner' => 1));
218
219
				$this->log('ADD', $data['ad_name']);
220
221
				$this->success('ACP_AD_ADD_SUCCESS');
222
			}
223
224
			$this->assign_locations($data);
225
			$this->assign_form_data($data);
226
		}
227
		else
228
		{
229
			$this->assign_locations();
230
		}
231
232
		// Set output vars for display in the template
233
		$this->template->assign_vars(array(
234
			'S_ADD_AD'           => true,
235
			'U_BACK'             => $this->u_action,
236
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
237
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
238
			'U_FIND_USERNAME'    => append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true'),
239
		));
240
	}
241
242
	/**
243
	 * Edit an advertisement
244
	 *
245
	 * @return void
246
	 */
247
	public function action_edit()
248
	{
249
		$ad_id = $this->request->variable('id', 0);
250
		$preview = $this->request->is_set_post('preview');
251
		$submit = $this->request->is_set_post('submit');
252
253
		$ad = $this->manager->get_ad($ad_id);
254
		if (empty($ad))
255
		{
256
			$this->error('ACP_AD_DOES_NOT_EXIST');
257
		}
258
259
		add_form_key('phpbb/ads/edit/' . $ad_id);
260
		if ($preview || $submit)
261
		{
262
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
263
264
			if ($preview)
265
			{
266
				$this->ad_preview($data['ad_code']);
267
			}
268
			else if (empty($this->errors))
269
			{
270
				// Delete u_phpbb_ads_owner permission if old owner owns no more ads
271 View Code Duplication
				if (count($this->manager->get_ads_by_owner($ad['ad_owner'])) == 1)
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...
272
				{
273
					$this->auth_admin->acl_delete('user', $ad['ad_owner'], false, 'u_phpbb_ads_owner');
274
				}
275
276
				$success = $this->manager->update_ad($ad_id, $data);
277
278 View Code Duplication
				if ($success)
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...
279
				{
280
					// Only insert new ad locations to DB when ad exists
281
					$this->manager->delete_ad_locations($ad_id);
282
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
283
284
					// Add u_phpbb_ads_owner permission to new owner
285
					$this->auth_admin->acl_set('user', 0, $data['ad_owner'], array('u_phpbb_ads_owner' => 1));
286
287
					$this->log('EDIT', $data['ad_name']);
288
289
					$this->success('ACP_AD_EDIT_SUCCESS');
290
				}
291
292
				$this->error('ACP_AD_DOES_NOT_EXIST');
293
			}
294
		}
295
		else
296
		{
297
			$data = $ad;
298
299
			// Load ad template locations
300
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
301
		}
302
303
		// Set output vars for display in the template
304
		$this->template->assign_vars(array(
305
			'S_EDIT_AD'          => true,
306
			'EDIT_ID'            => $ad_id,
307
			'U_BACK'             => $this->u_action,
308
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
309
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
310
			'U_FIND_USERNAME'    => append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true'),
311
		));
312
		$this->assign_locations($data);
313
		$this->assign_form_data($data);
314
	}
315
316
	/**
317
	 * Enable an advertisement
318
	 *
319
	 * @return void
320
	 */
321
	public function action_enable()
322
	{
323
		$this->ad_enable(true);
324
	}
325
326
	/**
327
	 * Disable an advertisement
328
	 *
329
	 * @return void
330
	 */
331
	public function action_disable()
332
	{
333
		$this->ad_enable(false);
334
	}
335
336
	/**
337
	 * Delete an advertisement
338
	 *
339
	 * @return void
340
	 */
341
	public function action_delete()
342
	{
343
		$ad_id = $this->request->variable('id', 0);
344
		if ($ad_id)
345
		{
346
			if (confirm_box(true))
347
			{
348
				// Get ad data so that we can log ad name
349
				$ad_data = $this->manager->get_ad($ad_id);
350
351
				// Delete ad and it's template locations
352
				$this->manager->delete_ad_locations($ad_id);
353
				$success = $this->manager->delete_ad($ad_id);
354
355
				// Delete u_phpbb_ads_owner permission if old owner owns no more ads
356 View Code Duplication
				if (count($this->manager->get_ads_by_owner($ad_data['ad_owner'])) == 1)
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...
357
				{
358
					$this->auth_admin->acl_delete('user', $ad_data['ad_owner'], false, 'u_phpbb_ads_owner');
359
				}
360
361
				// Only notify user on error or if not ajax
362
				if (!$success)
363
				{
364
					$this->error('ACP_AD_DELETE_ERRORED');
365
				}
366
				else
367
				{
368
					$this->log('DELETE', $ad_data['ad_name']);
369
370
					if (!$this->request->is_ajax())
371
					{
372
						$this->success('ACP_AD_DELETE_SUCCESS');
373
					}
374
				}
375
			}
376
			else
377
			{
378
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
379
					'id'     => $ad_id,
380
					'i'      => $this->request->variable('i', ''),
381
					'mode'   => $this->request->variable('mode', ''),
382
					'action' => 'delete'
383
				)));
384
			}
385
		}
386
	}
387
388
	/**
389
	 * Display the ads
390
	 *
391
	 * @return void
392
	 */
393
	public function list_ads()
394
	{
395
		foreach ($this->manager->get_all_ads() as $row)
396
		{
397
			$ad_enabled = (int) $row['ad_enabled'];
398
			$ad_end_date = (int) $row['ad_end_date'];
399
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
400
			if ($ad_expired && $ad_enabled)
401
			{
402
				$ad_enabled = 0;
403
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
404
			}
405
406
			$this->template->assign_block_vars('ads', array(
407
				'NAME'               => $row['ad_name'],
408
				'END_DATE'           => $ad_end_date ? $this->user->format_date($ad_end_date, self::DATE_FORMAT) : '',
409
				'VIEWS'              => $row['ad_views'],
410
				'CLICKS'             => $row['ad_clicks'],
411
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
412
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
413
				'S_END_DATE_EXPIRED' => $ad_expired,
414
				'S_ENABLED'          => $ad_enabled,
415
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
416
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
417
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
418
			));
419
		}
420
421
		// Set output vars for display in the template
422
		$this->template->assign_vars(array(
423
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
424
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
425
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
426
		));
427
	}
428
429
	/**
430
	 * Perform general tasks
431
	 *
432
	 * @return void
433
	 */
434
	protected function setup()
435
	{
436
		$this->user->add_lang_ext('phpbb/ads', 'acp');
437
438
		$this->template->assign_var('S_PHPBB_ADS', true);
439
	}
440
441
	/**
442
	 * Enable/disable an advertisement
443
	 *
444
	 * @param    bool $enable Enable or disable the advertisement?
445
	 * @return void
446
	 */
447
	protected function ad_enable($enable)
448
	{
449
		$ad_id = $this->request->variable('id', 0);
450
451
		$success = $this->manager->update_ad($ad_id, array(
452
			'ad_enabled' => (int) $enable,
453
		));
454
455
		// If AJAX was used, show user a result message
456
		if ($this->request->is_ajax())
457
		{
458
			$json_response = new \phpbb\json_response;
459
			$json_response->send(array(
460
				'text'  => $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
461
				'title' => $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
462
			));
463
		}
464
465
		// Otherwise, show traditional infobox
466
		if ($success)
467
		{
468
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
469
		}
470
		else
471
		{
472
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
473
		}
474
	}
475
476
	/**
477
	 * Get admin form data.
478
	 *
479
	 * @param    string $form_name The form name.
480
	 * @return    array    Form data
481
	 */
482
	protected function get_form_data($form_name)
483
	{
484
		$data = array(
485
			'ad_name'         => $this->request->variable('ad_name', '', true),
486
			'ad_note'         => $this->request->variable('ad_note', '', true),
487
			'ad_code'         => $this->request->variable('ad_code', '', true),
488
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
489
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
490
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
491
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
492
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
493
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
494
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
495
		);
496
497
		// Get owner id
498
		$data['ad_owner'] = $this->manager->get_owner_id_from_username($data['ad_owner']);
499
500
		// Validate form key
501
		if (!check_form_key($form_name))
502
		{
503
			$this->errors[] = $this->user->lang('FORM_INVALID');
504
		}
505
506
		// Validate ad name
507
		if ($data['ad_name'] === '')
508
		{
509
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
510
		}
511
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
512
		{
513
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
514
		}
515
516
		// Validate ad end date
517
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
518
		{
519
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
520
521
			if ($data['ad_end_date'] < time())
522
			{
523
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
524
			}
525
		}
526
		else if ($data['ad_end_date'] !== '')
527
		{
528
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
529
		}
530
		else
531
		{
532
			$data['ad_end_date'] = 0;
533
		}
534
535
		// Validate ad priority
536
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
537
		{
538
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
539
		}
540
541
		// Validate ad views limit
542
		if ($data['ad_views_limit'] < 0)
543
		{
544
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
545
		}
546
547
		// Validate ad clicks limit
548
		if ($data['ad_clicks_limit'] < 0)
549
		{
550
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
551
		}
552
553
		// Validate ad owner
554
		if ($data['ad_owner'] === false)
555
		{
556
			$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
557
		}
558
559
		return $data;
560
	}
561
562
	/**
563
	 * Assign form data to the template.
564
	 *
565
	 * @param    array $data The form data.
566
	 * @return void
567
	 */
568
	protected function assign_form_data($data)
569
	{
570
		$this->template->assign_vars(array(
571
			'S_ERROR'   => (bool) count($this->errors),
572
			'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
573
574
			'AD_NAME'         => $data['ad_name'],
575
			'AD_NOTE'         => $data['ad_note'],
576
			'AD_CODE'         => $data['ad_code'],
577
			'AD_ENABLED'      => $data['ad_enabled'],
578
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
579
			'AD_PRIORITY'     => $data['ad_priority'],
580
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
581
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
582
			'AD_OWNER'        => $this->manager->get_owner_username_from_id($data['ad_owner']),
583
		));
584
	}
585
586
	/**
587
	 * Prepare end date for display
588
	 *
589
	 * @param    mixed $end_date End date.
590
	 * @return    string    End date prepared for display.
591
	 */
592
	protected function prepare_end_date($end_date)
593
	{
594
		if (empty($end_date))
595
		{
596
			return '';
597
		}
598
599
		if (is_numeric($end_date))
600
		{
601
			return $this->user->format_date($end_date, self::DATE_FORMAT);
602
		}
603
604
		return (string) $end_date;
605
	}
606
607
	/**
608
	 * Assign template locations data to the template.
609
	 *
610
	 * @param    mixed $data The form data or nothing.
611
	 * @return    void
612
	 */
613
	protected function assign_locations($data = false)
614
	{
615
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
616
		{
617
			$this->template->assign_block_vars('ad_locations', array(
618
				'LOCATION_ID'   => $location_id,
619
				'LOCATION_DESC' => $location_data['desc'],
620
				'LOCATION_NAME' => $location_data['name'],
621
				'S_SELECTED'    => $data ? in_array($location_id, $data['ad_locations']) : false,
622
			));
623
		}
624
	}
625
626
	/**
627
	 * Prepare advertisement preview
628
	 *
629
	 * @param    string $code Ad code to preview
630
	 * @return    void
631
	 */
632
	protected function ad_preview($code)
633
	{
634
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
635
	}
636
637
	/**
638
	 * Print success message.
639
	 *
640
	 * It takes arguments in the form of a language key, followed by language substitution values.
641
	 */
642
	protected function success()
643
	{
644
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
645
	}
646
647
	/**
648
	 * Print error message.
649
	 *
650
	 * It takes arguments in the form of a language key, followed by language substitution values.
651
	 */
652
	protected function error()
653
	{
654
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
655
	}
656
657
	/**
658
	 * Log action
659
	 *
660
	 * @param    string $action  Performed action in uppercase
661
	 * @param    string $ad_name Advertisement name
662
	 * @return    void
663
	 */
664
	protected function log($action, $ad_name)
665
	{
666
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
667
	}
668
669
	/**
670
	 * Get auth_admin class
671
	 *
672
	 * @return \auth_admin
673
	 */
674
	protected function get_auth_admin()
675
	{
676
		if (!class_exists('auth_admin'))
677
		{
678
			include($this->root_path . 'includes/acp/auth.' . $this->php_ext);
679
		}
680
		return new \auth_admin();
681
	}
682
}
683