|
@@ -10,7 +10,15 @@
|
10
|
10
|
|
11
|
11
|
|
12
|
12
|
$id = mysqli_real_escape_string($connection, trim($_POST['id']));
|
|
13
|
+ $newTitle = mysqli_real_escape_string($connection, trim($_POST['newTitle']));
|
|
14
|
+ $newDescription = mysqli_real_escape_string($connection, trim($_POST['newDescription']));
|
|
15
|
+ $newType = mysqli_real_escape_string($connection, trim($_POST['newType']));
|
|
16
|
+ $newStart = mysqli_real_escape_string($connection, trim($_POST['newStart']));
|
|
17
|
+ $newEnd = mysqli_real_escape_string($connection, trim($_POST['newEnd']));
|
13
|
18
|
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+ // INSPECT EXPERIENCE ID
|
14
|
22
|
// Check that experience ID is not empty string
|
15
|
23
|
// And that it's registered in the database
|
16
|
24
|
if($id === "") {
|
|
@@ -22,84 +30,137 @@
|
22
|
30
|
echo json_encode(array("error" => "Given experience ID ($id) not in database."));
|
23
|
31
|
exit();
|
24
|
32
|
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+ // INSPECT TITLE
|
|
37
|
+ // Check that experience title is not empty
|
|
38
|
+ // And that it's less than 60 characters in length (database limit)
|
|
39
|
+ if($newTitle === "") {
|
|
40
|
+ http_response_code(400);
|
|
41
|
+ echo json_encode(array("error" => "Please specify title."));
|
|
42
|
+ exit();
|
|
43
|
+ } else if(mb_strlen($newTitle) > 60) {
|
|
44
|
+ http_response_code(400);
|
|
45
|
+ echo json_encode(array("error" => "Title too long (max. is 60 characters)."));
|
|
46
|
+ exit();
|
|
47
|
+ }
|
|
48
|
+
|
25
|
49
|
|
26
|
|
-
|
27
|
|
- // UPDATE TITLE
|
28
|
|
- if(isset($_POST['newTitle'])) {
|
29
|
50
|
|
30
|
|
- $newTitle = mysqli_real_escape_string($connection, trim($_POST['newTitle']));
|
31
|
|
-
|
32
|
|
- // Check that experience title is not empty
|
33
|
|
- // And that it's less than 60 characters in length (database limit)
|
34
|
|
- if($newTitle === "") {
|
35
|
|
- http_response_code(400);
|
36
|
|
- echo json_encode(array("error" => "Please specify title."));
|
37
|
|
- exit();
|
38
|
|
- } else if(mb_strlen($newTitle) > 60) {
|
39
|
|
- http_response_code(400);
|
40
|
|
- echo json_encode(array("error" => "Title too long (max. is 60 characters)."));
|
41
|
|
- exit();
|
42
|
|
- }
|
|
51
|
+ // INSPECT DESCRIPTION
|
|
52
|
+ // Check that experience title is not empty
|
|
53
|
+ // And that it's less than 60 characters in length (database limit)
|
|
54
|
+ if($newDescription === "") {
|
|
55
|
+ http_response_code(400);
|
|
56
|
+ echo json_encode(array("error" => "Please specify description."));
|
|
57
|
+ exit();
|
|
58
|
+ } else if(mb_strlen($newDescription) > 100) {
|
|
59
|
+ http_response_code(400);
|
|
60
|
+ echo json_encode(array("error" => "Description too long (max. is 100 characters)."));
|
|
61
|
+ exit();
|
|
62
|
+ }
|
43
|
63
|
|
44
|
|
- $query = "UPDATE `experience` SET `title` = '$newTitle' WHERE `id` = '$id';";
|
45
|
|
- $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
|
64
|
+
|
46
|
65
|
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ // INSPECT TYPE
|
|
69
|
+ // Check that experience type is not empty
|
|
70
|
+ // And that it's either Test, CBRE or URE
|
|
71
|
+ if($newType === "") {
|
|
72
|
+ http_response_code(400);
|
|
73
|
+ echo json_encode(array("error" => "Please specify type."));
|
|
74
|
+ exit();
|
|
75
|
+ } else if($newType !== 'Course-Based Research Experience' AND $newType !== 'Undergraduate Research Experience' AND $newType !== 'Test') {
|
|
76
|
+ http_response_code(400);
|
|
77
|
+ echo json_encode(array("error" => "Invalid type ($newType)."));
|
|
78
|
+ exit();
|
47
|
79
|
}
|
48
|
80
|
|
49
|
|
- // UPDATE DESCRIPTION
|
50
|
|
- if(isset($_POST['newDescription'])) {
|
51
|
81
|
|
52
|
|
- $newDescription = mysqli_real_escape_string($connection, trim($_POST['newDescription']));
|
53
|
|
-
|
54
|
|
- // Check that experience title is not empty
|
55
|
|
- // And that it's less than 60 characters in length (database limit)
|
56
|
|
- if($newDescription === "") {
|
57
|
|
- http_response_code(400);
|
58
|
|
- echo json_encode(array("error" => "Please specify description."));
|
59
|
|
- exit();
|
60
|
|
- } else if(mb_strlen($newDescription) > 100) {
|
61
|
|
- http_response_code(400);
|
62
|
|
- echo json_encode(array("error" => "Description too long (max. is 100 characters)."));
|
63
|
|
- exit();
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- $query = "UPDATE `experience` SET `description` = '$newDescription' WHERE `id` = '$id';";
|
67
|
|
- $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
68
|
|
-
|
69
|
|
- }
|
70
|
82
|
|
71
|
83
|
|
72
|
84
|
|
73
|
85
|
|
74
|
|
- /*** REMEMBER TO VALIDATE INPUT IF THE CODE BELOW IS TO BE USED ***/
|
|
86
|
+ function validDate($date) {
|
|
87
|
+ $d = date_create_from_format("Y-m-d", $date);
|
|
88
|
+ return $d && date_format($d, "Y-m-d") === $date;
|
|
89
|
+ }
|
75
|
90
|
|
76
|
|
- // UPDATE TYPE
|
77
|
|
-// if(isset($_POST['newType']) AND $_POST['newType'] != " ") {
|
78
|
|
-// $newType = mysqli_real_escape_string($connection, trim($_POST['newType']));
|
79
|
|
-// $query = "UPDATE `experience` SET `type` = '$newType' WHERE `id` = '$id';";
|
80
|
|
-// $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
81
|
|
-// }
|
82
|
91
|
|
83
|
|
- // UPDATE DURATION
|
84
|
|
-// if(isset($_POST['newDuration'])) {
|
85
|
|
-// $newDuration = mysqli_real_escape_string($connection, trim($_POST['newDuration']));
|
86
|
|
-// $query = "UPDATE `experience` SET `duration_weeks` = '$newDuration' WHERE `id` = '$id';";
|
87
|
|
-// $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ // INSPECT START DATE
|
|
95
|
+ // Check that startDate is not an empty string
|
|
96
|
+ // And that startDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
|
|
97
|
+ // WARNING: only handling AST
|
|
98
|
+ // HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
|
|
99
|
+// if($newStart === "") {
|
|
100
|
+// http_response_code(400);
|
|
101
|
+// echo json_encode(array("error" => "Please specify experience's start date."));
|
|
102
|
+// exit();
|
|
103
|
+// } else if(!validDate($newStart)) {
|
|
104
|
+// http_response_code(400);
|
|
105
|
+// echo json_encode(array("error" => "Experience's start date ($newStart) given in wrong format (use YYYY-MM-DD instead)."));
|
|
106
|
+// exit();
|
88
|
107
|
// }
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+ // INSPECT END DATE
|
|
111
|
+ // Check that endDate is not an empty string
|
|
112
|
+ // And that endDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
|
|
113
|
+ // WARNING: only handling AST
|
|
114
|
+ // HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
|
|
115
|
+ if($newEnd === "") {
|
|
116
|
+ http_response_code(400);
|
|
117
|
+ echo json_encode(array("error" => "Please specify experience's end date."));
|
|
118
|
+ exit();
|
|
119
|
+ } else if(!validDate($newEnd)) {
|
|
120
|
+ http_response_code(400);
|
|
121
|
+ echo json_encode(array("error" => "Experience's end date ($newEnd) given in wrong format (use YYYY-MM-DD instead)."));
|
|
122
|
+ exit();
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+ // Calculate duration in seconds
|
|
127
|
+ $duration_seconds = strtotime($newEnd) - strtotime($newStart);
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+ // Check that endDate occurs after the startDate
|
|
131
|
+ if($duration_seconds <= 0) {
|
|
132
|
+ http_response_code(400);
|
|
133
|
+ echo json_encode(array("error" => "Experience's end date ($newEnd) must occur at least a day after the start date ($newStart)."));
|
|
134
|
+ exit();
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+ // Change seconds to weeks and round up
|
|
139
|
+ $duration_weeks = round($duration_seconds / 604800); // 60 * 60 * 24 * 7
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+ // UPDATE TITLE, DESCRIPTION, TYPE, END DATE & DURATION
|
|
144
|
+ $newDuration = mysqli_real_escape_string($connection, trim($duration_weeks));
|
|
145
|
+ $query = "UPDATE `experience`
|
|
146
|
+ SET `title` = '$newTitle',
|
|
147
|
+ `description` = '$newDescription',
|
|
148
|
+ `type` = '$newType',
|
|
149
|
+ `end_date` = '$newEnd',
|
|
150
|
+ `duration_weeks` = '$newDuration'
|
|
151
|
+ WHERE `id` = '$id';";
|
|
152
|
+ $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
89
|
157
|
|
90
|
158
|
// UPDATE START DATE
|
91
|
|
-// if(isset($_POST['newStart'])) {
|
92
|
|
-// $newStart = mysqli_real_escape_string($connection, trim($_POST['newStart']));
|
93
|
|
-// $query = "UPDATE `experience` SET `start_date` = '$newStart' WHERE `id` = '$id';";
|
94
|
|
-// $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
95
|
|
-// }
|
|
159
|
+// $query = "UPDATE `experience` SET `start_date` = '$newStart' WHERE `id` = '$id';";
|
|
160
|
+// $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
|
161
|
+
|
|
162
|
+
|
96
|
163
|
|
97
|
|
- // UPDATE END DATE
|
98
|
|
-// if(isset($_POST['newEnd'])) {
|
99
|
|
-// $newEnd = mysqli_real_escape_string($connection, trim($_POST['newEnd']));
|
100
|
|
-// $query = "UPDATE `experience` SET `end_date` = '$newEnd' WHERE `id` = '$id';";
|
101
|
|
-// $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
|
102
|
|
-// }
|
103
|
164
|
|
104
|
165
|
|
105
|
166
|
|