TKK_E32230273/history_ajax.php

88 lines
3.7 KiB
PHP

<?php
include 'koneksi.php';
header('Content-Type: application/json');
$action = isset($_POST['action']) ? $_POST['action'] : '';
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
$ids = isset($_POST['ids']) && is_array($_POST['ids']) ? $_POST['ids'] : [];
if (empty($action) || (empty($id) && empty($ids))) {
echo json_encode(['status' => 'error', 'message' => 'Invalid parameters']);
exit;
}
try {
if ($action === 'mark_read') {
$stmt = $pdo->prepare("UPDATE history SET is_read = 1 WHERE id_history = ?");
$stmt->execute([$id]);
echo json_encode(['status' => 'success']);
} elseif ($action === 'toggle_star') {
// Toggle star status
$stmt = $pdo->prepare("SELECT is_starred FROM history WHERE id_history = ?");
$stmt->execute([$id]);
$current = $stmt->fetchColumn();
$newStat = $current ? 0 : 1;
$upd = $pdo->prepare("UPDATE history SET is_starred = ? WHERE id_history = ?");
$upd->execute([$newStat, $id]);
echo json_encode(['status' => 'success', 'starred' => $newStat]);
} elseif ($action === 'delete') {
if (!empty($ids)) {
$inQuery = implode(',', array_fill(0, count($ids), '?'));
$del = $pdo->prepare("UPDATE history SET is_deleted = 1, deleted_at = NOW() WHERE id_history IN ($inQuery)");
$del->execute($ids);
} else {
$del = $pdo->prepare("UPDATE history SET is_deleted = 1, deleted_at = NOW() WHERE id_history = ?");
$del->execute([$id]);
}
echo json_encode(['status' => 'success']);
} elseif ($action === 'restore') {
if (!empty($ids)) {
$inQuery = implode(',', array_fill(0, count($ids), '?'));
$res = $pdo->prepare("UPDATE history SET is_deleted = 0, deleted_at = NULL WHERE id_history IN ($inQuery)");
$res->execute($ids);
} else {
$res = $pdo->prepare("UPDATE history SET is_deleted = 0, deleted_at = NULL WHERE id_history = ?");
$res->execute([$id]);
}
echo json_encode(['status' => 'success']);
} elseif ($action === 'hard_delete') {
if (!empty($ids)) {
$inQuery = implode(',', array_fill(0, count($ids), '?'));
// Ambil schedule_id terkait
$getSchedules = $pdo->prepare("SELECT schedule_id FROM history WHERE id_history IN ($inQuery)");
$getSchedules->execute($ids);
$scheduleIds = $getSchedules->fetchAll(PDO::FETCH_COLUMN);
$res = $pdo->prepare("DELETE FROM history WHERE id_history IN ($inQuery)");
$res->execute($ids);
if (!empty($scheduleIds)) {
$schedInQuery = implode(',', array_fill(0, count($scheduleIds), '?'));
$delSchedule = $pdo->prepare("DELETE FROM schedule WHERE id_schedule IN ($schedInQuery)");
$delSchedule->execute($scheduleIds);
}
} else {
// Ambil schedule_id terkait
$getSchedules = $pdo->prepare("SELECT schedule_id FROM history WHERE id_history = ?");
$getSchedules->execute([$id]);
$scheduleId = $getSchedules->fetchColumn();
$res = $pdo->prepare("DELETE FROM history WHERE id_history = ?");
$res->execute([$id]);
if ($scheduleId) {
$delSchedule = $pdo->prepare("DELETE FROM schedule WHERE id_schedule = ?");
$delSchedule->execute([$scheduleId]);
}
}
echo json_encode(['status' => 'success']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Unknown action']);
}
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}