content = $content; $this->color = $color; $this->ownerid = $ownerid; $this->noteid = $noteid; } /** * Load a note from the database and return it. * @global type $database * @param int $noteid * @return \Note * @throws NoSuchNoteException When the note ID isn't found. */ public static function loadNote(int $noteid): Note { global $database; if (!$database->has('notes', ['noteid' => $noteid])) { throw new NoSuchNoteException(); } $notedata = $database->get('notes', ['noteid', 'ownerid', 'color', 'content'], ['noteid' => $noteid]); return new Note($notedata['content'], $notedata['color'], $notedata['ownerid'], $notedata['noteid']); } /** * Save the note to the database. * @global type $database * @param bool $saveas If true, save the note under a new ID. Forced to * true if the current note ID is missing or invalid. * @return int The database ID of the saved note * @throws Exception If there is no note owner set. */ public function saveNote(bool $saveas = false): int { global $database; $data = [ 'ownerid' => $this->ownerid, 'color' => $this->color, 'content' => $this->content ]; // We can't UPDATE the database, so use save as for INSERT if (empty($this->noteid) || !$database->has('notes', ['noteid' => $this->noteid])) { $saveas = true; } if (empty($this->ownerid)) { throw new Exception("No owner set."); } if ($saveas) { $database->insert('notes', $data); return $database->id(); } else { $database->update('notes', $data, ['noteid' => $this->noteid]); return $this->noteid; } } /** * Get the Markdown content of the note. * @return string */ public function getText(): string { return $this->content; } /** * Get the HTML render of the note. * @param bool $fragment Get just the HTML content, instead of a whole HTML5 file * @return string */ public function getHTML(bool $fragment = true): string { $parsedown = new Parsedown; $parsedown->setSafeMode(true); return $parsedown->text($this->content); } /** * Get the note ID. * @return int */ public function getID(): int { return $this->noteid; } /** * Get the note color as RRGGBB hex. * @return string */ public function getColor(): string { return $this->color; } /** * Get the owner UID * @return int */ public function getOwnerID(): int { return $this->ownerid; } /** * Get the owner as a User object * @return \User */ public function getOwner(): User { return new User($this->ownerid); } /** * Set the note content * @param string $markdown */ public function setText(string $markdown) { $this->content = $markdown; } /** * Set the note color to a hex string * @param string $color "RRGGBB" */ public function setColor(string $color) { $this->color = $color; } /** * Set the owner by UID * @param int $uid */ public function setOwnerID(int $uid) { $this->ownerid = $uid; } /** * Set the owner * @param User $user */ public function setOwner(User $owner) { $this->ownerid = $owner->getUID(); } /** * Get this note as an array. * @return string */ public function toArray(): array { $owner = new User($this->ownerid); $arr = [ 'noteid' => $this->noteid, 'color' => $this->color, 'content' => $this->content, 'owner' => [ 'uid' => $owner->getUID(), 'username' => $owner->getUsername(), 'name' => $owner->getName() ] ]; return $arr; } /** * Read an array with the structure from toArray(). * @param array $arr * @return \Note A Note constructed from the array data. */ public static function fromArray(array $arr): Note { return new Note($arr['content'], $arr['color'], $arr['owner']['uid'], $arr['noteid']); } }