Completed
Pull Request — master (#114)
by Jakub
26:35 queued 24:38
created

manager::get_ads_by_owner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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