Completed
Pull Request — master (#52)
by Jakub
06:47 queued 04:31
created

admin_controller::success()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
	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
		$this->template = $template;
87
		$this->user = $user;
88
		$this->request = $request;
89
		$this->manager = $manager;
90
		$this->location_manager = $location_manager;
91
		$this->log = $log;
92
		$this->config_text = $config_text;
93
		$this->config = $config;
94
		$this->files_upload = $files_upload;
95
		$this->filesystem = $filesystem;
96
		$this->root_path = $root_path;
97
		$this->php_ext = $php_ext;
98
		$this->ext_path = $ext_path;
99
	}
100
101
	/**
102
	 * Process user request for manage mode
103
	 *
104
	 * @return void
105
	 */
106
	public function mode_manage()
107
	{
108
		$this->setup();
109
110
		if (!function_exists('user_get_id_name'))
111
		{
112
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
113
		}
114
115
		// Trigger specific action
116
		$action = $this->request->variable('action', '');
117
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
118
		{
119
			$this->{'action_' . $action}();
120
		}
121
122
		// Otherwise default to this
123
		$this->list_ads();
124
	}
125
126
	/**
127
	 * Process user request for settings mode
128
	 *
129
	 * @return void
130
	 */
131
	public function mode_settings()
132
	{
133
		$this->setup();
134
135
		add_form_key('phpbb/ads/settings');
136
		if ($this->request->is_set_post('submit'))
137
		{
138
			// Validate form key
139
			if (!check_form_key('phpbb/ads/settings'))
140
			{
141
				$this->errors[] = $this->user->lang('FORM_INVALID');
142
			}
143
144
			if (empty($this->errors))
145
			{
146
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
147
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
148
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
149
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
150
151
				$this->success('ACP_AD_SETTINGS_SAVED');
152
			}
153
154
			$this->template->assign_vars(array(
155
				'S_ERROR'   => (bool) count($this->errors),
156
				'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
157
			));
158
		}
159
160
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
161
		$groups = $this->manager->load_groups();
162
		foreach ($groups as $group)
163
		{
164
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
165
166
			$this->template->assign_block_vars('groups', array(
167
				'ID'         => $group['group_id'],
168
				'NAME'       => $group_name,
169
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
170
			));
171
		}
172
173
		$this->template->assign_vars(array(
174
			'U_ACTION'          => $this->u_action,
175
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
176
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
177
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
178
		));
179
	}
180
181
	/**
182
	 * Set page url
183
	 *
184
	 * @param string $u_action Custom form action
185
	 * @return void
186
	 */
187
	public function set_page_url($u_action)
188
	{
189
		$this->u_action = $u_action;
190
	}
191
192
	/**
193
	 * Get ACP page title for Ads module
194
	 *
195
	 * @return string    Language string for Ads ACP module
196
	 */
197
	public function get_page_title()
198
	{
199
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
200
	}
201
202
	/**
203
	 * Add an advertisement
204
	 *
205
	 * @return void
206
	 */
207
	public function action_add()
208
	{
209
		$preview = $this->request->is_set_post('preview');
210
		$submit = $this->request->is_set_post('submit');
211
		$upload_banner = $this->request->is_set_post('upload_banner');
212
213
		add_form_key('phpbb/ads/add');
214
		if ($preview || $submit || $upload_banner)
215
		{
216
			$data = $this->get_form_data('phpbb/ads/add');
217
218 View Code Duplication
			if ($preview)
219
			{
220
				$this->ad_preview($data['ad_code']);
221
			}
222
			else if ($upload_banner)
223
			{
224
				$data['ad_code'] = $this->process_banner_upload($data['ad_code']);
225
			}
226
			else if (empty($this->errors))
227
			{
228
				$ad_id = $this->manager->insert_ad($data);
229
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
230
231
				$this->log('ADD', $data['ad_name']);
232
233
				$this->success('ACP_AD_ADD_SUCCESS');
234
			}
235
236
			$this->assign_locations($data);
237
			$this->assign_form_data($data);
238
		}
239
		else
240
		{
241
			$this->assign_locations();
242
		}
243
244
		// Set output vars for display in the template
245
		$this->template->assign_vars(array(
246
			'S_ADD_AD'           => true,
247
			'U_BACK'             => $this->u_action,
248
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
249
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
250
			'U_FIND_USERNAME'    => $this->get_find_username_link(),
251
		));
252
	}
253
254
	/**
255
	 * Edit an advertisement
256
	 *
257
	 * @return void
258
	 */
259
	public function action_edit()
