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