-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_note.php
81 lines (68 loc) · 2.68 KB
/
save_note.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
session_start();
require_once './db_connect.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
http_response_code(401);
die(json_encode(['success' => false, 'message' => 'Unauthorized']));
}
// Validate input
if (empty($_POST['title']) || empty($_POST['content'])) {
http_response_code(400);
die(json_encode(['success' => false, 'message' => 'Title and content are required']));
}
try {
// Start transaction
$conn->beginTransaction();
// Insert note
$stmt = $conn->prepare("
INSERT INTO notes (user_id, title, content, subject_id, created_at, updated_at)
VALUES (:user_id, :title, :content, :subject_id, NOW(), NOW())
");
$stmt->execute([
':user_id' => $_SESSION['user_id'],
':title' => $_POST['title'],
':content' => $_POST['content'],
':subject_id' => !empty($_POST['subject_id']) ? $_POST['subject_id'] : null
]);
$noteId = $conn->lastInsertId();
// Process tags if provided
if (!empty($_POST['tags'])) {
$tags = explode(',', $_POST['tags']);
foreach ($tags as $tagName) {
$tagName = trim($tagName);
if (empty($tagName)) continue;
// Check if tag exists
$tagStmt = $conn->prepare("SELECT id FROM tags WHERE name = :name");
$tagStmt->execute([':name' => $tagName]);
$tag = $tagStmt->fetch();
if (!$tag) {
// Create new tag
$tagStmt = $conn->prepare("INSERT INTO tags (name) VALUES (:name)");
$tagStmt->execute([':name' => $tagName]);
$tagId = $conn->lastInsertId();
} else {
$tagId = $tag['id'];
}
// Link tag to note
$linkStmt = $conn->prepare("INSERT INTO note_tags (note_id, tag_id) VALUES (:note_id, :tag_id)");
$linkStmt->execute([':note_id' => $noteId, ':tag_id' => $tagId]);
}
}
// Record activity
$activityStmt = $conn->prepare("
INSERT INTO activities (user_id, activity_type, description, note_id)
VALUES (:user_id, 'note_create', 'Created new note: " . substr($_POST['title'], 0, 50) . "', :note_id)
");
$activityStmt->execute([
':user_id' => $_SESSION['user_id'],
':note_id' => $noteId
]);
$conn->commit();
echo json_encode(['success' => true, 'message' => 'Note saved successfully']);
} catch (PDOException $e) {
$conn->rollBack();
error_log("Error saving note: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Error saving note']);
}