forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomments_like.php
152 lines (137 loc) · 5.17 KB
/
comments_like.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
<?php
global $global, $config;
if(!isset($global['systemRootPath'])){
require_once '../videos/configuration.php';
}
require_once $global['systemRootPath'].'objects/user.php';
class CommentsLike {
private $id;
private $like;
private $comments_id;
private $users_id;
function __construct($like, $comments_id) {
if(!User::isLogged()){
header('Content-Type: application/json');
die('{"error":"'.__("Permission denied").'"}');
}
$this->comments_id = $comments_id;
$this->users_id = User::getId();
$this->load();
// if click again in the same vote, remove the vote
if ($this->like == $like) {
$like = 0;
}
$this->setLike($like);
$this->save();
}
private function setLike($like) {
$like = intval($like);
if(!in_array($like, array(0,1,-1))){
$like = 0;
}
$this->like = $like;
}
private function load() {
$like = $this->getLike();
if (empty($like)) {
return false;
}
foreach ($like as $key => $value) {
$this->$key = $value;
}
}
private function getLike() {
global $global;
if (empty($this->users_id) || empty($this->comments_id)) {
header('Content-Type: application/json');
die('{"error":"You must have user and videos set to get a like"}');
}
$sql = "SELECT * FROM comments_likes WHERE users_id = ? AND comments_id = ? LIMIT 1";
$res = sqlDAL::readSql($sql,"ii",array($this->users_id,$this->comments_id));
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
return ($res) ? $result : false;
}
private function save() {
global $global;
if(!User::isLogged()){
header('Content-Type: application/json');
die('{"error":"'.__("Permission denied").'"}');
}
$formats = "";
$values = array();
if (!empty($this->id)) {
$sql = "UPDATE comments_likes SET `like` = ?, modified = now() WHERE id = ?";
$formats = "ii";
$values = array($this->like,$this->id);
} else {
$sql = "INSERT INTO comments_likes ( `like`,users_id, comments_id, created, modified) VALUES (?, ?, ?, now(), now())";
$formats = "iii";
$values = array($this->like,$this->users_id,$this->comments_id);
}
return sqlDAL::writeSql($sql,$formats,$values);
}
static function getLikes($comments_id) {
global $global;
$obj = new stdClass();
$obj->comments_id = $comments_id;
$obj->likes = 0;
$obj->dislikes = 0;
$obj->myVote = self::getMyVote($comments_id);
$sql = "SELECT count(*) as total FROM comments_likes WHERE comments_id = ? AND `like` = 1 "; // like
$res = sqlDAL::readSql($sql,"i",array($comments_id));
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if (!$res) {
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
$obj->likes = intval($result['total']);
$sql = "SELECT count(*) as total FROM comments_likes WHERE comments_id = ? AND `like` = -1 "; // dislike
$res = sqlDAL::readSql($sql,"i",array($comments_id));
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if (!$res) {
die($sql.'\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
$obj->dislikes = intval($result['total']);
return $obj;
}
static function getTotalLikes() {
global $global;
$obj = new stdClass();
$obj->likes = 0;
$obj->dislikes = 0;
$sql = "SELECT count(*) as total FROM comments_likes WHERE `like` = 1 "; // like
$res = sqlDAL::readSql($sql);
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if (!$res) {
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
$obj->likes = intval($result['total']);
$sql = "SELECT count(*) as total FROM comments_likes WHERE `like` = -1 "; // dislike
$res = sqlDAL::readSql($sql);
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if (!$res) {
die($sql.'\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
$obj->dislikes = intval($result['total']);
return $obj;
}
static function getMyVote($comments_id) {
global $global;
if (!User::isLogged()) {
return 0;
}
$id = User::getId();
$sql = "SELECT `like` FROM comments_likes WHERE comments_id = ? AND users_id = ? "; // like
$res = sqlDAL::readSql($sql,"ii",array($comments_id,$id));
$result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if (!empty($result)) {
return intval($result['like']);
}
return 0;
}
}