Completed
Pull Request — master (#53)
by Matt
06:16
created

admin_controller::mode_manage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

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