Initial commit

This commit is contained in:
Paul Nicoué 2022-06-17 17:51:59 +02:00
commit 73c6b816c0
716 changed files with 170045 additions and 0 deletions

507
kirby/src/Form/Field.php Normal file
View file

@ -0,0 +1,507 @@
<?php
namespace Kirby\Form;
use Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Component;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\V;
/**
* Form Field object that takes a Vue component style
* array of properties and methods and converts them
* to a usable field option array for the API.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Field extends Component
{
/**
* An array of all found errors
*
* @var array|null
*/
protected $errors;
/**
* Parent collection with all fields of the current form
*
* @var \Kirby\Form\Fields|null
*/
protected $formFields;
/**
* Registry for all component mixins
*
* @var array
*/
public static $mixins = [];
/**
* Registry for all component types
*
* @var array
*/
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 $formFields = null)
{
if (isset(static::$types[$type]) === false) {
throw new InvalidArgumentException('The field type "' . $type . '" does not exist');
}
if (isset($attrs['model']) === false) {
throw new InvalidArgumentException('Field requires a model');
}
$this->formFields = $formFields;
// use the type as fallback for the name
$attrs['name'] ??= $type;
$attrs['type'] = $type;
parent::__construct($type, $attrs);
}
/**
* Returns field api call
*
* @return mixed
*/
public function api()
{
if (
isset($this->options['api']) === true &&
is_a($this->options['api'], 'Closure') === true
) {
return $this->options['api']->call($this);
}
}
/**
* Returns field data
*
* @param bool $default
* @return mixed
*/
public function data(bool $default = false)
{
$save = $this->options['save'] ?? true;
if ($default === true && $this->isEmpty($this->value)) {
$value = $this->default();
} else {
$value = $this->value;
}
if ($save === false) {
return null;
}
if (is_a($save, 'Closure') === true) {
return $save->call($this, $value);
}
return $value;
}
/**
* Default props and computed of the field
*
* @return array
*/
public static function defaults(): array
{
return [
'props' => [
/**
* Optional text that will be shown after the input
*/
'after' => function ($after = null) {
return I18n::translate($after, $after);
},
/**
* Sets the focus on this field when the form loads. Only the first field with this label gets
*/
'autofocus' => function (bool $autofocus = null): bool {
return $autofocus ?? false;
},
/**
* Optional text that will be shown before the input
*/
'before' => function ($before = null) {
return I18n::translate($before, $before);
},
/**
* Default value for the field, which will be used when a page/file/user is created
*/
'default' => function ($default = null) {
return $default;
},
/**
* If `true`, the field is no longer editable and will not be saved
*/
'disabled' => function (bool $disabled = null): bool {
return $disabled ?? false;
},
/**
* Optional help text below the field
*/
'help' => function ($help = null) {
return I18n::translate($help, $help);
},
/**
* Optional icon that will be shown at the end of the field
*/
'icon' => function (string $icon = null) {
return $icon;
},
/**
* The field label can be set as string or associative array with translations
*/
'label' => function ($label = null) {
return I18n::translate($label, $label);
},
/**
* Optional placeholder value that will be shown when the field is empty
*/
'placeholder' => function ($placeholder = null) {
return I18n::translate($placeholder, $placeholder);
},
/**
* If `true`, the field has to be filled in correctly to be saved.
*/
'required' => function (bool $required = null): bool {
return $required ?? false;
},
/**
* If `false`, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.
*/
'translate' => function (bool $translate = true): bool {
return $translate;
},
/**
* Conditions when the field will be shown (since 3.1.0)
*/
'when' => function ($when = null) {
return $when;
},
/**
* The width of the field in the field grid. Available widths: `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`
*/
'width' => function (string $width = '1/1') {
return $width;
},
'value' => function ($value = null) {
return $value;
}
],
'computed' => [
'after' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->after !== null) {
return $this->model()->toString($this->after);
}
},
'before' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->before !== null) {
return $this->model()->toString($this->before);
}
},
'default' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->default === null) {
return;
}
if (is_string($this->default) === false) {
return $this->default;
}
return $this->model()->toString($this->default);
},
'help' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->help) {
$help = $this->model()->toSafeString($this->help);
$help = $this->kirby()->kirbytext($help);
return $help;
}
},
'label' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->label !== null) {
return $this->model()->toString($this->label);
}
},
'placeholder' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->placeholder !== null) {
return $this->model()->toString($this->placeholder);
}
}
]
];
}
/**
* 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 $formFields = null)
{
$field = static::$types[$type] ?? null;
if (is_string($field) && class_exists($field) === true) {
$attrs['siblings'] = $formFields;
return new $field($attrs);
}
return new static($type, $attrs, $formFields);
}
/**
* Parent collection with all fields of the current form
*
* @return \Kirby\Form\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
{
if ($this->errors === null) {
$this->validate();
}
return $this->errors;
}
/**
* Checks if the field is empty
*
* @param mixed ...$args
* @return bool
*/
public function isEmpty(...$args): bool
{
if (count($args) === 0) {
$value = $this->value();
} else {
$value = $args[0];
}
if (isset($this->options['isEmpty']) === true) {
return $this->options['isEmpty']->call($this, $value);
}
return in_array($value, [null, '', []], true);
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return empty($this->errors()) === false;
}
/**
* Checks if the field is required
*
* @return bool
*/
public function isRequired(): bool
{
return $this->required ?? false;
}
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
return empty($this->errors()) === true;
}
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby()
{
return $this->model()->kirby();
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model()
{
return $this->model;
}
/**
* Checks if the field needs a value before being saved;
* this is the case if all of the following requirements are met:
* - The field is saveable
* - 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) {
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();
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;
}
}
}
}
// either there was no `when` condition or all conditions matched
return true;
}
/**
* Checks if the field is saveable
*
* @return bool
*/
public function save(): bool
{
return ($this->options['save'] ?? true) !== false;
}
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
$array = parent::toArray();
unset($array['model']);
$array['saveable'] = $this->save();
$array['signature'] = md5(json_encode($array));
ksort($array);
return array_filter(
$array,
fn ($item) => $item !== null && is_object($item) === false
);
}
/**
* Runs the validations defined for the field
*
* @return void
*/
protected function validate(): void
{
$validations = $this->options['validations'] ?? [];
$this->errors = [];
// validate required values
if ($this->needsValue() === true) {
$this->errors['required'] = I18n::translate('error.validation.required');
}
foreach ($validations as $key => $validation) {
if (is_int($key) === true) {
// predefined validation
try {
Validations::$validation($this, $this->value());
} catch (Exception $e) {
$this->errors[$validation] = $e->getMessage();
}
continue;
}
if (is_a($validation, 'Closure') === true) {
try {
$validation->call($this, $this->value());
} catch (Exception $e) {
$this->errors[$key] = $e->getMessage();
}
}
}
if (
empty($this->validate) === false &&
($this->isEmpty() === false || $this->isRequired() === true)
) {
$rules = A::wrap($this->validate);
$errors = V::errors($this->value(), $rules);
if (empty($errors) === false) {
$this->errors = array_merge($this->errors, $errors);
}
}
}
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value()
{
return $this->save() ? $this->value : null;
}
}

