-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_vacancy_ajax.php
296 lines (221 loc) · 7.14 KB
/
edit_vacancy_ajax.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
<?php
/**
* This script updates a profile using ajax. It allows processing of both
* teachers and organisation edits.
* Pass in the USERNAME of the username being edited also.
* Pass in the constants EDIT_TYPE which has the type of profile (TEACHER/ORGANISATION) to edit.
* Then pass in the EDIT_FORM constant with the id of the form constant (should match the id in the html)
* This assumes all the relevant data is passed in
*/
require "database.php";
require "ajax.php";
require "constants.php";
define('ID','id');
//define('EDIT_FORM','edit_form');
$id = "";
$edit_type = "";
$edit_form = "";
$admin_override = false;
/**
* Throw error
*/
function throwError() {
die("Script called with missing variables");
}
/**
* Parses any post parameters
*/
function edit_vacancy_parsePOST() {
global $id;
global $edit_form;
if (isset($_POST[ID])) {
$id = $_POST[ID];
} else {
throwError();
}
if (isset($_POST[EDIT_FORM])) {
$edit_form = $_POST[EDIT_FORM];
} else {
throwError();
}
}
function processVacancyUpdate() {
global $id;
global $conn;
$job_title = (isset($_POST['job_title'])) ? $_POST['job_title']:null;
if ($job_title == null) {
respond(false, "Job title is a mandatory field");
}
$description = (isset($_POST['description'])) ? $_POST['description']:null;
$type = (isset($_POST['type'])) ? $_POST['type']:null;
$sql = "UPDATE vacancies SET job_title = ?, description = ?, type = ?
WHERE vacancy_id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("sssi", $param_job_title, $param_description, $param_type, $param_id);
$param_job_title = $job_title;
$param_description = $description;
$param_type = $type;
$param_id = $id;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
processAddSkills();
respond(true, "UPDATED");
} else {
die("Database error: {$conn->error}");
}
}
function processAddSkills() {
global $conn;
global $id;
$response_data = array(); // this will hold the new ids and names of any created skills that did not already exist
$skills_value = (isset($_POST['skills'])) ? $_POST['skills']:null;
if ($skills_value == null) {
return;
}
$sql = "INSERT INTO vacancy_skills (vacancy_id, skill_id) VALUES (?, ?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ii", $param_vacancy_id, $param_id);
$param_vacancy_id = $id;
$skills = explode(',', $skills_value);
foreach ($skills as $key => $value) {
$param_id = getSkill(trim($value), $response_data);
if (!skillAlreadyExists($param_id)) {
if (!$stmt->execute()) {
die("Database error : {$stmt->error}");
}
} else {
unset($response_data[$param_id]);
}
}
$stmt->close();
respondData(true, "UPDATED", $response_data);
} else {
die("Database error2: {$conn->error}");
}
}
function getSkill($skill_name, &$data) {
global $conn;
$sql = "SELECT * FROM skills WHERE name = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_name);
$param_name = $skill_name;
if ($stmt->execute()) {
$result = $stmt->get_result();
$id = 0;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['skill_id'];
}
} else {
$id = createSkill($skill_name);
}
$stmt->close();
$data[$id] = $skill_name;
return $id;
} else {
die("Database error: {$stmt->error}");
}
} else {
die("Database error: {$conn->error}");
}
}
function createSkill($skill_name) {
global $conn;
$sql = "INSERT INTO skills (name) VALUES (?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_name);
$param_name = $skill_name;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$id = $stmt->insert_id;
$stmt->close();
return $id;
} else {
die("Database error: {$conn->error}");
}
}
function skillAlreadyExists($skill_id) {
global $conn;
global $id;
$sql = "SELECT * FROM vacancy_skills WHERE vacancy_id = ? AND skill_id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ii", $param_vacancy_id, $param_id);
$param_vacancy_id = $id;
$param_id = $skill_id;
if ($stmt->execute()) {
$result = $stmt->get_result();
$exists = $result->num_rows > 0;
$stmt->close();
return $exists;
} else {
die("Database error: {$stmt->error}");
}
} else {
die("Database error: {$conn->error}");
}
}
function processSkillRemoval() {
global $conn;
global $id;
$skill_ids = (isset($_POST['skills_choice'])) ? $_POST['skills_choice']:null;
if ($skill_ids == null) {
respond(false, "Remove Skill is a mandatory field");
}
$sql = "DELETE FROM vacancy_skills WHERE vacancy_id = ? AND skill_id = ?;";
$data = array();
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ii", $param_vacancy_id, $param_id);
$param_vacancy_id = $id;
foreach ($skill_ids as $key => $value) {
$param_id = $value;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$data[] = $value;
}
$stmt->close();
respondData(true, "UPDATED", $data);
} else {
die("Database error: {$conn->error}");
}
}
function processDeleteVacancy() {
global $id;
global $conn;
$password = $_POST['delete_password'];
if ($password != 'CONFIRM') {
respond(false, "Incorrect confirmation");
}
$sql = "DELETE FROM vacancies WHERE vacancy_id = ?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("i", $param_vacancy_id);
$param_vacancy_id = $id;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
respond(true, "DELETED");
} else {
die("Database error: {$conn->error}");
}
}
function process() {
global $edit_form;
switch ($edit_form) {
case UPDATE_VACANCY: processVacancyUpdate();
break;
case REMOVE_SKILL: processSkillRemoval();
break;
case DELETE_VACANCY: processDeleteVacancy();
break;
}
//processVacancyUpdate();
//}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
edit_vacancy_parsePOST();
process();
}
?>