Completed
Push — master ( 619fd0...e5a000 )
by Matt
09:21 queued 02:41
created

admin_controller   D

Complexity

Total Complexity 87

Size/Duplication

Total Lines 760
Duplicated Lines 4.87 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 87
c 1
b 0
f 0
lcom 1
cbo 2
dl 37
loc 760
ccs 348
cts 387
cp 0.8992
rs 4.4444

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A mode_manage() 0 19 3
C mode_settings() 0 49 7
A set_page_url() 0 4 1
A get_page_title() 0 4 1
C action_add() 17 46 7
C action_edit() 20 62 9
A action_enable() 0 4 1
A action_disable() 0 4 1
B action_delete() 0 40 5
C list_ads() 0 35 7
A setup() 0 7 1
B ad_enable() 0 28 6
C process_banner_upload() 0 65 8
F get_form_data() 0 88 13
A assign_form_data() 0 17 2
A prepare_end_date() 0 14 3
A prepare_ad_owner() 0 13 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_find_username_link() 0 4 1

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

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
501
			{
502
				$file->set_error($this->user->lang($e->getMessage()));
503
			}
504 2
		}
505
506
		// Move file to proper location
507 2
		if (!$file->move_file('images/phpbb_ads'))
508 2
		{
509 2
			$file->set_error($this->user->lang('FILE_MOVE_UNSUCCESSFUL'));
510 2
		}
511
512
		// Problem with uploading
513 2
		if (count($file->error))
514 2
		{
515 1
			$file->remove();
516 1
			if ($this->request->is_ajax())
517 1
			{
518
				$json_response = new \phpbb\json_response;
519
				$json_response->send(array(
520
					'success'	=> false,
521
					'title'		=> $this->user->lang('INFORMATION'),
522
					'text'		=> implode('<br />', $file->error),
523
				));
524
			}
525
			else
526
			{
527 1
				$this->errors[] = implode('<br />', $file->error);
528
			}
529 1
		}
530
		else
531
		{
532 1
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
533
534 1
			if ($this->request->is_ajax())
535 1
			{
536
				$json_response = new \phpbb\json_response;
537
				$json_response->send(array(
538
					'success'	=> true,
539
					'text'		=> $banner_html,
540
				));
541
			}
542
543 1
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
544
		}
545
546 1
		return $ad_code;
547
	}
548
549
	/**
550
	* Get admin form data.
551
	*
552
	* @param	string	$form_name	The form name.
553
	* @return	array	Form data
554
	*/
555 27
	protected function get_form_data($form_name)
556
	{
557
		$data = array(
558 27
			'ad_name'         => $this->request->variable('ad_name', '', true),
559 27
			'ad_note'         => $this->request->variable('ad_note', '', true),
560 27
			'ad_code'         => $this->request->variable('ad_code', '', true),
561 27
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
562 27
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
563 27
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
564 27
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
565 27
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
566 27
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
567 27
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
568 27
		);
569
570
		// Validate form key
571 27
		if (!check_form_key($form_name))
572 27
		{
573 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
574 2
		}
575
576
		// Validate ad name
577 27
		if ($data['ad_name'] === '')
578 27
		{
579 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
580 2
		}
581 27
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
582 27
		{
583 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
584 2
		}
585
586
		// Validate ad end date
587 27
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
588 27
		{
589 4
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
590
591 4
			if ($data['ad_end_date'] < time())
592 4
			{
593 2
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
594 2
			}
595 4
		}
596 23
		else if ($data['ad_end_date'] !== '')
597 23
		{
598 2
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
599 2
		}
600
		else
601
		{
602 21
			$data['ad_end_date'] = 0;
603
		}
604
605
		// Validate ad priority
606 27
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
607 27
		{
608 6
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
609 6
		}
610
611
		// Validate ad views limit
612 27
		if ($data['ad_views_limit'] < 0)
613 27
		{
614 2
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
615 2
		}
616
617
		// Validate ad clicks limit
618 27
		if ($data['ad_clicks_limit'] < 0)
619 27
		{
620 2
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
621 2
		}
622
623
		// Validate ad owner. Username in $data['ad_owner'] will be replaced with user_id.
624 27
		if (!empty($data['ad_owner']))
625 27
		{
626
			// Function returns false if everything is OK.
627 4
			if (user_get_id_name($ad_owner_id, $data['ad_owner']))
628 4
			{
629 2
				$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
630 2
			}
631
			else
632
			{
633 2
				$data['ad_owner'] = $ad_owner_id[0];
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
634
			}
635 4
		}
636
		else
637
		{
638 23
			$data['ad_owner'] = 0;
639
		}
640
641 27
		return $data;
642
	}
