forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.php
341 lines (302 loc) · 13.2 KB
/
subscribe.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
global $global, $config;
if (!isset($global['systemRootPath'])) {
require_once '../videos/configuration.php';
}
require_once $global['systemRootPath'] . 'objects/bootGrid.php';
require_once $global['systemRootPath'] . 'objects/user.php';
class Subscribe {
private $id;
private $email;
private $status;
private $ip;
private $users_id;
private $notify;
private $subscriber_users_id;
function __construct($id, $email = "", $user_id = "", $subscriber_users_id = "") {
if (!empty($id)) {
$this->load($id);
}
if (!empty($subscriber_users_id)) {
$this->email = $email;
$this->users_id = $user_id;
$this->subscriber_users_id = $subscriber_users_id;
if (empty($this->id)) {
$this->loadFromId($this->subscriber_users_id, $user_id, "");
}
}
}
private function load($id) {
$obj = self::getSubscribe($id);
if (empty($obj))
return false;
foreach ($obj as $key => $value) {
$this->$key = $value;
}
return true;
}
private function loadFromEmail($email, $user_id, $status = "a") {
$obj = self::getSubscribeFromEmail($email, $user_id, $status);
if (empty($obj))
return false;
foreach ($obj as $key => $value) {
$this->$key = $value;
}
return true;
}
private function loadFromId($subscriber_users_id, $user_id, $status = "a") {
$obj = self::getSubscribeFromID($subscriber_users_id, $user_id, $status);
if (empty($obj))
return false;
foreach ($obj as $key => $value) {
$this->$key = $value;
}
return true;
}
function save() {
global $global;
if (!empty($this->id)) {
$sql = "UPDATE subscribes SET status = '{$this->status}', notify = '{$this->notify}',ip = '" . getRealIpAddr() . "', modified = now() WHERE id = {$this->id}";
} else {
$sql = "INSERT INTO subscribes ( users_id, email,status,ip, created, modified, subscriber_users_id) VALUES ('{$this->users_id}','{$this->email}', 'a', '" . getRealIpAddr() . "',now(), now(), '$this->subscriber_users_id')";
}
return sqlDAL::writeSql($sql);
}
static function getSubscribe($id) {
global $global;
$id = intval($id);
$sql = "SELECT * FROM subscribes WHERE id = $id LIMIT 1";
$res = sqlDAL::readSql($sql, "i", array($id));
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res != false) {
$subscribe = $data;
} else {
$subscribe = false;
}
return $subscribe;
}
static function getSubscribeFromEmail($email, $user_id, $status = "a") {
global $global;
$status = str_replace("'","", $status);
$sql = "SELECT * FROM subscribes WHERE email = '$email' AND users_id = {$user_id} ";
if (!empty($status)) {
$sql .= " AND status = '{$status}' ";
}
$sql .= " LIMIT 1";
$res = sqlDAL::readSql($sql);
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res != false) {
$subscribe = $data;
} else {
$subscribe = false;
}
return $subscribe;
}
static function getSubscribeFromID($subscriber_users_id, $user_id, $status = "a") {
global $global;
$status = str_replace("'","", $status);
$sql = "SELECT * FROM subscribes WHERE subscriber_users_id = '$subscriber_users_id' AND users_id = {$user_id} ";
if (!empty($status)) {
$sql .= " AND status = '{$status}' ";
}
$sql .= " LIMIT 1";
$res = sqlDAL::readSql($sql);
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res != false) {
$subscribe = $data;
} else {
$subscribe = false;
}
return $subscribe;
}
/**
* return all subscribers that has subscribe to an user channel
* @global type $global
* @param type $user_id
* @return boolean
*/
static function getAllSubscribes($user_id = "", $status = "a") {
global $global;
$status = str_replace("'","", $status);
$sql = "SELECT subscriber_users_id as subscriber_id, s.id, s.status, s.ip, s.users_id, s.notify, "
. " s.subscriber_users_id , s.created , s.modified, suId.email as email, suId.emailVerified as emailVerified FROM subscribes as s "
//. " LEFT JOIN users as su ON s.email = su.email "
. " LEFT JOIN users as suId ON suId.id = s.subscriber_users_id "
. " LEFT JOIN users as u ON users_id = u.id WHERE 1=1 AND subscriber_users_id > 0 ";
if (!empty($user_id)) {
$sql .= " AND users_id = {$user_id} ";
}
if (!empty($status)) {
$sql .= " AND u.status = '{$status}' ";
//$sql .= " AND su.status = '{$status}' ";
}
//$sql .= " GROUP BY subscriber_id ";
$sql .= BootGrid::getSqlFromPost(array('email'));
$res = sqlDAL::readSql($sql);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$subscribe = array();
if ($res != false) {
$emails = array();
foreach ($fullData as $row) {
if (in_array($row['email'], $emails)) {
//continue;
}
$emails[] = $row['email'];
$row['identification'] = User::getNameIdentificationById($row['subscriber_id']);
if ($row['identification'] === __("Unknown User")) {
$row['identification'] = $row['email'];
}
$row['backgroundURL'] = User::getBackground($row['subscriber_id']);
$row['photoURL'] = User::getPhoto($row['subscriber_id']);
$subscribe[] = $row;
}
//$subscribe = $res->fetch_all(MYSQLI_ASSOC);
} else {
$subscribe = false;
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $subscribe;
}
/**
* return all channels that a user has subscribed
* @global type $global
* @param type $user_id
* @return boolean
*/
static function getSubscribedChannels($user_id) {
global $global;
$sql = "SELECT s.* FROM subscribes as s WHERE status = 'a' AND subscriber_users_id = ? ";
$res = sqlDAL::readSql($sql, "i", array($user_id));
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$subscribe = array();
if ($res != false) {
foreach ($fullData as $row) {
if($row['users_id']==$user_id){
continue;
}
$row['identification'] = User::getNameIdentificationById($row['users_id']);
if ($row['identification'] === __("Unknown User")) {
$row['identification'] = $row['email'];
}
$row['backgroundURL'] = User::getBackground($row['users_id']);
$row['photoURL'] = User::getPhoto($row['users_id']);
$subscribe[] = $row;
}
//$subscribe = $res->fetch_all(MYSQLI_ASSOC);
} else {
$subscribe = false;
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $subscribe;
}
static function getTotalSubscribes($user_id = "") {
global $global;
$sql = "SELECT id FROM subscribes WHERE status = 'a' AND subscriber_users_id > 0 ";
if (!empty($user_id)) {
$sql .= " AND users_id = '{$user_id}' ";
}
$sql .= BootGrid::getSqlSearchFromPost(array('email'));
$res = sqlDAL::readSql($sql);
$numRows = sqlDAL::num_rows($res);
sqlDAL::close($res);
return $numRows;
}
function toggle() {
if (empty($this->status) || $this->status == "i") {
$this->status = 'a';
} else {
$this->status = 'i';
}
$this->save();
}
function notifyToggle() {
if (empty($this->notify)) {
$this->notify = 1;
} else {
$this->notify = 0;
}
$this->save();
}
function getStatus() {
return $this->status;
}
function getNotify() {
return $this->notify;
}
function setNotify($notify) {
$this->notify = $notify;
}
static function getButton($user_id) {
global $global, $advancedCustom;
if(!empty($advancedCustom->removeSubscribeButton)){
return "";
}
$total = static::getTotalSubscribes($user_id);
$subscribe = "<div class=\"btn-group\" >"
. "<button class='btn btn-xs subsB subs{$user_id} subscribeButton{$user_id}' "
. "title=\"" . __("Want to subscribe to this channel?") . "\" "
. "data-content=\"" . __("Sign in to subscribe to this channel") . "<hr><center><a class='btn btn-success btn-sm' href='{$global['webSiteRootURL']}user'>" . __("Sign in") . "</a></center>\" "
. "tabindex=\"0\" role=\"button\" data-html=\"true\" data-toggle=\"popover\" data-placement=\"bottom\" ><i class='fas fa-play-circle'></i> <b class='text'>" . __("Subscribe") . "</b></button>"
. "<button class='btn btn-xs subsB subs{$user_id}'><b class='textTotal{$user_id}'>{$total}</b></button>"
. "</div>";
//show subscribe button with mail field
$popover = "";
$script = "";
if (User::isLogged()) {
//check if the email is logged
$email = User::getMail();
$subs = Subscribe::getSubscribeFromID(User::getId(), $user_id);
$popover = "<input type=\"hidden\" placeholder=\"E-mail\" class=\"form-control\" id=\"subscribeEmail{$user_id}\" value=\"{$email}\">";
// show unsubscribe Button
$subscribe = "<div class=\"btn-group\">";
if(!empty($subs) && $subs['status']==='a'){
$subscribe .= "<button class='btn btn-xs subsB subscribeButton{$user_id} subscribed subs{$user_id}'><i class='fas fa-play-circle'></i> <b class='text'>" . __("Subscribed") . "</b></button>";
$subscribe .= "<button class='btn btn-xs subsB subscribed subs{$user_id}'><b class='textTotal{$user_id}'>$total</b></button>";
}else{
$subscribe .= "<button class='btn btn-xs subsB subscribeButton{$user_id} subs{$user_id}'><i class='fas fa-play-circle'></i> <b class='text'>" . __("Subscribe") . "</b></button>";
$subscribe .= "<button class='btn btn-xs subsB subs{$user_id}'><b class='textTotal{$user_id}'>$total</b></button>";
}
$subscribe .= "</div>";
if (!empty($subs['notify'])) {
$notify = '';
$notNotify = 'hidden';
} else {
$notify = 'hidden';
$notNotify = '';
}
$subscribe .= '<span class=" notify' . $user_id . ' ' . $notify . '"><button onclick="toogleNotify' . $user_id . '();" class="btn btn-default btn-xs " data-toggle="tooltip"
title="' . __("Stop getting notified for every new video") . '">
<i class="fa fa-bell" ></i>
</button></span><span class=" notNotify' . $user_id . ' ' . $notNotify . '"><button onclick="toogleNotify' . $user_id . '();" class="btn btn-default btn-xs " data-toggle="tooltip"
title="' . __("Get notified for every new video") . '">
<i class="fa fa-bell-slash"></i>
</button></span>';
$script = "<script>
function toogleNotify{$user_id}(){
email = $('#subscribeEmail{$user_id}').val();
subscribeNotify(email, '{$user_id}');
}
$(document).ready(function () {
$(\".subscribeButton{$user_id}\").off(\"click\");
$(\".subscribeButton{$user_id}\").click(function () {
email = $('#subscribeEmail{$user_id}').val();
subscribe(email, '{$user_id}');
});
$('[data-toggle=\"tooltip\"]').tooltip();
});
</script>";
}
return $subscribe . $popover . $script;
}
function getSubscriber_users_id() {
return $this->subscriber_users_id;
}
function setSubscriber_users_id($subscriber_users_id) {
$this->subscriber_users_id = $subscriber_users_id;
}
}