260
	{
261
		$ad_id = $this->request->variable('id', 0);
262
		$preview = $this->request->is_set_post('preview');
263
		$submit = $this->request->is_set_post('submit');
264
		$upload_banner = $this->request->is_set_post('upload_banner');
265
266
		add_form_key('phpbb/ads/edit/' . $ad_id);
267
		if ($preview || $submit || $upload_banner)
268
		{
269
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
270
271
			if ($preview)
272
			{
273
				$this->ad_preview($data['ad_code']);
274
			}
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
			{
277
				$data['ad_code'] = $this->process_banner_upload($data['ad_code']);
278
			}
279
			else if (empty($this->errors))
280
			{
281
				$success = $this->manager->update_ad($ad_id, $data);
282
283
				if ($success)
284
				{
285
					// Only insert new ad locations to DB when ad exists
286
					$this->manager->delete_ad_locations($ad_id);
287
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
288
289
					$this->log('EDIT', $data['ad_name']);
290
291
					$this->success('ACP_AD_EDIT_SUCCESS');
292
				}
293
294
				$this->error('ACP_AD_DOES_NOT_EXIST');
295
			}
296
		}
297
		else
298
		{
299
			$data = $this->manager->get_ad($ad_id);
300
			if (empty($data))
301
			{
302
				$this->error('ACP_AD_DOES_NOT_EXIST');
303
			}
304
305
			// Load ad template locations
306
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
307
		}
308
309
		// Set output vars for display in the template
310
		$this->template->assign_vars(array(
311
			'S_EDIT_AD'          => true,
312
			'EDIT_ID'            => $ad_id,
313
			'U_BACK'             => $this->u_action,
314
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
315
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
316
			'U_FIND_USERNAME'    => $this->get_find_username_link(),
317
		));
318
		$this->assign_locations($data);
319
		$this->assign_form_data($data);
320
	}
321
322
	/**
323
	 * Enable an advertisement
324
	 *
325
	 * @return void
326
	 */
327
	public function action_enable()
328
	{
329
		$this->ad_enable(true);
330
	}
331
332
	/**
333
	 * Disable an advertisement
334
	 *
335
	 * @return void
336
	 */
337
	public function action_disable()
338
	{
339
		$this->ad_enable(false);
340
	}
341
342
	/**
343
	 * Delete an advertisement
344
	 *
345
	 * @return void
346
	 */
347
	public function action_delete()
