Completed
Pull Request — master (#52)
by Jakub
32:24
created

admin_controller::action_add()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 46
Code Lines 26

Duplication

Lines 17
Ratio 36.96 %

Code Coverage

Tests 26
CRAP Score 7.0178

Importance

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