Completed
Pull Request — master (#48)
by Jakub
08:27
created

manager::get_owner_username_from_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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\ad;
12
13
class manager
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var \phpbb\config\config */
19
	protected $config;
20
21
	/** @var string */
22
	protected $ads_table;
23
24
	/** @var string */
25
	protected $ad_locations_table;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param    \phpbb\db\driver\driver_interface $db                 DB driver interface
31
	 * @param    \phpbb\config\config              $config             Config object
32
	 * @param    string                            $ads_table          Ads table
33
	 * @param    string                            $ad_locations_table Ad locations table
34
	 */
35 39
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table)
36
	{
37 39
		$this->db = $db;
38 39
		$this->config = $config;
39 39
		$this->ads_table = $ads_table;
40 39
		$this->ad_locations_table = $ad_locations_table;
41 39
	}
42
43
	/**
44
	* Get specific ad
45
	*
46
	* @param	int		$ad_id	Advertisement ID
47
	* @return	mixed	Array with advertisement data, false if ad doesn't exist
48
	*/
49 13 View Code Duplication
	public function get_ad($ad_id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
50
	{
51
		$sql = 'SELECT *
52 13
			FROM ' . $this->ads_table . '
53 13
			WHERE ad_id = ' . (int) $ad_id;
54 13
		$result = $this->db->sql_query($sql);
55 13
		$data = $this->db->sql_fetchrow($result);
56 13
		$this->db->sql_freeresult($result);
57
58 13
		return $data;
59
	}
60
61
	/**
62
	* Get one ad per every location
63
	*
64
	* @param	array	$ad_locations	List of ad locations to fetch ads for
65
	* @return	array	List of ad codes for each location
66
	*/
67 6
	public function get_ads($ad_locations)
68
	{
69 6
		$sql_where_views = $this->config['phpbb_ads_enable_views'] ? 'AND (a.ad_views_limit = 0 OR a.ad_views_limit > a.ad_views)' : '';
70 6
		$sql_where_clicks = $this->config['phpbb_ads_enable_clicks'] ? 'AND (a.ad_clicks_limit = 0 OR a.ad_clicks_limit > a.ad_clicks)' : '';
71
72
		$sql = 'SELECT location_id, ad_id, ad_code
73
			FROM (
74
				SELECT al.location_id, a.ad_id, a.ad_code
75 6
				FROM ' . $this->ad_locations_table . ' al
76 6
				LEFT JOIN ' . $this->ads_table . " a
77
					ON (al.ad_id = a.ad_id)
78
				WHERE a.ad_enabled = 1
79
					AND (a.ad_end_date = 0
80 6
						OR a.ad_end_date > " . time() . ")
81
					$sql_where_views
82 6
					$sql_where_clicks
83 6
					AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
84 6
				ORDER BY (' . $this->sql_random() . ' * a.ad_priority) DESC
85
			) z
86 6
			ORDER BY z.location_id';
87 6
		$result = $this->db->sql_query($sql);
88 6
		$data = $this->db->sql_fetchrowset($result);
89 6
		$this->db->sql_freeresult($result);
90
91 6
		$current_location_id = '';
92 6
		$data = array_filter($data, function($row) use (&$current_location_id) {
93 4
			$return = $current_location_id != $row['location_id'];
94 4
			$current_location_id = $row['location_id'];
95 4
			return $return;
96 6
		});
97
98 6
		return $data;
99
	}
100
101
	/**
102
	 * Get all advertisements. You can specify ad owner and only
103
	 * his ads will be returned.
104
	 *
105
	 * @param	int	$ad_owner	Ad owner
106
	 * @return	array	List of all ads
107
	 */
108 2
	public function get_all_ads($ad_owner = 0)
109
	{
110 2
		$sql_where = $ad_owner ? 'WHERE ad_owner = ' . (int) $ad_owner : '';
111
112
		$sql = 'SELECT ad_id, ad_name, ad_enabled, ad_end_date, ad_views, ad_clicks, ad_views_limit, ad_clicks_limit
113 2
			FROM ' . $this->ads_table . '
114 2
			' . $sql_where;
115 2
		$result = $this->db->sql_query($sql);
116 2
		$data = $this->db->sql_fetchrowset($result);
117 2
		$this->db->sql_freeresult($result);
118
119 2
		return $data;
120
	}
121
122
	/**
123
	* Increment views for specified ads
124
	*
125
	* Note, that views are incremented only by one even when
126
	* an ad is displayed multiple times on the same page.
127
	*
128
	* @param	array	$ad_ids	IDs of ads to increment views
129
	* @return	void
130
	*/
131 2
	public function increment_ads_views($ad_ids)
132
	{
133 2
		if (!empty($ad_ids))
134 2
		{
135 2
			$sql = 'UPDATE ' . $this->ads_table . '
136
				SET ad_views = ad_views + 1
137 2
				WHERE ' . $this->db->sql_in_set('ad_id', $ad_ids);
138 2
			$this->db->sql_query($sql);
139 2
		}
140 2
	}
141
142
	/**
143
	* Increment clicks for specified ad
144
	*
145
	* @param	int	$ad_id	ID of an ad to increment clicks
146
	* @return	void
147
	*/
148 2
	public function increment_ad_clicks($ad_id)
149
	{
150 2
		$sql = 'UPDATE ' . $this->ads_table . '
151
			SET ad_clicks = ad_clicks + 1
152 2
			WHERE ad_id = ' . (int) $ad_id;
153 2
		$this->db->sql_query($sql);
154 2
	}
155
156
	/**
157
	* Insert new advertisement to the database
158
	*
159
	* @param	array	$data	New ad data
160
	* @return	int		New advertisement ID
161
	*/
162 2 View Code Duplication
	public function insert_ad($data)
163
	{
164 2
		$data = $this->intersect_ad_data($data);
165
166 2
		$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
167 2
		$this->db->sql_query($sql);
168
169 2
		return $this->db->sql_nextid();
170
	}
171
172
	/**
173
	* Update advertisement
174
	*
175
	* @param	int		$ad_id	Advertisement ID
176
	* @param	array	$data	List of data to update in the database
177
	* @return	int		Number of affected rows. Can be used to determine if any ad has been updated.
178
	*/
179 4 View Code Duplication
	public function update_ad($ad_id, $data)
180
	{
181 4
		$data = $this->intersect_ad_data($data);
182
183 4
		$sql = 'UPDATE ' . $this->ads_table . '
184 4
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
185 4
			WHERE ad_id = ' . (int) $ad_id;
186 4
		$this->db->sql_query($sql);
187
188 4
		return $this->db->sql_affectedrows();
189
	}
190
191
	/**
192
	* Delete advertisement
193
	*
194
	* @param	int		$ad_id	Advertisement ID
195
	* @return	int		Number of affected rows. Can be used to determine if any ad has been deleted.
196
	*/
197 1
	public function delete_ad($ad_id)
198
	{
199 1
		$sql = 'DELETE FROM ' . $this->ads_table . '
200 1
			WHERE ad_id = ' . (int) $ad_id;
201 1
		$this->db->sql_query($sql);
202
203 1
		return $this->db->sql_affectedrows();
204
	}
205
206
	/**
207
	* Get all locations for specified advertisement
208
	*
209
	* @param	int		$ad_id	Advertisement ID
210
	* @return	array	List of template locations for specified ad
211
	*/
212 7 View Code Duplication
	public function get_ad_locations($ad_id)
213
	{
214 7
		$ad_locations = array();
215
216
		$sql = 'SELECT location_id
217 7
			FROM ' . $this->ad_locations_table . '
218 7
			WHERE ad_id = ' . (int) $ad_id;
219 7
		$result = $this->db->sql_query($sql);
220 7
		while ($row = $this->db->sql_fetchrow($result))
221
		{
222 5
			$ad_locations[] = $row['location_id'];
223 5
		}
224 7
		$this->db->sql_freeresult($result);
225
226 7
		return $ad_locations;
227
	}
228
229
	/**
230
	* Insert advertisement locations
231
	*
232
	* @param	int		$ad_id			Advertisement ID
233
	* @param	array	$ad_locations	List of template locations for this ad
234
	* @return	void
235
	*/
236 2
	public function insert_ad_locations($ad_id, $ad_locations)
237
	{
238 2
		$sql_ary = array();
239 2
		foreach ($ad_locations as $ad_location)
240
		{
241 2
			$sql_ary[] = array(
242 2
				'ad_id'			=> $ad_id,
243 2
				'location_id'	=> $ad_location,
244
			);
245 2
		}
246 2
		$this->db->sql_multi_insert($this->ad_locations_table, $sql_ary);
247 2
	}
248
249
	/**
250
	* Delete advertisement locations
251
	*
252
	* @param	int		$ad_id	Advertisement ID
253
	* @return	void
254
	*/
255 3
	public function delete_ad_locations($ad_id)
256
	{
257 3
		$sql = 'DELETE FROM ' . $this->ad_locations_table . '
258 3
			WHERE ad_id = ' . (int) $ad_id;
259 3
		$this->db->sql_query($sql);
260 3
	}
261
262
	/**
263
	* Load memberships of the user
264
	*
265
	* @param	int		$user_id	User ID to load memberships
266
	* @return	array	List of group IDs user is member of
267
	*/
268 7 View Code Duplication
	public function load_memberships($user_id)
269
	{
270 7
		$memberships = array();
271
		$sql = 'SELECT group_id
272 7
			FROM ' . USER_GROUP_TABLE . '
273 7
			WHERE user_id = ' . (int) $user_id . '
274 7
			AND user_pending = 0';
275 7
		$result = $this->db->sql_query($sql, 3600);
276 7
		while ($row = $this->db->sql_fetchrow($result))
277
		{
278 7
			$memberships[] = $row['group_id'];
279 7
		}
280 7
		$this->db->sql_freeresult($result);
281 7
		return $memberships;
282
	}
283
284
	/**
285
	* Load all board groups
286
	*
287
	* @return	array	List of groups
288
	*/
289 1 View Code Duplication
	public function load_groups()
290
	{
291
		$sql = 'SELECT group_id, group_name, group_type
292 1
			FROM ' . GROUPS_TABLE . '
293 1
			ORDER BY group_name ASC';
294 1
		$result = $this->db->sql_query($sql);
295 1
		$groups = $this->db->sql_fetchrowset($result);
296 1
		$this->db->sql_freeresult($result);
297
298 1
		return $groups;
299
	}
300
301
	/**
302
	 * Get user_id from username
303
	 *
304
	 * @param	string	$username	Username
305
	 * @return	mixed	User id matching username
306
	 */
307 View Code Duplication
	public function get_owner_id_from_username($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
308
	{
309
		$sql = 'SELECT user_id
310
			FROM ' . USERS_TABLE . "
311
			WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'";
312
		$result = $this->db->sql_query($sql);
313
		$user_id = $this->db->sql_fetchfield('user_id');
314
		$this->db->sql_freeresult($result);
315
316
		return $user_id;
317
	}
318
319
	/**
320
	 * Get user_id from username
321
	 *
322
	 * @param	int		$user_id	User ID
323
	 * @return	mixed	Username matching user_id
324
	 */
325 View Code Duplication
	public function get_owner_username_from_id($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
326
	{
327
		$sql = 'SELECT username
328
			FROM ' . USERS_TABLE . '
329
			WHERE user_id = ' . (int) $user_id;
330
		$result = $this->db->sql_query($sql);
331
		$username = $this->db->sql_fetchfield('username');
332
		$this->db->sql_freeresult($result);
333
334
		return $username;
335
	}
336
337
	/**
338
	* Make sure only necessary data make their way to SQL query
339
	*
340
	* @param	array	$data	List of data to query the database
341
	* @return	array	Cleaned data that contain only valid keys
342
	*/
343 6
	protected function intersect_ad_data($data)
344
	{
345 6
		return array_intersect_key($data, array(
346 6
			'ad_name'			=> '',
347 6
			'ad_note'			=> '',
348 6
			'ad_code'			=> '',
349 6
			'ad_enabled'		=> '',
350 6
			'ad_end_date'		=> '',
351 6
			'ad_priority'		=> '',
352 6
			'ad_views_limit'	=> '',
353 6
			'ad_clicks_limit'	=> '',
354 6
			'ad_owner'		=> '',
355 6
		));
356
	}
357
358
	/**
359
	* Get the random statement for this database layer
360
	* Random function should generate a float value between 0 and 1
361
	*
362
	* @return	string	Random statement for current database layer
363
	*/
364 6
	protected function sql_random()
365
	{
366 6
		switch ($this->db->get_sql_layer())
367
		{
368 6
			case 'oracle':
369
				return 'VALUE()';
370
371 6
			case 'postgres':
372
				return 'RANDOM()';
373
374
			// https://stackoverflow.com/a/35369410/2908600
375 6
			case 'sqlite':
376 6
			case 'sqlite3':
377
				return '(0.5 - RANDOM() / CAST(-9223372036854775808 AS REAL) / 2)';
378
379
			/* All other cases should use the default
1 ignored issue
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
380
			case 'mssql':
381
			case 'mssql_odbc':
382
			case 'mssqlnative':
383
			case 'mysql':
384
			case 'mysqli':*/
385 6
			default:
386 6
				return 'RAND()';
387 6
		}
388
	}
389
}
390