-
Notifications
You must be signed in to change notification settings - Fork 0
/
following.php
323 lines (282 loc) · 10.6 KB
/
following.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
<?php
// display error messages
error_reporting(E_ALL);
ini_set('display_errors', true);
// include library
include 'lib/utils.php';
// get current user id
if(isset($_COOKIE['author_id'])) $author_id = $_COOKIE['author_id'];
if(!isset($author_id)) $author_id = '';
function get_follow_users() {
global $author_id;
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) {echo json_encode(array('error_msg' => 'Error connecting to database!')); return;}
// set character set
mysqli_set_charset($cid, 'utf8');
$sql = 'select f.to_id, max(f.timestamp) as time, f.is_active, concat(a.given_name, \' \', a.last_name) as name from following f, authors a ' .
'where f.from_id = \'' . $author_id . '\' and f.to_id = a.author_id group by f.from_id, f.to_id having f.is_active = 1';
$result = mysqli_query($cid, $sql);
if(!$result) {echo json_encode(array('error_msg' => 'Error executing query!')); return;}
$following = array();
while($row = mysqli_fetch_assoc($result)) {
$fid = $row['to_id'];
$time = $row['time'];
$name = $row['name'];
$following[$fid] = array('id' => $fid, 'name' => $name, 'time' => $time);
}
mysqli_free_result($result);
// send result to client
echo json_encode($following);
// close connection
mysqli_close($cid);
}
function get_commented_articles() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
ob_start();
get_follow_users();
$followees = ob_get_contents();
ob_end_clean();
if(!isset($followees)) {
echo json_encode(array());
return;
}
$followees = json_decode($followees, true);
if(!$followees) {
echo json_encode(array());
return;
}
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get commented articles
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from comments where author_id in (\'' . implode('\', \'', array_keys($followees)) . '\') group by spu_id order by timestamp desc';
$result = mysqli_query($cid, $sql);
if(!$result) {return json_encode(array('error_msg' => 'Error reading like table!'));}
$latest = 1;
$spus = array();
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);
if(count($spus) == 0) {
echo json_encode(array());
return;
}
// 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);
}
// get articles which has recent likes
function get_liked_articles() {
global $dbhost;
global $dbusername;
global $dbpassword;
global $dbname;
ob_start();
get_follow_users();
$followees = ob_get_contents();
ob_end_clean();
if(!isset($followees)) {
echo json_encode(array());
return;
}
$followees = json_decode($followees, true);
if(!$followees) {
echo json_encode(array());
return;
}
// connect to database
$cid = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
if(!$cid) return;
// set character set
mysqli_set_charset($cid, 'utf8');
// get liked articles
$sql = 'select spu_id, max(unix_timestamp(timestamp)) as time from likes where author_id in (\'' . implode('\', \'', array_keys($followees)) . '\') group by spu_id order by timestamp desc';
$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);
}
?>
<html>
<head>
<title>openIvory - Following</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/index.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/bootstrap.min.css" />
<script language='javascript' type='text/javascript' charset='utf-8' src='js/jquery-1.8.3.min.js'></script>
<script language='javascript' type='text/javascript' charset='utf-8'>
$(function() {
// load header
$('#header').load('header.php');
// load sidebar
$('#sidebar').load('sidebar.php?' + new Date().getTime());
// load list of authors
var div = $('<div></div>');
div.append('<h3>Authors I follow</h3>');
var following = <?php echo get_follow_users(); ?>;
var authors = '';
if(size(following) == 0) authors = '<h5><i>No following any authors!</i></h5>';
else if(following['error_msg']) authors = '<h5><i>' + following['error_msg'] + '</i></h5>';
else {
var idx = 1;
for(var id in following) {
var name = following[id]['name'];
authors += '<div style=\'margin:10px;display:inline-block\'>' + /*(idx++) +*/ '<a href=\'author_detail.php?id=' + id + '\'>' + name + '</a></div>';
}
}
div.append(authors);
$('#main').append(div);
// add articles commented by the people whom are followed by the user
var commented = <?php echo get_commented_articles(); ?>;
if(size(commented) > 0) {
var comment_div = $('<div></div>');
comment_div.append('<h3>Commented articles by people I follow</h3>');
format_articles(comment_div, 'comment', commented);
$('#main').append(comment_div);
}
// add articles liked by the people whom are followed by the user
var liked = <?php echo get_liked_articles(); ?>;
if(size(liked) > 0) {
var liked_div = $('<div></div>');
liked_div.append('<h3>Liked articles by people I follow</h3>');
format_articles(liked_div, 'like', liked);
$('#main').append(liked_div);
}
});
/* format article */
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>');
}
}
/* function to count size of array / associative array */
function size(arr) {
var count = 0;
for(var i in arr) count++;
return count;
}
</script>
</head>
<body>
<div id="header"></div> <!-- header -->
<div id="sidebar"></div> <!-- sidebar -->
<div id="main"></div> <!-- main -->
</body>
</html>