|
1
|
|
|
<?php |
|
2
|
|
|
namespace AL\Common\DAO; |
|
3
|
|
|
|
|
4
|
|
|
require_once __DIR__."/../../lib/Db.php" ; |
|
5
|
|
|
require_once __DIR__."/../../lib/functions.php" ; |
|
6
|
|
|
require_once __DIR__."/../Model/News/NewsSubmission.php" ; |
|
7
|
|
|
|
|
8
|
|
|
use AL\Common\Model\NewsSubmission; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* DAO for News Submlissions |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
class NewsSubmissionDAO { |
|
15
|
|
|
private $mysqli; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($mysqli) { |
|
18
|
|
|
$this->mysqli = $mysqli; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function getSubmissionQuery($user_id = null, $news_id = null) { |
|
|
|
|
|
|
22
|
|
|
$query = "SELECT |
|
23
|
|
|
news_submission.news_submission_id, |
|
24
|
|
|
news_submission.news_headline, |
|
25
|
|
|
news_submission.news_text, |
|
26
|
|
|
news_submission.news_date, |
|
27
|
|
|
news_image.news_image_id, |
|
28
|
|
|
CONCAT(news_image.news_image_id, \".\", news_image.news_image_ext) as news_image, |
|
29
|
|
|
news_submission.user_id, |
|
30
|
|
|
users.userid, |
|
31
|
|
|
users.email, |
|
32
|
|
|
users.join_date, |
|
33
|
|
|
users.karma, |
|
34
|
|
|
users.show_email, |
|
35
|
|
|
users.avatar_ext, |
|
36
|
|
|
(SELECT COUNT(*) |
|
37
|
|
|
FROM news_submission |
|
38
|
|
|
WHERE news_submission.user_id = users.user_id) AS user_submission_count |
|
39
|
|
|
FROM news_submission |
|
40
|
|
|
LEFT JOIN news_image ON (news_submission.news_image_id = news_image.news_image_id) |
|
41
|
|
|
LEFT JOIN users ON (news_submission.user_id = users.user_id)"; |
|
42
|
|
|
|
|
43
|
|
|
if (isset($user_id)) { |
|
44
|
|
|
$query .= " WHERE news_submission.user_id = ?"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$query .= " ORDER BY news_date DESC"; |
|
48
|
|
|
|
|
49
|
|
|
return $query; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return all news submissions, sorted by descending date |
|
54
|
|
|
* @return \AL\Common\Model\News\News[] An array of news |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getAllSubmissions() { |
|
57
|
|
|
$stmt = \AL\Db\execute_query( |
|
58
|
|
|
"NewsSubmissionDAO: getAllSubmissions", |
|
59
|
|
|
$this->mysqli, |
|
60
|
|
|
$this->getSubmissionQuery(), |
|
61
|
|
|
null, null |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
\AL\Db\bind_result( |
|
65
|
|
|
"NewsSubmissionDAO: get All Submissions", |
|
66
|
|
|
$stmt, |
|
67
|
|
|
$id, |
|
68
|
|
|
$headline, |
|
|
|
|
|
|
69
|
|
|
$text, |
|
|
|
|
|
|
70
|
|
|
$date, |
|
|
|
|
|
|
71
|
|
|
$image_id, |
|
|
|
|
|
|
72
|
|
|
$image, |
|
|
|
|
|
|
73
|
|
|
$userid, |
|
|
|
|
|
|
74
|
|
|
$username, |
|
|
|
|
|
|
75
|
|
|
$email, |
|
|
|
|
|
|
76
|
|
|
$join_date, |
|
|
|
|
|
|
77
|
|
|
$karma, |
|
|
|
|
|
|
78
|
|
|
$show_email, |
|
|
|
|
|
|
79
|
|
|
$avatar_ext, |
|
|
|
|
|
|
80
|
|
|
$user_subm_count |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$news = []; |
|
84
|
|
|
|
|
85
|
|
|
while ($stmt->fetch()) { |
|
86
|
|
|
$text = nl2br($text); |
|
|
|
|
|
|
87
|
|
|
$text = InsertALCode($text); |
|
88
|
|
|
$text = trim($text); |
|
89
|
|
|
$text = RemoveSmillies($text); |
|
90
|
|
|
|
|
91
|
|
|
$news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission( |
|
92
|
|
|
$id, |
|
93
|
|
|
$headline, |
|
94
|
|
|
$text, |
|
95
|
|
|
$date, |
|
96
|
|
|
$image_id, |
|
97
|
|
|
$image, |
|
98
|
|
|
$userid, |
|
99
|
|
|
$username, |
|
100
|
|
|
$email, |
|
101
|
|
|
$join_date, |
|
102
|
|
|
$karma, |
|
103
|
|
|
$show_email, |
|
104
|
|
|
$avatar_ext, |
|
105
|
|
|
$user_subm_count |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$stmt->close(); |
|
110
|
|
|
|
|
111
|
|
|
return $news; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Return all news submissions, sorted by descending date |
|
116
|
|
|
* @return \AL\Common\Model\News\News[] An array of news |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getAllSubmissionsForUser($user_id) { |
|
119
|
|
|
$stmt = \AL\Db\execute_query( |
|
120
|
|
|
"NewsSubmissionDAO: getAllSubmissions", |
|
121
|
|
|
$this->mysqli, |
|
122
|
|
|
$this->getSubmissionQuery($user_id), |
|
123
|
|
|
"i", $user_id |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
\AL\Db\bind_result( |
|
127
|
|
|
"NewsSubmissionDAO: get All Submissions", |
|
128
|
|
|
$stmt, |
|
129
|
|
|
$id, |
|
130
|
|
|
$headline, |
|
|
|
|
|
|
131
|
|
|
$text, |
|
|
|
|
|
|
132
|
|
|
$date, |
|
|
|
|
|
|
133
|
|
|
$image_id, |
|
|
|
|
|
|
134
|
|
|
$image, |
|
|
|
|
|
|
135
|
|
|
$userid, |
|
|
|
|
|
|
136
|
|
|
$username, |
|
|
|
|
|
|
137
|
|
|
$email, |
|
|
|
|
|
|
138
|
|
|
$join_date, |
|
|
|
|
|
|
139
|
|
|
$karma, |
|
|
|
|
|
|
140
|
|
|
$show_email, |
|
|
|
|
|
|
141
|
|
|
$avatar_ext, |
|
|
|
|
|
|
142
|
|
|
$user_subm_count |
|
|
|
|
|
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$news = []; |
|
146
|
|
|
|
|
147
|
|
|
while ($stmt->fetch()) { |
|
148
|
|
|
$text = nl2br($text); |
|
|
|
|
|
|
149
|
|
|
$text = InsertALCode($text); |
|
150
|
|
|
$text = trim($text); |
|
151
|
|
|
$text = RemoveSmillies($text); |
|
152
|
|
|
|
|
153
|
|
|
$news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission( |
|
154
|
|
|
$id, |
|
155
|
|
|
$headline, |
|
156
|
|
|
$text, |
|
157
|
|
|
$date, |
|
158
|
|
|
$image_id, |
|
159
|
|
|
$image, |
|
160
|
|
|
$userid, |
|
161
|
|
|
$username, |
|
162
|
|
|
$email, |
|
163
|
|
|
$join_date, |
|
164
|
|
|
$karma, |
|
165
|
|
|
$show_email, |
|
166
|
|
|
$avatar_ext, |
|
167
|
|
|
$user_subm_count |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$stmt->close(); |
|
172
|
|
|
|
|
173
|
|
|
return $news; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Return a specific news submissions |
|
179
|
|
|
* @return \AL\Common\Model\News\News[] An array of news |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getSpecificSubmissions($news_id) { |
|
182
|
|
|
$stmt = \AL\Db\execute_query( |
|
183
|
|
|
"NewsSubmissionDAO: getSpecificSubmissions", |
|
184
|
|
|
$this->mysqli, |
|
185
|
|
|
"SELECT |
|
186
|
|
|
news_submission.news_submission_id, |
|
187
|
|
|
news_submission.news_headline, |
|
188
|
|
|
news_submission.news_text, |
|
189
|
|
|
news_submission.news_date, |
|
190
|
|
|
news_image.news_image_id, |
|
191
|
|
|
CONCAT(news_image.news_image_id, \".\", news_image.news_image_ext) as news_image, |
|
192
|
|
|
news_submission.user_id, |
|
193
|
|
|
users.userid, |
|
194
|
|
|
users.email, |
|
195
|
|
|
users.join_date, |
|
196
|
|
|
users.karma, |
|
197
|
|
|
users.show_email, |
|
198
|
|
|
users.avatar_ext, |
|
199
|
|
|
(SELECT COUNT(*) |
|
200
|
|
|
FROM news_submission |
|
201
|
|
|
WHERE news_submission.user_id = users.user_id) AS user_submission_count |
|
202
|
|
|
FROM news_submission |
|
203
|
|
|
LEFT JOIN news_image ON (news_submission.news_image_id = news_image.news_image_id) |
|
204
|
|
|
LEFT JOIN users ON (news_submission.user_id = users.user_id) |
|
205
|
|
|
WHERE news_submission.news_submission_id = ?", |
|
206
|
|
|
"i", |
|
207
|
|
|
$news_id |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
\AL\Db\bind_result( |
|
211
|
|
|
"NewsSubmissionDAO: get specific Submissions", |
|
212
|
|
|
$stmt, |
|
213
|
|
|
$id, |
|
214
|
|
|
$headline, |
|
|
|
|
|
|
215
|
|
|
$text, |
|
|
|
|
|
|
216
|
|
|
$date, |
|
|
|
|
|
|
217
|
|
|
$image_id, |
|
|
|
|
|
|
218
|
|
|
$image, |
|
|
|
|
|
|
219
|
|
|
$userid, |
|
|
|
|
|
|
220
|
|
|
$username, |
|
|
|
|
|
|
221
|
|
|
$email, |
|
|
|
|
|
|
222
|
|
|
$join_date, |
|
|
|
|
|
|
223
|
|
|
$karma, |
|
|
|
|
|
|
224
|
|
|
$show_email, |
|
|
|
|
|
|
225
|
|
|
$avatar_ext, |
|
|
|
|
|
|
226
|
|
|
$user_subm_count |
|
|
|
|
|
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
$news = []; |
|
230
|
|
|
|
|
231
|
|
|
while ($stmt->fetch()) { |
|
232
|
|
|
//$text = nl2br($text); |
|
233
|
|
|
//$text = InsertALCode($text); |
|
234
|
|
|
//$text = trim($text); |
|
235
|
|
|
//$text = RemoveSmillies($text); |
|
236
|
|
|
$breaks = array("<br />","<br>","<br/>"); |
|
237
|
|
|
$text = str_ireplace($breaks, "\r\n", $text); |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
$news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission( |
|
240
|
|
|
$id, |
|
241
|
|
|
$headline, |
|
242
|
|
|
$text, |
|
243
|
|
|
$date, |
|
244
|
|
|
$image_id, |
|
245
|
|
|
$image, |
|
246
|
|
|
$userid, |
|
247
|
|
|
$username, |
|
248
|
|
|
$email, |
|
249
|
|
|
$join_date, |
|
250
|
|
|
$karma, |
|
251
|
|
|
$show_email, |
|
252
|
|
|
$avatar_ext, |
|
253
|
|
|
$user_subm_count |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$stmt->close(); |
|
258
|
|
|
|
|
259
|
|
|
return $news; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Get the total count of comments on the website |
|
264
|
|
|
* |
|
265
|
|
|
* @param integer $user_id Optional ID of a user to count comments for |
|
266
|
|
|
* @return integer Number of comments */ |
|
267
|
|
|
public function getSubmissionCount($user_id = null) { |
|
268
|
|
|
if (isset($user_id)) { |
|
269
|
|
|
$stmt = \AL\Db\execute_query( |
|
270
|
|
|
"NewsSubmissionDAO: Get submission count for user_id $user_id", |
|
271
|
|
|
$this->mysqli, |
|
272
|
|
|
"SELECT COUNT(*) FROM news_submission WHERE user_id = ?", |
|
273
|
|
|
"i", |
|
274
|
|
|
$user_id |
|
275
|
|
|
); |
|
276
|
|
|
} else { |
|
277
|
|
|
$stmt = \AL\Db\execute_query( |
|
278
|
|
|
"NewsSubmissionDAO: Get submission count in database", |
|
279
|
|
|
$this->mysqli, |
|
280
|
|
|
"SELECT COUNT(*) FROM news_submission", |
|
281
|
|
|
null, |
|
282
|
|
|
null |
|
283
|
|
|
); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
\AL\Db\bind_result( |
|
287
|
|
|
"NewsSubmissionDAO: Get submissino count", |
|
288
|
|
|
$stmt, |
|
289
|
|
|
$count |
|
290
|
|
|
); |
|
291
|
|
|
|
|
292
|
|
|
$stmt->fetch(); |
|
293
|
|
|
$stmt->close(); |
|
294
|
|
|
|
|
295
|
|
|
return $count; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Get the comment text for a specific comment |
|
300
|
|
|
* |
|
301
|
|
|
* @param integer $comments_id ID of a comment |
|
302
|
|
|
* @return text the text of the comment |
|
|
|
|
|
|
303
|
|
|
*/ |
|
304
|
|
|
public function getNewsText($news_id = null) { |
|
305
|
|
|
if (isset($news_id)) { |
|
306
|
|
|
$stmt = \AL\Db\execute_query( |
|
307
|
|
|
"NewsSubmissionDAO: Get news text for news_id $news_id", |
|
308
|
|
|
$this->mysqli, |
|
309
|
|
|
"SELECT news_text FROM news_submission WHERE news_submission_id = ?", |
|
310
|
|
|
"i", |
|
311
|
|
|
$news_id |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
\AL\Db\bind_result( |
|
316
|
|
|
"NewsSubmissionDAO: Get news text", |
|
317
|
|
|
$stmt, |
|
|
|
|
|
|
318
|
|
|
$news |
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
|
|
$stmt->fetch(); |
|
322
|
|
|
$stmt->close(); |
|
323
|
|
|
|
|
324
|
|
|
return $news; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.