View file

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

View file

@ -0,0 +1,232 @@
<?php
namespace Kirby\Form\Field;
use Kirby\Cms\Blueprint;
use Kirby\Cms\Fieldset;
use Kirby\Cms\Layout;
use Kirby\Cms\Layouts;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Form\Form;
use Kirby\Toolkit\Str;
use Throwable;
class LayoutField extends BlocksField
{
protected $layouts;
protected $settings;
public function __construct(array $params)
{
$this->setModel($params['model'] ?? site());
$this->setLayouts($params['layouts'] ?? ['1/1']);
$this->setSettings($params['settings'] ?? null);
parent::__construct($params);
}
public function fill($value = null)
{
$value = $this->valueFromJson($value);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
foreach ($layouts as $layoutIndex => $layout) {
if ($this->settings !== null) {
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
}
foreach ($layout['columns'] as $columnIndex => $column) {
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
}
}
$this->value = $layouts;
}
public function attrsForm(array $input = [])
{
$settings = $this->settings();
return new Form([
'fields' => $settings ? $settings->fields() : [],
'model' => $this->model,
'strict' => true,
'values' => $input,
]);
}
public function layouts(): ?array
{
return $this->layouts;
}
public function props(): array
{
$settings = $this->settings();
return array_merge(parent::props(), [
'settings' => $settings !== null ? $settings->toArray() : null,
'layouts' => $this->layouts()
]);
}
public function routes(): array
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field) {
$defaults = $field->attrsForm([])->data(true);
$attrs = $field->attrsForm($defaults)->values();
$columns = get('columns') ?? ['1/1'];
return Layout::factory([
'attrs' => $attrs,
'columns' => array_map(fn ($width) => [
'blocks' => [],
'id' => uuid(),
'width' => $width,
], $columns)
])->toArray();
},
];
$routes[] = [
'pattern' => 'fields/(:any)/(:all?)',
'method' => 'ALL',
'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])
]);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
}
];
return $routes;
}
protected function setLayouts(array $layouts = [])
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
$layouts
);
}
protected function setSettings($settings = null)
{
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()
{
return $this->settings;
}
public function store($value)
{
$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 (empty($value) === true) {
return '';
}
foreach ($value as $layoutIndex => $layout) {
if ($this->settings !== null) {
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
}
foreach ($layout['columns'] as $columnIndex => $column) {
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
}
}
return $this->valueToJson($value, $this->pretty());
}
public function validations(): array
{
return [
'layout' => function ($value) {
$fields = [];
$layoutIndex = 0;
foreach ($value as $layout) {
$layoutIndex++;
// validate settings form
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
$errors = $field->errors();
if (empty($errors) === false) {
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'];
try {
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
} catch (Throwable $e) {
// skip invalid blocks
continue;
}
// store the fields for the next round
$fields[$blockType] = $blockFields;
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$errors = $field->errors();
// rough first validation
if (empty($errors) === false) {
throw new InvalidArgumentException([
'key' => 'layout.validation.block',
'data' => [
'blockIndex' => $blockIndex,
'layoutIndex' => $layoutIndex
]
]);
}
}
}
}
}
return true;
}
];
}
}

