Add favorite note feature, fix setModified when passing UNIX timestamp

This commit is contained in:
Skylar Ittner 2018-11-24 13:55:09 -07:00
parent 91636b15d2
commit 9742b70e71
5 changed files with 65 additions and 4 deletions

View File

@ -57,6 +57,7 @@ switch ($VARS['action']) {
} }
$note->deleteNote(); $note->deleteNote();
returnToSender("note_deleted"); returnToSender("note_deleted");
break;
case "downloadnote": case "downloadnote":
if (empty($VARS['noteid'])) { if (empty($VARS['noteid'])) {
die($Strings->get("invalid parameters", false)); die($Strings->get("invalid parameters", false));
@ -68,4 +69,17 @@ switch ($VARS['action']) {
header("Content-Type: text/markdown; charset=UTF-8"); header("Content-Type: text/markdown; charset=UTF-8");
header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".md\""); header("Content-disposition: attachment; filename=\"" . $note->getCleanTitle() . "_" . $note->getModified() . ".md\"");
echo $note->getText(); 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;
} }

Binary file not shown.

View File

@ -14,6 +14,7 @@ class Note {
private $title = ""; private $title = "";
private $modified = ""; private $modified = "";
private $color = "FFFFFF"; private $color = "FFFFFF";
private $favorite = false;
/** /**
* Create a new Note object. * Create a new Note object.
@ -42,11 +43,12 @@ class Note {
throw new NoSuchNoteException(); 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 = new Note($notedata['content'], $notedata['color'], $notedata['ownerid'], $notedata['noteid']);
$note->setTitle(is_null($notedata['title']) ? "" : $notedata['title']); $note->setTitle(is_null($notedata['title']) ? "" : $notedata['title']);
$note->setModified(is_null($notedata['modified']) ? date("Y-m-d H:i:s") : $notedata['modified']); $note->setModified(is_null($notedata['modified']) ? date("Y-m-d H:i:s") : $notedata['modified']);
$note->setFavorite($notedata['favorite'] == true);
return $note; return $note;
} }
@ -67,7 +69,8 @@ class Note {
'color' => $this->getColor(), 'color' => $this->getColor(),
'content' => $this->getText(), 'content' => $this->getText(),
'title' => $this->getTitle(), '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 // We can't UPDATE the database, so use save as for INSERT
@ -167,6 +170,10 @@ class Note {
return $title; return $title;
} }
/**
* Get the note title stripped of Markdown and trimmed
* @return string
*/
public function getCleanTitle(): string { public function getCleanTitle(): string {
$title = $this->getTitle(); $title = $this->getTitle();
$title = str_replace("*", "", $title); $title = str_replace("*", "", $title);
@ -187,6 +194,14 @@ class Note {
return date("Y-m-d H:i:s", strtotime($this->modified)); 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 * Set the note content
* @param string $markdown * @param string $markdown
@ -232,9 +247,21 @@ class Note {
* @param string $datetime * @param string $datetime
*/ */
public function setModified(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)); $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. * Get this note as an array.
* @return string * @return string
@ -247,6 +274,7 @@ class Note {
'content' => $this->getText(), 'content' => $this->getText(),
'title' => $this->getTitle(), 'title' => $this->getTitle(),
'modified' => $this->getModified(), 'modified' => $this->getModified(),
'favorite' => $this->getFavorite(),
'owner' => [ 'owner' => [
'uid' => $owner->getUID(), 'uid' => $owner->getUID(),
'username' => $owner->getUsername(), 'username' => $owner->getUsername(),
@ -269,7 +297,7 @@ class Note {
"title" => $this->getTitle(), "title" => $this->getTitle(),
"category" => null, "category" => null,
"content" => $this->getText(), "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 = new Note($arr['content'], $arr['color'], $arr['owner']['uid'], $arr['noteid']);
$note->setTitle($arr['title']); $note->setTitle($arr['title']);
$note->setModified($arr['modified']); $note->setModified($arr['modified']);
$note->setFavorite($arr['favorite']);
return $note; return $note;
} }

View File

@ -36,6 +36,8 @@ if (json_last_error() == JSON_ERROR_NONE) {
$requestdata = array_merge($requestdata, $requestjson); $requestdata = array_merge($requestdata, $requestjson);
} }
file_put_contents("/var/www/html/debug.log", var_export($requestdata, true));
switch ($_SERVER['REQUEST_METHOD']) { switch ($_SERVER['REQUEST_METHOD']) {
case "GET": case "GET":
if (count($route) == 1) { if (count($route) == 1) {
@ -67,6 +69,10 @@ switch ($_SERVER['REQUEST_METHOD']) {
$note->setModified($requestdata['modified']); $note->setModified($requestdata['modified']);
} }
if (!empty($requestdata['favorite']) && $requestdata['favorite'] == true) {
$note->setFavorite(true);
}
$note->setOwner($user); $note->setOwner($user);
$note->saveNote(); $note->saveNote();
@ -84,6 +90,11 @@ switch ($_SERVER['REQUEST_METHOD']) {
} else { } else {
$note->setModified($requestdata['modified']); $note->setModified($requestdata['modified']);
} }
if (!empty($requestdata['favorite']) && $requestdata['favorite'] == true) {
$note->setFavorite(true);
} else {
$note->setFavorite(false);
}
$note->saveNote(); $note->saveNote();
exit(json_encode($note->toNextcloud())); exit(json_encode($note->toNextcloud()));
} else { } else {

View File

@ -35,8 +35,15 @@ foreach ($notes as $note) {
<div class="col-12 col-sm-6 col-md-6 col-lg-4"> <div class="col-12 col-sm-6 col-md-6 col-lg-4">
<div class="card mb-3" data-color="<?php echo $note->getColor(); ?>" id="notecard_<?php echo $note->getID(); ?>"> <div class="card mb-3" data-color="<?php echo $note->getColor(); ?>" id="notecard_<?php echo $note->getID(); ?>">
<div class="card-body"> <div class="card-body">
<div class="float-right">
<a href="./action.php?action=favoritenote&noteid=<?php echo $note->getID(); ?>" class="text-<?php echo $note->getFavorite() ? "warning" : "body"; ?>">
<i class="fa<?php echo $note->getFavorite() ? "s" : "r"; ?> fa-star"></i>
</a>
</div>
<div class="card-text">
<?php echo $note->getHTML(); ?> <?php echo $note->getHTML(); ?>
</div> </div>
</div>
<div class="card-footer"> <div class="card-footer">
<a href="./app.php?page=editnote&note=<?php echo $note->getID(); ?>" class="text-body mr-2"> <a href="./app.php?page=editnote&note=<?php echo $note->getID(); ?>" class="text-body mr-2">