Completed
Pull Request — master (#54)
by Jakub
08:52
created

admin_helper::prepare_end_date()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
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 helper
15
 */
16
class admin_helper
17
{
18
	/** @var \phpbb\user */
19
	protected $user;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\log\log */
25
	protected $log;
26
27
	/** @var \phpbb\ads\location\manager */
28
	protected $location_manager;
29
30
	/** @var string root_path */
31
	protected $root_path;
32
33
	/** @var string php_ext */
34
	protected $php_ext;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\user						$user				User object
40
	 * @param \phpbb\template\template			$template			Template object
41
	 * @param \phpbb\log\log					$log				The phpBB log system
42
	 * @param \phpbb\ads\location\manager		$location_manager	Template location manager object
43
	 * @param string							$root_path			phpBB root path
44
	 * @param string							$php_ext			PHP extension
45
	 */
46
	public function __construct(\phpbb\user $user, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext)
47
	{
48
		$this->user = $user;
49
		$this->template = $template;
50
		$this->log = $log;
51
		$this->location_manager = $location_manager;
52
		$this->root_path = $root_path;
53
		$this->php_ext = $php_ext;
54
	}
55
56
	/**
57
	 * Assign template locations data to the template.
58
	 *
59
	 * @param	mixed	$ad_locations	The form data or nothing.
60
	 * @return	void
61
	 */
62
	public function assign_locations($ad_locations = false)
63
	{
64
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
65
		{
66
			$this->template->assign_block_vars('ad_locations', array(
67
				'LOCATION_ID'   => $location_id,
68
				'LOCATION_DESC' => $location_data['desc'],
69
				'LOCATION_NAME' => $location_data['name'],
70
				'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
71
			));
72
		}
73
	}
74
75
	/**
76
	 * Assign form data to the template.
77
	 *
78
	 * @param	array	$data	The form data.
79
	 * @return	void
80
	 */
81
	public function assign_form_data($data)
82
	{
83
		$this->template->assign_vars(array(
84
			'AD_NAME'         => $data['ad_name'],
85
			'AD_NOTE'         => $data['ad_note'],
86
			'AD_CODE'         => $data['ad_code'],
87
			'AD_ENABLED'      => $data['ad_enabled'],
88
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
89
			'AD_PRIORITY'     => $data['ad_priority'],
90
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
91
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
92
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
93
		));
94
	}
95
96
	public function assign_errors(array $errors)
97
	{
98
		$this->template->assign_vars(array(
99
			'S_ERROR'   => (bool) count($errors),
100
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
101
		));
102
	}
103
104
	/**
105
	 * Log action
106
	 *
107
	 * @param	string	$action		Performed action in uppercase
108
	 * @param	string	$ad_name	Advertisement name
109
	 * @return	void
110
	 */
111
	public function log($action, $ad_name)
112
	{
113
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
114
	}
115
116
	public function get_find_username_link()
117
	{
118
		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');
119
	}
120
121
122
	/**
123
	 * Prepare end date for display
124
	 *
125
	 * @param	mixed	$end_date	End date.
126
	 * @return	string	End date prepared for display.
127
	 */
128
	protected function prepare_end_date($end_date)
129
	{
130
		if (empty($end_date))
131
		{
132
			return '';
133
		}
134
135
		if (is_numeric($end_date))
136
		{
137
			return $this->user->format_date($end_date, self::DATE_FORMAT);
138
		}
139
140
		return (string) $end_date;
141
	}
142
143
	/**
144
	 * Prepare ad owner for display. Method takes user_id
145
	 * of the ad owner and returns his/her username.
146
	 *
147
	 * @param	int		$ad_owner	User ID
148
	 * @return	string	Username belonging to $ad_owner.
149
	 */
150
	protected function prepare_ad_owner($ad_owner)
151
	{
152
		// Returns false when no errors occur trying to find the user
153
		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...
154
		{
155
			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...
156
			{
157
				return $ad_owner[0];
158
			}
159
			return $ad_owner_name[(int) $ad_owner[0]];
160
		}
161
		return '';
162
	}
163
}
164