-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanageShareLists.php
184 lines (149 loc) · 5.71 KB
/
manageShareLists.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
<?php
require_once 'dbConfig.php';
require_once 'User.cls.php';
require_once 'SharedLists.cls.php';
require_once 'List.cls.php';
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");
try{
$connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$eData = file_get_contents("php://input");
$dData = json_decode($eData, true);
$operation = $dData['operation'];
function createShareWith(){
global $connection;
global $dData;
$list_id = $dData['listId'];
$email = $dData['email'];
if ($list_id !== '' && $email !== ''){
$user = new User();
$user->setEmail($email);
$user = $user->getUserByEmail($connection);
if ($user){
$userId = $user->getId();
$sharing = new SharedLists();
$sharing->setUser_id($userId);
$sharing->setList_id($list_id);
$isSharing = $sharing->getSharedLists($connection);
if ($isSharing){
echo json_encode("User already have access to this list!");
} else {
$newSharing = new SharedLists();
$newSharing->setUser_id($userId);
$newSharing->setList_id($list_id);
$result = $newSharing->create($connection);
if ($result){
echo json_encode("User now have access to your list!");
} else {
echo json_encode("An error occurred, try again!");
}
}
} else {
echo json_encode("User not found");
}
} else {
echo json_encode("Please enter a valid email");
}
}
function selectSharedWith(){
global $connection;
global $dData;
$list_id = $dData['list'];
if ($list_id !== ''){
$userId = new SharedLists();
$userId->setList_id($list_id);
$userId = $userId->getUserIdByListId($connection);
$users = [];
if ($userId) {
foreach ($userId as $oneUser) {
$u = new User();
$u->setId($oneUser['user_id']);
$userData = $u->getUserById($connection);
$users[] = [
'id' => $userData->getId(),
'name' => $userData->getName(),
'photo' => $userData->getPhoto()
];
}
}
echo json_encode($users);
}
}
function deleteSharedWith(){
global $connection;
global $dData;
$user_id = $dData['userId'];
$list_id = $dData['listId'];
$li = new SharedLists();
$li->setUser_id($user_id);
$li->setList_id($list_id);
$isDeleted = $li->delete($connection);
if ($isDeleted){
echo json_encode("success");
} else {
echo json_encode("An error occurred, try again!");
}
}
function getListsShared(){
global $connection;
global $dData;
$user_id = $dData['userId'];
$category = $dData['selectedCategory'];
$name = $dData['name'];
if ($user_id !== ''){
$listsId = new SharedLists();
$listsId->setUser_id($user_id);
$listsId = $listsId->getListIdByUserId($connection);
$lists = [];
if ($listsId) {
foreach ($listsId as $oneList) {
$l = new Lists();
$l->setId($oneList['list_id']);
// Check if category and name are not empty
if ($category !== '' && $name !== '') {
$l->setList_cat_id($category);
$l->setName($name);
$listData = $l->getListsByIdCategoryAndName($connection);
}
// Check if category is not empty and name is empty
else if ($category !== '' && $name === '') {
$l->setList_cat_id($category);
$listData = $l->getListsByIdAndCategory($connection);
}
// Check if category is empty and name is not empty
else if ($category === '' && $name !== '') {
$l->setName($name);
$listData = $l->getListsByIdAndName($connection);
}
// Default case if both category and name are empty
else {
$listData = $l->getListsById($connection);
}
if (!empty($listData)){
$lists[] = $listData;
}
}
echo json_encode($lists);
}
}
}
switch ($operation){
case 'create':
createShareWith();
break;
case 'select':
selectSharedWith();
break;
case 'delete':
deleteSharedWith();
break;
case 'listsShared':
getListsShared();
break;
}
} catch (PDOException $e){
$err = $connection->errorInfo();
echo json_encode($err);
}