|
1
|
|
|
<?php |
|
2
|
|
|
namespace AL\Common\DAO; |
|
3
|
|
|
|
|
4
|
|
|
require_once __DIR__."/../../lib/Db.php" ; |
|
5
|
|
|
require_once __DIR__."/../Model/News/News.php" ; |
|
6
|
|
|
require_once __DIR__."/../Model/User/User.php" ; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* DAO for News |
|
10
|
|
|
*/ |
|
11
|
|
|
class NewsDAO { |
|
12
|
|
|
private $mysqli; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($mysqli) { |
|
15
|
|
|
$this->mysqli = $mysqli; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
private function getLatestNewsQuery($user_id = null, $last_timestamp = null, $words = null) { |
|
19
|
|
|
// DISTINCT is currently needed because there may |
|
20
|
|
|
// be multiple matches for a word in news_search_wordlist, one for |
|
21
|
|
|
// the word is in the title and one for the word in the content. |
|
22
|
|
|
// That result in the same news being returned twice. It would be |
|
23
|
|
|
// preferable to fix the DB structure, but we have other priorities |
|
24
|
|
|
// for now... |
|
25
|
|
|
$query = "SELECT |
|
26
|
|
|
DISTINCT(news.news_id), |
|
27
|
|
|
news.news_headline, |
|
28
|
|
|
news.news_text, |
|
29
|
|
|
news.news_date, |
|
30
|
|
|
news.user_id, |
|
31
|
|
|
CONCAT(news_image.news_image_id, '.', news_image.news_image_ext) as news_image, |
|
32
|
|
|
news_image.news_image_id, |
|
33
|
|
|
users.user_id, |
|
34
|
|
|
users.userid, |
|
35
|
|
|
users.email, |
|
36
|
|
|
users.join_date, |
|
37
|
|
|
users.karma, |
|
38
|
|
|
users.show_email, |
|
39
|
|
|
users.avatar_ext, |
|
40
|
|
|
(SELECT COUNT(*) |
|
41
|
|
|
FROM news |
|
42
|
|
|
WHERE news.user_id = users.user_id) AS user_news_count |
|
43
|
|
|
FROM news |
|
44
|
|
|
LEFT JOIN news_image ON news.news_image_id = news_image.news_image_id |
|
45
|
|
|
LEFT JOIN users ON news.user_id = users.user_id"; |
|
46
|
|
|
|
|
47
|
|
|
if ($words != null) { |
|
48
|
|
|
$query .= " LEFT JOIN news_search_wordmatch |
|
49
|
|
|
ON news_search_wordmatch.news_id = news.news_id |
|
50
|
|
|
LEFT JOIN news_search_wordlist |
|
51
|
|
|
ON news_search_wordlist.news_word_id = news_search_wordmatch.news_word_id"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$constraints = []; |
|
55
|
|
|
if ($user_id != null) { |
|
56
|
|
|
$constraints[] = "news.user_id = ?"; |
|
57
|
|
|
} |
|
58
|
|
|
if ($last_timestamp != null) { |
|
59
|
|
|
$constraints[] = "news.news_date < ?"; |
|
60
|
|
|
} |
|
61
|
|
|
if ($words != null) { |
|
62
|
|
|
$constraints[] = "news_search_wordlist.news_word_text = ?"; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$query .= \AL\Db\assemble_constraints($constraints); |
|
66
|
|
|
|
|
67
|
|
|
$query .= " ORDER BY news_date DESC LIMIT ?"; |
|
68
|
|
|
|
|
69
|
|
|
return $query; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return the latest news, sorted by descending date |
|
74
|
|
|
* |
|
75
|
|
|
* @param integer $limit How many news to return |
|
76
|
|
|
* @param integer $user_id To retrieve the news of a specific user only |
|
77
|
|
|
* @param integer $user_id To retrieve only the news before a given timestamp |
|
78
|
|
|
* @return \AL\Common\Model\News\News[] An array of news |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getLatestNews($limit = 20, $user_id = null, $last_timestamp = null, $words = null) { |
|
81
|
|
|
$query = $this->getLatestNewsQuery($user_id, $last_timestamp, $words); |
|
82
|
|
|
$bind_string = ""; |
|
83
|
|
|
$bind_params = array(); |
|
84
|
|
|
|
|
85
|
|
|
if ($user_id != null) { |
|
|
|
|
|
|
86
|
|
|
$bind_string .= "i"; |
|
87
|
|
|
$bind_params[] = $user_id; |
|
88
|
|
|
} |
|
89
|
|
|
if ($last_timestamp != null) { |
|
90
|
|
|
$bind_string .= "i"; |
|
91
|
|
|
$bind_params[] = $last_timestamp; |
|
92
|
|
|
} |
|
93
|
|
|
if ($words != null) { |
|
94
|
|
|
$bind_string .= "s"; |
|
95
|
|
|
$bind_params[] = $words; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$bind_string .= "i"; |
|
99
|
|
|
$bind_params[] = $limit; |
|
100
|
|
|
|
|
101
|
|
|
$stmt = \AL\Db\execute_query( |
|
102
|
|
|
"NewsDAO: getLatestNews", |
|
103
|
|
|
$this->mysqli, |
|
104
|
|
|
$query, |
|
105
|
|
|
$bind_string, ...$bind_params |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
\AL\Db\bind_result( |
|
109
|
|
|
"NewsDAO: getLatestNews", |
|
110
|
|
|
$stmt, |
|
111
|
|
|
$id, |
|
112
|
|
|
$headline, |
|
|
|
|
|
|
113
|
|
|
$text, |
|
|
|
|
|
|
114
|
|
|
$date, |
|
|
|
|
|
|
115
|
|
|
$userid, |
|
|
|
|
|
|
116
|
|
|
$image, |
|
|
|
|
|
|
117
|
|
|
$image_id, |
|
|
|
|
|
|
118
|
|
|
$user_id, |
|
119
|
|
|
$user_userid, |
|
|
|
|
|
|
120
|
|
|
$user_email, |
|
|
|
|
|
|
121
|
|
|
$user_join_date, |
|
|
|
|
|
|
122
|
|
|
$user_karma, |
|
|
|
|
|
|
123
|
|
|
$user_show_email, |
|
|
|
|
|
|
124
|
|
|
$user_avatar_ext, |
|
|
|
|
|
|
125
|
|
|
$user_news_count |
|
|
|
|
|
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$news = []; |
|
129
|
|
|
while ($stmt->fetch()) { |
|
130
|
|
|
$user = new \AL\Common\Model\User\User( |
|
131
|
|
|
$user_id, |
|
132
|
|
|
$user_userid, |
|
133
|
|
|
$user_email, |
|
134
|
|
|
$user_join_date, |
|
135
|
|
|
$user_karma, |
|
136
|
|
|
$user_show_email, |
|
137
|
|
|
$user_avatar_ext, |
|
138
|
|
|
$user_news_count |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$news[] = new \AL\Common\Model\News\News( |
|
142
|
|
|
$id, |
|
143
|
|
|
$headline, |
|
144
|
|
|
$text, |
|
145
|
|
|
$date, |
|
146
|
|
|
$image, |
|
147
|
|
|
$image_id, |
|
148
|
|
|
$user |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$stmt->close(); |
|
153
|
|
|
|
|
154
|
|
|
return $news; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return a specific news article |
|
159
|
|
|
|
|
160
|
|
|
* @return \AL\Common\Model\News\News A single news |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getNews($news_id) { |
|
163
|
|
|
$stmt = \AL\Db\execute_query( |
|
164
|
|
|
"NewsDAO: getNews", |
|
165
|
|
|
$this->mysqli, |
|
166
|
|
|
"SELECT |
|
167
|
|
|
news.news_id, |
|
168
|
|
|
news.news_headline, |
|
169
|
|
|
news.news_text, |
|
170
|
|
|
news.news_date, |
|
171
|
|
|
news.user_id, |
|
172
|
|
|
CONCAT(news_image.news_image_id, '.', news_image.news_image_ext) as news_image, |
|
173
|
|
|
news_image.news_image_id, |
|
174
|
|
|
users.user_id, |
|
175
|
|
|
users.userid, |
|
176
|
|
|
users.email, |
|
177
|
|
|
users.join_date, |
|
178
|
|
|
users.karma, |
|
179
|
|
|
users.show_email, |
|
180
|
|
|
users.avatar_ext, |
|
181
|
|
|
(SELECT COUNT(*) |
|
182
|
|
|
FROM news |
|
183
|
|
|
WHERE news.user_id = users.user_id) AS user_news_count |
|
184
|
|
|
FROM news |
|
185
|
|
|
LEFT JOIN news_image ON news.news_image_id = news_image.news_image_id |
|
186
|
|
|
LEFT JOIN users ON news.user_id = users.user_id |
|
187
|
|
|
WHERE news.news_id = ?", |
|
188
|
|
|
"i", $news_id |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
\AL\Db\bind_result( |
|
192
|
|
|
"NewsDAO: getNews", |
|
193
|
|
|
$stmt, |
|
194
|
|
|
$id, |
|
195
|
|
|
$headline, |
|
|
|
|
|
|
196
|
|
|
$text, |
|
|
|
|
|
|
197
|
|
|
$date, |
|
|
|
|
|
|
198
|
|
|
$userid, |
|
|
|
|
|
|
199
|
|
|
$image, |
|
|
|
|
|
|
200
|
|
|
$image_id, |
|
|
|
|
|
|
201
|
|
|
$user_id, |
|
|
|
|
|
|
202
|
|
|
$user_userid, |
|
|
|
|
|
|
203
|
|
|
$user_email, |
|
|
|
|
|
|
204
|
|
|
$user_join_date, |
|
|
|
|
|
|
205
|
|
|
$user_karma, |
|
|
|
|
|
|
206
|
|
|
$user_show_email, |
|
|
|
|
|
|
207
|
|
|
$user_avatar_ext, |
|
|
|
|
|
|
208
|
|
|
$user_news_count |
|
|
|
|
|
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
$news = null; |
|
212
|
|
|
|
|
213
|
|
|
if ($stmt->fetch()) { |
|
214
|
|
|
$user = new \AL\Common\Model\User\User( |
|
215
|
|
|
$user_id, |
|
216
|
|
|
$user_userid, |
|
217
|
|
|
$user_email, |
|
218
|
|
|
$user_join_date, |
|
219
|
|
|
$user_karma, |
|
220
|
|
|
$user_show_email, |
|
221
|
|
|
$user_avatar_ext, |
|
222
|
|
|
$user_news_count |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
$news = new \AL\Common\Model\News\News( |
|
226
|
|
|
$id, |
|
227
|
|
|
$headline, |
|
228
|
|
|
$text, |
|
229
|
|
|
$date, |
|
230
|
|
|
$image, |
|
231
|
|
|
$image_id, |
|
232
|
|
|
$user |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$stmt->close(); |
|
237
|
|
|
|
|
238
|
|
|
return $news; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get the total count of news on the website |
|
243
|
|
|
* @return number Total number of news |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getNewsCount() { |
|
246
|
|
|
$stmt = \AL\Db\execute_query( |
|
247
|
|
|
"NewsDAO: getNewsCount", |
|
248
|
|
|
$this->mysqli, |
|
249
|
|
|
"SELECT COUNT(*) FROM news", |
|
250
|
|
|
null, |
|
251
|
|
|
null |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
\AL\Db\bind_result( |
|
255
|
|
|
"NewsDAO: getNewsCount", |
|
256
|
|
|
$stmt, |
|
257
|
|
|
$count |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
|
|
$stmt->fetch(); |
|
261
|
|
|
$stmt->close(); |
|
262
|
|
|
|
|
263
|
|
|
return $count; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|