107 lines
3.9 KiB
PHP
107 lines
3.9 KiB
PHP
<?php
|
|
session_start();
|
|
include 'koneksi.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$action = $_REQUEST['action'] ?? '';
|
|
$id = $_REQUEST['id'] ?? 0;
|
|
|
|
if ($action === 'check_new') {
|
|
$stmt = $pdo->prepare("SELECT id, title, message, icon FROM notifications WHERE is_read = 0 AND is_deleted = 0 ORDER BY time DESC LIMIT 1");
|
|
$stmt->execute();
|
|
$notif = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
echo json_encode(['status' => 'success', 'data' => $notif ?: null]);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'mark_read') {
|
|
$stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE id = ?");
|
|
$result = $stmt->execute([$id]);
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'mark_all_read') {
|
|
$stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE is_deleted = 0");
|
|
$result = $stmt->execute();
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'delete_all_to_trash') {
|
|
$stmt = $pdo->prepare("UPDATE notifications SET is_deleted = 1, deleted_at = CURRENT_TIMESTAMP WHERE is_deleted = 0");
|
|
$result = $stmt->execute();
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'move_trash') {
|
|
$stmt = $pdo->prepare("UPDATE notifications SET is_deleted = 1, deleted_at = CURRENT_TIMESTAMP WHERE id = ?");
|
|
$result = $stmt->execute([$id]);
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'restore') {
|
|
$stmt = $pdo->prepare("UPDATE notifications SET is_deleted = 0, deleted_at = NULL WHERE id = ?");
|
|
$result = $stmt->execute([$id]);
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'delete_permanently') {
|
|
$stmt = $pdo->prepare("DELETE FROM notifications WHERE id = ?");
|
|
$result = $stmt->execute([$id]);
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'empty_trash') {
|
|
$stmt = $pdo->prepare("DELETE FROM notifications WHERE is_deleted = 1");
|
|
$result = $stmt->execute();
|
|
echo json_encode(['status' => $result ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'create_notification') {
|
|
include 'notification_helper.php';
|
|
$icon = $_POST['icon'] ?? 'mdi-information';
|
|
$title = $_POST['title'] ?? 'Info';
|
|
$message = $_POST['message'] ?? '';
|
|
$detail = $_POST['detail'] ?? '';
|
|
$is_danger = isset($_POST['is_danger']) && $_POST['is_danger'] == '1' ? true : false;
|
|
|
|
$res = send_notification($pdo, $icon, $title, $message, $detail, $is_danger);
|
|
echo json_encode(['status' => $res ? 'success' : 'error']);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'get_warnings') {
|
|
$excludeCond = "AND title NOT LIKE '%baterai%' AND title NOT LIKE '%battery%' AND title NOT LIKE '%pesan%' AND title NOT LIKE '%message%' AND title NOT LIKE '%chat%'";
|
|
|
|
// Fetch top 5 recent UNREAD and DANGER notifications
|
|
$stmt = $pdo->prepare("SELECT id, icon, title, message, time FROM notifications WHERE is_deleted = 0 AND is_danger = 1 AND is_read = 0 $excludeCond ORDER BY time DESC LIMIT 5");
|
|
$stmt->execute();
|
|
$warnings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Also get total unread count of ALL notifications
|
|
$countStmt = $pdo->query("SELECT COUNT(*) FROM notifications WHERE is_deleted = 0 AND is_read = 0 $excludeCond");
|
|
$total_unread = $countStmt->fetchColumn();
|
|
|
|
// Also get total unread danger count
|
|
$dangerCountStmt = $pdo->query("SELECT COUNT(*) FROM notifications WHERE is_deleted = 0 AND is_danger = 1 AND is_read = 0 $excludeCond");
|
|
$danger_unread = $dangerCountStmt->fetchColumn();
|
|
|
|
echo json_encode(['status' => 'success', 'warnings' => $warnings, 'total_unread' => $total_unread, 'danger_unread' => $danger_unread]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
?>
|