-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_php_interface.php
More file actions
220 lines (208 loc) · 9.66 KB
/
js_php_interface.php
File metadata and controls
220 lines (208 loc) · 9.66 KB
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
<?php
include "functions.php";
$_POST = json_decode(file_get_contents('php://input'), true);
//Debug
/*
$response = array('post_dump' => var_export($_POST, true));
echo json_encode($response);
exit();
*/
try{
// ADD FUNCTIONS
if(isset($_POST['action']) && $_POST['action'] == 'add'){
if(isset($_POST['type'])&& $_POST['type'] == 'genre'){
$sth = $dbh->prepare("INSERT INTO user_pref_genre (user_id, genre_id) VALUES (:user_id, :genre_id)");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':genre_id', $_POST['pref_id']);
$sth->execute();
}
else if(isset($_POST['type']) && $_POST['type'] == 'mood'){
$sth = $dbh->prepare("INSERT INTO user_pref_mood (user_id, mood_id) VALUES (:user_id, :mood_id)");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':mood_id', $_POST['pref_id']);
$sth->execute();
}
$response = array('message' => 'Preference added successfully', 'status' => true);
echo json_encode($response);
exit();
}
//DELETE FUNCTIONS
elseif(isset($_POST['action']) && $_POST['action'] == 'delete'){
if(isset($_POST['type'])&& $_POST['type'] === 'genre'){
$sth = $dbh->prepare("DELETE FROM user_pref_genre WHERE user_id = :user_id and genre_id = :genre_id");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':genre_id', $_POST['pref_id']);
$sth->execute();
}
else if(isset($_POST['type']) && $_POST['type'] == 'mood'){
$sth = $dbh->prepare("DELETE FROM user_pref_mood WHERE user_id = :user_id and mood_id = :mood_id");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':mood_id', $_POST['pref_id']);
$sth->execute();
}
$response = array('message' => 'Preference deleted successfully', 'status' => true);
echo json_encode($response);
exit();
}
//UPDATE FUNCTIONS
elseif(isset($_POST['action']) && $_POST['action'] == 'update'){
if(isset($_POST['type'])&& $_POST['type'] === 'year'){
$sth = $dbh->prepare("UPDATE user_pref_gen SET oldest_track_year = :new_val WHERE user_id = :user_id");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':new_val', $_POST['value']);
$sth->execute();
//Update Session var
$_SESSION['year'] = $_POST['value'];
$response = array('message' => 'Preference updated successfully', 'status' => true);
echo json_encode($response);
exit();
}
else if(isset($_POST['type']) && $_POST['type'] == 'length'){
$sth = $dbh->prepare("UPDATE user_pref_gen SET playlist_length = :new_val WHERE user_id = :user_id");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':new_val', $_POST['value']);
$sth->execute();
//Update Session var
$_SESSION['list_len'] = $_POST['value'];
$response = array('message' => 'Preference updated successfully', 'status' => true);
echo json_encode($response);
exit();
}
else if(isset($_POST['type']) && $_POST['type'] == 'percentage'){
try{
$sth = $dbh->prepare("UPDATE user_pref_gen SET mood_weight_percentage = :new_perc WHERE user_id = :user_id");
$sth->bindParam(':user_id', $_SESSION['USER_ID']);
$sth->bindParam(':new_perc', $_POST['value']);
if($sth->execute()){
$response = array('status' => true, 'message' => 'Perc updated successfully');
$_SESSION['MOOD_WEIGHT'] = $_POST['value'];
}
else{
$response = array('status' => false, 'message' => 'Perc update failed');
}
echo json_encode($response);
exit();
}
catch(Exception $e)
{
$response = array('status' => false, 'message' => 'Percentage update failed');
echo json_encode($response);
exit();
}
}
else if(isset($_POST['type']) && $_POST['type'] == 'playlist'){
if(isset($_POST['state']) && $_POST['state'] == 'share')
{
$sth = $dbh->prepare("UPDATE playlists SET isshared = true WHERE playlist_id = :pid AND creator_id = :uid");
$sth->bindParam(':pid', $_POST['pid']);
$sth->bindParam(':uid', $_SESSION['USER_ID']);
$sth->execute();
$response = array('status' => true, 'message' => 'Playlist shared successfully');
echo json_encode($response);
exit();
}
else if(isset($_POST['state']) && $_POST['state'] == 'unshare')
{
$sth = $dbh->prepare("UPDATE playlists SET isshared = false WHERE playlist_id = :pid AND creator_id = :uid");
$sth->bindParam(':pid', $_POST['pid']);
$sth->bindParam(':uid', $_SESSION['USER_ID']);
$sth->execute();
$response = array('status' => true, 'message' => 'Playlist unshared successfully');
echo json_encode($response);
exit();
}
$response = array('message' => 'Playlist updated successfully', 'status' => true);
echo json_encode($response);
}
else if(isset($_POST['type']) && $_POST['type'] == 'user'){
try{
$sth = $dbh->prepare("UPDATE users SET username = :uname, email = :email WHERE user_id = :uid");
$sth->bindParam(':uname', $_POST['uname']);
$sth->bindParam(':email', $_POST['email']);
$sth->bindParam(':uid', $_SESSION['USER_ID']);
if($sth->execute()){
$response = array('status' => true, 'message' => 'User updated successfully');
$_SESSION['USER'] = $_POST['uname'];
$_SESSION['EMAIL'] = $_POST['email'];
}
else{
$response = array('status' => false, 'message' => 'User update failed');
}
echo json_encode($response);
exit();
}
catch(Exception $e)
{
$response = array('status' => false, 'message' => 'User update failed');
echo json_encode($response);
exit();
}
finally{
}
}
}
elseif(isset($_POST['action']) && $_POST['action'] == 'fill'){
if(isset($_POST['songlist'])){
try{
foreach ($_POST['songlist'] as $item){
$songsArray = $item['songsArray'];
$genres = $item['styles'];
$genreIds = [];
foreach($genres as $genre){
$sth = $dbh->prepare("SELECT genre_id FROM genres WHERE name = :genre");
$sth->bindParam(':genre', $genre);
$sth->execute();
$existingGenre = $sth->fetch();
if(!$existingGenre){
$sth = $dbh->prepare("INSERT INTO genres (name) VALUES (:genre) RETURNING genre_id");
$sth->bindParam(':genre', $genre);
$sth->execute();
$newGenre = $sth->fetch();
$genreIds[] = $newGenre->genre_id;
}else{
$genreIds[] = $existingGenre->genre_id;
}
}
$dbh->beginTransaction();
foreach($songsArray as $song)
{
$sth = $dbh->prepare("INSERT INTO tracks (title, ytlink, sclink, discogs, albumcoverlink, year) VALUES (:title, :ytlink, :sclink, :discogs, :albumcoverlink, :year) RETURNING track_id");
$sth->bindParam(':title', $song['title']);
$sth->bindParam(':ytlink', $song['ytlink']);
$sth->bindParam(':sclink', $song['sclink']);
$sth->bindParam(':discogs', $song['discogs']);
$sth->bindParam(':albumcoverlink', $song['cover']);
$sth->bindParam(':year', $song['year']);
$sth->execute();
$track = $sth->fetch();
$trackId = $track->track_id;
foreach ($genreIds as $genreId) {
$sth = $dbh->prepare("INSERT INTO track_is_genre (track_id, genre_id) VALUES (:track_id, :genre_id)");
$sth->bindParam(':track_id', $trackId);
$sth->bindParam(':genre_id', $genreId);
$sth->execute();
}
}
$dbh->commit();
}
}
catch(Exception $e){
$dbh->rollBack();
$error = array('error' => $e->getMessage(), 'status' => false, 'post_dump' => var_export($_POST, true));
echo json_encode($error);
exit();
}
$response = array('message' => 'Songs inserted successfully', 'status' => true);
echo json_encode($response);
exit();
}
}
else{
throw new Exception('Something went wrong');
}
}
catch(Exception $e){
$response = array('error' => $e->getMessage(), 'status' => false, 'post_dump' => var_export($_POST, true));
echo json_encode($response);
}
?>