downgrade to kirby v3
This commit is contained in:
@@ -6,7 +6,6 @@ use Kirby\Exception\DuplicateException;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\LogicException;
|
||||
use Kirby\Exception\PermissionException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
@@ -23,6 +22,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the sorting number of the page can be changed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param int|null $num
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the given number is invalid
|
||||
*/
|
||||
public static function changeNum(Page $page, int $num = null): bool
|
||||
@@ -37,6 +39,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the slug for the page can be changed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param string $slug
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\DuplicateException If a page with this slug already exists
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the slug
|
||||
*/
|
||||
@@ -52,27 +57,30 @@ class PageRules
|
||||
}
|
||||
|
||||
self::validateSlugLength($slug);
|
||||
self::validateSlugProtectedPaths($page, $slug);
|
||||
|
||||
$siblings = $page->parentModel()->children();
|
||||
$drafts = $page->parentModel()->drafts();
|
||||
|
||||
if ($siblings->find($slug)?->is($page) === false) {
|
||||
throw new DuplicateException([
|
||||
'key' => 'page.duplicate',
|
||||
'data' => [
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
if ($duplicate = $siblings->find($slug)) {
|
||||
if ($duplicate->is($page) === false) {
|
||||
throw new DuplicateException([
|
||||
'key' => 'page.duplicate',
|
||||
'data' => [
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($drafts->find($slug)?->is($page) === false) {
|
||||
throw new DuplicateException([
|
||||
'key' => 'page.draft.duplicate',
|
||||
'data' => [
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
if ($duplicate = $drafts->find($slug)) {
|
||||
if ($duplicate->is($page) === false) {
|
||||
throw new DuplicateException([
|
||||
'key' => 'page.draft.duplicate',
|
||||
'data' => [
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -81,31 +89,38 @@ class PageRules
|
||||
/**
|
||||
* Validates if the status for the page can be changed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param string $status
|
||||
* @param int|null $position
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the given status is invalid
|
||||
*/
|
||||
public static function changeStatus(
|
||||
Page $page,
|
||||
string $status,
|
||||
int $position = null
|
||||
): bool {
|
||||
public static function changeStatus(Page $page, string $status, int $position = null): bool
|
||||
{
|
||||
if (isset($page->blueprint()->status()[$status]) === false) {
|
||||
throw new InvalidArgumentException(['key' => 'page.status.invalid']);
|
||||
}
|
||||
|
||||
return match ($status) {
|
||||
'draft' => static::changeStatusToDraft($page),
|
||||
'listed' => static::changeStatusToListed($page, $position),
|
||||
'unlisted' => static::changeStatusToUnlisted($page),
|
||||
default => throw new InvalidArgumentException(['key' => 'page.status.invalid'])
|
||||
};
|
||||
switch ($status) {
|
||||
case 'draft':
|
||||
return static::changeStatusToDraft($page);
|
||||
case 'listed':
|
||||
return static::changeStatusToListed($page, $position);
|
||||
case 'unlisted':
|
||||
return static::changeStatusToUnlisted($page);
|
||||
default:
|
||||
throw new InvalidArgumentException(['key' => 'page.status.invalid']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates if a page can be converted to a draft
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status or the page cannot be converted to a draft
|
||||
*/
|
||||
public static function changeStatusToDraft(Page $page): bool
|
||||
public static function changeStatusToDraft(Page $page)
|
||||
{
|
||||
if ($page->permissions()->changeStatus() !== true) {
|
||||
throw new PermissionException([
|
||||
@@ -131,10 +146,13 @@ class PageRules
|
||||
/**
|
||||
* Validates if the status of a page can be changed to listed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param int $position
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the given position is invalid
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status or the status for the page cannot be changed by any user
|
||||
*/
|
||||
public static function changeStatusToListed(Page $page, int $position): bool
|
||||
public static function changeStatusToListed(Page $page, int $position)
|
||||
{
|
||||
// no need to check for status changing permissions,
|
||||
// instead we need to check for sorting permissions
|
||||
@@ -163,6 +181,8 @@ class PageRules
|
||||
/**
|
||||
* Validates if the status of a page can be changed to unlisted
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status
|
||||
*/
|
||||
public static function changeStatusToUnlisted(Page $page)
|
||||
@@ -175,6 +195,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the template of the page can be changed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param string $template
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\LogicException If the template of the page cannot be changed at all
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the template
|
||||
*/
|
||||
@@ -189,12 +212,7 @@ class PageRules
|
||||
]);
|
||||
}
|
||||
|
||||
$blueprints = $page->blueprints();
|
||||
|
||||
if (
|
||||
count($blueprints) <= 1 ||
|
||||
in_array($template, array_column($blueprints, 'name')) === false
|
||||
) {
|
||||
if (count($page->blueprints()) <= 1) {
|
||||
throw new LogicException([
|
||||
'key' => 'page.changeTemplate.invalid',
|
||||
'data' => ['slug' => $page->slug()]
|
||||
@@ -207,6 +225,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the title of the page can be changed
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param string $title
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the new title is empty
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the title
|
||||
*/
|
||||
@@ -221,7 +242,11 @@ class PageRules
|
||||
]);
|
||||
}
|
||||
|
||||
static::validateTitleLength($title);
|
||||
if (Str::length($title) === 0) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'page.changeTitle.empty',
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -229,6 +254,8 @@ class PageRules
|
||||
/**
|
||||
* Validates if the page can be created
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\DuplicateException If the same page or a draft already exists
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the slug is invalid
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to create this page
|
||||
@@ -245,7 +272,6 @@ class PageRules
|
||||
}
|
||||
|
||||
self::validateSlugLength($page->slug());
|
||||
self::validateSlugProtectedPaths($page, $page->slug());
|
||||
|
||||
if ($page->exists() === true) {
|
||||
throw new DuplicateException([
|
||||
@@ -280,6 +306,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the page can be deleted
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param bool $force
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\LogicException If the page has children and should not be force-deleted
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to delete the page
|
||||
*/
|
||||
@@ -304,13 +333,14 @@ class PageRules
|
||||
/**
|
||||
* Validates if the page can be duplicated
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param string $slug
|
||||
* @param array $options
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to duplicate the page
|
||||
*/
|
||||
public static function duplicate(
|
||||
Page $page,
|
||||
string $slug,
|
||||
array $options = []
|
||||
): bool {
|
||||
public static function duplicate(Page $page, string $slug, array $options = []): bool
|
||||
{
|
||||
if ($page->permissions()->duplicate() !== true) {
|
||||
throw new PermissionException([
|
||||
'key' => 'page.duplicate.permission',
|
||||
@@ -325,82 +355,12 @@ class PageRules
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the page can be moved
|
||||
* to the given parent
|
||||
*/
|
||||
public static function move(Page $page, Site|Page $parent): bool
|
||||
{
|
||||
// if nothing changes, there's no need for checks
|
||||
if ($parent->is($page->parent()) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($page->permissions()->move() !== true) {
|
||||
throw new PermissionException([
|
||||
'key' => 'page.move.permission',
|
||||
'data' => [
|
||||
'slug' => $page->slug()
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// the page cannot be moved into itself
|
||||
if ($parent instanceof Page && ($page->is($parent) === true || $page->isAncestorOf($parent) === true)) {
|
||||
throw new LogicException([
|
||||
'key' => 'page.move.ancestor',
|
||||
]);
|
||||
}
|
||||
|
||||
// check for duplicates
|
||||
if ($parent->childrenAndDrafts()->find($page->slug())) {
|
||||
throw new DuplicateException([
|
||||
'key' => 'page.move.duplicate',
|
||||
'data' => [
|
||||
'slug' => $page->slug(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$allowed = [];
|
||||
|
||||
// collect all allowed subpage templates
|
||||
foreach ($parent->blueprint()->sections() as $section) {
|
||||
// only take pages sections into consideration
|
||||
if ($section->type() !== 'pages') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// only consider page sections that list pages
|
||||
// of the targeted new parent page
|
||||
if ($section->parent() !== $parent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// go through all allowed blueprints and
|
||||
// add the name to the allow list
|
||||
foreach ($section->blueprints() as $blueprint) {
|
||||
$allowed[] = $blueprint['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// check if the template of this page is allowed as subpage type
|
||||
if (in_array($page->intendedTemplate()->name(), $allowed) === false) {
|
||||
throw new PermissionException([
|
||||
'key' => 'page.move.template',
|
||||
'data' => [
|
||||
'template' => $page->intendedTemplate()->name(),
|
||||
'parent' => $parent->id() ?? '/',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the page can be published
|
||||
* (status change from draft to listed or unlisted)
|
||||
*
|
||||
* @param Page $page
|
||||
* @return bool
|
||||
*/
|
||||
public static function publish(Page $page): bool
|
||||
{
|
||||
@@ -426,6 +386,9 @@ class PageRules
|
||||
/**
|
||||
* Validates if the page can be updated
|
||||
*
|
||||
* @param \Kirby\Cms\Page $page
|
||||
* @param array $content
|
||||
* @return bool
|
||||
* @throws \Kirby\Exception\PermissionException If the user is not allowed to update the page
|
||||
*/
|
||||
public static function update(Page $page, array $content = []): bool
|
||||
@@ -446,9 +409,11 @@ class PageRules
|
||||
* Ensures that the slug is not empty and doesn't exceed the maximum length
|
||||
* to make sure that the directory name will be accepted by the filesystem
|
||||
*
|
||||
* @param string $slug New slug to check
|
||||
* @return void
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the slug is empty or too long
|
||||
*/
|
||||
public static function validateSlugLength(string $slug): void
|
||||
protected static function validateSlugLength(string $slug): void
|
||||
{
|
||||
$slugLength = Str::length($slug);
|
||||
|
||||
@@ -471,48 +436,4 @@ class PageRules
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ensure that a top-level page path does not start with one of
|
||||
* the reserved URL paths, e.g. for API or the Panel
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the page ID starts as one of the disallowed paths
|
||||
*/
|
||||
protected static function validateSlugProtectedPaths(
|
||||
Page $page,
|
||||
string $slug
|
||||
): void {
|
||||
if ($page->parent() === null) {
|
||||
$paths = A::map(
|
||||
['api', 'assets', 'media', 'panel'],
|
||||
fn ($url) => $page->kirby()->url($url, true)->path()->toString()
|
||||
);
|
||||
|
||||
$index = array_search($slug, $paths);
|
||||
|
||||
if ($index !== false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'page.changeSlug.reserved',
|
||||
'data' => [
|
||||
'path' => $paths[$index]
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the page title is not empty
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the title is empty
|
||||
*/
|
||||
public static function validateTitleLength(string $title): void
|
||||
{
|
||||
if (Str::length($title) === 0) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'page.changeTitle.empty',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user