View file

@ -0,0 +1,875 @@
<?php
namespace Kirby\Form;
use Exception;
use Kirby\Cms\HasSiblings;
use Kirby\Cms\ModelWithContent;
use Kirby\Data\Data;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;
use Throwable;
/**
* Abstract field class to be used instead
* of functional field components for more
* control.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
abstract class FieldClass
{
use HasSiblings;
/**
* @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;
$this->setAfter($params['after'] ?? null);
$this->setAutofocus($params['autofocus'] ?? false);
$this->setBefore($params['before'] ?? null);
$this->setDefault($params['default'] ?? null);
$this->setDisabled($params['disabled'] ?? false);
$this->setHelp($params['help'] ?? null);
$this->setIcon($params['icon'] ?? null);
$this->setLabel($params['label'] ?? null);
$this->setModel($params['model'] ?? site());
$this->setName($params['name'] ?? null);
$this->setPlaceholder($params['placeholder'] ?? null);
$this->setRequired($params['required'] ?? false);
$this->setSiblings($params['siblings'] ?? null);
$this->setTranslate($params['translate'] ?? true);
$this->setWhen($params['when'] ?? null);
$this->setWidth($params['width'] ?? null);
if (array_key_exists('value', $params) === true) {
$this->fill($params['value']);
}
}
/**
* @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;
}
/**
* @return string|null
*/
public function before(): ?string
{
return $this->stringTemplate($this->before);
}
/**
* @deprecated 3.5.0
* @todo remove when the general field class setup has been refactored
*
* 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)
{
return $this->store($this->value($default));
}
/**
* Returns the default value for the field,
* which will be used when a page/file/user is created
*
* @return mixed
*/
public function default()
{
if (is_string($this->default) === false) {
return $this->default;
}
return $this->stringTemplate($this->default);
}
/**
* If `true`, the field is no longer editable and will not be saved
*
* @return bool
*/
public function disabled(): bool
{
return $this->disabled;
}
/**
* Runs all validations and returns an array of
* error messages
*
* @return array
*/
public function errors(): array
{
return $this->validate();
}
/**
* Setter for the value
*
* @param mixed $value
* @return void
*/
public function fill($value = null)
{
$this->value = $value;
}
/**
* Optional help text below the field
*
* @return string|null
*/
public function help(): ?string
{
if (empty($this->help) === false) {
$help = $this->stringTemplate($this->help);
$help = $this->kirby()->kirbytext($help);
return $help;
}
return 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
{
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());
}
/**
* @param mixed $value
* @return bool
*/
public function isEmptyValue($value = null): bool
{
return in_array($value, [null, '', []], true);
}
/**
* 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;
}
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
return empty($this->errors()) === true;
}
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\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()));
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model()
{
return $this->model;
}
/**
* Returns the field name
*
* @return string
*/
public function name(): string
{
return $this->name ?? $this->type();
}
/**
* Checks if the field needs a value before being saved;
* this is the case if all of the following requirements are met:
* - The field is saveable
* - 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->isSaveable() === 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->siblings();
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;
}
}
}
}
// either there was no `when` condition or all conditions matched
return true;
}
/**
* Returns all original params for the field
*
* @return array
*/
public function params(): array
{
return $this->params;
}
/**
* Optional placeholder value that will be shown when the field is empty
*
* @return string|null
*/
public function placeholder(): ?string
{
return $this->stringTemplate($this->placeholder);
}
/**
* Define the props that will be sent to
* the Vue component
*
* @return array
*/
public function props(): array
{
return [
'after' => $this->after(),
'autofocus' => $this->autofocus(),
'before' => $this->before(),
'default' => $this->default(),
'disabled' => $this->isDisabled(),
'help' => $this->help(),
'icon' => $this->icon(),
'label' => $this->label(),
'name' => $this->name(),
'placeholder' => $this->placeholder(),
'required' => $this->isRequired(),
'saveable' => $this->isSaveable(),
'translate' => $this->translate(),
'type' => $this->type(),
'when' => $this->when(),
'width' => $this->width(),
];
}
/**
* If `true`, the field has to be filled in correctly to be saved.
*
* @return bool
*/
public function required(): bool
{
return $this->required;
}
/**
* Routes for the field API
*
* @return array
*/
public function routes(): array
{
return [];
}
/**
* @deprecated 3.5.0
* @todo remove when the general field class setup has been refactored
* @return bool
*/
public function save()
{
return $this->isSaveable();
}
/**
* @param array|string|null $after
* @return void
*/
protected function setAfter($after = null)
{
$this->after = $this->i18n($after);
}
/**
* @param bool $autofocus
* @return void
*/
protected function setAutofocus(bool $autofocus = false)
{
$this->autofocus = $autofocus;
}
/**
* @param array|string|null $before
* @return void
*/
protected function setBefore($before = null)
{
$this->before = $this->i18n($before);
}
/**
* @param mixed $default
* @return void
*/
protected function setDefault($default = null)
{
$this->default = $default;
}
/**
* @param bool $disabled
* @return void
*/
protected function setDisabled(bool $disabled = false)
{
$this->disabled = $disabled;
}
/**
* @param array|string|null $help
* @return void
*/
protected function setHelp($help = null)
{
$this->help = $this->i18n($help);
}
/**
* @param string|null $icon
* @return void
*/
protected function setIcon(?string $icon = null)
{
$this->icon = $icon;
}
/**
* @param array|string|null $label
* @return void
*/
protected function setLabel($label = null)
{
$this->label = $this->i18n($label);
}
/**
* @param \Kirby\Cms\ModelWithContent $model
* @return void
*/
protected function setModel(ModelWithContent $model)
{
$this->model = $model;
}
/**
* @param string|null $name
* @return void
*/
protected function setName(string $name = null)
{
$this->name = $name;
}
/**
* @param array|string|null $placeholder
* @return void
*/
protected function setPlaceholder($placeholder = null)
{
$this->placeholder = $this->i18n($placeholder);
}
/**
* @param bool $required
* @return void
*/
protected function setRequired(bool $required = false)
{
$this->required = $required;
}
/**
* @param \Kirby\Form\Fields|null $siblings
* @return void
*/
protected function setSiblings(?Fields $siblings = null)
{
$this->siblings = $siblings ?? new Fields([$this]);
}
/**
* @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($when = null)
{
$this->when = $when;
}
/**
* Setter for the field width
*
* @param string|null $width
* @return void
*/
protected function setWidth(string $width = null)
{
$this->width = $width;
}
/**
* Returns all sibling fields
*
* @return \Kirby\Form\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 $string = null): ?string
{
if ($string !== null) {
return $this->model->toString($string);
}
return null;
}
/**
* Converts the given value to a value
* that can be stored in the text file
*
* @param mixed $value
* @return mixed
*/
public function store($value)
{
return $value;
}
/**
* Should the field be translatable?
*
* @return bool
*/
public function translate(): bool
{
return $this->translate;
}
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
$props = $this->props();
$props['signature'] = md5(json_encode($props));
ksort($props);
return array_filter($props, fn ($item) => $item !== null);
}
/**
* Returns the field type
*
* @return string
*/
public function type(): string
{
return lcfirst(basename(str_replace(['\\', 'Field'], ['/', ''], static::class)));
}
/**
* Runs the validations defined for the field
*
* @return array
*/
protected function validate(): array
{
$validations = $this->validations();
$value = $this->value();
$errors = [];
// validate required values
if ($this->needsValue() === true) {
$errors['required'] = I18n::translate('error.validation.required');
}
foreach ($validations as $key => $validation) {
if (is_int($key) === true) {
// predefined validation
try {
Validations::$validation($this, $value);
} catch (Exception $e) {
$errors[$validation] = $e->getMessage();
}
continue;
}
if (is_a($validation, 'Closure') === true) {
try {
$validation->call($this, $value);
} catch (Exception $e) {
$errors[$key] = $e->getMessage();
}
}
}
return $errors;
}
/**
* Defines all validation rules
*
* @return array
*/
protected function validations(): array
{
return [];
}
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value(bool $default = false)
{
if ($this->isSaveable() === false) {
return null;
}
if ($default === true && $this->isEmpty() === true) {
return $this->default();
}
return $this->value;
}
/**
* @param mixed $value
* @return array
*/
protected function valueFromJson($value): array
{
try {
return Data::decode($value, 'json');
} catch (Throwable $e) {
return [];
}
}
/**
* @param mixed $value
* @return array
*/
protected function valueFromYaml($value): array
{
return Data::decode($value, 'yaml');
}
/**
* @param array|null $value
* @param bool $pretty
* @return string
*/
protected function valueToJson(array $value = null, bool $pretty = false): string
{
if ($pretty === true) {
return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return json_encode($value);
}
/**
* @param array|null $value
* @return string
*/
protected function valueToYaml(array $value = null): string
{
return Data::encode($value, 'yaml');
}
/**
* Conditions when the field will be shown
*
* @return array|null
*/
public function when(): ?array
{
return $this->when;
}
/**
* Returns the width of the field in
* the Panel grid
*
* @return string
*/
public function width(): string
{
return $this->width ?? '1/1';
}
}

