-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforplay.php
433 lines (343 loc) · 16.1 KB
/
forplay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
include ('../../forsecret/db.php');
if (getenv('REQUEST_METHOD') == 'GET') {
/**
* TODO: Tag is not a tag, but ID, url is the real tag.
* Replace this onde day with the proper wording.
*/
$get_tag = isset($_GET['tag']) ? "{$_GET ['tag']}" : "ANY (SELECT for_articles.article_id FROM for_articles)";
$get_url = isset($_GET['url']) ? "'{$_GET ['url']}'" : "ANY (SELECT url FROM for_articles)";
$get_type = isset($_GET['type']) ? "'{$_GET ['type']}'" : "ANY (SELECT `type` FROM for_articles)";
$get_subtype = isset($_GET['subtype']) ? "'{$_GET ['subtype']}'" : "ANY (SELECT subtype FROM for_articles)";
$get_issue = isset($_GET['issue']) ? "'{$_GET ['issue']}'" : false;
$get_author = isset($_GET['author']) ? "'{$_GET ['author']}'" : false;
$get_suggest = isset($_GET['suggest']) ? "{$_GET ['suggest']}" : false;
/**
* This is the pagination to lazy load articles.
* By default we start from the first result.
*/
$get_offset = isset($_GET['offset']) ? "{$_GET ['offset']}" : 0;
$get_limit = isset($_GET['limit']) ? "{$_GET ['limit']}" : 50;
/**
* 1 of 5: Get portal based on type and subtype.
*/
$get_article_sql = "SELECT for_articles.*,
for_issues.`name` AS issue,
for_issues.tag AS issue_tag
FROM for_articles
INNER JOIN for_rel_issues ON for_articles.article_id = for_rel_issues.article_id
INNER JOIN for_issues ON for_issues.issue_id = for_rel_issues.issue_id
WHERE for_articles.article_id = $get_tag
AND (url = $get_url)
AND (`type` = $get_type)
AND (subtype = $get_subtype)
AND for_articles.`date` <= now()
ORDER BY date DESC
LIMIT $get_limit OFFSET $get_offset;";
/**
* 2 of 5: Main page load - all articles, excluding some quotes and carets.
* Also suggested articles by tag when opening another material.
*/
if (count($_GET) <= 0 || $get_issue || (count($_GET) == 1 &&
isset($_GET['offset']) && ! isset($_GET['tag'])) ||
(count($_GET) == 1 && isset($_GET['offset']) &&
! isset($_GET['tag'])) || (count($_GET) == 3 &&
isset($_GET['suggest']) && isset($_GET['tag']) &&
isset($_GET['offset']))) {
$sort = array();
$not = '';
if ($get_suggest) {
$php_suggest = explode(',', urldecode($get_suggest));
foreach ($php_suggest as $tag) {
array_push($sort,
"group_concat(for_rel_tags.tag_id SEPARATOR ',') LIKE '%$tag%' DESC");
}
$sort = join(', ', $sort) . ', ';
$not = "AND for_articles.article_id != $get_tag";
} else {
$sort = '';
}
$get_article_sql = "SELECT for_articles.*,
for_issues.`name` AS issue,
for_issues.tag AS issue_tag
FROM for_articles
INNER JOIN for_rel_issues ON for_articles.article_id = for_rel_issues.article_id
INNER JOIN for_issues ON for_issues.issue_id = for_rel_issues.issue_id
INNER JOIN for_rel_tags ON for_articles.article_id = for_rel_tags.article_id
WHERE (subtype
IN ('news', 'video', 'review', 'feature')
OR (subtype = 'aside' AND priority = 'aside'))
AND for_articles.`date` <= now()
$not
GROUP BY for_articles.article_id
ORDER BY $sort date DESC
LIMIT $get_limit OFFSET $get_offset;";
}
/**
* TODO: 3 of 5: Get specific issue or the latest one.
* This is the backup for getting the latest issue:
* AND for_rel_issues.issue_id = (
* SELECT max(for_rel_issues.issue_id)
* FROM for_rel_issues)
*/
/**
* 4 of 5: Get articles by author.
*/
if ($get_author) {
$get_article_sql = "SELECT for_articles.*,
for_issues.`name` AS issue,
for_issues.tag AS issue_tag
FROM for_articles
INNER JOIN for_rel_issues ON for_articles.article_id = for_rel_issues.article_id
INNER JOIN for_issues ON for_issues.issue_id = for_rel_issues.issue_id
INNER JOIN for_rel_authors ON for_articles.article_id = for_rel_authors.article_id
WHERE (subtype
IN ('news', 'video', 'review', 'feature')
OR (subtype = 'aside' AND priority = 'aside'))
AND for_articles.`date` <= now()
AND for_rel_authors.author_id = $get_author
ORDER BY date DESC
LIMIT $get_limit OFFSET $get_offset;";
}
/**
* There is duplicate of this query below.
*/
$get_authors_sql = "SELECT for_authors.* FROM for_authors
LEFT JOIN for_rel_authors
ON for_authors.author_id = for_rel_authors.author_id
WHERE for_rel_authors.article_id = $get_tag
GROUP BY author_id;";
$get_tags_sql = "SELECT for_tags.*, for_rel_tags.prime FROM for_tags
LEFT JOIN for_rel_tags
ON for_tags.tag_id = for_rel_tags.tag_id
WHERE for_rel_tags.article_id = $get_tag
GROUP BY tag_id
ORDER BY for_rel_tags.prime DESC;";
$get_layouts_sql = "SELECT for_layouts.*
FROM for_layouts
WHERE article_id = $get_tag
ORDER BY `order`;";
$get_issue_sql = "SELECT for_issues.issue_id,
for_issues.name AS en_name,
for_issues.tag
FROM for_issues
LEFT JOIN for_rel_issues
ON for_issues.issue_id = for_rel_issues.issue_id
WHERE for_rel_issues.article_id = $get_tag
GROUP BY issue_id;";
$get_article_result = true;
$get_authors_result = true;
$get_layouts_result = true;
$get_tags_result = true;
$get_issue_result = true;
$get_imgs_result = true;
$articles = array();
$covers_counter = 0;
/**
* This is for the listing on the portal page.
* Article basic information.
*/
$get_article_result = mysqli_query($link, $get_article_sql);
if (! $get_article_result) {
goto end;
}
/**
* 5 of 5: Work in progress articles from the adminstration UI.
*/
if (mysqli_num_rows($get_article_result) <= 0 && ! isset($_GET['offset'])) {
$get_article_sql = "SELECT for_articles.*
FROM for_articles
WHERE for_articles.article_id = $get_tag
AND (url = $get_url)
AND (`type` = $get_type)
AND (subtype = $get_subtype)
ORDER BY date DESC
LIMIT $get_limit OFFSET $get_offset;";
$get_article_result = mysqli_query($link, $get_article_sql);
}
while ($article = mysqli_fetch_assoc($get_article_result)) {
/**
* Place comments only as a total number.
*/
$get_comments_sql = "SELECT for_comments.comment_id
FROM for_comments
WHERE for_comments.article_id = {$article['article_id']}
AND for_comments.deleted = 0;";
$get_comments_result = mysqli_query($link, $get_comments_sql);
if ($get_comments_result) {
$article['comments'] = mysqli_num_rows($get_comments_result);
}
/**
* For revies attache the better, worse and equal.
*/
if ($article['subtype'] == 'review' || $article['subtype'] == 'video') {
$get_better_sql = "SELECT for_tags.*
FROM for_tags
WHERE tag_id = {$article['better']};";
$get_worse_sql = "SELECT for_tags.*
FROM for_tags
WHERE tag_id = {$article['worse']};";
$get_equal_sql = "SELECT for_tags.*
FROM for_tags
WHERE tag_id = {$article['equal']};";
$get_bwe_result = mysqli_query($link, $get_better_sql);
if ($get_bwe_result) {
while ($bwe = mysqli_fetch_assoc($get_bwe_result)) {
$article['better'] = array(
$bwe
);
}
}
$get_bwe_result = mysqli_query($link, $get_worse_sql);
if ($get_bwe_result) {
while ($bwe = mysqli_fetch_assoc($get_bwe_result)) {
$article['worse'] = array(
$bwe
);
}
}
$get_bwe_result = mysqli_query($link, $get_equal_sql);
if ($get_bwe_result) {
while ($bwe = mysqli_fetch_assoc($get_bwe_result)) {
$article['equal'] = array(
$bwe
);
}
}
}
/**
* There is duplicate of this query above.
* Append authors when getting article.
* There is not other way expect to save a preview string,
* but it also has to be handled on ui side.
* The server is always faster, so better do this there.
*/
$get_author_sql = "SELECT for_authors.* FROM for_authors
LEFT JOIN for_rel_authors
ON for_authors.author_id = for_rel_authors.author_id
WHERE for_rel_authors.article_id = {$article['article_id']}
GROUP BY author_id;";
$get_author_result = mysqli_query($link, $get_author_sql);
$article['authors'] = array();
if ($get_author_result) {
while ($author = mysqli_fetch_assoc($get_author_result)) {
$article['authors'][] = $author;
}
}
if ($article['priority'] == 'cover' && $covers_counter < 5) {
$covers_counter ++;
$get_stickers_sql = "SELECT for_stickers.*
FROM for_stickers
LEFT JOIN for_rel_relative
ON for_stickers.sticker_id = for_rel_relative.related_tag_id
LEFT JOIN for_rel_tags
ON for_rel_tags.tag_id = for_rel_relative.tag_id
LEFT JOIN for_articles
ON for_articles.article_id = for_rel_tags.article_id
WHERE for_articles.article_id = {$article['article_id']}
AND for_rel_relative.related_subtype = 'sticker'
GROUP BY sticker_id;";
$get_stickers_result = mysqli_query($link, $get_stickers_sql);
if ($get_stickers_result) {
while ($sticker = mysqli_fetch_assoc($get_stickers_result)) {
$article['stickers'][] = $sticker;
}
}
}
$articles[] = $article;
}
if (isset($_GET['tag']) && ! $get_suggest) {
/**
* Merge version tested.
* To display the aside info the id of the platform is not enough.
*/
if ($articles[0]['subtype'] == 'review' ||
$articles[0]['subtype'] == 'video') {
$get_platform_sql = "SELECT for_platforms.*
FROM for_platforms
WHERE platform_id = {$articles[0]['platform']};";
$get_platform_result = mysqli_query($link, $get_platform_sql);
if ($get_platform_result) {
$platform = mysqli_fetch_assoc($get_platform_result);
$articles[0]['version_tested'] = $platform;
}
}
/**
* Merge auhors.
*/
$get_authors_result = mysqli_query($link, $get_authors_sql);
if (! $get_authors_result) {
goto end;
}
while ($author = mysqli_fetch_assoc($get_author_result)) {
$articles[0]['authors'][] = $author;
}
/**
* Merge tags.
*/
$get_tags_result = mysqli_query($link, $get_tags_sql);
if (! $get_tags_result) {
goto end;
}
while ($tag = mysqli_fetch_assoc($get_tags_result)) {
$articles[0]['tags'][] = $tag;
if ($tag['prime'] == 1) {
$articles[0]['prime'] = $tag;
}
}
/**
* Merge layouts.
*/
$get_layouts_result = mysqli_query($link, $get_layouts_sql);
if (! $get_layouts_result) {
goto end;
}
while ($layout = mysqli_fetch_assoc($get_layouts_result)) {
$get_imgs_sql = "SELECT for_rel_imgs.*
FROM for_rel_imgs
WHERE layout_id = {$layout['layout_id']}
ORDER BY `order`;";
$get_imgs_result = mysqli_query($link, $get_imgs_sql);
if ($get_imgs_result) {
while ($img = mysqli_fetch_assoc($get_imgs_result)) {
$layout['imgs'][] = $img;
}
}
$articles[0]['layouts'][] = $layout;
}
/**
* Merge issue.
*/
$get_issue_result = mysqli_query($link, $get_issue_sql);
if (! $get_issue_result) {
goto end;
}
while ($issue = mysqli_fetch_assoc($get_issue_result)) {
/**
* Reset this to array and push all info about the issue.
*/
$articles[0]['issue'] = array();
$articles[0]['issue'][] = $issue;
}
}
end:
if (! $get_article_result || ! $get_authors_result || ! $get_tags_result ||
! $get_issue_result || ! $get_layouts_result || ! $get_imgs_result) {
$events['mysql']['result'] = false;
$events['mysql']['code'] = mysqli_errno($link);
$events['mysql']['error'] = mysqli_error($link);
} else {
$events['mysql']['result'] = true;
}
/**
* Send the response back to the browser in JSON format.
* And finally close the connection.
*/
echo json_encode(
array(
'articles' => $articles,
'events' => $events
));
mysqli_close($link);
}
?>