Completed
Pull Request — master (#52)
by Jakub
06:34
created

admin_controller::error()   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.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.037
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
		$dir = $this->root_path . 'images/phpbb_ads';
495 2
		if (!$this->filesystem->exists($dir))
496 2
		{
497
			try
498
			{
499 2
				$this->filesystem->mkdir($this->root_path . 'images/phpbb_ads');
500
			}
501 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...
502
			{
503
				$file->set_error($this->user->lang($e->getMessage()));
504
			}
505 2
		}
506
507
		// Move file to proper location
508 2
		if (!$file->move_file('images/phpbb_ads'))
509 2
		{
510 2
			$file->set_error($this->user->lang('CANNOT_CREATE_DIRECTORY'));
511 2
		}
512
513
		// Problem with uploading
514 2
		if (count($file->error))
515 2
		{
516 1
			$file->remove();
517 1
			if ($this->request->is_ajax())
518 1
			{
519
				$json_response = new \phpbb\json_response;
520
				$json_response->send(array(
521
					'success'	=> false,
522
					'title'		=> $this->user->lang('INFORMATION'),
523
					'text'		=> implode('<br />', $file->error),
524
				));
525
			}
526
			else
527
			{
528 1
				$this->errors[] = implode('<br />', $file->error);
529
			}
530 1
		}
531
		else
532
		{
533 1
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
534
535 1
			if ($this->request->is_ajax())
536 1
			{
537
				$json_response = new \phpbb\json_response;
538
				$json_response->send(array(
539
					'success'	=> true,
540
					'text'		=> $banner_html,
541
				));
542
			}
543
544 1
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
545
		}
546
547 1
		return $ad_code;
548
	}
549
550
	/**
551
	* Get admin form data.
552
	*
553
	* @param	string	$form_name	The form name.
554
	* @return	array	Form data
555
	*/
556 27
	protected function get_form_data($form_name)
557
	{
558
		$data = array(
559 27
			'ad_name'         => $this->request->variable('ad_name', '', true),
560 27
			'ad_note'         => $this->request->variable('ad_note', '', true),
561 27
			'ad_code'         => $this->request->variable('ad_code', '', true),
562 27
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
563 27
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
564 27
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
565 27
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
566 27
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
567 27
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
568 27
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
569 27
		);
570
571
		// Validate form key
572 27
		if (!check_form_key($form_name))
573 27
		{
574 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
575 2
		}
576
577
		// Validate ad name
578 27
		if ($data['ad_name'] === '')
579 27
		{
580 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
581 2
		}
582 27
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
583 27
		{
584 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
585 2
		}
586
587
		// Validate ad end date
588 27
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
589 27
		{
590 4
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
591
592 4
			if ($data['ad_end_date'] < time())
593 4
			{
594 2
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
595 2
			}
596 4
		}
597 23
		else if ($data['ad_end_date'] !== '')
598 23
		{
599 2
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
600 2
		}
601
		else
602
		{
603 21
			$data['ad_end_date'] = 0;
604
		}
605
606
		// Validate ad priority
607 27
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
608 27
		{
609 6
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
610 6
		}
611
612
		// Validate ad views limit
613 27
		if ($data['ad_views_limit'] < 0)
614 27
		{
615 2
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
616 2
		}
617
618
		// Validate ad clicks limit
619 27
		if ($data['ad_clicks_limit'] < 0)
620 27
		{
621 2
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
622 2
		}
623
624
		// Validate ad owner. Username in $data['ad_owner'] will be replaced with user_id.
625 27
		if (!empty($data['ad_owner']))
626 27
		{
627
			// Function returns false if everything is OK.
628 4
			if (user_get_id_name($ad_owner_id, $data['ad_owner']))
629 4
			{
630 2
				$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
631 2
			}
632
			else
633
			{
634 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...
635
			}
636 4
		}
637
		else
638
		{
639 23
			$data['ad_owner'] = 0;
640
		}
641
642 27
		return $data;
643
	}
644
645
	/**
646
	 * Assign form data to the template.
647
	 *
648
	 * @param    array $data The form data.
649
	 * @return void
650
	 */
651 25
	protected function assign_form_data($data)
652
	{
653 25
		$this->template->assign_vars(array(
654 25
			'S_ERROR'   => (bool) count($this->errors),
655 25
			'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
656
657 25
			'AD_NAME'         => $data['ad_name'],
658 25
			'AD_NOTE'         => $data['ad_note'],
659 25
			'AD_CODE'         => $data['ad_code'],
660 25
			'AD_ENABLED'      => $data['ad_enabled'],
661 25
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
662 25
			'AD_PRIORITY'     => $data['ad_priority'],
663 25
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
664 25
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
665 25
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
666 25
		));
667 25
	}
668
669
	/**
670
	 * Prepare end date for display
671
	 *
672
	 * @param    mixed $end_date End date.
673
	 * @return    string    End date prepared for display.
674
	 */
675 25
	protected function prepare_end_date($end_date)
676
	{
677 25
		if (empty($end_date))
678 25
		{
679 20
			return '';
680
		}
681
682 5
		if (is_numeric($end_date))
683 5
		{
684 3
			return $this->user->format_date($end_date, self::DATE_FORMAT);
685
		}
686
687 2
		return (string) $end_date;
688
	}
689
690
	/**
691
	 * Prepare ad owner for display. Method takes user_id
692
	 * of the ad owner and returns his/her username.
693
	 *
694
	 * @param	int		$ad_owner	User ID
695
	 * @return	string	Username belonging to $ad_owner.
696
	 */
697 25
	protected function prepare_ad_owner($ad_owner)
698
	{
699
		// Returns false when no errors occur trying to find the user
700 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...
701 25
		{
702 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...
703 1
			{
704
				return $ad_owner[0];
705
			}
706 1
			return $ad_owner_name[(int) $ad_owner[0]];
707
		}
708 24
		return '';
709
	}
710
	/**
711
	 * Assign template locations data to the template.
712
	 *
713
	 * @param    mixed $data The form data or nothing.
714
	 * @return    void
715
	 */
716 26
	protected function assign_locations($data = false)
717
	{
718 26
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
719
		{
720 26
			$this->template->assign_block_vars('ad_locations', array(
721 26
				'LOCATION_ID'   => $location_id,
722 26
				'LOCATION_DESC' => $location_data['desc'],
723 26
				'LOCATION_NAME' => $location_data['name'],
724 26
				'S_SELECTED'    => $data ? in_array($location_id, $data['ad_locations']) : false,
725 26
			));
726 26
		}
727 26
	}
728
729
	/**
730
	 * Prepare advertisement preview
731
	 *
732
	 * @param    string $code Ad code to preview
733
	 * @return    void
734
	 */
735 2
	protected function ad_preview($code)
736
	{
737 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
738 2
	}
739
740
	/**
741
	 * Print success message.
742
	 *
743
	 * It takes arguments in the form of a language key, followed by language substitution values.
744
	 */
745 6
	protected function success()
746
	{
747 6
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
748
	}
749
750
	/**
751
	 * Print error message.
752
	 *
753
	 * It takes arguments in the form of a language key, followed by language substitution values.
754
	 */
755 5
	protected function error()
756
	{
757 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
758
	}
759
760
	/**
761
	 * Log action
762
	 *
763
	 * @param    string $action  Performed action in uppercase
764
	 * @param    string $ad_name Advertisement name
765
	 * @return    void
766
	 */
767 3
	protected function log($action, $ad_name)
768
	{
769 3
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
770 3
	}
771
772 26
	protected function get_find_username_link()
773
	{
774 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');
775
	}
776
}
777