-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_available_roles.php
More file actions
96 lines (76 loc) · 2.82 KB
/
get_available_roles.php
File metadata and controls
96 lines (76 loc) · 2.82 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
<?php
// Prevent any output before headers
ob_start();
// Include database connection
require 'db_connection.php';
// Set headers
header('Content-Type: application/json');
try {
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Get POST data
$jsonData = file_get_contents('php://input');
$data = json_decode($jsonData, true);
if ($data === null) {
throw new Exception('Invalid JSON data received');
}
// Extract parameters
$roleId = intval($data['role'] ?? null);
$rideId = intval($data['shift'] ?? null);
$day = $data['day'] ?? null;
$time = $data['startTime'] ?? null;
$shift_start = trim(strval(date('h:i A', strtotime($time) + 900)));
// Validate required fields
if (!$roleId || !$rideId || !$day || !$time) {
throw new Exception("Missing required parameters");
}
// Get ride_id from rides table based on start time
$getRideQuery = $db->prepare('SELECT id FROM rides WHERE day = :day AND time = :shift_start');
$getRideQuery->bindValue(':shift_start', $shift_start, SQLITE3_TEXT);
$getRideQuery->bindValue(':day', $day, SQLITE3_TEXT);
$rideResult = $getRideQuery->execute();
if (!$rideResult) {
throw new Exception("Failed to fetch ride_id");
}
$rideRow = $rideResult->fetchArray(SQLITE3_ASSOC);
if (!$rideRow) {
throw new Exception("No ride found for the given start time");
}
$ride_id = $rideRow['id'];
// Prepare and execute query to get volunteer slots
$stmt = $db->prepare('SELECT id FROM volunteer_slots WHERE ride_id = :ride_id');
$stmt->bindValue(':ride_id', $ride_id, SQLITE3_INTEGER);
// Execute query
$result = $stmt->execute();
// Fetch results into a simple array of IDs
$slotIds = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$slotIds[] = $row['id'];
}
// Debug array properly using print_r()
// Fetch availability data for each slot
$arrays = [];
foreach ($slotIds as $slotId) {
$availabilityStmt = $db->prepare('SELECT * FROM shift_availability_cache WHERE shift_id = :shift_id');
$availabilityStmt->bindValue(':shift_id', $slotId, SQLITE3_INTEGER);
$availabilityResult = $availabilityStmt->execute();
while ($availRow = $availabilityResult->fetchArray(SQLITE3_ASSOC)) {
$arrays[] = $availRow;
}
}
// Return success response
echo json_encode([
'success' => true,
'data' => $arrays,
'message' => empty($arrays) ? 'No data found' : 'Data retrieved successfully'
]);
} catch (Exception $e) {
// Return error response
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
// Close database connection
$db->close();