Update: 2026-07-12 05:40:28

This commit is contained in:
Hamza-Ayed
2026-07-12 05:40:28 +03:00
parent 1fa517acc9
commit 24fb56f08f
75 changed files with 5051 additions and 9 deletions
+65
View File
@@ -0,0 +1,65 @@
<?php
// transit/admin/dashboard.php — لوحة اليوم
require_once __DIR__ . '/../../transit/connect_admin.php';
$today = date('Y-m-d');
$stTrips = $transit_con->prepare(
"SELECT t.id, t.status, t.started_at, t.completed_at, t.delay_minutes, t.current_stop_seq,
r.name_ar AS route_name, sc.departure_time,
v.plate AS vehicle_plate,
d.name AS driver_name, d.phone AS driver_phone
FROM transit_trips t
JOIN transit_routes r ON r.id = t.route_id
JOIN transit_schedules sc ON sc.id = t.schedule_id
JOIN transit_vehicles v ON v.id = t.vehicle_id
JOIN transit_drivers d ON d.id = t.driver_id
WHERE t.org_id = ? AND t.trip_date = ?
ORDER BY sc.departure_time ASC"
);
$stTrips->execute([$transit_org_id, $today]);
$trips = $stTrips->fetchAll();
foreach ($trips as &$trip) {
$trip['live_position'] = null;
if ($trip['status'] === 'started') {
$pos = transitGetBusPosition((int)$trip['id']);
if ($pos) $trip['live_position'] = ['lat' => (float)$pos['lat'], 'lng' => (float)$pos['lng'], 'ts' => (int)$pos['ts']];
}
}
unset($trip);
$stStats = $transit_con->prepare(
"SELECT COUNT(*) total_trips,
SUM(status='started') active_now,
SUM(status='completed') completed_today,
SUM(status='no_show') no_show_today
FROM transit_trips WHERE org_id=? AND trip_date=?"
);
$stStats->execute([$transit_org_id, $today]);
$stats = $stStats->fetch();
$stMembers = $transit_con->prepare(
"SELECT COUNT(*) active_members FROM transit_enrollments WHERE org_id=? AND status='active'"
);
$stMembers->execute([$transit_org_id]);
$stBc = $transit_con->prepare(
"SELECT id, title_ar, body_ar, sent_at, target_type FROM transit_broadcasts
WHERE org_id=? ORDER BY created_at DESC LIMIT 5"
);
$stBc->execute([$transit_org_id]);
jsonSuccess([
'date' => $today,
'trips' => $trips,
'stats' => [
'total_trips' => (int)$stats['total_trips'],
'active_now' => (int)$stats['active_now'],
'completed_today' => (int)$stats['completed_today'],
'no_show_today' => (int)$stats['no_show_today'],
'active_members' => (int)$stMembers->fetchColumn(),
],
'recent_broadcasts' => $stBc->fetchAll(),
]);
+34
View File
@@ -0,0 +1,34 @@
<?php
// transit/admin/login_request.php — الخطوة 1: هاتف المشرف → OTP
// POST: phone
require_once __DIR__ . '/../../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
try { $transit_con = Database::get('transit'); }
catch (Exception $e) { jsonError('Transit service unavailable', 503); }
$rawPhone = filterRequest('phone');
if (!$rawPhone) jsonError('Phone is required');
$phone = normalizePhone($rawPhone);
if (transitIsOtpLocked($phone)) {
jsonError('Too many attempts. Try again in 30 minutes.', 429);
}
$phoneEnc = $encryptionHelper->encryptData($phone);
$st = $transit_con->prepare(
"SELECT a.id, o.contract_status
FROM transit_org_admins a
JOIN transit_orgs o ON o.id = a.org_id
WHERE a.phone = ? AND a.is_active = 1 LIMIT 1"
);
$st->execute([$phoneEnc]);
$admin = $st->fetch();
if (!$admin) jsonError('Invalid credentials', 401);
if ($admin['contract_status'] === 'terminated') jsonError('Account suspended', 403);
transitSendAdminOtp($phone);
jsonSuccess(null, 'OTP sent successfully');
+48
View File
@@ -0,0 +1,48 @@
<?php
// transit/admin/login_verify.php — الخطوة 2: OTP → session token
// POST: phone, otp
require_once __DIR__ . '/../../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
try { $transit_con = Database::get('transit'); }
catch (Exception $e) { jsonError('Transit service unavailable', 503); }
requireTransitFields(['phone', 'otp']);
$phone = normalizePhone(filterRequest('phone'));
$otp = preg_replace('/\D/', '', filterRequest('otp'));
if (!transitVerifyAdminOtp($phone, $otp)) jsonError('Invalid or expired OTP', 401);
$phoneEnc = $encryptionHelper->encryptData($phone);
$st = $transit_con->prepare(
"SELECT a.id, a.org_id, a.name, a.role, a.permissions,
o.name_ar, o.name_en, o.type, o.contract_status, o.logo_url
FROM transit_org_admins a
JOIN transit_orgs o ON o.id = a.org_id
WHERE a.phone = ? AND a.is_active = 1 LIMIT 1"
);
$st->execute([$phoneEnc]);
$admin = $st->fetch();
if (!$admin) jsonError('Admin not found', 401);
$token = transitCreateSession((int)$admin['id'], (int)$admin['org_id']);
jsonSuccess([
'token' => $token,
'expires_in' => 86400,
'admin' => [
'id' => (int)$admin['id'],
'name' => $admin['name'],
'role' => $admin['role'],
'permissions' => json_decode($admin['permissions'] ?? '{}', true),
],
'org' => [
'id' => (int)$admin['org_id'],
'name_ar' => $admin['name_ar'],
'name_en' => $admin['name_en'],
'type' => $admin['type'],
'contract_status' => $admin['contract_status'],
'logo_url' => $admin['logo_url'],
],
], 'Login successful');