Completed
Push — master ( 580858...3ade41 )
by Matt
12s queued 10s
created

helper::get_date()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use phpbb\ads\ext;
14
15
/**
16
 * Helper
17
 */
18
class helper
19
{
20
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	protected $user;
22
23
	/** @var \phpbb\user_loader */
0 ignored issues
show
Bug introduced by
The type phpbb\user_loader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $user_loader;
25
26
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $language;
28
29
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
	protected $template;
31
32
	/** @var \phpbb\log\log */
0 ignored issues
show
Bug introduced by
The type phpbb\log\log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
	protected $log;
34
35
	/** @var \phpbb\ads\ad\manager */
36
	protected $manager;
37
38
	/** @var \phpbb\ads\location\manager */
39
	protected $location_manager;
40
41
	/** @var \phpbb\group\helper */
0 ignored issues
show
Bug introduced by
The type phpbb\group\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
	protected $group_helper;
43
44
	/** @var string root_path */
45
	protected $root_path;
46
47
	/** @var string php_ext */
48
	protected $php_ext;
49
50
	/**
51
	 * Constructor
52
	 *
53
	 * @param \phpbb\user                 $user             User object
54
	 * @param \phpbb\user_loader          $user_loader      User loader object
55
	 * @param \phpbb\language\language    $language         Language object
56
	 * @param \phpbb\template\template    $template         Template object
57
	 * @param \phpbb\log\log              $log              The phpBB log system
58
	 * @param \phpbb\ads\ad\manager 	  $manager 			Ad manager object
59
	 * @param \phpbb\ads\location\manager $location_manager Template location manager object
60
	 * @param \phpbb\group\helper         $group_helper     Group helper object
61
	 * @param string                      $root_path        phpBB root path
62 17
	 * @param string                      $php_ext          PHP extension
63
	 */
64 17
	public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\group\helper $group_helper, $root_path, $php_ext)
65 17
	{
66 17
		$this->user = $user;
67 17
		$this->user_loader = $user_loader;
68 17
		$this->language = $language;
69 17
		$this->template = $template;
70 17
		$this->log = $log;
71 17
		$this->location_manager = $location_manager;
72 17
		$this->manager = $manager;
73 17
		$this->group_helper = $group_helper;
74 17
		$this->root_path = $root_path;
75
		$this->php_ext = $php_ext;
76
	}
77
78
	/**
79
	 * Assign ad data for ACP form template.
80
	 *
81
	 * @param	array	$data	Ad data
82 5
	 * @param	array	$errors	Validation errors
83
	 */
84 5
	public function assign_data($data, $errors)
85 5
	{
86
		$this->assign_locations($data['ad_locations']);
87 5
		$this->assign_groups(($data['ad_id'] ?? 0), ($data['ad_groups'] ?? array()));
88 5
89 5
		$errors = array_map(array($this->language, 'lang'), $errors);
90 5
		$this->template->assign_vars(array(
91
			'S_ERROR'   => (bool) count($errors),
92 5
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
93 5
94 5
			'AD_NAME'         	=> $data['ad_name'],
95 5
			'AD_NOTE'         	=> $data['ad_note'],
96 5
			'AD_CODE'         	=> $data['ad_code'],
97 5
			'AD_ENABLED'      	=> $data['ad_enabled'],
98 5
			'AD_START_DATE'     => $data['ad_start_date'],
99 5
			'AD_END_DATE'     	=> $data['ad_end_date'],
100 5
			'AD_PRIORITY'     	=> $data['ad_priority'],
101 5
			'AD_CONTENT_ONLY'	=> $data['ad_content_only'],
102 5
			'AD_VIEWS_LIMIT'  	=> $data['ad_views_limit'],
103 5
			'AD_CLICKS_LIMIT' 	=> $data['ad_clicks_limit'],
104 5
			'AD_OWNER'        	=> $this->get_username($data['ad_owner']),
105 5
			'AD_CENTERING'      => $data['ad_centering'],
106
		));
107
	}
108
109
	/**
110
	 * Assign template locations data to the template.
111
	 *
112
	 * @param array $ad_locations The form data or nothing.
113 7
	 * @return void
114
	 */
115 7
	public function assign_locations($ad_locations = array())
116
	{
117 2
		foreach ($this->location_manager->get_all_locations() as $location_category_id => $location_category)
118 2
		{
119 2
			$this->template->assign_block_vars('ad_locations', array(
120
				'CATEGORY_NAME' => $this->language->lang($location_category_id),
121 2
			));
122
123 2
			foreach ($location_category as $location_id => $location_data)
124 2
			{
125 2
				$this->template->assign_block_vars('ad_locations', array(
126 2
					'LOCATION_ID'   => $location_id,
127 2
					'LOCATION_DESC' => $location_data['desc'],
128 2
					'LOCATION_NAME' => $location_data['name'],
129 2
					'S_SELECTED'    => in_array($location_id, $ad_locations),
130 7
				));
131 7
			}
132
		}
133
	}
134
135
	/**
136
	 * Assign groups data to the template.
137
	 *
138
	 * @param int   $ad_id Advertisement ID
139 6
	 * @param array $selected Array of selected groups from the form
140
	 * @return void
141 6
	 */
142 6
	public function assign_groups($ad_id = 0, $selected = array())
143
	{
144 1
		$groups = $this->manager->load_groups($ad_id);
145 1
146 1
		if (!$ad_id && count($selected))
147 1
		{
148 1
			array_walk($groups, function (&$group) use ($selected) {
149 6
				$group['group_selected'] = in_array($group['group_id'], $selected);
150 6
			});
151
		}
152
153
		foreach ($groups as $group)
154
		{
155
			$this->template->assign_block_vars('groups', array(
156
				'ID'         => $group['group_id'],
157
				'NAME'       => $this->group_helper->get_name($group['group_name']),
158
				'S_SELECTED' => (bool) $group['group_selected'],
159 1
			));
160
		}
161 1
	}
162 1
163
	/**
164
	 * Log action
165
	 *
166
	 * @param	string	$action		Performed action in uppercase
167
	 * @param	string	$ad_name	Advertisement name
168
	 * @return	void
169 1
	 */
170
	public function log($action, $ad_name)
171 1
	{
172
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
173
	}
174
175
	/**
176
	 * Get "Find username" URL to easily look for ad owner.
177
	 *
178
	 * @return	string	Find username URL
179
	 */
180 7
	public function get_find_username_link()
181
	{
182 7
		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');
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
		return /** @scrutinizer ignore-call */ append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true');
Loading history...
183 7
	}
184 1
185
	/**
186
	 * Get a date in the current user's timezone and the correct format.
187 6
	 *
188 6
	 * @param string $time
189 1
	 * @return string Formatted date time
190
	 */
191
	public function get_date($time = 'now')
192 5
	{
193 5
		return $this->user->create_datetime($time)->format(ext::DATE_FORMAT);
194 1
	}
195
196
	/**
197 4
	 * Is an ad expired?
198
	 *
199
	 * @param	array	$row	Advertisement data
200
	 * @return	bool	True if expired, false otherwise
201
	 */
202
	public function is_expired($row)
203
	{
204
		$latest_timezone_end = strtotime('now -12 hours'); // UTC-12 is the last timezone
205
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < $latest_timezone_end)
206
		{
207 5
			return true;
208
		}
209 5
210 5
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
211 2
		{
212
			return true;
213
		}
214 3
215 3
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
216
		{
217
			return true;
218
		}
219
220
		return false;
221
	}
222
223
	/**
224
	 * Prepare ad owner for display. Method takes user_id
225
	 * of the ad owner and returns username.
226
	 *
227
	 * @param	int		$user_id	User ID
228
	 * @return	string	Username belonging to $user_id.
229
	 */
230
	protected function get_username($user_id)
231
	{
232
		if (!$user_id)
233
		{
234
			return '';
235
		}
236
237
		$this->user_loader->load_users(array($user_id));
238
		return $this->user_loader->get_username($user_id, 'username');
239
	}
240
}
241