From 48f1fb89234ca169d64a5d6e492e45ad51cb95a1 Mon Sep 17 00:00:00 2001 From: Felix Ivo Date: Mon, 7 Jul 2025 10:21:15 +0200 Subject: [PATCH 1/5] added upload to createNotes --- Controller/NotesController.php | 8 ++--- Model/NotesModel.php | 54 ++++++++++++++++++++++++++++++++++ Views/Notes/createNote.phtml | 27 +++++++++++++++-- Views/Notes/showNotes.phtml | 4 +++ 4 files changed, 86 insertions(+), 7 deletions(-) diff --git a/Controller/NotesController.php b/Controller/NotesController.php index d4b4187..cc53f2d 100644 --- a/Controller/NotesController.php +++ b/Controller/NotesController.php @@ -77,15 +77,15 @@ class NotesController $_POST['content'], $_SESSION['user_id'] ); - + if ($note) { - // Redirect to show notes page after successful creation + // Redirect to show notes page after successful update header('Location: ?controller=Notes&page=showNotes&do=showNotes'); exit(); } else { - // If creation failed, show error message and stay on the form + // If update failed, show error message and stay on the form $this->view->setVars([ - 'error' => 'Failed to create note. Please try again.' + 'error' => 'Failed to update note. Please try again.' ]); } } diff --git a/Model/NotesModel.php b/Model/NotesModel.php index c3cb495..d4c6d7c 100644 --- a/Model/NotesModel.php +++ b/Model/NotesModel.php @@ -69,6 +69,11 @@ class NotesModel extends Database try { $stmt = $pdo->prepare("INSERT INTO notes (user_id, title, content) VALUES (?, ?, ?)"); $stmt->execute([$userId, trim($title), $content]); // user_id is current session user + $noteId = $pdo->lastInsertId(); + $uploadResult = $this->uploadFiles($noteId); + if (!$uploadResult['success']) { + return $uploadResult; + } return ['success' => true, 'message' => 'Note created successfully.']; } catch (PDOException $e) { error_log("Create Note Error: " . $e->getMessage()); @@ -130,6 +135,19 @@ class NotesModel extends Database } } + function getUploadedFiles($noteId) { + $pdo = $this->linkDB(); + if (!$pdo) return []; + try { + $stmt = $pdo->prepare("SELECT * FROM files WHERE note_id = ?"); + $stmt->execute([$noteId]); + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } catch (PDOException $e) { + error_log("Get Uploaded Files Error: " . $e->getMessage()); + return []; + } + } + function getNoteCount() { $pdo = $this->linkDB(); if (!$pdo) return 0; @@ -150,4 +168,40 @@ class NotesModel extends Database function isAdmin() { return $this->isLoggedIn() && isset($_SESSION['role']) && $_SESSION['role'] === 'admin'; } + + public function uploadFiles($noteId) { + $pdo = $this->linkDB(); + if (!$pdo) return ['success' => false, 'message' => 'Database error.']; + + $uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/EIANotesApp/Uploads/'; + $uploadedFileNames = []; + + if (isset($_FILES['attachments']) && !empty($_FILES['attachments']['name'][0])) { + $files = $_FILES['attachments']; + + foreach ($files['name'] as $key => $name) { + if ($files['error'][$key] === UPLOAD_ERR_OK) { + $tmpName = $files['tmp_name'][$key]; + $safeFilename = basename($name); + $uniqueFilename = time() . '-' . preg_replace('/[^A-Za-z0-9.\-]/', '_', $safeFilename); + $destination = $uploadDir . $uniqueFilename; + + if (move_uploaded_file($tmpName, $destination)) { + $uploadedFileNames[] = $uniqueFilename; + + $stmt = $pdo->prepare("INSERT INTO files (note_id, original_filename, stored_filename, file_type, file_size, uploaded_at) VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([$noteId, $safeFilename, $uniqueFilename, $files['type'][$key], $files['size'][$key], date('Y-m-d H:i:s')]); + } else { + $errmsg = "Error: Could not move uploaded file '$safeFilename'."; + } + } else { + $errmsg = "Error uploading file '$name'. Error code: " . $files['error'][$key]; + } + } + } + if (isset($errmsg)) { + return ['success' => false, 'message' => $errmsg]; + } + return ['success' => true, 'message' => 'Files uploaded successfully.', 'fileNames' => $uploadedFileNames]; + } } \ No newline at end of file diff --git a/Views/Notes/createNote.phtml b/Views/Notes/createNote.phtml index f014524..537c136 100644 --- a/Views/Notes/createNote.phtml +++ b/Views/Notes/createNote.phtml @@ -2,6 +2,16 @@ use ppa\Model\NotesModel; include dirname(__DIR__).'/header.phtml'; +// Test write permissions +// This is the directory we will upload files to. +$uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/EIANotesApp/Uploads/'; +if (!file_exists($uploadDir)) { + mkdir($uploadDir, 0777, true); +} +$testFile = $uploadDir . 'test_write.txt'; +$testContent = 'Test write operation at ' . date('Y-m-d H:i:s'); +$writeResult = file_put_contents($testFile, $testContent); + $parsedown = new Parsedown(); $parsedown->setSafeMode(true); @@ -26,12 +36,16 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') { + +
Drag & drop a .txt or .md file here, or fill manually.
-
+
@@ -47,6 +61,13 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') { Start typing or drop a file to see preview...
+ +
+ + +
+ +
@@ -54,4 +75,4 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') {
- + \ No newline at end of file diff --git a/Views/Notes/showNotes.phtml b/Views/Notes/showNotes.phtml index 2eb3a13..ed34abd 100644 --- a/Views/Notes/showNotes.phtml +++ b/Views/Notes/showNotes.phtml @@ -25,6 +25,10 @@ Create New Note + + + + From 9db4d93ce31cfce722a1adab3a12b376f12f188c Mon Sep 17 00:00:00 2001 From: Felix Ivo Date: Mon, 7 Jul 2025 10:27:19 +0200 Subject: [PATCH 2/5] comment out test_write file --- Views/Notes/createNote.phtml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Views/Notes/createNote.phtml b/Views/Notes/createNote.phtml index 537c136..9df15fb 100644 --- a/Views/Notes/createNote.phtml +++ b/Views/Notes/createNote.phtml @@ -2,15 +2,15 @@ use ppa\Model\NotesModel; include dirname(__DIR__).'/header.phtml'; -// Test write permissions -// This is the directory we will upload files to. -$uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/EIANotesApp/Uploads/'; -if (!file_exists($uploadDir)) { - mkdir($uploadDir, 0777, true); -} -$testFile = $uploadDir . 'test_write.txt'; -$testContent = 'Test write operation at ' . date('Y-m-d H:i:s'); -$writeResult = file_put_contents($testFile, $testContent); +//// Test write permissions +//// This is the directory we will upload files to. +//$uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/EIANotesApp/Uploads/'; +//if (!file_exists($uploadDir)) { +// mkdir($uploadDir, 0777, true); +//} +//$testFile = $uploadDir . 'test_write.txt'; +//$testContent = 'Test write operation at ' . date('Y-m-d H:i:s'); +//$writeResult = file_put_contents($testFile, $testContent); $parsedown = new Parsedown(); $parsedown->setSafeMode(true); From 0799db48f0936bf6ec1c7a661eb0ec793d95e5b0 Mon Sep 17 00:00:00 2001 From: Felix Ivo Date: Mon, 7 Jul 2025 10:42:12 +0200 Subject: [PATCH 3/5] add uploaded files to edit, note details, welcome page --- Model/NotesModel.php | 18 ++++++++++++++++++ Views/Notes/createNote.phtml | 1 - Views/Notes/editNote.phtml | 20 +++++++++++++++++++- Views/Notes/showNoteDetails.phtml | 16 ++++++++++++++++ Views/Welcome/showWelcome.phtml | 9 ++++++++- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Model/NotesModel.php b/Model/NotesModel.php index d4c6d7c..82fb70e 100644 --- a/Model/NotesModel.php +++ b/Model/NotesModel.php @@ -94,6 +94,11 @@ class NotesModel extends Database $params = [trim($title), $content, $noteId, $userId]; } $stmt->execute($params); + + $uploadResult = $this->uploadFiles($noteId); + if (!$uploadResult['success']) { + return $uploadResult; + } if ($stmt->rowCount() > 0) { return ['success' => true, 'message' => 'Note updated successfully.']; @@ -148,6 +153,19 @@ class NotesModel extends Database } } + function getFileCount() { + $pdo = $this->linkDB(); + if (!$pdo) return 0; + try { + $stmt = $pdo->prepare("SELECT COUNT(*) FROM files"); + $stmt->execute(); + return $stmt->fetchColumn(); + } catch (PDOException $e) { + error_log("Get Files Count Error: " . $e->getMessage()); + return 0; + } + } + function getNoteCount() { $pdo = $this->linkDB(); if (!$pdo) return 0; diff --git a/Views/Notes/createNote.phtml b/Views/Notes/createNote.phtml index 9df15fb..cbd9d25 100644 --- a/Views/Notes/createNote.phtml +++ b/Views/Notes/createNote.phtml @@ -67,7 +67,6 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') { -
diff --git a/Views/Notes/editNote.phtml b/Views/Notes/editNote.phtml index df170ea..5e869e4 100644 --- a/Views/Notes/editNote.phtml +++ b/Views/Notes/editNote.phtml @@ -15,6 +15,8 @@ if (!$note) { echo "Back to Dashboard"; } +$files = $this->notesModel->getUploadedFiles($noteId); + function isLoggedIn() { return isset($_SESSION['user_id']); } @@ -37,7 +39,7 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') {
Drag & drop a .txt or .md file here, or fill manually.
-
+
@@ -54,6 +56,22 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') { text(sanitize($note['content'])); else echo "Start typing or drop a file to see preview..."; ?>
+
+ + +
+ 0): ?> +
+ +
    + +
  • + +
  • + +
