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 string */ |
19
|
|
|
protected $ads_table; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $ad_locations_table; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
* |
27
|
|
|
* @param \phpbb\db\driver\driver_interface $db DB driver interface |
28
|
|
|
* @param string $ads_table Ads table |
29
|
|
|
* @param string $ad_locations_table Ad locations table |
30
|
|
|
*/ |
31
|
46 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, $ads_table, $ad_locations_table) |
32
|
|
|
{ |
33
|
46 |
|
$this->db = $db; |
34
|
46 |
|
$this->ads_table = $ads_table; |
35
|
46 |
|
$this->ad_locations_table = $ad_locations_table; |
36
|
46 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get specific ad |
40
|
|
|
* |
41
|
|
|
* @param int $ad_id Advertisement ID |
42
|
|
|
* @return mixed Array with advertisement data, false if ad doesn't exist |
43
|
|
|
*/ |
44
|
4 |
View Code Duplication |
public function get_ad($ad_id) |
45
|
|
|
{ |
46
|
|
|
$sql = 'SELECT * |
47
|
4 |
|
FROM ' . $this->ads_table . ' |
48
|
4 |
|
WHERE ad_id = ' . (int) $ad_id; |
49
|
4 |
|
$result = $this->db->sql_query($sql); |
50
|
4 |
|
$data = $this->db->sql_fetchrow($result); |
51
|
4 |
|
$this->db->sql_freeresult($result); |
52
|
|
|
|
53
|
4 |
|
return $data; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get one ad per every location |
58
|
|
|
* |
59
|
|
|
* @param array $ad_locations List of ad locations to fetch ads for |
60
|
|
|
* @return array List of ad codes for each location |
61
|
|
|
*/ |
62
|
2 |
|
public function get_ads($ad_locations) |
63
|
|
|
{ |
64
|
|
|
$sql = 'SELECT location_id, ad_code |
65
|
|
|
FROM ( |
66
|
|
|
SELECT al.location_id, a.ad_code |
67
|
2 |
|
FROM ' . $this->ad_locations_table . ' al |
68
|
2 |
|
LEFT JOIN ' . $this->ads_table . ' a |
69
|
|
|
ON (al.ad_id = a.ad_id) |
70
|
|
|
WHERE a.ad_enabled = 1 |
71
|
|
|
AND (a.ad_end_date = 0 |
72
|
2 |
|
OR a.ad_end_date > ' . time() . ') |
73
|
2 |
|
AND ' . $this->db->sql_in_set('al.location_id', $ad_locations) . ' |
74
|
2 |
|
ORDER BY (' . $this->sql_random() . ' * a.ad_priority) |
75
|
|
|
) z |
76
|
2 |
|
ORDER BY z.location_id'; |
77
|
2 |
|
$result = $this->db->sql_query($sql); |
78
|
2 |
|
$data = $this->db->sql_fetchrowset($result); |
79
|
2 |
|
$this->db->sql_freeresult($result); |
80
|
|
|
|
81
|
2 |
|
return $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get all advertisements |
86
|
|
|
* |
87
|
|
|
* @return array List of all ads |
88
|
|
|
*/ |
89
|
1 |
View Code Duplication |
public function get_all_ads() |
90
|
|
|
{ |
91
|
|
|
$sql = 'SELECT ad_id, ad_name, ad_enabled, ad_end_date |
92
|
1 |
|
FROM ' . $this->ads_table; |
93
|
1 |
|
$result = $this->db->sql_query($sql); |
94
|
1 |
|
$data = $this->db->sql_fetchrowset($result); |
95
|
1 |
|
$this->db->sql_freeresult($result); |
96
|
|
|
|
97
|
1 |
|
return $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Insert new advertisement to the database |
102
|
|
|
* |
103
|
|
|
* @param array $data New ad data |
104
|
|
|
* @return int New advertisement ID |
105
|
|
|
*/ |
106
|
1 |
View Code Duplication |
public function insert_ad($data) |
107
|
|
|
{ |
108
|
1 |
|
$data = $this->intersect_ad_data($data); |
109
|
|
|
|
110
|
1 |
|
$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data); |
111
|
1 |
|
$this->db->sql_query($sql); |
112
|
|
|
|
113
|
1 |
|
return $this->db->sql_nextid(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Update advertisement |
118
|
|
|
* |
119
|
|
|
* @param int $ad_id Advertisement ID |
120
|
|
|
* @param array $data List of data to update in the database |
121
|
|
|
* @return int Number of affected rows. Can be used to determine if any ad has been updated. |
122
|
|
|
*/ |
123
|
7 |
View Code Duplication |
public function update_ad($ad_id, $data) |
124
|
|
|
{ |
125
|
7 |
|
$data = $this->intersect_ad_data($data); |
126
|
|
|
|
127
|
7 |
|
$sql = 'UPDATE ' . $this->ads_table . ' |
128
|
7 |
|
SET ' . $this->db->sql_build_array('UPDATE', $data) . ' |
129
|
7 |
|
WHERE ad_id = ' . (int) $ad_id; |
130
|
7 |
|
$this->db->sql_query($sql); |
131
|
|
|
|
132
|
7 |
|
return $this->db->sql_affectedrows(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Delete advertisement |
137
|
|
|
* |
138
|
|
|
* @param int $ad_id Advertisement ID |
139
|
|
|
* @return int Number of affected rows. Can be used to determine if any ad has been deleted. |
140
|
|
|
*/ |
141
|
2 |
|
public function delete_ad($ad_id) |
142
|
|
|
{ |
143
|
2 |
|
$sql = 'DELETE FROM ' . $this->ads_table . ' |
144
|
2 |
|
WHERE ad_id = ' . (int) $ad_id; |
145
|
2 |
|
$this->db->sql_query($sql); |
146
|
|
|
|
147
|
2 |
|
return $this->db->sql_affectedrows(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get all locations for specified advertisement |
152
|
|
|
* |
153
|
|
|
* @param int $ad_id Advertisement ID |
154
|
|
|
* @return array List of template locations for specified ad |
155
|
|
|
*/ |
156
|
1 |
View Code Duplication |
public function get_ad_locations($ad_id) |
157
|
|
|
{ |
158
|
1 |
|
$ad_locations = array(); |
159
|
|
|
|
160
|
|
|
$sql = 'SELECT location_id |
161
|
1 |
|
FROM ' . $this->ad_locations_table . ' |
162
|
1 |
|
WHERE ad_id = ' . (int) $ad_id; |
163
|
1 |
|
$result = $this->db->sql_query($sql); |
164
|
1 |
|
while ($row = $this->db->sql_fetchrow($result)) |
165
|
|
|
{ |
166
|
1 |
|
$ad_locations[] = $row['location_id']; |
167
|
1 |
|
} |
168
|
1 |
|
$this->db->sql_freeresult($result); |
169
|
|
|
|
170
|
1 |
|
return $ad_locations; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Insert advertisement locations |
175
|
|
|
* |
176
|
|
|
* @param int $ad_id Advertisement ID |
177
|
|
|
* @param array $ad_locations List of template locations for this ad |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
2 |
|
public function insert_ad_locations($ad_id, $ad_locations) |
181
|
|
|
{ |
182
|
2 |
|
$sql_ary = array(); |
183
|
2 |
|
foreach ($ad_locations as $ad_location) |
184
|
|
|
{ |
185
|
2 |
|
$sql_ary[] = array( |
186
|
2 |
|
'ad_id' => $ad_id, |
187
|
2 |
|
'location_id' => $ad_location, |
188
|
|
|
); |
189
|
2 |
|
} |
190
|
2 |
|
$this->db->sql_multi_insert($this->ad_locations_table, $sql_ary); |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Delete advertisement locations |
195
|
|
|
* |
196
|
|
|
* @param int $ad_id Advertisement ID |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
3 |
|
public function delete_ad_locations($ad_id) |
200
|
|
|
{ |
201
|
3 |
|
$sql = 'DELETE FROM ' . $this->ad_locations_table . ' |
202
|
3 |
|
WHERE ad_id = ' . (int) $ad_id; |
203
|
3 |
|
$this->db->sql_query($sql); |
204
|
3 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Load memberships of the user |
208
|
|
|
* |
209
|
|
|
* @param int $user_id User ID to load memberships |
210
|
|
|
* @return array List of group IDs user is member of |
211
|
|
|
*/ |
212
|
4 |
View Code Duplication |
public function load_memberships($user_id) |
213
|
|
|
{ |
214
|
4 |
|
$memberships = array(); |
215
|
|
|
$sql = 'SELECT group_id |
216
|
4 |
|
FROM ' . USER_GROUP_TABLE . ' |
217
|
4 |
|
WHERE user_id = ' . (int) $user_id . ' |
218
|
4 |
|
AND user_pending = 0'; |
219
|
4 |
|
$result = $this->db->sql_query($sql, 3600); |
220
|
4 |
|
while ($row = $this->db->sql_fetchrow($result)) |
221
|
|
|
{ |
222
|
4 |
|
$memberships[] = $row['group_id']; |
223
|
4 |
|
} |
224
|
4 |
|
$this->db->sql_freeresult($result); |
225
|
4 |
|
return $memberships; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Load all board groups |
230
|
|
|
* |
231
|
|
|
* @return array List of groups |
232
|
|
|
*/ |
233
|
2 |
View Code Duplication |
public function load_groups() |
234
|
|
|
{ |
235
|
|
|
$sql = 'SELECT group_id, group_name, group_type |
236
|
2 |
|
FROM ' . GROUPS_TABLE . ' |
237
|
2 |
|
ORDER BY group_name ASC'; |
238
|
2 |
|
$result = $this->db->sql_query($sql); |
239
|
2 |
|
$groups = $this->db->sql_fetchrowset($result); |
240
|
2 |
|
$this->db->sql_freeresult($result); |
241
|
|
|
|
242
|
2 |
|
return $groups; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Make sure only necessary data make their way to SQL query |
247
|
|
|
* |
248
|
|
|
* @param array $data List of data to query the database |
249
|
|
|
* @return array Cleaned data that contain only valid keys |
250
|
|
|
*/ |
251
|
8 |
|
protected function intersect_ad_data($data) |
252
|
|
|
{ |
253
|
8 |
|
return array_intersect_key($data, array( |
254
|
8 |
|
'ad_name' => '', |
255
|
8 |
|
'ad_note' => '', |
256
|
8 |
|
'ad_code' => '', |
257
|
8 |
|
'ad_enabled' => '', |
258
|
8 |
|
'ad_end_date' => '', |
259
|
8 |
|
'ad_priority' => '', |
260
|
8 |
|
)); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get the random statement for this database layer |
265
|
|
|
* Random function should generate a float value between 0 and 1 |
266
|
|
|
* |
267
|
|
|
* @return string Random statement for current database layer |
268
|
|
|
*/ |
269
|
2 |
|
protected function sql_random() |
270
|
|
|
{ |
271
|
2 |
|
var_dump(mt_rand() / mt_getrandmax()); |
|
|
|
|
272
|
2 |
|
switch ($this->db->get_sql_layer()) |
273
|
|
|
{ |
274
|
2 |
|
case 'oracle': |
275
|
|
|
return 'VALUE()'; |
276
|
|
|
|
277
|
2 |
|
case 'postgres': |
278
|
|
|
return 'RANDOM()'; |
279
|
|
|
|
280
|
2 |
|
case 'sqlite': |
281
|
2 |
|
case 'sqlite3': |
282
|
|
|
return '(RANDOM() / 9223372036854775808 + 0.5)'; |
283
|
|
|
|
284
|
|
|
/* All other cases should use the default |
|
|
|
|
285
|
|
|
case 'mssql': |
286
|
|
|
case 'mssql_odbc': |
287
|
|
|
case 'mssqlnative': |
288
|
|
|
case 'mysql': |
289
|
|
|
case 'mysqli':*/ |
290
|
2 |
|
default: |
291
|
2 |
|
return 'RAND()'; |
292
|
2 |
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|