34394-vm/delete-note.php
Flatlogic Bot 3d274404c6 0004
2025-09-25 21:18:43 +00:00

50 lines
1.5 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/audit.php';
header('Content-Type: application/json');
// If user is not logged in, return error
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'message' => 'User not logged in.']);
exit;
}
$noteId = $_GET['note_id'] ?? null;
if ($noteId) {
try {
$pdo = db();
// First, get the client_id for redirection
$stmt = $pdo->prepare("SELECT client_id FROM notes WHERE note_id = ?");
$stmt->execute([$noteId]);
$note = $stmt->fetch(PDO::FETCH_ASSOC);
$clientId = $note['client_id'] ?? null;
if ($clientId) {
log_audit_event('note_delete', $_SESSION['user_id'], 'note', $noteId);
// Now, delete the note
$deleteStmt = $pdo->prepare("DELETE FROM notes WHERE note_id = ?");
$deleteStmt->execute([$noteId]);
echo json_encode(['success' => true]);
exit;
} else {
// Note not found or no client_id associated
echo json_encode(['success' => false, 'message' => 'Note not found.']);
exit;
}
} catch (PDOException $e) {
// Optional: Log error
echo json_encode(['success' => false, 'message' => 'Database error.']);
exit;
}
} else {
// Redirect if note_id is missing
echo json_encode(['success' => false, 'message' => 'Note ID is required.']);
exit;
}
?>