51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
// api/get_logs.php - Fetch Activity Logs for Super Admin
|
|
|
|
header("Content-Type: application/json; charset=UTF-8");
|
|
require_once '../config/db.php';
|
|
|
|
try {
|
|
$agencyId = $_GET['agency_id'] ?? '';
|
|
if ($agencyId !== 'SUPER_ADMIN') {
|
|
echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$startDate = $_GET['start_date'] ?? '';
|
|
$endDate = $_GET['end_date'] ?? '';
|
|
$search = $_GET['search'] ?? '';
|
|
|
|
$db = getDbConnection();
|
|
$pdo = $db['pdo'];
|
|
|
|
$query = "SELECT * FROM activity_logs WHERE 1=1";
|
|
$params = [];
|
|
|
|
if ($startDate) {
|
|
$query .= " AND created_at >= ?";
|
|
$params[] = $startDate . ' 00:00:00';
|
|
}
|
|
if ($endDate) {
|
|
$query .= " AND created_at <= ?";
|
|
$params[] = $endDate . ' 23:59:59';
|
|
}
|
|
if ($search) {
|
|
$query .= " AND (user_name LIKE ? OR action LIKE ? OR details LIKE ?)";
|
|
$searchParam = "%$search%";
|
|
$params[] = $searchParam;
|
|
$params[] = $searchParam;
|
|
$params[] = $searchParam;
|
|
}
|
|
|
|
$query .= " ORDER BY created_at DESC LIMIT 500"; // Limit to prevent massive payload
|
|
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute($params);
|
|
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo json_encode(['status' => 'success', 'data' => $logs]);
|
|
|
|
} catch (\Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|