Update Kirby and dependencies

This commit is contained in:
Paul Nicoué 2022-08-31 15:02:43 +02:00
parent 503b339974
commit 399fa20902
439 changed files with 66915 additions and 64442 deletions

View file

@ -2,6 +2,7 @@
namespace Kirby\Form\Field;
use Kirby\Cms\App;
use Kirby\Cms\Block;
use Kirby\Cms\Blocks as BlocksCollection;
use Kirby\Cms\Fieldsets;
@ -12,270 +13,273 @@ use Kirby\Form\Form;
use Kirby\Form\Mixin\EmptyState;
use Kirby\Form\Mixin\Max;
use Kirby\Form\Mixin\Min;
use Kirby\Toolkit\Str;
use Throwable;
class BlocksField extends FieldClass
{
use EmptyState;
use Max;
use Min;
use EmptyState;
use Max;
use Min;
protected $blocks;
protected $fieldsets;
protected $group;
protected $pretty;
protected $value = [];
protected $blocks;
protected $fieldsets;
protected $group;
protected $pretty;
protected $value = [];
public function __construct(array $params = [])
{
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? site());
public function __construct(array $params = [])
{
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? App::instance()->site());
parent::__construct($params);
parent::__construct($params);
$this->setEmpty($params['empty'] ?? null);
$this->setGroup($params['group'] ?? 'blocks');
$this->setMax($params['max'] ?? null);
$this->setMin($params['min'] ?? null);
$this->setPretty($params['pretty'] ?? false);
}
$this->setEmpty($params['empty'] ?? null);
$this->setGroup($params['group'] ?? 'blocks');
$this->setMax($params['max'] ?? null);
$this->setMin($params['min'] ?? null);
$this->setPretty($params['pretty'] ?? false);
}
public function blocksToValues($blocks, $to = 'values'): array
{
$result = [];
$fields = [];
public function blocksToValues($blocks, $to = 'values'): array
{
$result = [];
$fields = [];
foreach ($blocks as $block) {
try {
$type = $block['type'];
foreach ($blocks as $block) {
try {
$type = $block['type'];
// get and cache fields at the same time
$fields[$type] ??= $this->fields($block['type']);
// get and cache fields at the same time
$fields[$type] ??= $this->fields($block['type']);
// overwrite the block content with form values
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
// overwrite the block content with form values
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
$result[] = $block;
} catch (Throwable $e) {
$result[] = $block;
$result[] = $block;
} catch (Throwable $e) {
$result[] = $block;
// skip invalid blocks
continue;
}
}
// skip invalid blocks
continue;
}
}
return $result;
}
return $result;
}
public function fields(string $type)
{
return $this->fieldset($type)->fields();
}
public function fields(string $type)
{
return $this->fieldset($type)->fields();
}
public function fieldset(string $type)
{
if ($fieldset = $this->fieldsets->find($type)) {
return $fieldset;
}
public function fieldset(string $type)
{
if ($fieldset = $this->fieldsets->find($type)) {
return $fieldset;
}
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
}
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
}
public function fieldsets()
{
return $this->fieldsets;
}
public function fieldsets()
{
return $this->fieldsets;
}
public function fieldsetGroups(): ?array
{
$fieldsetGroups = $this->fieldsets()->groups();
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
}
public function fieldsetGroups(): ?array
{
$fieldsetGroups = $this->fieldsets()->groups();
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
}
public function fill($value = null)
{
$value = BlocksCollection::parse($value);
$blocks = BlocksCollection::factory($value);
$this->value = $this->blocksToValues($blocks->toArray());
}
public function fill($value = null)
{
$value = BlocksCollection::parse($value);
$blocks = BlocksCollection::factory($value);
$this->value = $this->blocksToValues($blocks->toArray());
}
public function form(array $fields, array $input = [])
{
return new Form([
'fields' => $fields,
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
public function form(array $fields, array $input = [])
{
return new Form([
'fields' => $fields,
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
public function isEmpty(): bool
{
return count($this->value()) === 0;
}
public function isEmpty(): bool
{
return count($this->value()) === 0;
}
public function group(): string
{
return $this->group;
}
public function group(): string
{
return $this->group;
}
public function pretty(): bool
{
return $this->pretty;
}
public function pretty(): bool
{
return $this->pretty;
}
public function props(): array
{
return [
'empty' => $this->empty(),
'fieldsets' => $this->fieldsets()->toArray(),
'fieldsetGroups' => $this->fieldsetGroups(),
'group' => $this->group(),
'max' => $this->max(),
'min' => $this->min(),
] + parent::props();
}
public function props(): array
{
return [
'empty' => $this->empty(),
'fieldsets' => $this->fieldsets()->toArray(),
'fieldsetGroups' => $this->fieldsetGroups(),
'group' => $this->group(),
'max' => $this->max(),
'min' => $this->min(),
] + parent::props();
}
public function routes(): array
{
$field = $this;
public function routes(): array
{
$field = $this;
return [
[
'pattern' => 'uuid',
'action' => fn () => ['uuid' => uuid()]
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () use ($field) {
$value = BlocksCollection::parse(get('html'));
$blocks = BlocksCollection::factory($value);
return $field->blocksToValues($blocks->toArray());
}
],
[
'pattern' => 'fieldsets/(:any)',
'method' => 'GET',
'action' => function ($fieldsetType) use ($field) {
$fields = $field->fields($fieldsetType);
$defaults = $field->form($fields, [])->data(true);
$content = $field->form($fields, $defaults)->values();
return [
[
'pattern' => 'uuid',
'action' => fn () => ['uuid' => Str::uuid()]
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () use ($field) {
$request = App::instance()->request();
return Block::factory([
'content' => $content,
'type' => $fieldsetType
])->toArray();
}
],
[
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
$fields = $field->fields($fieldsetType);
$field = $field->form($fields)->field($fieldName);
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
return $field->blocksToValues($blocks->toArray());
}
],
[
'pattern' => 'fieldsets/(:any)',
'method' => 'GET',
'action' => function ($fieldsetType) use ($field) {
$fields = $field->fields($fieldsetType);
$defaults = $field->form($fields, [])->data(true);
$content = $field->form($fields, $defaults)->values();
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
]);
return Block::factory([
'content' => $content,
'type' => $fieldsetType
])->toArray();
}
],
[
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
$fields = $field->fields($fieldsetType);
$field = $field->form($fields)->field($fieldName);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
],
];
}
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
]);
public function store($value)
{
$blocks = $this->blocksToValues((array)$value, 'content');
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
],
];
}
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if (empty($blocks) === true) {
return '';
}
public function store($value)
{
$blocks = $this->blocksToValues((array)$value, 'content');
return $this->valueToJson($blocks, $this->pretty());
}
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if (empty($blocks) === true) {
return '';
}
protected function setFieldsets($fieldsets, $model)
{
if (is_string($fieldsets) === true) {
$fieldsets = [];
}
return $this->valueToJson($blocks, $this->pretty());
}
$this->fieldsets = Fieldsets::factory($fieldsets, [
'parent' => $model
]);
}
protected function setFieldsets($fieldsets, $model)
{
if (is_string($fieldsets) === true) {
$fieldsets = [];
}
protected function setGroup(string $group = null)
{
$this->group = $group;
}
$this->fieldsets = Fieldsets::factory($fieldsets, [
'parent' => $model
]);
}
protected function setPretty(bool $pretty = false)
{
$this->pretty = $pretty;
}
protected function setGroup(string $group = null)
{
$this->group = $group;
}
public function validations(): array
{
return [
'blocks' => function ($value) {
if ($this->min && count($value) < $this->min) {
throw new InvalidArgumentException([
'key' => 'blocks.min.' . ($this->min === 1 ? 'singular' : 'plural'),
'data' => [
'min' => $this->min
]
]);
}
protected function setPretty(bool $pretty = false)
{
$this->pretty = $pretty;
}
if ($this->max && count($value) > $this->max) {
throw new InvalidArgumentException([
'key' => 'blocks.max.' . ($this->max === 1 ? 'singular' : 'plural'),
'data' => [
'max' => $this->max
]
]);
}
public function validations(): array
{
return [
'blocks' => function ($value) {
if ($this->min && count($value) < $this->min) {
throw new InvalidArgumentException([
'key' => 'blocks.min.' . ($this->min === 1 ? 'singular' : 'plural'),
'data' => [
'min' => $this->min
]
]);
}
$fields = [];
$index = 0;
if ($this->max && count($value) > $this->max) {
throw new InvalidArgumentException([
'key' => 'blocks.max.' . ($this->max === 1 ? 'singular' : 'plural'),
'data' => [
'max' => $this->max
]
]);
}
foreach ($value as $block) {
$index++;
$blockType = $block['type'];
$fields = [];
$index = 0;
try {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
foreach ($value as $block) {
$index++;
$blockType = $block['type'];
// store the fields for the next round
$fields[$blockType] = $blockFields;
try {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
// store the fields for the next round
$fields[$blockType] = $blockFields;
// rough first validation
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'blocks.validation',
'data' => [
'index' => $index,
]
]);
}
}
}
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
return true;
}
];
}
// rough first validation
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'blocks.validation',
'data' => [
'index' => $index,
]
]);
}
}
}
return true;
}
];
}
}

