34 lines
1.2 KiB
PHP
34 lines
1.2 KiB
PHP
<?php
|
|
// api/notifikasi.php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, PUT, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
|
|
|
|
require_once '../includes/config.php';
|
|
requireLogin();
|
|
startSession();
|
|
$uid = (int)$_SESSION['user_id'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
$only_unread = isset($_GET['unread']) ? "AND is_read=0" : "";
|
|
$res = $conn->query("SELECT * FROM notifikasi WHERE id_user=$uid $only_unread ORDER BY created_at DESC LIMIT 50");
|
|
$data = [];
|
|
while ($r = $res->fetch_assoc()) $data[] = $r;
|
|
$unread = $conn->query("SELECT COUNT(*) c FROM notifikasi WHERE id_user=$uid AND is_read=0")->fetch_assoc()['c'];
|
|
jsonResponse(['notifikasi'=>$data,'unread'=>(int)$unread]);
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
|
|
$d = json_decode(file_get_contents('php://input'), true);
|
|
$id = (int)($d['id']??0);
|
|
if ($id) {
|
|
$conn->query("UPDATE notifikasi SET is_read=1 WHERE id=$id AND id_user=$uid");
|
|
} else {
|
|
$conn->query("UPDATE notifikasi SET is_read=1 WHERE id_user=$uid");
|
|
}
|
|
jsonResponse(['success'=>true]);
|
|
}
|
|
?>
|