348
	{
349
		$ad_id = $this->request->variable('id', 0);
350
		if ($ad_id)
351
		{
352
			if (confirm_box(true))
353
			{
354
				// Get ad data so that we can log ad name
355
				$ad_data = $this->manager->get_ad($ad_id);
356
357
				// Delete ad and it's template locations
358
				$this->manager->delete_ad_locations($ad_id);
359
				$success = $this->manager->delete_ad($ad_id);
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('posting'); // Used by process_banner_upload() file errors
437
		$this->user->add_lang_ext('phpbb/ads', 'acp');
438
439
		$this->template->assign_var('S_PHPBB_ADS', true);
440
	}
441
442
	/**
443
	 * Enable/disable an advertisement
444
	 *
445
	 * @param    bool $enable Enable or disable the advertisement?
446
	 * @return void
447
	 */
448
	protected function ad_enable($enable)
449
	{
450
		$ad_id = $this->request->variable('id', 0);
451
452
		$success = $this->manager->update_ad($ad_id, array(
453
			'ad_enabled' => (int) $enable,
454
		));
455
456
		// If AJAX was used, show user a result message
457
		if ($this->request->is_ajax())
458
		{
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
		{
469
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
470
		}
471
		else
472
		{
473
			$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
	protected function process_banner_upload($ad_code)
484
	{
485
		// Set file restrictions
486
		$this->files_upload->reset_vars();
487
		$this->files_upload->set_allowed_extensions(array('gif', 'jpg', 'jpeg', 'png'));
488
489
		// Upload file
490
		$file = $this->files_upload->handle_upload('files.types.form', 'banner');
491
		$file->clean_filename('unique_ext');
492
493
		// First lets create phpbb_ads directory if needed
494
		$dir = $this->root_path . 'images/phpbb_ads';
495
		if (!$this->filesystem->exists($dir))
496
		{
497
			try
498
			{
499
				$this->filesystem->mkdir($this->root_path . 'images/phpbb_ads');
500
			}
501
			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
		}
506
507
		// Move file to proper location
508
		if (!$file->move_file('images/phpbb_ads'))
509
		{
510
			$file->set_error($this->user->lang('CANNOT_CREATE_DIRECTORY'));
511
		}
512
513
		// Problem with uploading
514
		if (count($file->error))
515
		{
516
			$file->remove();
517
			if ($this->request->is_ajax())
518
			{
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
				$this->errors[] = implode('<br />', $file->error);
529
			}
530
		}
531
		else
532
		{
533
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
534
535
			if ($this->request->is_ajax())
536
			{
537
				$json_response = new \phpbb\json_response;
538
				$json_response->send(array(
539
					'success'	=> true,
540
					'text'		=> $banner_html,
541
				));
542
			}
543
544
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
545
		}
546
547
		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
	protected function get_form_data($form_name)
557
	{
558
		$data = array(
559
			'ad_name'         => $this->request->variable('ad_name', '', true),
560
			'ad_note'         => $this->request->variable('ad_note', '', true),
561
			'ad_code'         => $this->request->variable('ad_code', '', true),
562
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
563
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
564
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
565
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
566
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
567
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
568
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
569
		);
570
571
		// Validate form key
572
		if (!check_form_key($form_name))
573
		{
574
			$this->errors[] = $this->user->lang('FORM_INVALID');
575
		}
576
577
		// Validate ad name
578
		if ($data['ad_name'] === '')
579
		{
580
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
581
		}
582
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
583
		{
584
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
585
		}
586
587
		// Validate ad end date
588
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
589
		{
590
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
591
592
			if ($data['ad_end_date'] < time())
593
			{
594
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
595
			}
596
		}
597
		else if ($data['ad_end_date'] !== '')
598
		{
599
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
600
		}
601
		else
602
		{
603
			$data['ad_end_date'] = 0;
604
		}
605
606
		// Validate ad priority
607
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
608
		{
609
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
610
		}
611
612
		// Validate ad views limit
613
		if ($data['ad_views_limit'] < 0)
614
		{
615
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
616
		}
617
618
		// Validate ad clicks limit
619
		if ($data['ad_clicks_limit'] < 0)
620
		{
621
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
622
		}
623
624
		// Validate ad owner. Username in $data['ad_owner'] will be replaced with user_id.
625
		if (!empty($data['ad_owner']))
626
		{
627
			// Function returns false if everything is OK.
628
			if (user_get_id_name($ad_owner_id, $data['ad_owner']))
629
			{
630
				$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
631
			}
632
			else
633
			{
634
				$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
		}
637
		else
638
		{
639
			$data['ad_owner'] = 0;
640
		}
641
642
		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
	protected function assign_form_data($data)
652
	{
653
		$this->template->assign_vars(array(
654
			'S_ERROR'   => (bool) count($this->errors),
655
			'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
656
657
			'AD_NAME'         => $data['ad_name'],
658
			'AD_NOTE'         => $data['ad_note'],
659
			'AD_CODE'         => $data['ad_code'],
660
			'AD_ENABLED'      => $data['ad_enabled'],
661
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
662
			'AD_PRIORITY'     => $data['ad_priority'],
663
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
664
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
665
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
666
		));
667
	}
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
	protected function prepare_end_date($end_date)
676
	{
677
		if (empty($end_date))
678
		{
679
			return '';
680
		}
681
682
		if (is_numeric($end_date))
683
		{
684
			return $this->user->format_date($end_date, self::DATE_FORMAT);
685
		}
686
687
		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
	protected function prepare_ad_owner($ad_owner)
698
	{
699
		// Returns false when no errors occur trying to find the user
700
		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
		{
702
			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
			{
704
				return $ad_owner[0];
705
			}
706
			return $ad_owner_name[(int) $ad_owner[0]];
707
		}
708
		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
	protected function assign_locations($data = false)
717
	{
718
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
719
		{
720
			$this->template->assign_block_vars('ad_locations', array(
721
				'LOCATION_ID'   => $location_id,
722
				'LOCATION_DESC' => $location_data['desc'],
723
				'LOCATION_NAME' => $location_data['name'],
724
				'S_SELECTED'    => $data ? in_array($location_id, $data['ad_locations']) : false,
725
			));
726
		}
727
	}
728
729
	/**
730
	 * Prepare advertisement preview
731
	 *
732
	 * @param    string $code Ad code to preview
733
	 * @return    void
734
	 */
735
	protected function ad_preview($code)
736
	{
737
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
738
	}
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
	protected function success()
746
	{
747
		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
	protected function error()
756
	{
757
		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
	protected function log($action, $ad_name)
768
	{
769
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
770
	}
771
772
	protected function get_find_username_link()
773
	{
774
		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