From 407df6e228bf60d98cf4a78ed76f5b945a13080a Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 22 Nov 2018 00:25:27 -0700 Subject: [PATCH] Add basic create/edit/delete functionality, close #1 --- .gitmodules | 3 +++ action.php | 24 ++++++++++++++++++- langs/en/notes.json | 5 +++- langs/messages.php | 4 ++++ lib/Note.lib.php | 15 ++++++++++-- pages.php | 11 +++++++++ pages/editnote.php | 34 ++++++++++++++++++++++++++ pages/home.php | 15 ++++++++---- static/css/editnote.css | 13 ++++++++++ static/easy-markdown-editor | 1 + static/js/editnote.js | 48 +++++++++++++++++++++++++++++++++++++ 11 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 pages/editnote.php create mode 100644 static/css/editnote.css create mode 160000 static/easy-markdown-editor create mode 100644 static/js/editnote.js diff --git a/.gitmodules b/.gitmodules index 7cba5c4..58b232e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "static/css/material-color"] path = static/css/material-color url = https://source.netsyms.com/Netsyms/Material-Color +[submodule "static/easy-markdown-editor"] + path = static/easy-markdown-editor + url = https://source.netsyms.com/Netsyms/easy-markdown-editor.git diff --git a/action.php b/action.php index d1ea966..f5d196e 100644 --- a/action.php +++ b/action.php @@ -7,7 +7,6 @@ /** * Make things happen when buttons are pressed and forms submitted. */ - require_once __DIR__ . "/required.php"; if ($VARS['action'] !== "signout") { @@ -35,4 +34,27 @@ switch ($VARS['action']) { session_destroy(); header('Location: index.php'); die("Logged out."); + case "savenote": + if (empty($VARS['content']) || empty($VARS['noteid'])) { + die($Strings->get("invalid parameters")); + } + http_response_code(204); + $note = Note::loadNote($VARS['noteid']); + if ($note->getOwnerID() != $_SESSION['uid']) { + die($Strings->get("invalid parameters")); + } + $note->setText($VARS['content']); + $note->setColor($VARS['color']); + $note->saveNote(); + break; + case "deletenote": + if (empty($VARS['noteid'])) { + die($Strings->get("invalid parameters")); + } + $note = Note::loadNote($VARS['noteid']); + if ($note->getOwnerID() != $_SESSION['uid']) { + die($Strings->get("invalid parameters")); + } + $note->deleteNote(); + returnToSender("note_deleted"); } \ No newline at end of file diff --git a/langs/en/notes.json b/langs/en/notes.json index 37bf50d..3596c26 100644 --- a/langs/en/notes.json +++ b/langs/en/notes.json @@ -1,5 +1,8 @@ { "New": "New", + "New note": "New note", "Note": "Note", - "Edit": "Edit" + "Edit": "Edit", + "Delete": "Delete", + "Note deleted": "Note deleted" } diff --git a/langs/messages.php b/langs/messages.php index 0744629..3b904e3 100644 --- a/langs/messages.php +++ b/langs/messages.php @@ -16,5 +16,9 @@ define("MESSAGES", [ "404_error" => [ "string" => "page not found", "type" => "info" + ], + "note_deleted" => [ + "string" => "Note deleted", + "type" => "success" ] ]); diff --git a/lib/Note.lib.php b/lib/Note.lib.php index 6f943ee..410e6d2 100644 --- a/lib/Note.lib.php +++ b/lib/Note.lib.php @@ -73,11 +73,22 @@ class Note { if ($saveas) { $database->insert('notes', $data); - return $database->id(); + $this->noteid = $database->id(); } else { $database->update('notes', $data, ['noteid' => $this->noteid]); - return $this->noteid; } + + return $this->noteid; + } + + /** + * Delete this note from the database. + * @global type $database + */ + public function deleteNote() { + global $database; + + $database->delete('notes', ['noteid' => $this->noteid]); } /** diff --git a/pages.php b/pages.php index a8a466a..6419dca 100644 --- a/pages.php +++ b/pages.php @@ -13,5 +13,16 @@ define("PAGES", [ ], "404" => [ "title" => "404 error" + ], + "editnote" => [ + "title" => "Edit Note", + "styles" => [ + "static/easy-markdown-editor/dist/easymde.min.css", + "static/css/editnote.css" + ], + "scripts" => [ + "static/easy-markdown-editor/dist/easymde.min.js", + "static/js/editnote.js" + ] ] ]); \ No newline at end of file diff --git a/pages/editnote.php b/pages/editnote.php new file mode 100644 index 0000000..d3f5150 --- /dev/null +++ b/pages/editnote.php @@ -0,0 +1,34 @@ +getOwnerID() != $_SESSION['uid']) { + header("Location: app.php?msg=no_permission"); + die(); +} + +$note->saveNote(); + +?> + +
+ + + + +
\ No newline at end of file diff --git a/pages/home.php b/pages/home.php index 9d8cfcc..64bad31 100644 --- a/pages/home.php +++ b/pages/home.php @@ -16,13 +16,17 @@ foreach ($noteids as $n) { getID() . " {\n" - . " background-color: #" . $note->getColor() . ";\n" - . " border: 1px solid #" . $note->getColor() . ";\n" - . "}\n"; + . " background-color: #" . $note->getColor() . ";\n" + . " border: 1px solid #" . $note->getColor() . ";\n" + . "}\n"; } ?> +
+ get("New note"); ?> +
+
diff --git a/static/css/editnote.css b/static/css/editnote.css new file mode 100644 index 0000000..a2a9d17 --- /dev/null +++ b/static/css/editnote.css @@ -0,0 +1,13 @@ +/* +This Source Code Form is subject to the terms of the Mozilla Public +License, v. 2.0. If a copy of the MPL was not distributed with this +file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +.editor-toolbar.fullscreen, .CodeMirror-fullscreen { + margin-top: 60px; +} + +.editor-preview-active-side { + margin-top: 10px; +} \ No newline at end of file diff --git a/static/easy-markdown-editor b/static/easy-markdown-editor new file mode 160000 index 0000000..3551c6c --- /dev/null +++ b/static/easy-markdown-editor @@ -0,0 +1 @@ +Subproject commit 3551c6c003c5d382ab65d05a873f4c7d665d14a8 diff --git a/static/js/editnote.js b/static/js/editnote.js new file mode 100644 index 0000000..1cd4fb5 --- /dev/null +++ b/static/js/editnote.js @@ -0,0 +1,48 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + + +var easymde = new EasyMDE({ + element: $("#note_content")[0], + autoDownloadFontAwesome: false, + autofocus: true, + forceSync: true, + status: false, + toolbar: [ + { + name: "save", + action: function saveNote(editor) { + $("#noteform").submit(); + }, + className: "fas fa-save", + title: "Save", + }, + { + name: "exit", + action: function exit(editor) { + document.location.href = "./app.php"; + }, + className: "fas fa-times", + title: "Close", + }, + "|", + "bold", + "italic", + "heading", + "|", + "quote", + "unordered-list", + "ordered-list", + "horizontal-rule", + "|", + "side-by-side", + "fullscreen" + ] +}); + +var autosaveTimer = setInterval(function () { + $("#noteform").submit(); +}, 10 * 1000); \ No newline at end of file