Files
Siro/backend/Admin/marketing/get_campaigns_log.php
2026-06-21 18:58:13 +03:00

65 lines
2.1 KiB
PHP

<?php
// ============================================================
// Admin/marketing/get_campaigns_log.php
// API Endpoint to fetch marketing campaign delivery logs for Admin dashboard
// ============================================================
require_once __DIR__ . '/../../connect.php';
// 1. Authorize Admin/Super Admin
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized access. Admin role required.']);
exit;
}
try {
$limit = filterRequest('limit', 'int') ?? 50;
$countryCode = filterRequest('country_code');
$sql = "SELECT l.*, p.first_name, p.last_name
FROM marketing_campaigns_log l
LEFT JOIN passengers p ON p.id = l.passenger_id";
$params = [];
if ($countryCode) {
$sql .= " WHERE l.country_code = :country";
$params[':country'] = strtoupper($countryCode);
}
$sql .= " ORDER BY l.sent_at DESC LIMIT :limit";
$stmt = $con->prepare($sql);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
foreach ($params as $key => $val) {
$stmt->bindValue($key, $val);
}
$stmt->execute();
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Decrypt names or just return them
// (Names are not encrypted in this schema, only phones are, so we can return directly)
// Aggregate statistics for Dashboard charts
$sqlStats = "SELECT message_type, COUNT(*) as count
FROM marketing_campaigns_log";
if ($countryCode) {
$sqlStats .= " WHERE country_code = :country";
$stmtStats = $con->prepare($sqlStats);
$stmtStats->execute([':country' => strtoupper($countryCode)]);
} else {
$stmtStats = $con->prepare($sqlStats);
$stmtStats->execute();
}
$stats = $stmtStats->fetchAll(PDO::FETCH_ASSOC);
jsonSuccess([
'logs' => $logs,
'stats' => $stats
]);
} catch (Exception $e) {
error_log("[get_campaigns_log.php] Error: " . $e->getMessage());
jsonError("Failed to fetch campaigns log: " . $e->getMessage());
}