Skip to content

Commit

Permalink
Fix page creation from panel
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Oct 7, 2024
1 parent f71bf3d commit 601f3f5
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions formwork/src/Panel/Controllers/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Formwork\Panel\ContentHistory\ContentHistoryEvent;
use Formwork\Parsers\Yaml;
use Formwork\Router\RouteParams;
use Formwork\Schemes\Schemes;
use Formwork\Site;
use Formwork\Utils\Arr;
use Formwork\Utils\Constraint;
Expand Down Expand Up @@ -48,7 +47,7 @@ class PagesController extends AbstractController
/**
* Pages@index action
*/
public function index(Schemes $schemes): Response
public function index(): Response
{
$this->ensurePermission('pages.index');

Expand Down Expand Up @@ -80,7 +79,7 @@ public function index(Schemes $schemes): Response
/**
* Pages@create action
*/
public function create(Schemes $schemes): RedirectResponse
public function create(): RedirectResponse
{
$this->ensurePermission('pages.create');

Expand Down Expand Up @@ -213,8 +212,8 @@ public function edit(RouteParams $routeParams): Response

$this->modal('renameFile');

$contentHistory = $page->path()
? new ContentHistory($page->path())
$contentHistory = $page->contentPath()
? new ContentHistory($page->contentPath())
: null;

return new Response($this->view('pages.editor', [
Expand Down Expand Up @@ -332,12 +331,12 @@ public function delete(RouteParams $routeParams): RedirectResponse
return $this->redirectToReferer(default: '/pages/');
}

if ($page->path() !== null) {
if ($page->contentPath() !== null) {
// Delete just the content file only if there are more than one language
if ($page->contentFile() !== null && $routeParams->has('language') && count($page->languages()->available()) > 1) {
FileSystem::delete($page->contentFile()->path());
} else {
FileSystem::delete($page->path(), recursive: true);
FileSystem::delete($page->contentPath(), recursive: true);
}
}

Expand Down Expand Up @@ -396,7 +395,7 @@ public function deleteFile(RouteParams $routeParams): RedirectResponse
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $routeParams->get('page')]));
}

FileSystem::delete($page->path() . $routeParams->get('filename'));
FileSystem::delete($page->contentPath() . $routeParams->get('filename'));

$this->panel()->notify($this->translate('panel.pages.page.fileDeleted'), 'success');
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $routeParams->get('page')]));
Expand Down Expand Up @@ -432,7 +431,7 @@ public function renameFile(RouteParams $routeParams, Request $request): Redirect
if ($page->files()->has($newName)) {
$this->panel()->notify($this->translate('panel.pages.page.cannotRenameFile.fileAlreadyExists'), 'error');
} else {
FileSystem::move($page->path() . $previousName, $page->path() . $newName);
FileSystem::move($page->contentPath() . $previousName, $page->contentPath() . $newName);
$this->panel()->notify($this->translate('panel.pages.page.fileRenamed'), 'success');
}
}
Expand Down Expand Up @@ -555,7 +554,7 @@ protected function createPage(FieldCollection $fieldCollection): Page
$scheme = $this->app->schemes()->get('pages.' . $fieldCollection->get('template')->value());

$path = FileSystem::joinPaths(
(string) $parent->path(),
(string) $parent->contentPath(),
$this->makePageNum($parent, $scheme->options()->get('num')) . '-' . $fieldCollection->get('slug')->value(),
'/'
);
Expand Down Expand Up @@ -651,18 +650,18 @@ protected function updatePage(Page $page, RequestData $requestData, FieldCollect

$fileContent = Str::wrap(Yaml::encode($frontmatter), '---' . PHP_EOL) . $content;

if ($page->path() === null) {
if ($page->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing page path');
}

if ($this->site()->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing site path');
}

FileSystem::write($page->path() . $filename, $fileContent);
FileSystem::write($page->contentPath() . $filename, $fileContent);
FileSystem::touch($this->site()->contentPath());

$contentHistory = new ContentHistory($page->path());
$contentHistory = new ContentHistory($page->contentPath());

$contentHistory->update(ContentHistoryEvent::Edited, $this->user()->username(), time());
$contentHistory->save();
Expand Down Expand Up @@ -744,18 +743,18 @@ protected function updatePage(Page $page, RequestData $requestData, FieldCollect
*/
protected function processPageUploads(array $files, Page $page, ?array $mimeTypes = null, ?string $name = null, bool $overwrite = false): void
{
$mimeTypes ??= Arr::map($this->config->get('system.files.allowedExtensions'), fn (string $ext) => MimeType::fromExtension($ext));
$mimeTypes ??= Arr::map($this->config->get('system.files.allowedExtensions'), fn(string $ext) => MimeType::fromExtension($ext));

$fileUploader = new FileUploader($mimeTypes);

foreach ($files as $file) {
if (!$file->isUploaded()) {
throw new TranslatedException(sprintf('Cannot upload file "%s"', $file->fieldName()), $file->getErrorTranslationString());
}
if ($page->path() === null) {
if ($page->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing page path');
}
$uploadedFile = $fileUploader->upload($file, $page->path(), $name, $overwrite);
$uploadedFile = $fileUploader->upload($file, $page->contentPath(), $name, $overwrite);
// Process JPEG and PNG images according to system options (e.g. quality)
if ($this->config->get('system.uploads.processImages') && in_array($uploadedFile->mimeType(), ['image/jpeg', 'image/png'], true)) {
$image = new Image($uploadedFile->path(), $this->config->get('system.images'));
Expand Down Expand Up @@ -784,12 +783,12 @@ protected function makePageNum(Page|Site $parent, ?string $mode): string
*/
protected function changePageName(Page $page, string $name): Page
{
if ($page->path() === null) {
if ($page->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing page path');
}
$directory = dirname($page->path());
$directory = dirname($page->contentPath());
$destination = FileSystem::joinPaths($directory, $name, DS);
FileSystem::moveDirectory($page->path(), $destination);
FileSystem::moveDirectory($page->contentPath(), $destination);
return $this->site()->retrievePage($destination);
}

Expand All @@ -798,21 +797,21 @@ protected function changePageName(Page $page, string $name): Page
*/
protected function changePageParent(Page $page, Page|Site $parent): Page
{
if ($parent->path() === null) {
if ($parent->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing parent page path');
}

if ($page->path() === null) {
if ($page->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing page path');
}

if ($page->relativePath() === null) {
if ($page->contentRelativePath() === null) {
throw new UnexpectedValueException('Unexpected missing page relative path');
}

$destination = FileSystem::joinPaths($parent->path(), basename($page->relativePath()), DS);
$destination = FileSystem::joinPaths($parent->contentPath(), basename($page->contentRelativePath()), DS);

FileSystem::moveDirectory($page->path(), $destination);
FileSystem::moveDirectory($page->contentPath(), $destination);
return $this->site()->retrievePage($destination);
}

Expand All @@ -821,15 +820,15 @@ protected function changePageParent(Page $page, Page|Site $parent): Page
*/
protected function changePageTemplate(Page $page, string $template): Page
{
if ($page->path() === null) {
if ($page->contentPath() === null) {
throw new UnexpectedValueException('Unexpected missing page path');
}

if ($page->contentFile() === null) {
throw new UnexpectedValueException('Unexpected missing content file');
}

$destination = $page->path() . $template . $this->config->get('system.pages.content.extension');
$destination = $page->contentPath() . $template . $this->config->get('system.pages.content.extension');
FileSystem::move($page->contentFile()->path(), $destination);
$page->reload();
return $page;
Expand Down

0 comments on commit 601f3f5

Please sign in to comment.