Upgrade to 3.2.0

This commit is contained in:
Bastian Allgeier
2019-06-25 09:56:08 +02:00
parent 9e18cf635d
commit 9c89153d35
296 changed files with 14408 additions and 2504 deletions

View File

@@ -3,7 +3,7 @@
namespace Kirby\Cms;
use Closure;
use Kirby\Data\Data;
use Kirby\Exception\DuplicateException;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
@@ -12,6 +12,15 @@ use Kirby\Toolkit\A;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
/**
* PageActions
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://getkirby.com/license
*/
trait PageActions
{
@@ -23,7 +32,7 @@ trait PageActions
* @param int $num
* @return self
*/
public function changeNum(int $num = null): self
public function changeNum(int $num = null)
{
if ($this->isDraft() === true) {
throw new LogicException('Drafts cannot change their sorting number');
@@ -63,7 +72,7 @@ trait PageActions
* @param string $language
* @return self
*/
public function changeSlug(string $slug, string $languageCode = null): self
public function changeSlug(string $slug, string $languageCode = null)
{
// always sanitize the slug
$slug = Str::slug($slug);
@@ -90,8 +99,11 @@ trait PageActions
'root' => null
]);
// actually move stuff on disk
if ($oldPage->exists() === true) {
// remove the lock of the old page
$oldPage->lock()->remove();
// actually move stuff on disk
if (Dir::move($oldPage->root(), $newPage->root()) !== true) {
throw new LogicException('The page directory cannot be moved');
}
@@ -117,7 +129,7 @@ trait PageActions
* @param string $language
* @return self
*/
protected function changeSlugForLanguage(string $slug, string $languageCode = null): self
protected function changeSlugForLanguage(string $slug, string $languageCode = null)
{
$language = $this->kirby()->language($languageCode);
@@ -145,9 +157,9 @@ trait PageActions
*
* @param string $status "draft", "listed" or "unlisted"
* @param integer $position Optional sorting number
* @return Page
* @return self
*/
public function changeStatus(string $status, int $position = null): self
public function changeStatus(string $status, int $position = null)
{
switch ($status) {
case 'draft':
@@ -161,7 +173,7 @@ trait PageActions
}
}
protected function changeStatusToDraft(): self
protected function changeStatusToDraft()
{
$page = $this->commit('changeStatus', [$this, 'draft'], function ($page) {
return $page->unpublish();
@@ -170,7 +182,11 @@ trait PageActions
return $page;
}
protected function changeStatusToListed(int $position = null): self
/**
* @param int $position
* @return self
*/
protected function changeStatusToListed(int $position = null)
{
// create a sorting number for the page
$num = $this->createNum($position);
@@ -191,7 +207,10 @@ trait PageActions
return $page;
}
protected function changeStatusToUnlisted(): self
/**
* @return self
*/
protected function changeStatusToUnlisted()
{
if ($this->status() === 'unlisted') {
return $this;
@@ -212,7 +231,7 @@ trait PageActions
* @param string $template
* @return self
*/
public function changeTemplate(string $template): self
public function changeTemplate(string $template)
{
if ($template === $this->template()->name()) {
return $this;
@@ -259,7 +278,7 @@ trait PageActions
* @param string|null $languageCode
* @return self
*/
public function changeTitle(string $title, string $languageCode = null): self
public function changeTitle(string $title, string $languageCode = null)
{
return $this->commit('changeTitle', [$this, $title, $languageCode], function ($page, $title, $languageCode) {
return $page->save(['title' => $title], $languageCode);
@@ -291,13 +310,76 @@ trait PageActions
return $result;
}
/**
* Copies the page to a new parent
*
* @param array $options
* @return Kirby\Cms\Page
*/
public function copy(array $options = [])
{
$slug = $options['slug'] ?? $this->slug();
$isDraft = $options['isDraft'] ?? $this->isDraft();
$parent = $options['parent'] ?? null;
$parentModel = $options['parent'] ?? $this->site();
$num = $options['num'] ?? null;
$children = $options['children'] ?? false;
$files = $options['files'] ?? false;
// clean up the slug
$slug = Str::slug($slug);
if ($parentModel->findPageOrDraft($slug)) {
throw new DuplicateException([
'key' => 'page.duplicate',
'data' => [
'slug' => $slug
]
]);
}
$tmp = new static([
'isDraft' => $isDraft,
'num' => $num,
'parent' => $parent,
'slug' => $slug,
]);
$ignore = [];
// don't copy files
if ($files === false) {
foreach ($this->files() as $file) {
$ignore[] = $file->root();
// append all content files
array_push($ignore, ...$file->contentFiles());
}
}
Dir::copy($this->root(), $tmp->root(), $children, $ignore);
$copy = $parentModel->clone()->findPageOrDraft($slug);
// remove all translated slugs
if ($this->kirby()->multilang() === true) {
foreach ($this->kirby()->languages() as $language) {
if ($language->isDefault() === false) {
$copy = $copy->save(['slug' => null], $language->code());
}
}
}
return $copy;
}
/**
* Creates and stores a new page
*
* @param array $props
* @return self
*/
public static function create(array $props): self
public static function create(array $props)
{
// clean up the slug
$props['slug'] = Str::slug($props['slug'] ?? $props['content']['title'] ?? null);
@@ -352,7 +434,7 @@ trait PageActions
* @param array $props
* @return self
*/
public function createChild(array $props): self
public function createChild(array $props)
{
$props = array_merge($props, [
'url' => null,
@@ -381,10 +463,7 @@ trait PageActions
case 'date':
case 'datetime':
$format = $mode === 'date' ? 'Ymd' : 'YmdHi';
$date = $this->content()->get('date')->value();
$time = empty($date) === true ? time() : strtotime($date);
return date($format, $time);
return $this->date()->toDate($format, 'now');
break;
case 'default':
@@ -473,6 +552,31 @@ trait PageActions
});
}
/**
* Duplicates the page with the given
* slug and optionally copies all files
*
* @param string $slug
* @param array $options
* @return Kirby\Cms\Page
*/
public function duplicate(string $slug = null, array $options = [])
{
// create the slug for the duplicate
$slug = Str::slug($slug ?? $this->slug() . '-copy');
return $this->commit('duplicate', [$this, $slug, $options], function ($page, $slug, $options) {
return $this->copy([
'parent' => $this->parent(),
'slug' => $slug,
'isDraft' => true,
'files' => $options['files'] ?? false,
'children' => $options['children'] ?? false,
]);
});
}
public function publish()
{
if ($this->isDraft() === false) {
@@ -508,8 +612,9 @@ trait PageActions
/**
* Clean internal caches
* @return self
*/
public function purge(): self
public function purge()
{
$this->children = null;
$this->blueprint = null;