51 lines
1.7 KiB
PHP
51 lines
1.7 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;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$clientId = $_POST['client_id'] ?? null;
|
|
$note = $_POST['note'] ?? null;
|
|
$userId = $_SESSION['user_id'];
|
|
|
|
if ($clientId && $note) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("INSERT INTO notes (client_id, user_id, note) VALUES (?, ?, ?)");
|
|
$stmt->execute([$clientId, $userId, $note]);
|
|
$newNoteId = $pdo->lastInsertId();
|
|
log_audit_event('note_create', $userId, 'note', $newNoteId);
|
|
|
|
// Fetch the new note to return to the client
|
|
$stmt = $pdo->prepare(
|
|
"SELECT n.*, u.display_name FROM notes n " .
|
|
"JOIN users u ON n.user_id = u.user_id " .
|
|
"WHERE n.note_id = ?"
|
|
);
|
|
$stmt->execute([$newNoteId]);
|
|
$newNote = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
echo json_encode(['success' => true, 'note' => $newNote]);
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
// Optional: Log error
|
|
echo json_encode(['success' => false, 'message' => 'Database error.']);
|
|
exit;
|
|
}
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Client ID and note are required.']);
|
|
exit;
|
|
}
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
|
|
exit;
|
|
}
|
|
?>
|