643
644
	/**
645
	 * Assign form data to the template.
646
	 *
647
	 * @param    array $data The form data.
648
	 * @return void
649
	 */
650 25
	protected function assign_form_data($data)
651
	{
652 25
		$this->template->assign_vars(array(
653 25
			'S_ERROR'   => (bool) count($this->errors),
654 25
			'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
655
656 25
			'AD_NAME'         => $data['ad_name'],
657 25
			'AD_NOTE'         => $data['ad_note'],
658 25
			'AD_CODE'         => $data['ad_code'],
659 25
			'AD_ENABLED'      => $data['ad_enabled'],
660 25
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
661 25
			'AD_PRIORITY'     => $data['ad_priority'],
662 25
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
663 25
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
664 25
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
665 25
		));
666 25
	}
667
668
	/**
669
	 * Prepare end date for display
670
	 *
671
	 * @param    mixed $end_date End date.
672
	 * @return    string    End date prepared for display.
673
	 */
674 25
	protected function prepare_end_date($end_date)
675
	{
676 25
		if (empty($end_date))
677 25
		{
678 20
			return '';
679
		}
680
681 5
		if (is_numeric($end_date))
682 5
		{
683 3
			return $this->user->format_date($end_date, self::DATE_FORMAT);
684
		}
685
686 2
		return (string) $end_date;
687
	}
688
689
	/**
690
	 * Prepare ad owner for display. Method takes user_id
691
	 * of the ad owner and returns his/her username.
692
	 *
693
	 * @param	int		$ad_owner	User ID
694
	 * @return	string	Username belonging to $ad_owner.
695
	 */
696 25
	protected function prepare_ad_owner($ad_owner)
697
	{
698
		// Returns false when no errors occur trying to find the user
699 25
		if (false === user_get_id_name($ad_owner, $ad_owner_name))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_name does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
700 25
		{
701 1
			if (empty($ad_owner_name))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_name does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
702 1
			{
703
				return $ad_owner[0];
704
			}
705 1
			return $ad_owner_name[(int) $ad_owner[0]];
706
		}
707 24
		return '';
708
	}
709
	/**
710
	 * Assign template locations data to the template.
711
	 *
712
	 * @param    mixed $data The form data or nothing.
713
	 * @return    void
714
	 */
715 26
	protected function assign_locations($data = false)
716
	{
717 26
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
718
		{
719 26
			$this->template->assign_block_vars('ad_locations', array(
720 26
				'LOCATION_ID'   => $location_id,
721 26
				'LOCATION_DESC' => $location_data['desc'],
722 26
				'LOCATION_NAME' => $location_data['name'],
723 26
				'S_SELECTED'    => $data ? in_array($location_id, $data['ad_locations']) : false,
724 26
			));
725 26
		}
726 26
	}
727
728
	/**
729
	 * Prepare advertisement preview
730
	 *
731
	 * @param    string $code Ad code to preview
732
	 * @return    void
733
	 */
734 2
	protected function ad_preview($code)
735
	{
736 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
737 2
	}
738
739
	/**
740
	 * Print success message.
741
	 *
742
	 * It takes arguments in the form of a language key, followed by language substitution values.
743
	 */
744 6
	protected function success()
745
	{
746 6
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
747
	}
748
749
	/**
750
	 * Print error message.
751
	 *
752
	 * It takes arguments in the form of a language key, followed by language substitution values.
753
	 */
754 5
	protected function error()
755
	{
756 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
757
	}
758
759
	/**
760
	 * Log action
761
	 *
762
	 * @param    string $action  Performed action in uppercase
763
	 * @param    string $ad_name Advertisement name
764
	 * @return    void
765
	 */
766 3
	protected function log($action, $ad_name)
767
	{
768 3
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
769 3
	}
770
771 26
	protected function get_find_username_link()
772
	{
773 26
		return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true');
774
	}
775
}
776