1
0

downgrade to kirby v3

This commit is contained in:
Philip Wagner
2024-09-01 10:47:15 +02:00
parent a4b2aece7b
commit af86acb7a1
1085 changed files with 54743 additions and 65042 deletions

View File

@@ -2,9 +2,7 @@
namespace Kirby\Form;
use Closure;
use Exception;
use Kirby\Cms\App;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Component;
@@ -26,37 +24,44 @@ class Field extends Component
{
/**
* An array of all found errors
*
* @var array|null
*/
protected array|null $errors = null;
protected $errors;
/**
* Parent collection with all fields of the current form
*
* @var \Kirby\Form\Fields|null
*/
protected Fields|null $formFields;
protected $formFields;
/**
* Registry for all component mixins
*
* @var array
*/
public static array $mixins = [];
public static $mixins = [];
/**
* Registry for all component types
*
* @var array
*/
public static array $types = [];
public static $types = [];
/**
* Field constructor
*
* @param string $type
* @param array $attrs
* @param \Kirby\Form\Fields|null $formFields
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function __construct(
string $type,
array $attrs = [],
Fields|null $formFields = null
) {
public function __construct(string $type, array $attrs = [], ?Fields $formFields = null)
{
if (isset(static::$types[$type]) === false) {
throw new InvalidArgumentException([
'key' => 'field.type.missing',
'data' => ['name' => $attrs['name'] ?? '-', 'type' => $type]
]);
throw new InvalidArgumentException('The field type "' . $type . '" does not exist');
}
if (isset($attrs['model']) === false) {
@@ -74,23 +79,26 @@ class Field extends Component
/**
* Returns field api call
*
* @return mixed
*/
public function api(): mixed
public function api()
{
if (
isset($this->options['api']) === true &&
$this->options['api'] instanceof Closure
is_a($this->options['api'], 'Closure') === true
) {
return $this->options['api']->call($this);
}
return null;
}
/**
* Returns field data
*
* @param bool $default
* @return mixed
*/
public function data(bool $default = false): mixed
public function data(bool $default = false)
{
$save = $this->options['save'] ?? true;
@@ -104,7 +112,7 @@ class Field extends Component
return null;
}
if ($save instanceof Closure) {
if (is_a($save, 'Closure') === true) {
return $save->call($this, $value);
}
@@ -113,6 +121,8 @@ class Field extends Component
/**
* Default props and computed of the field
*
* @return array
*/
public static function defaults(): array
{
@@ -249,44 +259,16 @@ class Field extends Component
];
}
/**
* Returns optional dialog routes for the field
*/
public function dialogs(): array
{
if (
isset($this->options['dialogs']) === true &&
$this->options['dialogs'] instanceof Closure
) {
return $this->options['dialogs']->call($this);
}
return [];
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
if (
isset($this->options['drawers']) === true &&
$this->options['drawers'] instanceof Closure
) {
return $this->options['drawers']->call($this);
}
return [];
}
/**
* Creates a new field instance
*
* @param string $type
* @param array $attrs
* @param Fields|null $formFields
* @return static
*/
public static function factory(
string $type,
array $attrs = [],
Fields|null $formFields = null
): static|FieldClass {
public static function factory(string $type, array $attrs = [], ?Fields $formFields = null)
{
$field = static::$types[$type] ?? null;
if (is_string($field) && class_exists($field) === true) {
@@ -299,14 +281,18 @@ class Field extends Component
/**
* Parent collection with all fields of the current form
*
* @return \Kirby\Form\Fields|null
*/
public function formFields(): Fields|null
public function formFields(): ?Fields
{
return $this->formFields;
}
/**
* Validates when run for the first time and returns any errors
*
* @return array
*/
public function errors(): array
{
@@ -319,31 +305,29 @@ class Field extends Component
/**
* Checks if the field is empty
*
* @param mixed ...$args
* @return bool
*/
public function isEmpty(mixed ...$args): bool
public function isEmpty(...$args): bool
{
$value = match (count($args)) {
0 => $this->value(),
default => $args[0]
};
if (count($args) === 0) {
$value = $this->value();
} else {
$value = $args[0];
}
if ($empty = $this->options['isEmpty'] ?? null) {
return $empty->call($this, $value);
if (isset($this->options['isEmpty']) === true) {
return $this->options['isEmpty']->call($this, $value);
}
return in_array($value, [null, '', []], true);
}
/**
* Checks if the field is hidden
*/
public function isHidden(): bool
{
return ($this->options['hidden'] ?? false) === true;
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
@@ -352,6 +336,8 @@ class Field extends Component
/**
* Checks if the field is required
*
* @return bool
*/
public function isRequired(): bool
{
@@ -360,6 +346,8 @@ class Field extends Component
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
@@ -368,16 +356,20 @@ class Field extends Component
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby(): App
public function kirby()
{
return $this->model()->kirby();
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model(): mixed
public function model()
{
return $this->model;
}
@@ -389,33 +381,31 @@ class Field extends Component
* - The field is required
* - The field is currently empty
* - The field is not currently inactive because of a `when` rule
*
* @return bool
*/
protected function needsValue(): bool
{
// check simple conditions first
if (
$this->save() === false ||
$this->isRequired() === false ||
$this->isEmpty() === false
) {
if ($this->save() === false || $this->isRequired() === false || $this->isEmpty() === false) {
return false;
}
// check the data of the relevant fields if there is a `when` option
if (
empty($this->when) === false &&
is_array($this->when) === true &&
$formFields = $this->formFields()
) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
if (empty($this->when) === false && is_array($this->when) === true) {
$formFields = $this->formFields();
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
if ($formFields !== null) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field !== null ? $field->value() : '';
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
}
}
}
@@ -426,6 +416,8 @@ class Field extends Component
/**
* Checks if the field is saveable
*
* @return bool
*/
public function save(): bool
{
@@ -434,6 +426,8 @@ class Field extends Component
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
@@ -441,7 +435,6 @@ class Field extends Component
unset($array['model']);
$array['hidden'] = $this->isHidden();
$array['saveable'] = $this->save();
$array['signature'] = md5(json_encode($array));
@@ -455,6 +448,8 @@ class Field extends Component
/**
* Runs the validations defined for the field
*
* @return void
*/
protected function validate(): void
{
@@ -477,7 +472,7 @@ class Field extends Component
continue;
}
if ($validation instanceof Closure) {
if (is_a($validation, 'Closure') === true) {
try {
$validation->call($this, $this->value());
} catch (Exception $e) {
@@ -502,8 +497,10 @@ class Field extends Component
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value(): mixed
public function value()
{
return $this->save() ? $this->value : null;
}

View File

@@ -5,9 +5,7 @@ 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\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Form\FieldClass;
@@ -24,17 +22,15 @@ class BlocksField extends FieldClass
use Max;
use Min;
protected Fieldsets $fieldsets;
protected string|null $group;
protected bool $pretty;
protected mixed $value = [];
protected $blocks;
protected $fieldsets;
protected $group;
protected $pretty;
protected $value = [];
public function __construct(array $params = [])
{
$this->setFieldsets(
$params['fieldsets'] ?? null,
$params['model'] ?? App::instance()->site()
);
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? App::instance()->site());
parent::__construct($params);
@@ -45,10 +41,8 @@ class BlocksField extends FieldClass
$this->setPretty($params['pretty'] ?? false);
}
public function blocksToValues(
array $blocks,
string $to = 'values'
): array {
public function blocksToValues($blocks, $to = 'values'): array
{
$result = [];
$fields = [];
@@ -60,58 +54,53 @@ class BlocksField extends FieldClass
$fields[$type] ??= $this->fields($block['type']);
// overwrite the block content with form values
$block['content'] = $this->form(
$fields[$type],
$block['content']
)->$to();
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
// create id if not exists
$block['id'] ??= Str::uuid();
} catch (Throwable) {
// skip invalid blocks
} finally {
$result[] = $block;
} catch (Throwable $e) {
$result[] = $block;
// skip invalid blocks
continue;
}
}
return $result;
}
public function fields(string $type): array
public function fields(string $type)
{
return $this->fieldset($type)->fields();
}
public function fieldset(string $type): 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(): Fieldsets
public function fieldsets()
{
return $this->fieldsets;
}
public function fieldsetGroups(): array|null
public function fieldsetGroups(): ?array
{
$groups = $this->fieldsets()->groups();
return empty($groups) === true ? null : $groups;
$fieldsetGroups = $this->fieldsets()->groups();
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
}
public function fill(mixed $value = null): void
public function fill($value = null)
{
$value = BlocksCollection::parse($value);
$blocks = BlocksCollection::factory($value)->toArray();
$this->value = $this->blocksToValues($blocks);
$blocks = BlocksCollection::factory($value);
$this->value = $this->blocksToValues($blocks->toArray());
}
public function form(array $fields, array $input = []): Form
public function form(array $fields, array $input = [])
{
return new Form([
'fields' => $fields,
@@ -136,30 +125,6 @@ class BlocksField extends FieldClass
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 [
@@ -179,25 +144,23 @@ class BlocksField extends FieldClass
return [
[
'pattern' => 'uuid',
'action' => fn (): array => ['uuid' => Str::uuid()]
'action' => fn () => ['uuid' => Str::uuid()]
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () use ($field): array {
'action' => function () use ($field) {
$request = App::instance()->request();
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
return $field->pasteBlocks($blocks->toArray());
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
return $field->blocksToValues($blocks->toArray());
}
],
[
'pattern' => 'fieldsets/(:any)',
'method' => 'GET',
'action' => function (
string $fieldsetType
) use ($field): array {
'action' => function ($fieldsetType) use ($field) {
$fields = $field->fields($fieldsetType);
$defaults = $field->form($fields, [])->data(true);
$content = $field->form($fields, $defaults)->values();
@@ -211,33 +174,22 @@ class BlocksField extends FieldClass
[
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (
string $fieldsetType,
string $fieldName,
string $path = null
) use ($field) {
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
$fields = $field->fields($fieldsetType);
$field = $field->form($fields)->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge(
$this->data(),
['field' => $field]
)
'data' => array_merge($this->data(), ['field' => $field])
]);
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
],
];
}
public function store(mixed $value): mixed
public function store($value)
{
$blocks = $this->blocksToValues((array)$value, 'content');
@@ -250,38 +202,23 @@ class BlocksField extends FieldClass
return $this->valueToJson($blocks, $this->pretty());
}
protected function setDefault(mixed $default = null): void
protected function setFieldsets($fieldsets, $model)
{
// 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]
);
$this->fieldsets = Fieldsets::factory($fieldsets, [
'parent' => $model
]);
}
protected function setGroup(string $group = null): void
protected function setGroup(string $group = null)
{
$this->group = $group;
}
protected function setPretty(bool $pretty = false): void
protected function setPretty(bool $pretty = false)
{
$this->pretty = $pretty;
}
@@ -313,22 +250,20 @@ class BlocksField extends FieldClass
foreach ($value as $block) {
$index++;
$type = $block['type'];
$blockType = $block['type'];
try {
$fieldset = $this->fieldset($type);
$blockFields = $fields[$type] ?? $fieldset->fields() ?? [];
} catch (Throwable) {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
// store the fields for the next round
$fields[$type] = $blockFields;
$fields[$blockType] = $blockFields;
// overwrite the content with the serialized form
$form = $this->form($blockFields, $block['content']);
foreach ($form->fields() as $field) {
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
// rough first validation
@@ -336,9 +271,7 @@ class BlocksField extends FieldClass
throw new InvalidArgumentException([
'key' => 'blocks.validation',
'data' => [
'field' => $field->label(),
'fieldset' => $fieldset->name(),
'index' => $index
'index' => $index,
]
]);
}

View File

@@ -14,21 +14,19 @@ use Throwable;
class LayoutField extends BlocksField
{
protected array|null $layouts;
protected array|null $selector;
protected Fieldset|null $settings;
protected $layouts;
protected $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);
}
public function fill(mixed $value = null): void
public function fill($value = null)
{
$value = $this->valueFromJson($value);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
@@ -46,88 +44,30 @@ class LayoutField extends BlocksField
$this->value = $layouts;
}
public function attrsForm(array $input = []): Form
public function attrsForm(array $input = [])
{
$settings = $this->settings();
return new Form([
'fields' => $settings?->fields() ?? [],
'fields' => $settings ? $settings->fields() : [],
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
public function layouts(): array|null
public function layouts(): ?array
{
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()) === 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
{
$settings = $this->settings();
return array_merge(parent::props(), [
'layouts' => $this->layouts(),
'selector' => $this->selector(),
'settings' => $settings?->toArray()
'settings' => $settings !== null ? $settings->toArray() : null,
'layouts' => $this->layouts()
]);
}
@@ -135,15 +75,13 @@ class LayoutField extends BlocksField
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field): array {
'action' => function () use ($field) {
$request = App::instance()->request();
$input = $request->get('attrs') ?? [];
$defaults = $field->attrsForm($input)->data(true);
$defaults = $field->attrsForm([])->data(true);
$attrs = $field->attrsForm($defaults)->values();
$columns = $request->get('columns') ?? ['1/1'];
@@ -158,79 +96,26 @@ class LayoutField extends BlocksField
},
];
$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 $path = null
) use ($field): array {
'action' => function (string $fieldName, string $path = null) use ($field) {
$form = $field->attrsForm();
$field = $form->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge(
$this->data(),
['field' => $field]
)
'data' => array_merge($this->data(), ['field' => $field])
]);
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
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
protected function setLayouts(array $layouts = [])
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
@@ -238,15 +123,7 @@ class LayoutField extends BlocksField
);
}
/**
* 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
protected function setSettings($settings = null)
{
if (empty($settings) === true) {
$this->settings = null;
@@ -262,12 +139,12 @@ class LayoutField extends BlocksField
$this->settings = Fieldset::factory($settings);
}
public function settings(): Fieldset|null
public function settings()
{
return $this->settings;
}
public function store(mixed $value): mixed
public function store($value)
{
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
@@ -301,9 +178,7 @@ class LayoutField extends BlocksField
$layoutIndex++;
// validate settings form
$form = $this->attrsForm($layout['attrs'] ?? []);
foreach ($form->fields() as $field) {
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
$errors = $field->errors();
if (empty($errors) === false) {
@@ -325,9 +200,8 @@ class LayoutField extends BlocksField
$blockType = $block['type'];
try {
$fieldset = $this->fieldset($blockType);
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable) {
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
@@ -336,9 +210,7 @@ class LayoutField extends BlocksField
$fields[$blockType] = $blockFields;
// overwrite the content with the serialized form
$form = $this->form($blockFields, $block['content']);
foreach ($form->fields() as $field) {
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
// rough first validation
@@ -347,8 +219,6 @@ class LayoutField extends BlocksField
'key' => 'layout.validation.block',
'data' => [
'blockIndex' => $blockIndex,
'field' => $field->label(),
'fieldset' => $fieldset->name(),
'layoutIndex' => $layoutIndex
]
]);

View File

@@ -2,7 +2,6 @@
namespace Kirby\Form;
use Closure;
use Exception;
use Kirby\Cms\App;
use Kirby\Cms\HasSiblings;
@@ -27,27 +26,117 @@ abstract class FieldClass
{
use HasSiblings;
protected string|null $after;
protected bool $autofocus;
protected string|null $before;
protected mixed $default;
protected bool $disabled;
protected string|null $help;
protected string|null $icon;
protected string|null $label;
protected ModelWithContent $model;
protected string|null $name;
protected string|null $placeholder;
protected bool $required;
protected Fields $siblings;
protected bool $translate;
protected mixed $value = null;
protected array|null $when;
protected string|null $width;
/**
* @var string|null
*/
protected $after;
/**
* @var bool
*/
protected $autofocus;
/**
* @var string|null
*/
protected $before;
/**
* @var mixed
*/
protected $default;
/**
* @var bool
*/
protected $disabled;
/**
* @var string|null
*/
protected $help;
/**
* @var string|null
*/
protected $icon;
/**
* @var string|null
*/
protected $label;
/**
* @var \Kirby\Cms\ModelWithContent
*/
protected $model;
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $params;
/**
* @var string|null
*/
protected $placeholder;
/**
* @var bool
*/
protected $required;
/**
* @var \Kirby\Form\Fields
*/
protected $siblings;
/**
* @var bool
*/
protected $translate;
/**
* @var mixed
*/
protected $value;
/**
* @var array|null
*/
protected $when;
/**
* @var string|null
*/
protected $width;
/**
* @param string $param
* @param array $args
* @return mixed
*/
public function __call(string $param, array $args)
{
if (isset($this->$param) === true) {
return $this->$param;
}
return $this->params[$param] ?? null;
}
/**
* @param array $params
*/
public function __construct(array $params = [])
{
$this->params = $params;
public function __construct(
protected array $params = []
) {
$this->setAfter($params['after'] ?? null);
$this->setAutofocus($params['autofocus'] ?? false);
$this->setBefore($params['before'] ?? null);
@@ -70,31 +159,34 @@ abstract class FieldClass
}
}
public function __call(string $param, array $args): mixed
{
if (isset($this->$param) === true) {
return $this->$param;
}
return $this->params[$param] ?? null;
}
public function after(): string|null
/**
* @return string|null
*/
public function after(): ?string
{
return $this->stringTemplate($this->after);
}
/**
* @return array
*/
public function api(): array
{
return $this->routes();
}
/**
* @return bool
*/
public function autofocus(): bool
{
return $this->autofocus;
}
public function before(): string|null
/**
* @return string|null
*/
public function before(): ?string
{
return $this->stringTemplate($this->before);
}
@@ -106,8 +198,11 @@ abstract class FieldClass
* Returns the field data
* in a format to be stored
* in Kirby's content fields
*
* @param bool $default
* @return mixed
*/
public function data(bool $default = false): mixed
public function data(bool $default = false)
{
return $this->store($this->value($default));
}
@@ -115,8 +210,10 @@ abstract class FieldClass
/**
* Returns the default value for the field,
* which will be used when a page/file/user is created
*
* @return mixed
*/
public function default(): mixed
public function default()
{
if (is_string($this->default) === false) {
return $this->default;
@@ -125,33 +222,21 @@ abstract class FieldClass
return $this->stringTemplate($this->default);
}
/**
* Returns optional dialog routes for the field
*/
public function dialogs(): array
{
return [];
}
/**
* If `true`, the field is no longer editable and will not be saved
*
* @return bool
*/
public function disabled(): bool
{
return $this->disabled;
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
return [];
}
/**
* Runs all validations and returns an array of
* error messages
*
* @return array
*/
public function errors(): array
{
@@ -160,16 +245,21 @@ abstract class FieldClass
/**
* Setter for the value
*
* @param mixed $value
* @return void
*/
public function fill(mixed $value = null): void
public function fill($value = null)
{
$this->value = $value;
}
/**
* Optional help text below the field
*
* @return string|null
*/
public function help(): string|null
public function help(): ?string
{
if (empty($this->help) === false) {
$help = $this->stringTemplate($this->help);
@@ -180,57 +270,79 @@ abstract class FieldClass
return null;
}
protected function i18n(string|array|null $param = null): string|null
/**
* @param string|array|null $param
* @return string|null
*/
protected function i18n($param = null): ?string
{
return empty($param) === false ? I18n::translate($param, $param) : null;
}
/**
* Optional icon that will be shown at the end of the field
*
* @return string|null
*/
public function icon(): string|null
public function icon(): ?string
{
return $this->icon;
}
/**
* @return string
*/
public function id(): string
{
return $this->name();
}
/**
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->isEmptyValue($this->value());
}
public function isEmptyValue(mixed $value = null): bool
/**
* @param mixed $value
* @return bool
*/
public function isEmptyValue($value = null): bool
{
return in_array($value, [null, '', []], true);
}
public function isHidden(): bool
{
return false;
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return $this->isValid() === false;
}
/**
* @return bool
*/
public function isRequired(): bool
{
return $this->required;
}
/**
* @return bool
*/
public function isSaveable(): bool
{
return true;
@@ -238,6 +350,8 @@ abstract class FieldClass
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
@@ -246,32 +360,38 @@ abstract class FieldClass
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby(): App
public function kirby()
{
return $this->model->kirby();
}
/**
* The field label can be set as string or associative array with translations
*
* @return string
*/
public function label(): string
{
return $this->stringTemplate(
$this->label ?? Str::ucfirst($this->name())
);
return $this->stringTemplate($this->label ?? Str::ucfirst($this->name()));
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model(): ModelWithContent
public function model()
{
return $this->model;
}
/**
* Returns the field name
*
* @return string
*/
public function name(): string
{
@@ -285,6 +405,8 @@ abstract class FieldClass
* - The field is required
* - The field is currently empty
* - The field is not currently inactive because of a `when` rule
*
* @return bool
*/
protected function needsValue(): bool
{
@@ -298,20 +420,20 @@ abstract class FieldClass
}
// check the data of the relevant fields if there is a `when` option
if (
empty($this->when) === false &&
is_array($this->when) === true &&
$formFields = $this->siblings()
) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
if (empty($this->when) === false && is_array($this->when) === true) {
$formFields = $this->siblings();
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
if ($formFields !== null) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field !== null ? $field->value() : '';
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
}
}
}
@@ -322,6 +444,8 @@ abstract class FieldClass
/**
* Returns all original params for the field
*
* @return array
*/
public function params(): array
{
@@ -330,8 +454,10 @@ abstract class FieldClass
/**
* Optional placeholder value that will be shown when the field is empty
*
* @return string|null
*/
public function placeholder(): string|null
public function placeholder(): ?string
{
return $this->stringTemplate($this->placeholder);
}
@@ -339,6 +465,8 @@ abstract class FieldClass
/**
* Define the props that will be sent to
* the Vue component
*
* @return array
*/
public function props(): array
{
@@ -349,7 +477,6 @@ abstract class FieldClass
'default' => $this->default(),
'disabled' => $this->isDisabled(),
'help' => $this->help(),
'hidden' => $this->isHidden(),
'icon' => $this->icon(),
'label' => $this->label(),
'name' => $this->name(),
@@ -365,6 +492,8 @@ abstract class FieldClass
/**
* If `true`, the field has to be filled in correctly to be saved.
*
* @return bool
*/
public function required(): bool
{
@@ -373,6 +502,8 @@ abstract class FieldClass
/**
* Routes for the field API
*
* @return array
*/
public function routes(): array
{
@@ -382,110 +513,178 @@ abstract class FieldClass
/**
* @deprecated 3.5.0
* @todo remove when the general field class setup has been refactored
* @return bool
*/
public function save(): bool
public function save()
{
return $this->isSaveable();
}
protected function setAfter(array|string|null $after = null): void
/**
* @param array|string|null $after
* @return void
*/
protected function setAfter($after = null)
{
$this->after = $this->i18n($after);
}
protected function setAutofocus(bool $autofocus = false): void
/**
* @param bool $autofocus
* @return void
*/
protected function setAutofocus(bool $autofocus = false)
{
$this->autofocus = $autofocus;
}
protected function setBefore(array|string|null $before = null): void
/**
* @param array|string|null $before
* @return void
*/
protected function setBefore($before = null)
{
$this->before = $this->i18n($before);
}
protected function setDefault(mixed $default = null): void
/**
* @param mixed $default
* @return void
*/
protected function setDefault($default = null)
{
$this->default = $default;
}
protected function setDisabled(bool $disabled = false): void
/**
* @param bool $disabled
* @return void
*/
protected function setDisabled(bool $disabled = false)
{
$this->disabled = $disabled;
}
protected function setHelp(array|string|null $help = null): void
/**
* @param array|string|null $help
* @return void
*/
protected function setHelp($help = null)
{
$this->help = $this->i18n($help);
}
protected function setIcon(string|null $icon = null): void
/**
* @param string|null $icon
* @return void
*/
protected function setIcon(?string $icon = null)
{
$this->icon = $icon;
}
protected function setLabel(array|string|null $label = null): void
/**
* @param array|string|null $label
* @return void
*/
protected function setLabel($label = null)
{
$this->label = $this->i18n($label);
}
protected function setModel(ModelWithContent $model): void
/**
* @param \Kirby\Cms\ModelWithContent $model
* @return void
*/
protected function setModel(ModelWithContent $model)
{
$this->model = $model;
}
protected function setName(string|null $name = null): void
/**
* @param string|null $name
* @return void
*/
protected function setName(string $name = null)
{
$this->name = $name;
}
protected function setPlaceholder(array|string|null $placeholder = null): void
/**
* @param array|string|null $placeholder
* @return void
*/
protected function setPlaceholder($placeholder = null)
{
$this->placeholder = $this->i18n($placeholder);
}
protected function setRequired(bool $required = false): void
/**
* @param bool $required
* @return void
*/
protected function setRequired(bool $required = false)
{
$this->required = $required;
}
protected function setSiblings(Fields|null $siblings = null): void
/**
* @param \Kirby\Form\Fields|null $siblings
* @return void
*/
protected function setSiblings(?Fields $siblings = null)
{
$this->siblings = $siblings ?? new Fields([$this]);
}
protected function setTranslate(bool $translate = true): void
/**
* @param bool $translate
* @return void
*/
protected function setTranslate(bool $translate = true)
{
$this->translate = $translate;
}
/**
* Setter for the when condition
*
* @param mixed $when
* @return void
*/
protected function setWhen(array|null $when = null): void
protected function setWhen($when = null)
{
$this->when = $when;
}
/**
* Setter for the field width
*
* @param string|null $width
* @return void
*/
protected function setWidth(string|null $width = null): void
protected function setWidth(string $width = null)
{
$this->width = $width;
}
/**
* Returns all sibling fields
*
* @return \Kirby\Form\Fields
*/
protected function siblingsCollection(): Fields
protected function siblingsCollection()
{
return $this->siblings;
}
/**
* Parses a string template in the given value
*
* @param string|null $string
* @return string|null
*/
protected function stringTemplate(string|null $string = null): string|null
protected function stringTemplate(?string $string = null): ?string
{
if ($string !== null) {
return $this->model->toString($string);
@@ -497,14 +696,19 @@ abstract class FieldClass
/**
* Converts the given value to a value
* that can be stored in the text file
*
* @param mixed $value
* @return mixed
*/
public function store(mixed $value): mixed
public function store($value)
{
return $value;
}
/**
* Should the field be translatable?
*
* @return bool
*/
public function translate(): bool
{
@@ -513,6 +717,8 @@ abstract class FieldClass
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
@@ -526,6 +732,8 @@ abstract class FieldClass
/**
* Returns the field type
*
* @return string
*/
public function type(): string
{
@@ -534,6 +742,8 @@ abstract class FieldClass
/**
* Runs the validations defined for the field
*
* @return array
*/
protected function validate(): array
{
@@ -557,7 +767,7 @@ abstract class FieldClass
continue;
}
if ($validation instanceof Closure) {
if (is_a($validation, 'Closure') === true) {
try {
$validation->call($this, $value);
} catch (Exception $e) {
@@ -571,7 +781,8 @@ abstract class FieldClass
/**
* Defines all validation rules
* @codeCoverageIgnore
*
* @return array
*/
protected function validations(): array
{
@@ -581,8 +792,10 @@ abstract class FieldClass
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value(bool $default = false): mixed
public function value(bool $default = false)
{
if ($this->isSaveable() === false) {
return null;
@@ -595,33 +808,46 @@ abstract class FieldClass
return $this->value;
}
protected function valueFromJson(mixed $value): array
/**
* @param mixed $value
* @return array
*/
protected function valueFromJson($value): array
{
try {
return Data::decode($value, 'json');
} catch (Throwable) {
} catch (Throwable $e) {
return [];
}
}
protected function valueFromYaml(mixed $value): array
/**
* @param mixed $value
* @return array
*/
protected function valueFromYaml($value): array
{
return Data::decode($value, 'yaml');
}
protected function valueToJson(
array $value = null,
bool $pretty = false
): string {
$constants = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
/**
* @param array|null $value
* @param bool $pretty
* @return string
*/
protected function valueToJson(array $value = null, bool $pretty = false): string
{
if ($pretty === true) {
$constants |= JSON_PRETTY_PRINT;
return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return json_encode($value, $constants);
return json_encode($value);
}
/**
* @param array|null $value
* @return string
*/
protected function valueToYaml(array $value = null): string
{
return Data::encode($value, 'yaml');
@@ -629,8 +855,10 @@ abstract class FieldClass
/**
* Conditions when the field will be shown
*
* @return array|null
*/
public function when(): array|null
public function when(): ?array
{
return $this->when;
}
@@ -638,6 +866,8 @@ abstract class FieldClass
/**
* Returns the width of the field in
* the Panel grid
*
* @return string
*/
public function width(): string
{

View File

@@ -21,7 +21,9 @@ class Fields extends Collection
* This takes care of validation and of setting
* the collection prop on each object correctly.
*
* @param string $name
* @param object|array $field
* @return void
*/
public function __set(string $name, $field): void
{
@@ -38,6 +40,9 @@ class Fields extends Collection
* Converts the fields collection to an
* array and also does that for every
* included field.
*
* @param \Closure|null $map
* @return array
*/
public function toArray(Closure $map = null): array
{

View File

@@ -2,13 +2,10 @@
namespace Kirby\Form;
use Closure;
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\Model;
use Kirby\Data\Data;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use Throwable;
@@ -28,21 +25,29 @@ class Form
{
/**
* An array of all found errors
*
* @var array|null
*/
protected array|null $errors = null;
protected $errors;
/**
* Fields in the form
*
* @var \Kirby\Form\Fields|null
*/
protected Fields|null $fields;
protected $fields;
/**
* All values of form
*
* @var array
*/
protected array $values = [];
protected $values = [];
/**
* Form constructor
*
* @param array $props
*/
public function __construct(array $props)
{
@@ -74,12 +79,15 @@ class Form
// inject the name
$props['name'] = $name = strtolower($name);
// check if the field is disabled and
// check if the field is disabled
$disabled = $props['disabled'] ?? false;
// overwrite the field value if not set
$props['value'] = match ($props['disabled'] ?? false) {
true => $values[$name] ?? null,
default => $input[$name] ?? $values[$name] ?? null
};
if ($disabled === true) {
$props['value'] = $values[$name] ?? null;
} else {
$props['value'] = $input[$name] ?? $values[$name] ?? null;
}
try {
$field = Field::factory($props['type'], $props, $this->fields);
@@ -100,7 +108,9 @@ class Form
$input = array_merge($values, $input);
foreach ($input as $key => $value) {
$this->values[$key] ??= $value;
if (isset($this->values[$key]) === false) {
$this->values[$key] = $value;
}
}
}
}
@@ -108,6 +118,8 @@ class Form
/**
* Returns the data required to write to the content file
* Doesn't include default and null values
*
* @return array
*/
public function content(): array
{
@@ -118,6 +130,8 @@ class Form
* Returns data for all fields in the form
*
* @param false $defaults
* @param bool $includeNulls
* @return array
*/
public function data($defaults = false, bool $includeNulls = true): array
{
@@ -140,6 +154,8 @@ class Form
/**
* An array of all found errors
*
* @return array
*/
public function errors(): array
{
@@ -163,16 +179,17 @@ class Form
/**
* Shows the error with the field
*
* @param \Throwable $exception
* @param array $props
* @return \Kirby\Form\Field
*/
public static function exceptionField(
Throwable $exception,
array $props = []
): Field {
public static function exceptionField(Throwable $exception, array $props = [])
{
$message = $exception->getMessage();
if (App::instance()->option('debug') === true) {
$message .= ' in file: ' . $exception->getFile();
$message .= ' line: ' . $exception->getLine();
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
}
$props = array_merge($props, [
@@ -188,9 +205,11 @@ class Form
* Get the field object by name
* and handle nested fields correctly
*
* @param string $name
* @throws \Kirby\Exception\NotFoundException
* @return \Kirby\Form\Field
*/
public function field(string $name): Field|FieldClass
public function field(string $name)
{
$form = $this;
$fieldNames = Str::split($name, '+');
@@ -205,11 +224,9 @@ class Form
if ($count !== $index) {
$form = $field->form();
}
continue;
} else {
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
}
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
}
// it can get this error only if $name is an empty string as $name = ''
@@ -222,23 +239,28 @@ class Form
/**
* Returns form fields
*
* @return \Kirby\Form\Fields|null
*/
public function fields(): Fields|null
public function fields()
{
return $this->fields;
}
public static function for(
ModelWithContent $model,
array $props = []
): static {
/**
* @param \Kirby\Cms\Model $model
* @param array $props
* @return static
*/
public static function for(Model $model, array $props = [])
{
// get the original model data
$original = $model->content($props['language'] ?? null)->toArray();
$values = $props['values'] ?? [];
// convert closures to values
foreach ($values as $key => $value) {
if ($value instanceof Closure) {
if (is_a($value, 'Closure') === true) {
$values[$key] = $value($original[$key] ?? null);
}
}
@@ -249,10 +271,7 @@ class Form
$props['model'] = $model;
// search for the blueprint
if (
method_exists($model, 'blueprint') === true &&
$blueprint = $model->blueprint()
) {
if (method_exists($model, 'blueprint') === true && $blueprint = $model->blueprint()) {
$props['fields'] = $blueprint->fields();
}
@@ -271,14 +290,18 @@ class Form
/**
* Checks if the form is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return $this->isValid() === false;
return empty($this->errors()) === false;
}
/**
* Checks if the form is valid
*
* @return bool
*/
public function isValid(): bool
{
@@ -288,19 +311,23 @@ class Form
/**
* Disables fields in secondary languages when
* they are configured to be untranslatable
*
* @param array $fields
* @param string|null $language
* @return array
*/
protected static function prepareFieldsForLanguage(
array $fields,
string|null $language = null
): array {
protected static function prepareFieldsForLanguage(array $fields, ?string $language = null): array
{
$kirby = App::instance(null, true);
// only modify the fields if we have a valid Kirby multilang instance
if ($kirby?->multilang() !== true) {
if (!$kirby || $kirby->multilang() === false) {
return $fields;
}
$language ??= $kirby->language()->code();
if ($language === null) {
$language = $kirby->language()->code();
}
if ($language !== $kirby->defaultLanguage()->code()) {
foreach ($fields as $fieldName => $fieldProps) {
@@ -319,20 +346,29 @@ class Form
* Converts the data of fields to strings
*
* @param false $defaults
* @return array
*/
public function strings($defaults = false): array
{
return A::map(
$this->data($defaults),
fn ($value) => match (true) {
is_array($value) => Data::encode($value, 'yaml'),
default => $value
$strings = [];
foreach ($this->data($defaults) as $key => $value) {
if ($value === null) {
$strings[$key] = null;
} elseif (is_array($value) === true) {
$strings[$key] = Data::encode($value, 'yaml');
} else {
$strings[$key] = $value;
}
);
}
return $strings;
}
/**
* Converts the form to a plain array
*
* @return array
*/
public function toArray(): array
{
@@ -347,6 +383,8 @@ class Form
/**
* Returns form values
*
* @return array
*/
public function values(): array
{

View File

@@ -4,14 +4,14 @@ namespace Kirby\Form\Mixin;
trait EmptyState
{
protected string|null $empty;
protected $empty;
protected function setEmpty(string|array|null $empty = null): void
protected function setEmpty($empty = null)
{
$this->empty = $this->i18n($empty);
}
public function empty(): string|null
public function empty(): ?string
{
return $this->stringTemplate($this->empty);
}

View File

@@ -4,9 +4,9 @@ namespace Kirby\Form\Mixin;
trait Max
{
protected int|null $max;
protected $max;
public function max(): int|null
public function max(): ?int
{
return $this->max;
}

View File

@@ -4,9 +4,9 @@ namespace Kirby\Form\Mixin;
trait Min
{
protected int|null $min;
protected $min;
public function min(): int|null
public function min(): ?int
{
return $this->min;
}

210
kirby/src/Form/Options.php Normal file
View File

@@ -0,0 +1,210 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\App;
use Kirby\Toolkit\I18n;
/**
* Foundation for the Options query
* classes, that are used to generate
* options arrays for select fields,
* radio boxes, checkboxes and more.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Options
{
/**
* Returns the classes of predefined Kirby objects
*
* @return array
*/
protected static function aliases(): array
{
return [
'Kirby\Cms\File' => 'file',
'Kirby\Toolkit\Obj' => 'arrayItem',
'Kirby\Cms\Block' => 'block',
'Kirby\Cms\Page' => 'page',
'Kirby\Cms\StructureObject' => 'structureItem',
'Kirby\Cms\User' => 'user',
];
}
/**
* Brings options through api
*
* @param $api
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function api($api, $model = null): array
{
$model ??= App::instance()->site();
$fetch = null;
$text = null;
$value = null;
if (is_array($api) === true) {
$fetch = $api['fetch'] ?? null;
$text = $api['text'] ?? null;
$value = $api['value'] ?? null;
$url = $api['url'] ?? null;
} else {
$url = $api;
}
$optionsApi = new OptionsApi([
'data' => static::data($model),
'fetch' => $fetch,
'url' => $url,
'text' => $text,
'value' => $value
]);
return $optionsApi->options();
}
/**
* @param \Kirby\Cms\Model $model
* @return array
*/
protected static function data($model): array
{
$kirby = $model->kirby();
// default data setup
$data = [
'kirby' => $kirby,
'site' => $kirby->site(),
'users' => $kirby->users(),
];
// add the model by the proper alias
foreach (static::aliases() as $className => $alias) {
if (is_a($model, $className) === true) {
$data[$alias] = $model;
}
}
return $data;
}
/**
* Brings options by supporting both api and query
*
* @param $options
* @param array $props
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function factory($options, array $props = [], $model = null): array
{
switch ($options) {
case 'api':
$options = static::api($props['api'], $model);
break;
case 'query':
$options = static::query($props['query'], $model);
break;
case 'children':
case 'grandChildren':
case 'siblings':
case 'index':
case 'files':
case 'images':
case 'documents':
case 'videos':
case 'audio':
case 'code':
case 'archives':
$options = static::query('page.' . $options, $model);
break;
case 'pages':
$options = static::query('site.index', $model);
break;
}
if (is_array($options) === false) {
return [];
}
$result = [];
foreach ($options as $key => $option) {
if (is_array($option) === false || isset($option['value']) === false) {
$option = [
'value' => is_int($key) ? $option : $key,
'text' => $option
];
}
// fallback for the text
$option['text'] ??= $option['value'];
// translate the option text
if (is_array($option['text']) === true) {
$option['text'] = I18n::translate($option['text'], $option['text']);
}
// add the option to the list
$result[] = $option;
}
return $result;
}
/**
* Brings options with query
*
* @param $query
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function query($query, $model = null): array
{
$model ??= App::instance()->site();
// default text setup
$text = [
'arrayItem' => '{{ arrayItem.value }}',
'block' => '{{ block.type }}: {{ block.id }}',
'file' => '{{ file.filename }}',
'page' => '{{ page.title }}',
'structureItem' => '{{ structureItem.title }}',
'user' => '{{ user.username }}',
];
// default value setup
$value = [
'arrayItem' => '{{ arrayItem.value }}',
'block' => '{{ block.id }}',
'file' => '{{ file.id }}',
'page' => '{{ page.id }}',
'structureItem' => '{{ structureItem.id }}',
'user' => '{{ user.email }}',
];
// resolve array query setup
if (is_array($query) === true) {
$text = $query['text'] ?? $text;
$value = $query['value'] ?? $value;
$query = $query['fetch'] ?? null;
}
$optionsQuery = new OptionsQuery([
'aliases' => static::aliases(),
'data' => static::data($model),
'query' => $query,
'text' => $text,
'value' => $value
]);
return $optionsQuery->options();
}
}

View File

@@ -0,0 +1,242 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\Nest;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Http\Remote;
use Kirby\Http\Url;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Query;
use Kirby\Toolkit\Str;
/**
* The OptionsApi class handles fetching options
* from any REST API with valid JSON data.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class OptionsApi
{
use Properties;
/**
* @var array
*/
protected $data;
/**
* @var string|null
*/
protected $fetch;
/**
* @var array|string|null
*/
protected $options;
/**
* @var string
*/
protected $text = '{{ item.value }}';
/**
* @var string
*/
protected $url;
/**
* @var string
*/
protected $value = '{{ item.key }}';
/**
* OptionsApi constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setProperties($props);
}
/**
* @return array
*/
public function data(): array
{
return $this->data;
}
/**
* @return mixed
*/
public function fetch()
{
return $this->fetch;
}
/**
* @param string $field
* @param array $data
* @return string
*/
protected function field(string $field, array $data): string
{
$value = $this->$field();
return Str::safeTemplate($value, $data);
}
/**
* @return array
* @throws \Exception
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function options(): array
{
if (is_array($this->options) === true) {
return $this->options;
}
if (Url::isAbsolute($this->url()) === true) {
// URL, request via cURL
$data = Remote::get($this->url())->json();
} else {
// local file, get contents locally
// ensure the file exists before trying to load it as the
// file_get_contents() warnings need to be suppressed
if (is_file($this->url()) !== true) {
throw new Exception('Local file ' . $this->url() . ' was not found');
}
$content = @file_get_contents($this->url());
if (is_string($content) !== true) {
throw new Exception('Unexpected read error'); // @codeCoverageIgnore
}
if (empty($content) === true) {
return [];
}
$data = json_decode($content, true);
}
if (is_array($data) === false) {
throw new InvalidArgumentException('Invalid options format');
}
$result = (new Query($this->fetch(), Nest::create($data)))->result();
$options = [];
foreach ($result as $item) {
$data = array_merge($this->data(), ['item' => $item]);
$options[] = [
'text' => $this->field('text', $data),
'value' => $this->field('value', $data),
];
}
return $options;
}
/**
* @param array $data
* @return $this
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param string|null $fetch
* @return $this
*/
protected function setFetch(?string $fetch = null)
{
$this->fetch = $fetch;
return $this;
}
/**
* @param array|string|null $options
* @return $this
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param string $text
* @return $this
*/
protected function setText(?string $text = null)
{
$this->text = $text;
return $this;
}
/**
* @param string $url
* @return $this
*/
protected function setUrl(string $url)
{
$this->url = $url;
return $this;
}
/**
* @param string|null $value
* @return $this
*/
protected function setValue(?string $value = null)
{
$this->value = $value;
return $this;
}
/**
* @return string
*/
public function text(): string
{
return $this->text;
}
/**
* @return array
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function toArray(): array
{
return $this->options();
}
/**
* @return string
*/
public function url(): string
{
return Str::template($this->url, $this->data());
}
/**
* @return string
*/
public function value(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,271 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\Field;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Query;
use Kirby\Toolkit\Str;
/**
* Option Queries are run against any set
* of data. In case of Kirby, you can query
* pages, files, users or structures to create
* options out of them.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class OptionsQuery
{
use Properties;
/**
* @var array
*/
protected $aliases = [];
/**
* @var array
*/
protected $data;
/**
* @var array|string|null
*/
protected $options;
/**
* @var string
*/
protected $query;
/**
* @var mixed
*/
protected $text;
/**
* @var mixed
*/
protected $value;
/**
* OptionsQuery constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setProperties($props);
}
/**
* @return array
*/
public function aliases(): array
{
return $this->aliases;
}
/**
* @return array
*/
public function data(): array
{
return $this->data;
}
/**
* @param string $object
* @param string $field
* @param array $data
* @return string
* @throws \Kirby\Exception\NotFoundException
*/
protected function template(string $object, string $field, array $data)
{
$value = $this->$field();
if (is_array($value) === true) {
if (isset($value[$object]) === false) {
throw new NotFoundException('Missing "' . $field . '" definition');
}
$value = $value[$object];
}
return Str::safeTemplate($value, $data);
}
/**
* @return array
*/
public function options(): array
{
if (is_array($this->options) === true) {
return $this->options;
}
$data = $this->data();
$query = new Query($this->query(), $data);
$result = $query->result();
$result = $this->resultToCollection($result);
$options = [];
foreach ($result as $item) {
$alias = $this->resolve($item);
$data = array_merge($data, [$alias => $item]);
$options[] = [
'text' => $this->template($alias, 'text', $data),
'value' => $this->template($alias, 'value', $data)
];
}
return $this->options = $options;
}
/**
* @return string
*/
public function query(): string
{
return $this->query;
}
/**
* @param $object
* @return mixed|string|null
*/
public function resolve($object)
{
// fast access
if ($alias = ($this->aliases[get_class($object)] ?? null)) {
return $alias;
}
// slow but precise resolving
foreach ($this->aliases as $className => $alias) {
if (is_a($object, $className) === true) {
return $alias;
}
}
return 'item';
}
/**
* @param $result
* @throws \Kirby\Exception\InvalidArgumentException
*/
protected function resultToCollection($result)
{
if (is_array($result)) {
foreach ($result as $key => $item) {
if (is_scalar($item) === true) {
$result[$key] = new Obj([
'key' => new Field(null, 'key', $key),
'value' => new Field(null, 'value', $item),
]);
}
}
$result = new Collection($result);
}
if (is_a($result, 'Kirby\Toolkit\Collection') === false) {
throw new InvalidArgumentException('Invalid query result data');
}
return $result;
}
/**
* @param array|null $aliases
* @return $this
*/
protected function setAliases(?array $aliases = null)
{
$this->aliases = $aliases;
return $this;
}
/**
* @param array $data
* @return $this
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param array|string|null $options
* @return $this
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param string $query
* @return $this
*/
protected function setQuery(string $query)
{
$this->query = $query;
return $this;
}
/**
* @param mixed $text
* @return $this
*/
protected function setText($text)
{
$this->text = $text;
return $this;
}
/**
* @param mixed $value
* @return $this
*/
protected function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function text()
{
return $this->text;
}
public function toArray(): array
{
return $this->options();
}
/**
* @return mixed
*/
public function value()
{
return $this->value;
}
}

View File

@@ -20,6 +20,8 @@ class Validations
* Validates if the field value is boolean
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function boolean($field, $value): bool
@@ -38,9 +40,12 @@ class Validations
/**
* Validates if the field value is valid date
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function date(Field|FieldClass $field, mixed $value): bool
public static function date($field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::date($value) !== true) {
@@ -56,9 +61,12 @@ class Validations
/**
* Validates if the field value is valid email
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function email(Field|FieldClass $field, mixed $value): bool
public static function email($field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::email($value) === false) {
@@ -74,14 +82,14 @@ class Validations
/**
* Validates if the field value is maximum
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function max(Field|FieldClass $field, mixed $value): bool
public static function max($field, $value): bool
{
if (
$field->isEmpty($value) === false &&
$field->max() !== null
) {
if ($field->isEmpty($value) === false && $field->max() !== null) {
if (V::max($value, $field->max()) === false) {
throw new InvalidArgumentException(
V::message('max', $value, $field->max())
@@ -95,14 +103,14 @@ class Validations
/**
* Validates if the field value is max length
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function maxlength(Field|FieldClass $field, mixed $value): bool
public static function maxlength($field, $value): bool
{
if (
$field->isEmpty($value) === false &&
$field->maxlength() !== null
) {
if ($field->isEmpty($value) === false && $field->maxlength() !== null) {
if (V::maxLength($value, $field->maxlength()) === false) {
throw new InvalidArgumentException(
V::message('maxlength', $value, $field->maxlength())
@@ -116,14 +124,14 @@ class Validations
/**
* Validates if the field value is minimum
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function min(Field|FieldClass $field, mixed $value): bool
public static function min($field, $value): bool
{
if (
$field->isEmpty($value) === false &&
$field->min() !== null
) {
if ($field->isEmpty($value) === false && $field->min() !== null) {
if (V::min($value, $field->min()) === false) {
throw new InvalidArgumentException(
V::message('min', $value, $field->min())
@@ -137,14 +145,14 @@ class Validations
/**
* Validates if the field value is min length
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function minlength(Field|FieldClass $field, mixed $value): bool
public static function minlength($field, $value): bool
{
if (
$field->isEmpty($value) === false &&
$field->minlength() !== null
) {
if ($field->isEmpty($value) === false && $field->minlength() !== null) {
if (V::minLength($value, $field->minlength()) === false) {
throw new InvalidArgumentException(
V::message('minlength', $value, $field->minlength())
@@ -158,9 +166,12 @@ class Validations
/**
* Validates if the field value matches defined pattern
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function pattern(Field|FieldClass $field, mixed $value): bool
public static function pattern($field, $value): bool
{
if ($field->isEmpty($value) === false && $field->pattern() !== null) {
if (V::match($value, '/' . $field->pattern() . '/i') === false) {
@@ -176,15 +187,14 @@ class Validations
/**
* Validates if the field value is required
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function required(Field|FieldClass $field, mixed $value): bool
public static function required($field, $value): bool
{
if (
$field->isRequired() === true &&
$field->save() === true &&
$field->isEmpty($value) === true
) {
if ($field->isRequired() === true && $field->save() === true && $field->isEmpty($value) === true) {
throw new InvalidArgumentException([
'key' => 'validation.required'
]);
@@ -196,9 +206,12 @@ class Validations
/**
* Validates if the field value is in defined options
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function option(Field|FieldClass $field, mixed $value): bool
public static function option($field, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
@@ -216,9 +229,12 @@ class Validations
/**
* Validates if the field values is in defined options
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function options(Field|FieldClass $field, mixed $value): bool
public static function options($field, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
@@ -237,9 +253,12 @@ class Validations
/**
* Validates if the field value is valid time
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function time(Field|FieldClass $field, mixed $value): bool
public static function time($field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::time($value) !== true) {
@@ -255,9 +274,12 @@ class Validations
/**
* Validates if the field value is valid url
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function url(Field|FieldClass $field, mixed $value): bool
public static function url($field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::url($value) === false) {