From e6171255a4f7b8992d7f72673f7b961754237352 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 21 Oct 2023 11:53:56 +0200 Subject: [PATCH] Create base page creation form backend --- .../src/Http/DashboardController.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/realtime-compiler/src/Http/DashboardController.php b/packages/realtime-compiler/src/Http/DashboardController.php index ea85c49af65..11bca08ab1c 100644 --- a/packages/realtime-compiler/src/Http/DashboardController.php +++ b/packages/realtime-compiler/src/Http/DashboardController.php @@ -6,9 +6,13 @@ use Hyde\Hyde; use OutOfBoundsException; +use Hyde\Pages\BladePage; use Illuminate\Support\Str; use Illuminate\Support\Arr; +use Hyde\Pages\MarkdownPage; +use Hyde\Pages\MarkdownPost; use Hyde\Pages\Concerns\HydePage; +use Hyde\Pages\DocumentationPage; use Illuminate\Support\HtmlString; use Hyde\Foundation\Facades\Routes; use Illuminate\Support\Facades\Process; @@ -16,6 +20,8 @@ use Hyde\Framework\Actions\AnonymousViewCompiler; use Desilva\Microserve\Request; use Composer\InstalledVersions; +use Hyde\Framework\Actions\CreatesNewPageSourceFile; +use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; use Symfony\Component\HttpKernel\Exception\HttpException; use function abort; @@ -62,6 +68,7 @@ protected function handlePostRequest(): void { $actions = array_combine($actions = [ 'openInEditor', + 'createPage', ], $actions); $action = $this->request->data['action'] ?? abort(400, 'Must provide action'); @@ -72,6 +79,10 @@ protected function handlePostRequest(): void $page = Routes::getOrFail($routeKey)->getPage(); $this->openInEditor($page); } + + if ($action === 'createPage') { + $this->createPage(); + } } public function show(): string @@ -174,6 +185,38 @@ protected function openInEditor(HydePage $page): void } } + protected function createPage(): void + { + if ($this->enableEditor()) { + // Required data + $title = $this->request->data['titleInput'] ?? abort(400, 'Must provide title'); + $content = $this->request->data['contentInput'] ?? abort(400, 'Must provide content'); + $pageType = $this->request->data['pageTypeSelection'] ?? abort(400, 'Must provide page type'); + + // Optional data + $postDescription = $this->request->data['postDescription'] ?? null; + $postCategory = $this->request->data['postCategory'] ?? null; + $postAuthor = $this->request->data['postAuthor'] ?? null; + $postDate = $this->request->data['postDate'] ?? null; + + // Match page class + $pageClass = match ($pageType) { + 'blade-page' => BladePage::class, + 'markdown-page' => MarkdownPage::class, + 'markdown-post' => MarkdownPost::class, + 'documentation-page' => DocumentationPage::class, + default => throw new HttpException(400, "Invalid page type '$pageType'"), + }; + + if ($pageClass === MarkdownPost::class) { + $creator = new CreatesNewMarkdownPostFile($title, $postDescription, $postCategory, $postAuthor, $postDate, $content); + } else { + $creator = new CreatesNewPageSourceFile($title, $pageClass, false, $content); + } + $creator->save(); + } + } + protected static function injectDashboardButton(string $contents): string { return str_replace('', sprintf('%s', self::button()), $contents);