-
Notifications
You must be signed in to change notification settings - Fork 0
/
upVote.php
52 lines (42 loc) · 1.19 KB
/
upVote.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
<?php
require_once 'core/init.php';
$user = new User();
$data = $user->data();
if($user->hasPermission('banned')){
exit("you are banned");
}
if(!$user->isLoggedIn()){
Redirect::to('index.php');
exit("you are not logged in");
}
$voted = false;
if(!$postId = Input::get('post')){
exit("Nothing to vote!");
}else{
$like = DB::getInstance()->query("SELECT * FROM likes WHERE postID = ? AND userID = ?", array($postId, $data->id));
if($like->count() != 0){ // returns 5
$voted = true;
}
if($voted == true){ // they want to - vote
if(DB::getInstance()->query("DELETE FROM likes WHERE postID = ? AND userID = ?", array($postId, $data->id))){
DB::getInstance()->query("UPDATE posts SET like_count = like_count - 1 WHERE id = ?", array($postId));
exit("DOWN: voted");
};
}
if($voted == false){ // they haven't voted
if(DB::getInstance()->query("UPDATE posts SET like_count = like_count + 1 WHERE id = ?", array($postId))){
try {
$user->create('like','likes',array(
'postID' => $postId,
'userID' => $data->id
));
} catch(Exception $e){
//delete vote
die($e->getMessage());
}
echo "YES: count Updated";
}else{
echo "Sorry no upvote";
}
}
}