Initial commit
This commit is contained in:
commit
efa5624dab
687 changed files with 162710 additions and 0 deletions
364
kirby/src/Form/Field/BlocksField.php
Normal file
364
kirby/src/Form/Field/BlocksField.php
Normal file
|
|
@ -0,0 +1,364 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Block;
|
||||
use Kirby\Cms\Blocks as BlocksCollection;
|
||||
use Kirby\Cms\Fieldset;
|
||||
use Kirby\Cms\Fieldsets;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Data\Json;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\NotFoundException;
|
||||
use Kirby\Form\FieldClass;
|
||||
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;
|
||||
|
||||
protected Fieldsets $fieldsets;
|
||||
protected string|null $group;
|
||||
protected bool $pretty;
|
||||
protected mixed $value = [];
|
||||
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->setFieldsets(
|
||||
$params['fieldsets'] ?? null,
|
||||
$params['model'] ?? App::instance()->site()
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function blocksToValues(
|
||||
array $blocks,
|
||||
string $to = 'toFormValues'
|
||||
): array {
|
||||
$result = [];
|
||||
$fields = [];
|
||||
$forms = [];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
try {
|
||||
$type = $block['type'];
|
||||
|
||||
// get and cache fields at the same time
|
||||
$fields[$type] ??= $this->fields($block['type']);
|
||||
$forms[$type] ??= $this->form($fields[$type]);
|
||||
|
||||
// overwrite the block content with form values
|
||||
$block['content'] = $forms[$type]->reset()->fill(input: $block['content'])->$to();
|
||||
|
||||
// create id if not exists
|
||||
$block['id'] ??= Str::uuid();
|
||||
} catch (Throwable) {
|
||||
// skip invalid blocks
|
||||
} finally {
|
||||
$result[] = $block;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fields(string $type): array
|
||||
{
|
||||
return $this->fieldset($type)->fields();
|
||||
}
|
||||
|
||||
public function fieldset(string $type): Fieldset
|
||||
{
|
||||
if ($fieldset = $this->fieldsets->find($type)) {
|
||||
return $fieldset;
|
||||
}
|
||||
|
||||
throw new NotFoundException(
|
||||
'The fieldset ' . $type . ' could not be found'
|
||||
);
|
||||
}
|
||||
|
||||
public function fieldsets(): Fieldsets
|
||||
{
|
||||
return $this->fieldsets;
|
||||
}
|
||||
|
||||
public function fieldsetGroups(): array|null
|
||||
{
|
||||
$groups = $this->fieldsets()->groups();
|
||||
return $groups === [] ? null : $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-suppress MethodSignatureMismatch
|
||||
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
|
||||
*/
|
||||
public function fill(mixed $value): static
|
||||
{
|
||||
$value = BlocksCollection::parse($value);
|
||||
$blocks = BlocksCollection::factory($value)->toArray();
|
||||
$this->value = $this->blocksToValues($blocks);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function form(array $fields): Form
|
||||
{
|
||||
return new Form(
|
||||
fields: $fields,
|
||||
model: $this->model,
|
||||
language: 'current'
|
||||
);
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return count($this->value()) === 0;
|
||||
}
|
||||
|
||||
public function group(): string
|
||||
{
|
||||
return $this->group;
|
||||
}
|
||||
|
||||
public function pretty(): bool
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paste action for blocks:
|
||||
* - generates new uuids for the blocks
|
||||
* - filters only supported fieldsets
|
||||
* - applies max limit if defined
|
||||
*/
|
||||
public function pasteBlocks(array $blocks): array
|
||||
{
|
||||
$blocks = $this->blocksToValues($blocks);
|
||||
|
||||
foreach ($blocks as $index => &$block) {
|
||||
$block['id'] = Str::uuid();
|
||||
|
||||
// remove the block if it's not available
|
||||
try {
|
||||
$this->fieldset($block['type']);
|
||||
} catch (Throwable) {
|
||||
unset($blocks[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($blocks);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return [
|
||||
[
|
||||
'pattern' => 'uuid',
|
||||
'action' => fn (): array => ['uuid' => Str::uuid()]
|
||||
],
|
||||
[
|
||||
'pattern' => 'paste',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field): array {
|
||||
$request = App::instance()->request();
|
||||
$value = BlocksCollection::parse($request->get('html'));
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
|
||||
return $field->pasteBlocks($blocks->toArray());
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)',
|
||||
'method' => 'GET',
|
||||
'action' => function (
|
||||
string $fieldsetType
|
||||
) use ($field): array {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$form = $field->form($fields);
|
||||
|
||||
$form->fill(input: $form->defaults());
|
||||
|
||||
return Block::factory([
|
||||
'content' => $form->toFormValues(),
|
||||
'type' => $fieldsetType
|
||||
])->toArray();
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (
|
||||
string $fieldsetType,
|
||||
string $fieldName,
|
||||
string|null $path = null
|
||||
) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$field = $field->form($fields)->field($fieldName);
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => [
|
||||
...$this->data(),
|
||||
'field' => $field
|
||||
]
|
||||
]);
|
||||
|
||||
return $fieldApi->call(
|
||||
$path,
|
||||
$this->requestMethod(),
|
||||
$this->requestData()
|
||||
);
|
||||
}
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function setDefault(mixed $default = null): void
|
||||
{
|
||||
// set id for blocks if not exists
|
||||
if (is_array($default) === true) {
|
||||
array_walk($default, function (&$block) {
|
||||
$block['id'] ??= Str::uuid();
|
||||
});
|
||||
}
|
||||
|
||||
parent::setDefault($default);
|
||||
}
|
||||
|
||||
protected function setFieldsets(
|
||||
string|array|null $fieldsets,
|
||||
ModelWithContent $model
|
||||
): void {
|
||||
if (is_string($fieldsets) === true) {
|
||||
$fieldsets = [];
|
||||
}
|
||||
|
||||
$this->fieldsets = Fieldsets::factory(
|
||||
$fieldsets,
|
||||
['parent' => $model]
|
||||
);
|
||||
}
|
||||
|
||||
protected function setGroup(string|null $group = null): void
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
protected function setPretty(bool $pretty = false): void
|
||||
{
|
||||
$this->pretty = $pretty;
|
||||
}
|
||||
|
||||
public function toStoredValue(bool $default = false): mixed
|
||||
{
|
||||
$value = $this->toFormValue($default);
|
||||
$blocks = $this->blocksToValues((array)$value, 'toStoredValues');
|
||||
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if ($blocks === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Json::encode($blocks, pretty: $this->pretty());
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'blocks' => function ($value) {
|
||||
if ($this->min && count($value) < $this->min) {
|
||||
throw new InvalidArgumentException(
|
||||
key: match ($this->min) {
|
||||
1 => 'blocks.min.singular',
|
||||
default => 'blocks.min.plural'
|
||||
},
|
||||
data: ['min' => $this->min]
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->max && count($value) > $this->max) {
|
||||
throw new InvalidArgumentException(
|
||||
key: match ($this->max) {
|
||||
1 => 'blocks.max.singular',
|
||||
default => 'blocks.max.plural'
|
||||
},
|
||||
data: ['max' => $this->max]
|
||||
);
|
||||
}
|
||||
|
||||
$forms = [];
|
||||
$index = 0;
|
||||
|
||||
foreach ($value as $block) {
|
||||
$index++;
|
||||
$type = $block['type'];
|
||||
|
||||
// create the form for the block
|
||||
// and cache it for later use
|
||||
if (isset($forms[$type]) === false) {
|
||||
try {
|
||||
$fieldset = $this->fieldset($type);
|
||||
$fields = $fieldset->fields() ?? [];
|
||||
$forms[$type] = $this->form($fields);
|
||||
} catch (Throwable) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
$form = $forms[$type]->reset()->fill($block['content']);
|
||||
|
||||
foreach ($form->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
// rough first validation
|
||||
if (count($errors) > 0) {
|
||||
throw new InvalidArgumentException(
|
||||
key:'blocks.validation',
|
||||
data: [
|
||||
'field' => $field->label(),
|
||||
'fieldset' => $fieldset->name(),
|
||||
'index' => $index
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
211
kirby/src/Form/Field/EntriesField.php
Normal file
211
kirby/src/Form/Field/EntriesField.php
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Data\Data;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Form\FieldClass;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Form\Mixin\EmptyState;
|
||||
use Kirby\Form\Mixin\Max;
|
||||
use Kirby\Form\Mixin\Min;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Main class file of the entries field
|
||||
*
|
||||
* @package Kirby Field
|
||||
* @author Ahmet Bora <ahmet@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
* @since 5.0.0
|
||||
*/
|
||||
class EntriesField extends FieldClass
|
||||
{
|
||||
use EmptyState;
|
||||
use Max;
|
||||
use Min;
|
||||
|
||||
protected array $field;
|
||||
protected Form $form;
|
||||
protected bool $sortable = true;
|
||||
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
$this->setEmpty($params['empty'] ?? null);
|
||||
$this->setField($params['field'] ?? null);
|
||||
$this->setMax($params['max'] ?? null);
|
||||
$this->setMin($params['min'] ?? null);
|
||||
$this->setSortable($params['sortable'] ?? true);
|
||||
}
|
||||
|
||||
public function field(): array
|
||||
{
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
public function fieldProps(): array
|
||||
{
|
||||
return $this->form()->fields()->first()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-suppress MethodSignatureMismatch
|
||||
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
|
||||
*/
|
||||
public function fill(mixed $value): static
|
||||
{
|
||||
$this->value = Data::decode($value ?? '', 'yaml');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function form(): Form
|
||||
{
|
||||
return $this->form ??= new Form(
|
||||
fields: [$this->field()],
|
||||
model: $this->model
|
||||
);
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
...parent::props(),
|
||||
'empty' => $this->empty(),
|
||||
'field' => $this->fieldProps(),
|
||||
'max' => $this->max(),
|
||||
'min' => $this->min(),
|
||||
'sortable' => $this->sortable(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function setField(array|string|null $attrs = null): void
|
||||
{
|
||||
if (is_string($attrs) === true) {
|
||||
$attrs = ['type' => $attrs];
|
||||
}
|
||||
|
||||
$attrs ??= ['type' => 'text'];
|
||||
|
||||
if (in_array($attrs['type'], $this->supports()) === false) {
|
||||
throw new InvalidArgumentException(
|
||||
key: 'entries.supports',
|
||||
data: ['type' => $attrs['type']]
|
||||
);
|
||||
}
|
||||
|
||||
// remove the unsupported props from the entry field
|
||||
unset($attrs['counter'], $attrs['label']);
|
||||
|
||||
$this->field = $attrs;
|
||||
}
|
||||
|
||||
protected function setSortable(bool|null $sortable = true): void
|
||||
{
|
||||
$this->sortable = $sortable;
|
||||
}
|
||||
|
||||
public function sortable(): bool
|
||||
{
|
||||
return $this->sortable;
|
||||
}
|
||||
|
||||
public function supports(): array
|
||||
{
|
||||
return [
|
||||
'color',
|
||||
'date',
|
||||
'email',
|
||||
'number',
|
||||
'select',
|
||||
'slug',
|
||||
'tel',
|
||||
'text',
|
||||
'time',
|
||||
'url'
|
||||
];
|
||||
}
|
||||
|
||||
public function toFormValue(): mixed
|
||||
{
|
||||
$form = $this->form();
|
||||
$value = parent::toFormValue() ?? [];
|
||||
|
||||
return A::map(
|
||||
$value,
|
||||
fn ($value) => $form
|
||||
->reset()
|
||||
->fill(input: [$value])
|
||||
->fields()
|
||||
->first()
|
||||
->toFormValue()
|
||||
);
|
||||
}
|
||||
|
||||
public function toStoredValue(): mixed
|
||||
{
|
||||
$form = $this->form();
|
||||
$value = parent::toStoredValue();
|
||||
|
||||
return A::map(
|
||||
$value,
|
||||
fn ($value) => $form
|
||||
->reset()
|
||||
->submit(input: [$value])
|
||||
->fields()
|
||||
->first()
|
||||
->toStoredValue()
|
||||
);
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'entries' => function ($value) {
|
||||
if ($this->min && count($value) < $this->min) {
|
||||
throw new InvalidArgumentException(
|
||||
key: match ($this->min) {
|
||||
1 => 'entries.min.singular',
|
||||
default => 'entries.min.plural'
|
||||
},
|
||||
data: ['min' => $this->min]
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->max && count($value) > $this->max) {
|
||||
throw new InvalidArgumentException(
|
||||
key: match ($this->max) {
|
||||
1 => 'entries.max.singular',
|
||||
default => 'entries.max.plural'
|
||||
},
|
||||
data: ['max' => $this->max]
|
||||
);
|
||||
}
|
||||
|
||||
$form = $this->form();
|
||||
|
||||
foreach ($value as $index => $val) {
|
||||
$form->reset()->submit(input: [$val]);
|
||||
|
||||
foreach ($form->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
if ($errors !== []) {
|
||||
throw new InvalidArgumentException(
|
||||
key: 'entries.validation',
|
||||
data: [
|
||||
'field' => $this->label() ?? Str::ucfirst($this->name()),
|
||||
'index' => $index + 1
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
371
kirby/src/Form/Field/LayoutField.php
Normal file
371
kirby/src/Form/Field/LayoutField.php
Normal file
|
|
@ -0,0 +1,371 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Blueprint;
|
||||
use Kirby\Cms\Fieldset;
|
||||
use Kirby\Cms\Layout;
|
||||
use Kirby\Cms\Layouts;
|
||||
use Kirby\Data\Data;
|
||||
use Kirby\Data\Json;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
class LayoutField extends BlocksField
|
||||
{
|
||||
protected array|null $layouts;
|
||||
protected array|null $selector;
|
||||
protected Fieldset|null $settings;
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
$this->setModel($params['model'] ?? App::instance()->site());
|
||||
$this->setLayouts($params['layouts'] ?? ['1/1']);
|
||||
$this->setSelector($params['selector'] ?? null);
|
||||
$this->setSettings($params['settings'] ?? null);
|
||||
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-suppress MethodSignatureMismatch
|
||||
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
|
||||
*/
|
||||
public function fill(mixed $value): static
|
||||
{
|
||||
$attrs = $this->attrsForm();
|
||||
$value = Data::decode($value, type: 'json', fail: false);
|
||||
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
foreach ($layouts as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$layouts[$layoutIndex]['attrs'] = $attrs->reset()->fill($layout['attrs'])->toFormValues();
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->value = $layouts;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function attrsForm(): Form
|
||||
{
|
||||
return new Form(
|
||||
fields: $this->settings()?->fields() ?? [],
|
||||
model: $this->model
|
||||
);
|
||||
}
|
||||
|
||||
public function layouts(): array|null
|
||||
{
|
||||
return $this->layouts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates form values for each layout
|
||||
*/
|
||||
public function layoutsToValues(array $layouts): array
|
||||
{
|
||||
foreach ($layouts as &$layout) {
|
||||
$layout['id'] ??= Str::uuid();
|
||||
$layout['columns'] ??= [];
|
||||
|
||||
array_walk($layout['columns'], function (&$column) {
|
||||
$column['id'] ??= Str::uuid();
|
||||
$column['blocks'] = $this->blocksToValues($column['blocks'] ?? []);
|
||||
});
|
||||
}
|
||||
|
||||
return $layouts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paste action for layouts:
|
||||
* - generates new uuids for layout, column and blocks
|
||||
* - filters only supported layouts
|
||||
* - filters only supported fieldsets
|
||||
*/
|
||||
public function pasteLayouts(array $layouts): array
|
||||
{
|
||||
$layouts = $this->layoutsToValues($layouts);
|
||||
|
||||
foreach ($layouts as $layoutIndex => &$layout) {
|
||||
$layout['id'] = Str::uuid();
|
||||
|
||||
// remove the row if layout not available for the pasted layout field
|
||||
$columns = array_column($layout['columns'], 'width');
|
||||
if (in_array($columns, $this->layouts(), true) === false) {
|
||||
unset($layouts[$layoutIndex]);
|
||||
continue;
|
||||
}
|
||||
|
||||
array_walk($layout['columns'], function (&$column) {
|
||||
$column['id'] = Str::uuid();
|
||||
|
||||
array_walk($column['blocks'], function (&$block, $index) use ($column) {
|
||||
$block['id'] = Str::uuid();
|
||||
|
||||
// remove the block if it's not available
|
||||
try {
|
||||
$this->fieldset($block['type']);
|
||||
} catch (Throwable) {
|
||||
unset($column['blocks'][$index]);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return $layouts;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
...parent::props(),
|
||||
'layouts' => $this->layouts(),
|
||||
'selector' => $this->selector(),
|
||||
'settings' => $this->settings()?->toArray()
|
||||
];
|
||||
}
|
||||
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
$routes = parent::routes();
|
||||
|
||||
$routes[] = [
|
||||
'pattern' => 'layout',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field): array {
|
||||
$request = App::instance()->request();
|
||||
|
||||
$columns = $request->get('columns') ?? ['1/1'];
|
||||
$form = $field->attrsForm();
|
||||
|
||||
$form->fill(input: $form->defaults());
|
||||
$form->submit(input: $request->get('attrs') ?? []);
|
||||
|
||||
return Layout::factory([
|
||||
'attrs' => $form->toFormValues(),
|
||||
'columns' => array_map(fn ($width) => [
|
||||
'blocks' => [],
|
||||
'id' => Str::uuid(),
|
||||
'width' => $width,
|
||||
], $columns)
|
||||
])->toArray();
|
||||
},
|
||||
];
|
||||
|
||||
$routes[] = [
|
||||
'pattern' => 'layout/paste',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field): array {
|
||||
$request = App::instance()->request();
|
||||
$value = Layouts::parse($request->get('json'));
|
||||
$layouts = Layouts::factory($value);
|
||||
|
||||
return $field->pasteLayouts($layouts->toArray());
|
||||
}
|
||||
];
|
||||
|
||||
$routes[] = [
|
||||
'pattern' => 'fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (
|
||||
string $fieldName,
|
||||
string|null $path = null
|
||||
) use ($field): array {
|
||||
$form = $field->attrsForm();
|
||||
$field = $form->field($fieldName);
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => [
|
||||
...$this->data(),
|
||||
'field' => $field
|
||||
]
|
||||
]);
|
||||
|
||||
return $fieldApi->call(
|
||||
$path,
|
||||
$this->requestMethod(),
|
||||
$this->requestData()
|
||||
);
|
||||
}
|
||||
];
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
public function selector(): array|null
|
||||
{
|
||||
return $this->selector;
|
||||
}
|
||||
|
||||
protected function setDefault(mixed $default = null): void
|
||||
{
|
||||
// set id for layouts, columns and blocks within layout if not exists
|
||||
if (is_array($default) === true) {
|
||||
array_walk($default, function (&$layout) {
|
||||
$layout['id'] ??= Str::uuid();
|
||||
|
||||
// set columns id within layout
|
||||
if (isset($layout['columns']) === true) {
|
||||
array_walk($layout['columns'], function (&$column) {
|
||||
$column['id'] ??= Str::uuid();
|
||||
|
||||
// set blocks id within column
|
||||
if (isset($column['blocks']) === true) {
|
||||
array_walk($column['blocks'], function (&$block) {
|
||||
$block['id'] ??= Str::uuid();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
parent::setDefault($default);
|
||||
}
|
||||
|
||||
protected function setLayouts(array $layouts = []): void
|
||||
{
|
||||
$this->layouts = array_map(
|
||||
fn ($layout) => Str::split($layout),
|
||||
$layouts
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Layout selector's styles such as size (`small`, `medium`, `large` or `huge`) and columns
|
||||
*/
|
||||
protected function setSelector(array|null $selector = null): void
|
||||
{
|
||||
$this->selector = $selector;
|
||||
}
|
||||
|
||||
protected function setSettings(array|string|null $settings = null): void
|
||||
{
|
||||
if (empty($settings) === true) {
|
||||
$this->settings = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = Blueprint::extend($settings);
|
||||
|
||||
$settings['icon'] = 'dashboard';
|
||||
$settings['type'] = 'layout';
|
||||
$settings['parent'] = $this->model();
|
||||
|
||||
$this->settings = Fieldset::factory($settings);
|
||||
}
|
||||
|
||||
public function settings(): Fieldset|null
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function toStoredValue(bool $default = false): mixed
|
||||
{
|
||||
$attrs = $this->attrsForm();
|
||||
$value = $this->toFormValue($default);
|
||||
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if ($value === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
foreach ($value as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$value[$layoutIndex]['attrs'] = $attrs->reset()->fill($layout['attrs'])->toStoredValues();
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
|
||||
}
|
||||
}
|
||||
|
||||
return Json::encode($value, pretty: $this->pretty());
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'layout' => function ($value) {
|
||||
$attrsForm = $this->attrsForm();
|
||||
$blockForms = [];
|
||||
$layoutIndex = 0;
|
||||
|
||||
foreach ($value as $layout) {
|
||||
$layoutIndex++;
|
||||
|
||||
// validate settings form
|
||||
$form = $attrsForm->reset()->fill($layout['attrs'] ?? []);
|
||||
|
||||
foreach ($form->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
if (count($errors) > 0) {
|
||||
throw new InvalidArgumentException(
|
||||
key:'layout.validation.settings',
|
||||
data: ['index' => $layoutIndex]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// validate blocks in the layout
|
||||
$blockIndex = 0;
|
||||
|
||||
foreach ($layout['columns'] ?? [] as $column) {
|
||||
foreach ($column['blocks'] ?? [] as $block) {
|
||||
$blockIndex++;
|
||||
$blockType = $block['type'];
|
||||
|
||||
if (isset($blockForms[$blockType]) === false) {
|
||||
try {
|
||||
$fieldset = $this->fieldset($blockType);
|
||||
$fields = $this->fields($blockType) ?? [];
|
||||
$blockForms[$blockType] = $this->form($fields);
|
||||
} catch (Throwable) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
$form = $blockForms[$blockType]->reset()->fill($block['content']);
|
||||
|
||||
foreach ($form->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
// rough first validation
|
||||
if (count($errors) > 0) {
|
||||
throw new InvalidArgumentException(
|
||||
key: 'layout.validation.block',
|
||||
data: [
|
||||
'blockIndex' => $blockIndex,
|
||||
'field' => $field->label(),
|
||||
'fieldset' => $fieldset->name(),
|
||||
'layoutIndex' => $layoutIndex
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
74
kirby/src/Form/Field/StatsField.php
Normal file
74
kirby/src/Form/Field/StatsField.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Form\FieldClass;
|
||||
use Kirby\Panel\Ui\Stats;
|
||||
|
||||
/**
|
||||
* Stats field
|
||||
*
|
||||
* @package Kirby Field
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
* @since 5.1.0
|
||||
*/
|
||||
class StatsField extends FieldClass
|
||||
{
|
||||
/**
|
||||
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
|
||||
*/
|
||||
protected array|string $reports;
|
||||
|
||||
/**
|
||||
* The size of the report cards. Available sizes: `tiny`, `small`, `medium`, `large`
|
||||
*/
|
||||
protected string $size;
|
||||
|
||||
/**
|
||||
* Cache for the Stats UI component
|
||||
*/
|
||||
protected Stats $stats;
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
$this->reports = $params['reports'] ?? [];
|
||||
$this->size = $params['size'] ?? 'large';
|
||||
}
|
||||
|
||||
public function hasValue(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function reports(): array
|
||||
{
|
||||
return $this->stats()->reports();
|
||||
}
|
||||
|
||||
public function size(): string
|
||||
{
|
||||
return $this->stats()->size();
|
||||
}
|
||||
|
||||
public function stats(): Stats
|
||||
{
|
||||
return $this->stats ??= Stats::from(
|
||||
model: $this->model,
|
||||
reports: $this->reports,
|
||||
size: $this->size
|
||||
);
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
...parent::props(),
|
||||
...$this->stats()->props()
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue