Issues (97)

controller/helper.php (7 issues)

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

180
		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...
181
	}
182 7
183 7
	/**
184 1
	 * Is an ad expired?
185
	 *
186
	 * @param	array	$row	Advertisement data
187 6
	 * @return	bool	True if expired, false otherwise
188 6
	 */
189 1
	public function is_expired($row)
190
	{
191
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time())
192 5
		{
193 5
			return true;
194 1
		}
195
196
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
197 4
		{
198
			return true;
199
		}
200
201
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
202
		{
203
			return true;
204
		}
205
206
		return false;
207 5
	}
208
209 5
	/**
210 5
	 * Prepare ad owner for display. Method takes user_id
211 2
	 * of the ad owner and returns username.
212
	 *
213
	 * @param	int		$user_id	User ID
214 3
	 * @return	string	Username belonging to $user_id.
215 3
	 */
216
	protected function get_username($user_id)
217
	{
218
		if (!$user_id)
219
		{
220
			return '';
221
		}
222
223
		$this->user_loader->load_users(array($user_id));
224
		return $this->user_loader->get_username($user_id, 'username');
225
	}
226
}
227