diff --git a/action.php b/action.php index 8bd8bd2..35693c3 100644 --- a/action.php +++ b/action.php @@ -57,6 +57,7 @@ switch ($VARS['action']) { } $note->deleteNote(); returnToSender("note_deleted"); + break; case "downloadnote": if (empty($VARS['noteid'])) { die($Strings->get("invalid parameters", false)); @@ -68,4 +69,17 @@ switch ($VARS['action']) { header("Content-Type: text/markdown; charset=UTF-8"); header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".md\""); echo $note->getText(); + break; + case "favoritenote": + if (empty($VARS['noteid'])) { + die($Strings->get("invalid parameters")); + } + $note = Note::loadNote($VARS['noteid']); + if (!$note->hasWriteAccess(new User($_SESSION['uid']))) { + die($Strings->get("invalid parameters")); + } + $note->setFavorite(!$note->getFavorite()); + $note->saveNote(); + returnToSender(""); + break; } \ No newline at end of file diff --git a/database.mwb b/database.mwb index 63826da..63811fd 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/lib/Note.lib.php b/lib/Note.lib.php index 619d838..16d1085 100644 --- a/lib/Note.lib.php +++ b/lib/Note.lib.php @@ -14,6 +14,7 @@ class Note { private $title = ""; private $modified = ""; private $color = "FFFFFF"; + private $favorite = false; /** * Create a new Note object. @@ -42,11 +43,12 @@ class Note { throw new NoSuchNoteException(); } - $notedata = $database->get('notes', ['noteid', 'ownerid', 'color', 'content', 'title', 'modified'], ['noteid' => $noteid]); + $notedata = $database->get('notes', ['noteid', 'ownerid', 'color', 'content', 'title', 'modified', 'favorite'], ['noteid' => $noteid]); $note = new Note($notedata['content'], $notedata['color'], $notedata['ownerid'], $notedata['noteid']); $note->setTitle(is_null($notedata['title']) ? "" : $notedata['title']); $note->setModified(is_null($notedata['modified']) ? date("Y-m-d H:i:s") : $notedata['modified']); + $note->setFavorite($notedata['favorite'] == true); return $note; } @@ -67,7 +69,8 @@ class Note { 'color' => $this->getColor(), 'content' => $this->getText(), 'title' => $this->getTitle(), - 'modified' => $this->getModified() + 'modified' => $this->getModified(), + 'favorite' => $this->getFavorite() ? 1 : 0 ]; // We can't UPDATE the database, so use save as for INSERT @@ -167,6 +170,10 @@ class Note { return $title; } + /** + * Get the note title stripped of Markdown and trimmed + * @return string + */ public function getCleanTitle(): string { $title = $this->getTitle(); $title = str_replace("*", "", $title); @@ -187,6 +194,14 @@ class Note { return date("Y-m-d H:i:s", strtotime($this->modified)); } + /** + * Get if the note is favorited (starred). + * @return bool + */ + public function getFavorite(): bool { + return $this->favorite; + } + /** * Set the note content * @param string $markdown @@ -232,9 +247,21 @@ class Note { * @param string $datetime */ public function setModified(string $datetime) { + if (is_numeric($datetime)) { + $this->modified = date("Y-m-d H:i:s", $datetime); + return; + } $this->modified = date("Y-m-d H:i:s", strtotime($datetime)); } + /** + * Set the note as favorite or not + * @param bool $favorite + */ + public function setFavorite(bool $favorite) { + $this->favorite = $favorite; + } + /** * Get this note as an array. * @return string @@ -247,6 +274,7 @@ class Note { 'content' => $this->getText(), 'title' => $this->getTitle(), 'modified' => $this->getModified(), + 'favorite' => $this->getFavorite(), 'owner' => [ 'uid' => $owner->getUID(), 'username' => $owner->getUsername(), @@ -269,7 +297,7 @@ class Note { "title" => $this->getTitle(), "category" => null, "content" => $this->getText(), - "favorite" => false + "favorite" => $this->getFavorite() ]; } @@ -304,6 +332,7 @@ class Note { $note = new Note($arr['content'], $arr['color'], $arr['owner']['uid'], $arr['noteid']); $note->setTitle($arr['title']); $note->setModified($arr['modified']); + $note->setFavorite($arr['favorite']); return $note; } diff --git a/lib/nextcloudnotes.php b/lib/nextcloudnotes.php index abe047c..62f6910 100644 --- a/lib/nextcloudnotes.php +++ b/lib/nextcloudnotes.php @@ -36,6 +36,8 @@ if (json_last_error() == JSON_ERROR_NONE) { $requestdata = array_merge($requestdata, $requestjson); } +file_put_contents("/var/www/html/debug.log", var_export($requestdata, true)); + switch ($_SERVER['REQUEST_METHOD']) { case "GET": if (count($route) == 1) { @@ -67,6 +69,10 @@ switch ($_SERVER['REQUEST_METHOD']) { $note->setModified($requestdata['modified']); } + if (!empty($requestdata['favorite']) && $requestdata['favorite'] == true) { + $note->setFavorite(true); + } + $note->setOwner($user); $note->saveNote(); @@ -84,6 +90,11 @@ switch ($_SERVER['REQUEST_METHOD']) { } else { $note->setModified($requestdata['modified']); } + if (!empty($requestdata['favorite']) && $requestdata['favorite'] == true) { + $note->setFavorite(true); + } else { + $note->setFavorite(false); + } $note->saveNote(); exit(json_encode($note->toNextcloud())); } else { diff --git a/pages/home.php b/pages/home.php index f2eccb3..702fe23 100644 --- a/pages/home.php +++ b/pages/home.php @@ -35,7 +35,14 @@ foreach ($notes as $note) {
- getHTML(); ?> + +
+ getHTML(); ?> +