View file

@ -2,6 +2,7 @@
namespace Kirby\Form\Field;
use Kirby\Cms\App;
use Kirby\Cms\Blueprint;
use Kirby\Cms\Fieldset;
use Kirby\Cms\Layout;
@ -13,220 +14,222 @@ use Throwable;
class LayoutField extends BlocksField
{
protected $layouts;
protected $settings;
protected $layouts;
protected $settings;
public function __construct(array $params)
{
$this->setModel($params['model'] ?? site());
$this->setLayouts($params['layouts'] ?? ['1/1']);
$this->setSettings($params['settings'] ?? null);
public function __construct(array $params)
{
$this->setModel($params['model'] ?? App::instance()->site());
$this->setLayouts($params['layouts'] ?? ['1/1']);
$this->setSettings($params['settings'] ?? null);
parent::__construct($params);
}
parent::__construct($params);
}
public function fill($value = null)
{
$value = $this->valueFromJson($value);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
public function fill($value = null)
{
$value = $this->valueFromJson($value);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
foreach ($layouts as $layoutIndex => $layout) {
if ($this->settings !== null) {
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
}
foreach ($layouts as $layoutIndex => $layout) {
if ($this->settings !== null) {
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
}
foreach ($layout['columns'] as $columnIndex => $column) {
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
}
}
foreach ($layout['columns'] as $columnIndex => $column) {
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
}
}
$this->value = $layouts;
}
$this->value = $layouts;
}
public function attrsForm(array $input = [])
{
$settings = $this->settings();
public function attrsForm(array $input = [])
{
$settings = $this->settings();
return new Form([
'fields' => $settings ? $settings->fields() : [],
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
return new Form([
'fields' => $settings ? $settings->fields() : [],
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
public function layouts(): ?array
{
return $this->layouts;
}
public function layouts(): ?array
{
return $this->layouts;
}
public function props(): array
{
$settings = $this->settings();
public function props(): array
{
$settings = $this->settings();
return array_merge(parent::props(), [
'settings' => $settings !== null ? $settings->toArray() : null,
'layouts' => $this->layouts()
]);
}
return array_merge(parent::props(), [
'settings' => $settings !== null ? $settings->toArray() : null,
'layouts' => $this->layouts()
]);
}
public function routes(): array
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field) {
$defaults = $field->attrsForm([])->data(true);
$attrs = $field->attrsForm($defaults)->values();
$columns = get('columns') ?? ['1/1'];
public function routes(): array
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field) {
$request = App::instance()->request();
return Layout::factory([
'attrs' => $attrs,
'columns' => array_map(fn ($width) => [
'blocks' => [],
'id' => uuid(),
'width' => $width,
], $columns)
])->toArray();
},
];
$defaults = $field->attrsForm([])->data(true);
$attrs = $field->attrsForm($defaults)->values();
$columns = $request->get('columns') ?? ['1/1'];
$routes[] = [
'pattern' => 'fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldName, string $path = null) use ($field) {
$form = $field->attrsForm();
$field = $form->field($fieldName);
return Layout::factory([
'attrs' => $attrs,
'columns' => array_map(fn ($width) => [
'blocks' => [],
'id' => Str::uuid(),
'width' => $width,
], $columns)
])->toArray();
},
];
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
]);
$routes[] = [
'pattern' => 'fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldName, string $path = null) use ($field) {
$form = $field->attrsForm();
$field = $form->field($fieldName);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
];
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
]);
return $routes;
}
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
];
protected function setLayouts(array $layouts = [])
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
$layouts
);
}
return $routes;
}
protected function setSettings($settings = null)
{
if (empty($settings) === true) {
$this->settings = null;
return;
}
protected function setLayouts(array $layouts = [])
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
$layouts
);
}
$settings = Blueprint::extend($settings);
protected function setSettings($settings = null)
{
if (empty($settings) === true) {
$this->settings = null;
return;
}
$settings['icon'] = 'dashboard';
$settings['type'] = 'layout';
$settings['parent'] = $this->model();
$settings = Blueprint::extend($settings);
$this->settings = Fieldset::factory($settings);
}
$settings['icon'] = 'dashboard';
$settings['type'] = 'layout';
$settings['parent'] = $this->model();
public function settings()
{
return $this->settings;
}
$this->settings = Fieldset::factory($settings);
}
public function store($value)
{
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
public function settings()
{
return $this->settings;
}
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if (empty($value) === true) {
return '';
}
public function store($value)
{
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
foreach ($value as $layoutIndex => $layout) {
if ($this->settings !== null) {
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
}
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if (empty($value) === true) {
return '';
}
foreach ($layout['columns'] as $columnIndex => $column) {
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
}
}
foreach ($value as $layoutIndex => $layout) {
if ($this->settings !== null) {
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
}
return $this->valueToJson($value, $this->pretty());
}
foreach ($layout['columns'] as $columnIndex => $column) {
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
}
}
public function validations(): array
{
return [
'layout' => function ($value) {
$fields = [];
$layoutIndex = 0;
return $this->valueToJson($value, $this->pretty());
}
foreach ($value as $layout) {
$layoutIndex++;
public function validations(): array
{
return [
'layout' => function ($value) {
$fields = [];
$layoutIndex = 0;
// validate settings form
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
$errors = $field->errors();
foreach ($value as $layout) {
$layoutIndex++;
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'layout.validation.settings',
'data' => [
'index' => $layoutIndex
]
]);
}
}
// validate settings form
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
$errors = $field->errors();
// validate blocks in the layout
$blockIndex = 0;
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'layout.validation.settings',
'data' => [
'index' => $layoutIndex
]
]);
}
}
foreach ($layout['columns'] ?? [] as $column) {
foreach ($column['blocks'] ?? [] as $block) {
$blockIndex++;
$blockType = $block['type'];
// validate blocks in the layout
$blockIndex = 0;
try {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
foreach ($layout['columns'] ?? [] as $column) {
foreach ($column['blocks'] ?? [] as $block) {
$blockIndex++;
$blockType = $block['type'];
// store the fields for the next round
$fields[$blockType] = $blockFields;
try {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
// store the fields for the next round
$fields[$blockType] = $blockFields;
// rough first validation
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'layout.validation.block',
'data' => [
'blockIndex' => $blockIndex,
'layoutIndex' => $layoutIndex
]
]);
}
}
}
}
}
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
return true;
}
];
}
// rough first validation
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'layout.validation.block',
'data' => [
'blockIndex' => $blockIndex,
'layoutIndex' => $layoutIndex
]
]);
}
}
}
}
}
return true;
}
];
}
}