From 39fcc5d52ac4c6e76ff87d95ef5b4b63300fe071 Mon Sep 17 00:00:00 2001 From: winter Date: Sat, 22 Mar 2025 19:41:55 +0000 Subject: implement editing pages --- routes/pages_fragment.php | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 routes/pages_fragment.php (limited to 'routes') diff --git a/routes/pages_fragment.php b/routes/pages_fragment.php new file mode 100644 index 0000000..886fc81 --- /dev/null +++ b/routes/pages_fragment.php @@ -0,0 +1,56 @@ +mount('/fragment/page/:id', function (array $args) { + $note = Note::find($args['id']); + render_template('pages/page_item', ['note' => $note, 'page' => $note->getPage()]); +}); + +Router::getInstance()->mount('/fragment/page/:id/edit', function (array $args) { + // edit form for a page + $note = Note::find($args['id']); + $user = UserAccount::requireByCurrentSession(); + if ($note->author != $user->actor && !$user->isAdmin) { + throw new Forbidden('you may not edit that page!'); + } + if (!$note->author->isLocal) { + throw new BadRequest("can't edit a non-local page"); + } + + if (isset($_POST['title'])) { + $title = trim($_POST['title']); + $body = trim($_POST['body']); + + $page = $note->getPage(); + $page->title = $title; + $page->content = [ + 'text/markdown' => $body, + 'text/html' => CommonMark::getInstance()->renderToHtml($body) + ]; + + $note->setPage($page); + $note->modified = new DateTimeImmutable(); + if (!PolicyManager::getInstance()->check($note)) { + throw new PolicyRejected(); + } + $note->save(); + $note->publish(); + + if ($user->actor != $note->author) { + (new AdminEditedNoteNotif($user, $note))->processNotifications(); + } + + render_template('pages/page_item', ['note' => $note, 'page' => $page]); + } else { + render_template('pages/editable', ['note' => $note, 'page' => $note->getPage()]); + } +}); \ No newline at end of file -- cgit v1.3