+
+
diff --git a/Views/Notes/showNoteDetails.phtml b/Views/Notes/showNoteDetails.phtml index 3c7aa7f..3365142 100644 --- a/Views/Notes/showNoteDetails.phtml +++ b/Views/Notes/showNoteDetails.phtml @@ -3,6 +3,9 @@ setSafeMode(true); + +$this->notesModel = new \ppa\Model\NotesModel(); +$files = $this->notesModel->getUploadedFiles($note['id']); ?>
@@ -24,6 +27,19 @@ $parsedown->setSafeMode(true); text($note['content'] ?? ''); ?>
+
+ 0): ?> +

Attached Files:

+
    + +
  • + +
  • + +
+ +
+
Back to Notes diff --git a/Views/Welcome/showWelcome.phtml b/Views/Welcome/showWelcome.phtml index b1fb24b..ed32bb9 100644 --- a/Views/Welcome/showWelcome.phtml +++ b/Views/Welcome/showWelcome.phtml @@ -26,7 +26,14 @@ $this->userModel = new \ppa\Model\UserModel(); echo $this->userModel->getUserCount(); ?> Users - +
+ + + notesModel->getFileCount(); + ?> + Files +
From 4ae6971b9cc07c56fa7c47c18b70ee937f58fc8e Mon Sep 17 00:00:00 2001 From: Felix Ivo Date: Mon, 7 Jul 2025 10:50:51 +0200 Subject: [PATCH 4/5] added gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95a48fb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +uploads \ No newline at end of file From c5ebde8b205522e03076d734b4291f743c0f5918 Mon Sep 17 00:00:00 2001 From: Felix Ivo Date: Mon, 7 Jul 2025 10:54:32 +0200 Subject: [PATCH 5/5] fix filepath link --- Views/Notes/editNote.phtml | 2 +- Views/Notes/showNoteDetails.phtml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Views/Notes/editNote.phtml b/Views/Notes/editNote.phtml index 5e869e4..301a1ae 100644 --- a/Views/Notes/editNote.phtml +++ b/Views/Notes/editNote.phtml @@ -66,7 +66,7 @@ function sanitize($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') {
  • - +
diff --git a/Views/Notes/showNoteDetails.phtml b/Views/Notes/showNoteDetails.phtml index 3365142..bf9b5f8 100644 --- a/Views/Notes/showNoteDetails.phtml +++ b/Views/Notes/showNoteDetails.phtml @@ -33,7 +33,7 @@ $files = $this->notesModel->getUploadedFiles($note['id']);
  • - +