57
kirby/src/Form/Fields.php Normal file
View file

@ -0,0 +1,57 @@
<?php
namespace Kirby\Form;
use Closure;
use Kirby\Toolkit\Collection;
/**
* A collection of Field objects
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Fields extends Collection
{
/**
* Internal setter for each object in the Collection.
* This takes care of validation and of setting
* the collection prop on each object correctly.
*
* @param string $name
* @param object|array $field
* @return $this
*/
public function __set(string $name, $field)
{
if (is_array($field) === true) {
// use the array key as name if the name is not set
$field['name'] ??= $name;
$field = Field::factory($field['type'], $field, $this);
}
return parent::__set($field->name(), $field);
}
/**
* 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
{
$array = [];
foreach ($this as $field) {
$array[$field->name()] = $field->toArray();
}
return $array;
}
}

395
kirby/src/Form/Form.php Normal file
View file

@ -0,0 +1,395 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\App;
use Kirby\Cms\Model;
use Kirby\Data\Data;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\Str;
use Throwable;
/**
* The main form class, that is being
* used to create a list of form fields
* and handles global form validation
* and submission
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Form
{
/**
* An array of all found errors
*
* @var array|null
*/
protected $errors;
/**
* Fields in the form
*
* @var \Kirby\Form\Fields|null
*/
protected $fields;
/**
* All values of form
*
* @var array
*/
protected $values = [];
/**
* Form constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$fields = $props['fields'] ?? [];
$values = $props['values'] ?? [];
$input = $props['input'] ?? [];
$strict = $props['strict'] ?? false;
$inject = $props;
// prepare field properties for multilang setups
$fields = static::prepareFieldsForLanguage(
$fields,
$props['language'] ?? null
);
// lowercase all value names
$values = array_change_key_case($values);
$input = array_change_key_case($input);
unset($inject['fields'], $inject['values'], $inject['input']);
$this->fields = new Fields();
$this->values = [];
foreach ($fields as $name => $props) {
// inject stuff from the form constructor (model, etc.)
$props = array_merge($inject, $props);
// inject the name
$props['name'] = $name = strtolower($name);
// check if the field is disabled
$disabled = $props['disabled'] ?? false;
// overwrite the field value if not set
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);
} catch (Throwable $e) {
$field = static::exceptionField($e, $props);
}
if ($field->save() !== false) {
$this->values[$name] = $field->value();
}
$this->fields->append($name, $field);
}
if ($strict !== true) {
// use all given values, no matter
// if there's a field or not.
$input = array_merge($values, $input);
foreach ($input as $key => $value) {
if (isset($this->values[$key]) === false) {
$this->values[$key] = $value;
}
}
}
}
/**
* Returns the data required to write to the content file
* Doesn't include default and null values
*
* @return array
*/
public function content(): array
{
return $this->data(false, false);
}
/**
* 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
{
$data = $this->values;
foreach ($this->fields as $field) {
if ($field->save() === false || $field->unset() === true) {
if ($includeNulls === true) {
$data[$field->name()] = null;
} else {
unset($data[$field->name()]);
}
} else {
$data[$field->name()] = $field->data($defaults);
}
}
return $data;
}
/**
* An array of all found errors
*
* @return array
*/
public function errors(): array
{
if ($this->errors !== null) {
return $this->errors;
}
$this->errors = [];
foreach ($this->fields as $field) {
if (empty($field->errors()) === false) {
$this->errors[$field->name()] = [
'label' => $field->label(),
'message' => $field->errors()
];
}
}
return $this->errors;
}
/**
* Shows the error with the field
*
* @param \Throwable $exception
* @param array $props
* @return \Kirby\Form\Field
*/
public static function exceptionField(Throwable $exception, array $props = [])
{
$message = $exception->getMessage();
if (App::instance()->option('debug') === true) {
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
}
$props = array_merge($props, [
'label' => 'Error in "' . $props['name'] . '" field.',
'theme' => 'negative',
'text' => strip_tags($message),
]);
return Field::factory('info', $props);
}
/**
* 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)
{
$form = $this;
$fieldNames = Str::split($name, '+');
$index = 0;
$count = count($fieldNames);
$field = null;
foreach ($fieldNames as $fieldName) {
$index++;
if ($field = $form->fields()->get($fieldName)) {
if ($count !== $index) {
$form = $field->form();
}
} else {
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
}
}
// it can get this error only if $name is an empty string as $name = ''
if ($field === null) {
throw new NotFoundException('No field could be loaded');
}
return $field;
}
/**
* Returns form fields
*
* @return \Kirby\Form\Fields|null
*/
public function fields()
{
return $this->fields;
}
/**
* @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 (is_a($value, 'Closure') === true) {
$values[$key] = $value($original[$key] ?? null);
}
}
// set a few defaults
$props['values'] = array_merge($original, $values);
$props['fields'] ??= [];
$props['model'] = $model;
// search for the blueprint
if (method_exists($model, 'blueprint') === true && $blueprint = $model->blueprint()) {
$props['fields'] = $blueprint->fields();
}
$ignoreDisabled = $props['ignoreDisabled'] ?? false;
// REFACTOR: this could be more elegant
if ($ignoreDisabled === true) {
$props['fields'] = array_map(function ($field) {
$field['disabled'] = false;
return $field;
}, $props['fields']);
}
return new static($props);
}
/**
* Checks if the form is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return empty($this->errors()) === false;
}
/**
* Checks if the form is valid
*
* @return bool
*/
public function isValid(): bool
{
return empty($this->errors()) === true;
}
/**
* 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 $language = null): array
{
$kirby = App::instance(null, true);
// only modify the fields if we have a valid Kirby multilang instance
if (!$kirby || $kirby->multilang() === false) {
return $fields;
}
if ($language === null) {
$language = $kirby->language()->code();
}
if ($language !== $kirby->defaultLanguage()->code()) {
foreach ($fields as $fieldName => $fieldProps) {
// switch untranslatable fields to readonly
if (($fieldProps['translate'] ?? true) === false) {
$fields[$fieldName]['unset'] = true;
$fields[$fieldName]['disabled'] = true;
}
}
}
return $fields;
}
/**
* Converts the data of fields to strings
*
* @param false $defaults
* @return array
*/
public function strings($defaults = false): array
{
$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
{
$array = [
'errors' => $this->errors(),
'fields' => $this->fields->toArray(fn ($item) => $item->toArray()),
'invalid' => $this->isInvalid()
];
return $array;
}
/**
* Returns form values
*
* @return array
*/
public function values(): array
{
return $this->values;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Kirby\Form\Mixin;
trait EmptyState
{
protected $empty;
protected function setEmpty($empty = null)
{
$this->empty = $this->i18n($empty);
}
public function empty(): ?string
{
return $this->stringTemplate($this->empty);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Kirby\Form\Mixin;
trait Max
{
protected $max;
public function max(): ?int
{
return $this->max;
}
protected function setMax(int $max = null)
{
$this->max = $max;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Kirby\Form\Mixin;
trait Min
{
protected $min;
public function min(): ?int
{
return $this->min;
}
protected function setMin(int $min = null)
{
$this->min = $min;
}
}

207
kirby/src/Form/Options.php Normal file
View file

@ -0,0 +1,207 @@
<?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
];
}
// 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

@ -0,0 +1,294 @@
<?php
namespace Kirby\Form;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\V;
/**
* Often used validation rules for fields
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
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
{
if ($field->isEmpty($value) === false) {
if (is_bool($value) === false) {
throw new InvalidArgumentException([
'key' => 'validation.boolean'
]);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::date($value) !== true) {
throw new InvalidArgumentException(
V::message('date', $value)
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::email($value) === false) {
throw new InvalidArgumentException(
V::message('email', $value)
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false && $field->max() !== null) {
if (V::max($value, $field->max()) === false) {
throw new InvalidArgumentException(
V::message('max', $value, $field->max())
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false && $field->maxlength() !== null) {
if (V::maxLength($value, $field->maxlength()) === false) {
throw new InvalidArgumentException(
V::message('maxlength', $value, $field->maxlength())
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false && $field->min() !== null) {
if (V::min($value, $field->min()) === false) {
throw new InvalidArgumentException(
V::message('min', $value, $field->min())
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false && $field->minlength() !== null) {
if (V::minLength($value, $field->minlength()) === false) {
throw new InvalidArgumentException(
V::message('minlength', $value, $field->minlength())
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false && $field->pattern() !== null) {
if (V::match($value, '/' . $field->pattern() . '/i') === false) {
throw new InvalidArgumentException(
V::message('match')
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isRequired() === true && $field->save() === true && $field->isEmpty($value) === true) {
throw new InvalidArgumentException([
'key' => 'validation.required'
]);
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
if (in_array($value, $values, true) !== true) {
throw new InvalidArgumentException([
'key' => 'validation.option'
]);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
foreach ($value as $val) {
if (in_array($val, $values, true) === false) {
throw new InvalidArgumentException([
'key' => 'validation.option'
]);
}
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::time($value) !== true) {
throw new InvalidArgumentException(
V::message('time', $value)
);
}
}
return true;
}
/**
* 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, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::url($value) === false) {
throw new InvalidArgumentException(
V::message('url', $value)
);
}
}
return true;
}
}