150 lines
5.7 KiB
PHP
150 lines
5.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../../connect.php';
|
|
|
|
// 1. Log the start of the request
|
|
$phone = filterRequest("phone");
|
|
error_log("[MONITOR_RIDE] ---------------- START REQUEST ----------------");
|
|
error_log("[MONITOR_RIDE] 1. Received Phone: " . $phone);
|
|
|
|
//------------------------------------------------------------------------
|
|
// 1) البحث عن الهاتف أولاً في جدول السائق ثم جدول الراكب
|
|
//------------------------------------------------------------------------
|
|
|
|
$encPhone = $encryptionHelper->encryptData($phone);
|
|
error_log("[MONITOR_RIDE] 2. Encrypted Phone: " . $encPhone);
|
|
|
|
// Check Driver Table
|
|
$driverQuery = $con->prepare("SELECT id AS driverID FROM driver WHERE phone = :phone LIMIT 1");
|
|
$driverQuery->execute([':phone' => $encPhone]);
|
|
$driver = $driverQuery->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// Check Passenger Table
|
|
$customerQuery = $con->prepare("SELECT id AS customerID FROM passengers WHERE phone = :phone LIMIT 1");
|
|
$customerQuery->execute([':phone' => $encPhone]);
|
|
$customer = $customerQuery->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
// حدد نوع المستخدم
|
|
$userType = '';
|
|
$driverID = null;
|
|
$customerID = null;
|
|
|
|
if ($driver) {
|
|
$userType = 'driver';
|
|
$driverID = $driver['driverID'];
|
|
error_log("[MONITOR_RIDE] 3. User Found: Type = DRIVER, ID = " . $driverID);
|
|
} elseif ($customer) {
|
|
$userType = 'customer';
|
|
$customerID = $customer['customerID'];
|
|
error_log("[MONITOR_RIDE] 3. User Found: Type = CUSTOMER, ID = " . $customerID);
|
|
} else {
|
|
error_log("[MONITOR_RIDE] 3. FAILURE: Phone number not found in Driver or Passenger tables.");
|
|
jsonError("رقم الهاتف غير موجود في النظام.");
|
|
exit;
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// 2) جلب آخر رحلة حالتها "بدأت" بناءً على نوع المستخدم
|
|
//------------------------------------------------------------------------
|
|
|
|
if ($userType == 'driver') {
|
|
error_log("[MONITOR_RIDE] 4. Searching for active ride for Driver ID: " . $driverID);
|
|
$rideQuery = $con->prepare("
|
|
SELECT * FROM ride
|
|
WHERE driver_id = :driverID AND status = 'Begin'
|
|
ORDER BY id DESC LIMIT 1
|
|
");
|
|
$rideQuery->execute([':driverID' => $driverID]);
|
|
} else {
|
|
error_log("[MONITOR_RIDE] 4. Searching for active ride for Customer ID: " . $customerID);
|
|
$rideQuery = $con->prepare("
|
|
SELECT * FROM ride
|
|
WHERE passenger_id = :customerID AND status = 'Begin'
|
|
ORDER BY id DESC LIMIT 1
|
|
");
|
|
$rideQuery->execute([':customerID' => $customerID]);
|
|
}
|
|
|
|
$ride = $rideQuery->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$ride) {
|
|
error_log("[MONITOR_RIDE] 4. FAILURE: No ride with status 'Begin' found.");
|
|
jsonError("لا توجد رحلة بدأت لهذا المستخدم.");
|
|
exit;
|
|
} else {
|
|
error_log("[MONITOR_RIDE] 4. SUCCESS: Active Ride Found. Ride ID: " . $ride['id']);
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// 3) جلب معلومات السائق من الرحلة
|
|
//------------------------------------------------------------------------
|
|
|
|
// FIX 1: Safe assignment of driver ID (checking driverID vs driver_id)
|
|
$rideDriverID = $ride['driverID'] ?? $ride['driver_id'];
|
|
|
|
error_log("[MONITOR_RIDE] 5. Fetching info for Driver ID from Ride: " . $rideDriverID);
|
|
|
|
// FIX 2: Select first_name and last_name instead of fullname
|
|
$driverInfoQuery = $con->prepare("
|
|
SELECT id, first_name, last_name, phone
|
|
FROM driver
|
|
WHERE id = :driverID
|
|
LIMIT 1
|
|
");
|
|
|
|
$driverInfoQuery->execute([':driverID' => $rideDriverID]);
|
|
$driverInfo = $driverInfoQuery->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($driverInfo) {
|
|
// فك التشفير للهاتف
|
|
$driverInfo['phone'] = $encryptionHelper->decryptData($driverInfo['phone']);
|
|
|
|
// FIX 4: Decrypt First Name and Last Name
|
|
$driverInfo['first_name'] = $encryptionHelper->decryptData($driverInfo['first_name']);
|
|
$driverInfo['last_name'] = $encryptionHelper->decryptData($driverInfo['last_name']);
|
|
|
|
// Construct fullname for the response
|
|
$fullName = $driverInfo['first_name'] . " " . $driverInfo['last_name'];
|
|
$driverInfo['fullname'] = $fullName;
|
|
|
|
error_log("[MONITOR_RIDE] 5. Driver Info Found: " . $fullName);
|
|
} else {
|
|
error_log("[MONITOR_RIDE] 5. WARNING: Driver info not found for ID " . $rideDriverID);
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// 4) جلب آخر موقع للسائق من جدول driver_location بشرط الحالة ON
|
|
//------------------------------------------------------------------------
|
|
|
|
error_log("[MONITOR_RIDE] 6. Querying Tracking DB for Driver ID: " . $rideDriverID);
|
|
|
|
// FIX 3: Changed ORDER BY id DESC to ORDER BY updated_at DESC
|
|
$locationQuery = $con_tracking->prepare("
|
|
SELECT latitude, longitude, speed, heading, updated_at
|
|
FROM car_locations
|
|
WHERE driver_id = :driverID AND status = 'ON'
|
|
ORDER BY updated_at DESC LIMIT 1
|
|
");
|
|
$locationQuery->execute([':driverID' => $rideDriverID]);
|
|
$location = $locationQuery->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($location) {
|
|
error_log("[MONITOR_RIDE] 6. Location Found: Lat=" . $location['latitude'] . " Lng=" . $location['longitude'] . " Updated=" . $location['updated_at']);
|
|
} else {
|
|
error_log("[MONITOR_RIDE] 6. WARNING: No live location found (status=ON) or list empty.");
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// 5) تجهيز البيانات للرد
|
|
//------------------------------------------------------------------------
|
|
|
|
$response = [
|
|
"ride_details" => $ride,
|
|
"driver_details" => $driverInfo,
|
|
"driver_location" => $location ?: "No live location"
|
|
];
|
|
|
|
error_log("[MONITOR_RIDE] 7. Sending Success Response.");
|
|
jsonSuccess($response);
|
|
|
|
?>
|