-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfront.php
292 lines (256 loc) · 10.6 KB
/
front.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
<?php
// warning messages
//error_reporting(E_ALL);
//ini_set('display_errors', true);
// include library
include 'lib/utils.php';
$recentk = 3;
// get the recently added articles
function get_recentk_articles() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get recent 3 articles
$sql = 'select spu_id, unix_timestamp(timestamp) as time from spu_versions where version = 1 order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading versions table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
}
mysqli_free_result($result);
// get last updated timestamp
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from spu_versions where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\') group by spu_id';
$result = mysqli_query($cid, $sql);
if(!$result) {echo '<h3>Error retrieving version table!</h3>'; return;}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$time = $row['time'];
$spus[$spu_id]['last_updated'] = Timesince($time);
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank ' .
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$spu_id = $row['spu_id'];
$rank = $row['rank'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close connection
mysqli_close($cid);
// return result
return json_encode($spus);
}
// get articles which has recent likes
function get_recentk_likes() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get recent likes
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from likes group by spu_id order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading like table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank '.
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$spu_id = $row['spu_id'];
$rank = $row['rank'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close connection
mysqli_close($cid);
// return result
return json_encode($spus);
}
// get articles which has recent likes
function get_recentk_comments() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
global $recentk;
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get recent comments
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from comments group by spu_id order by time desc limit ' . $recentk;
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading like table!'));}
$spus = array();
$latest = 1;
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$timestamp = $row['time'];
$spus[$spu_id] = array('id' => $spu_id, 'timestamp' => Timesince($timestamp), 'rank' => $latest);
$latest++;
}
mysqli_free_result($result);
// get title of the articles
$sql = 'select spu_id, title from spu where spu_id in (\'' . implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading article table!'));}
while($row = mysqli_fetch_assoc($result)) {
$spu_id = $row['spu_id'];
$title = $row['title'];
if(!isset($spus[$spu_id])) continue;
$spus[$spu_id]['title'] = $title;
}
mysqli_free_result($result);
// get authors
$sql = 'select aship.spu_id as spu_id, aship.author_id as author_id, concat(a.given_name, \' \', a.last_name) as name, aship.rank ' .
'from authorships aship, authors a where aship.author_id = a.author_id and aship.spu_id in (\'' .
implode('\', \'', array_keys($spus)) . '\')';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading authors table!'));}
while($row = mysqli_fetch_assoc($result)) {
$author_id = $row['author_id'];
$author_name = $row['name'];
$spu_id = $row['spu_id'];
$rank = $row['rank'];
$authors = array();
if(isset($spus[$spu_id]['authors'])) $authors = $spus[$spu_id]['authors'];
$authors[$author_id] = array('id' => $author_id, 'name' => $author_name, 'rank' => $rank);
$spus[$spu_id]['authors'] = $authors;
}
mysqli_free_result($result);
// close database
mysqli_close($cid);
// return result
return json_encode($spus);
}
?>
<link rel="stylesheet" type="text/css" href="css/front.css">
<script language="javascript" type="text/javascript" charset="utf-8">
$(function() {
// generate recent articles
var articles = <?php echo get_recentk_articles(); ?>;
if(!articles) $('#article_tab').css('display', 'none');
else format_articles($('#recent_articles'), 'article', articles);
// generate recent likes
var likes = <?php echo get_recentk_likes(); ?>;
if(!likes) $('#likes_tab').css('display', 'none');
else format_articles($('#recent_likes'), 'like', likes);
// generate recent comments
var comments = <?php echo get_recentk_comments(); ?>;
if(!comments) $('#comments_tab').css('display', 'none');
else format_articles($('#recent_comments'), 'comment', comments);
var sidebar_height = $('#sidebar').height();
var content_height = $('#main').height();
if(sidebar_height < content_height) $('#sidebar').css('height', content_height + 'px');
});
function format_articles(elm, type, map) {
var arr = new Array();
for(var id in map) arr.push(map[id]);
arr.sort(function(a, b) {
var rank_a = a['rank'];
var rank_b = b['rank'];
return rank_a - rank_b;
});
for(var idx in arr) {
var obj = arr[idx];
var id = obj['id'];
var title = obj['title'];
var timestamp = obj['timestamp'];
var lastUpdate = obj['last_update'];
var author_map = obj['authors'];
var authors = new Array();
var author_arr = new Array();
for(var i in author_map) {author_arr.push(author_map[i]);}
author_arr.sort(function(a, b) {return a.rank - b.rank;});
for(var i in author_arr) {
var author = author_arr[i];
var author_id = author['id'];
var author_name = author['name'];
authors.push('<a href=\'author_detail.php?id=\'' + author_id + '>' + author_name + '</a>');
}
authors = authors.join(', ');
var p = '';
if(type == 'article') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>First uploaded ' + timestamp + '</p>';
if(lastUpdate && (lastUpdate != '') && (lastUpdate != timestamp))
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>First uploaded ' + timestamp + '<br>Last updated on ' + lastUpdate + '</p>';
}
else if(type == 'like') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>Last like ' + timestamp + '</p>';
}
else if(type == 'comment') {
p = '<div class=\'spu_box\'><p><a href=\'spu_detail.php?id=' + id + '\'>' + title + '</a><br>Authors: ' + authors + '<br>Last commented ' + timestamp + '</p>';
}
$(elm).append(p).append('<br>');
}
}
</script>
<div id='article_tab'><h4>Recent uploaded articles</h4><div id='recent_articles'></div></div>
<div id='likes_tab'><h4>Recent liked articles</h4><div id='recent_likes'></div></div>
<div id='comment_tab'><h4>Recent commented articles</h4><div id='recent_comments'></div></div>