/
api.php
257 lines (211 loc) · 6.3 KB
/
api.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
<?php
/* Name/NIM : Nicholas Posma Nasution Indra Nicolosi Waskita/18213041 */
/* Date : Tuesday, 24 November 2015 */
function putCSV($csv) {
$errors = array();
$file_name = $_FILES['csv']['name'];
$file_size =$_FILES['csv']['size'];
$file_tmp = $_FILES['csv']['tmp_name'];
$file_type = $_FILES['csv']['type'];
$file_ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$expensions = array("csv");
if(in_array($file_ext, $expensions) === false) {
$errors[] = "extension not allowed, please choose a CSV file.";
}
if($file_size > 2097152){
$errors[] = 'File size must be excately 2 MB';
}
if(empty($errors) == true){
$columnArray = array();
$dataArray = array();
$firstRule = true;
$handle = fopen($csv, "r");
while($data = fgetcsv($handle, 1000, ",")) {
if($firstRule) {
foreach($data as $columnName) {
$columnArray[] = $columnName;
}
$firstRule = false;
}else {
$rule = array();
for($i = 0; $i < count($data) ; $i++) {
$rule[$columnArray[$i]] = $data[$i];
}
$dataArray[] = $rule;
}
}
$rows = "Fail";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbprogif";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
foreach($dataArray as $data) {
foreach($data as $element) {
$query = "INSERT INTO `detil_event1` (" .implode(',', $columnArray). ")
VALUES (" .$element. ")";
mysqli_query($conn, $query) or die(mysql_error());
}
}
$rows = "Success";
}
$conn->close();
fclose($handle);
} else {
$rows = "Fail";
}
return $rows;
}
function getCalendar($year, $month) {
include('simple_html_dom.php');
$homepage = file_get_html('http://www.itb.ac.id/calendar/monthly/' . $year . $month .'/');
foreach ($homepage->find ('.span8b') as $e)
echo $e->innertext.'<br>';
$rows = " Kalender Akademik ITB " ;
return $rows;
}
function searchEvent($criteria) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbprogif";
$conn = new mysqli($servername, $username, $password, $dbname);
$rows = array();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$rows = "fail";
} else {
$sql = "SELECT id_event, nama, tanggal, lokasi, biaya, rating FROM detil_event1 WHERE nama LIKE '%" . $criteria . "%' OR deskripsi LIKE '%" . $criteria ."%'";
$result = $conn->query($sql);
while($r = $result->fetch_assoc()){
$rows[] = array('data' => $r);
}
}
$conn->close();
return $rows;
}
function sortEvent($criteria, $order) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbprogif";
$conn = new mysqli($servername, $username, $password, $dbname);
$rows = array();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$rows = "fail";
} else {
$sql = "SELECT id_event, nama, tanggal, lokasi, biaya, rating FROM detil_event1 ORDER BY $criteria" . " " . $order;
$result = $conn->query($sql);
while($r = $result->fetch_assoc()){
$rows[] = array('data' => $r);
}
}
$conn->close();
return $rows;
}
function filterEvent($year, $month, $target, $location) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbprogif";
$conn = new mysqli($servername, $username, $password, $dbname);
$rows = array();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$rows = "fail";
} else {
$sql = "SELECT id_event, nama, tanggal, lokasi, biaya, rating FROM detil_event1 ";
if(empty($year)) {
if(empty($month)) {
if(empty($target)) {
if(empty($location)) {
} else {
$sql = $sql . "WHERE SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
} else {
$sql = $sql . "WHERE target_peserta IN ($target) ";
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
}
} else {
$sql = $sql . "WHERE Month(tanggal) IN (" . mysql_real_escape_string($month) . ") ";
if(empty($target)) {
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
} else {
$sql = $sql . "AND target_peserta IN ($target) ";
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
}
}
} else {
$sql = $sql . "WHERE Year(tanggal) IN (" . mysql_real_escape_string($year) . ") ";
if(empty($month)) {
if(empty($target)) {
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
} else {
$sql = $sql . "AND target_peserta IN ($target) ";
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
}
} else {
$sql = $sql . "AND Month(tanggal) IN (" . mysql_real_escape_string($month) . ") ";
if(empty($target)) {
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
} else {
$sql = $sql . "AND target_peserta IN ($target) ";
if(empty($location)) {
} else {
$sql = $sql . "AND SUBSTRING_INDEX(lokasi, ' ', 1) IN ($location) ";
}
}
}
}
$result = $conn->query($sql);
while($r = $result->fetch_assoc()){
$rows[] = array('data' => $r);
}
}
$conn->close();
return $rows;
}
if(isset($_GET["action"])) {
switch($_GET["action"]) {
case "search";
$rows = searchEvent($_GET["criteria"]);
break;
case "sort";
$rows = sortEvent($_GET["criteria"], $_GET["order"]);
break;
case "filter";
$rows = filterEvent($_GET["year"], $_GET["month"], $_GET["target"], $_GET["location"]);
break;
case "calendar";
$rows = getCalendar($_GET["year"], $_GET["month"]);
break;
}
exit(json_encode($rows));
} else {
$rows = putCSV($_FILES['csv']['tmp_name']);
header("Location: " . $_SERVER['HTTP_REFERER']);
exit();
}
?>