Update Kirby and dependencies
This commit is contained in:
parent
503b339974
commit
399fa20902
439 changed files with 66915 additions and 64442 deletions
|
@ -22,486 +22,486 @@ use Kirby\Toolkit\V;
|
|||
*/
|
||||
class Field extends Component
|
||||
{
|
||||
/**
|
||||
* An array of all found errors
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $errors;
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* 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 mixins
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $mixins = [];
|
||||
|
||||
/**
|
||||
* Registry for all component types
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $types = [];
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
if (isset($attrs['model']) === false) {
|
||||
throw new InvalidArgumentException('Field requires a model');
|
||||
}
|
||||
|
||||
$this->formFields = $formFields;
|
||||
$this->formFields = $formFields;
|
||||
|
||||
// use the type as fallback for the name
|
||||
$attrs['name'] ??= $type;
|
||||
$attrs['type'] = $type;
|
||||
// use the type as fallback for the name
|
||||
$attrs['name'] ??= $type;
|
||||
$attrs['type'] = $type;
|
||||
|
||||
parent::__construct($type, $attrs);
|
||||
}
|
||||
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 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;
|
||||
/**
|
||||
* 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 ($default === true && $this->isEmpty($this->value)) {
|
||||
$value = $this->default();
|
||||
} else {
|
||||
$value = $this->value;
|
||||
}
|
||||
|
||||
if ($save === false) {
|
||||
return null;
|
||||
}
|
||||
if ($save === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_a($save, 'Closure') === true) {
|
||||
return $save->call($this, $value);
|
||||
}
|
||||
if (is_a($save, 'Closure') === true) {
|
||||
return $save->call($this, $value);
|
||||
}
|
||||
|
||||
return $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;
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
}
|
||||
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;
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
if (is_string($field) && class_exists($field) === true) {
|
||||
$attrs['siblings'] = $formFields;
|
||||
return new $field($attrs);
|
||||
}
|
||||
|
||||
return new static($type, $attrs, $formFields);
|
||||
}
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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];
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
if (isset($this->options['isEmpty']) === true) {
|
||||
return $this->options['isEmpty']->call($this, $value);
|
||||
}
|
||||
|
||||
return in_array($value, [null, '', []], true);
|
||||
}
|
||||
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 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 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;
|
||||
}
|
||||
/**
|
||||
* 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 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;
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
// 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 ($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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
/**
|
||||
* Converts the field to a plain array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$array = parent::toArray();
|
||||
|
||||
unset($array['model']);
|
||||
unset($array['model']);
|
||||
|
||||
$array['saveable'] = $this->save();
|
||||
$array['signature'] = md5(json_encode($array));
|
||||
$array['saveable'] = $this->save();
|
||||
$array['signature'] = md5(json_encode($array));
|
||||
|
||||
ksort($array);
|
||||
ksort($array);
|
||||
|
||||
return array_filter(
|
||||
$array,
|
||||
fn ($item) => $item !== null && is_object($item) === false
|
||||
);
|
||||
}
|
||||
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 = [];
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
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 (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($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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Returns the value of the field if saveable
|
||||
* otherwise it returns null
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function value()
|
||||
{
|
||||
return $this->save() ? $this->value : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Block;
|
||||
use Kirby\Cms\Blocks as BlocksCollection;
|
||||
use Kirby\Cms\Fieldsets;
|
||||
|
@ -12,270 +13,273 @@ use Kirby\Form\Form;
|
|||
use Kirby\Form\Mixin\EmptyState;
|
||||
use Kirby\Form\Mixin\Max;
|
||||
use Kirby\Form\Mixin\Min;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
class BlocksField extends FieldClass
|
||||
{
|
||||
use EmptyState;
|
||||
use Max;
|
||||
use Min;
|
||||
use EmptyState;
|
||||
use Max;
|
||||
use Min;
|
||||
|
||||
protected $blocks;
|
||||
protected $fieldsets;
|
||||
protected $group;
|
||||
protected $pretty;
|
||||
protected $value = [];
|
||||
protected $blocks;
|
||||
protected $fieldsets;
|
||||
protected $group;
|
||||
protected $pretty;
|
||||
protected $value = [];
|
||||
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? site());
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? App::instance()->site());
|
||||
|
||||
parent::__construct($params);
|
||||
parent::__construct($params);
|
||||
|
||||
$this->setEmpty($params['empty'] ?? null);
|
||||
$this->setGroup($params['group'] ?? 'blocks');
|
||||
$this->setMax($params['max'] ?? null);
|
||||
$this->setMin($params['min'] ?? null);
|
||||
$this->setPretty($params['pretty'] ?? false);
|
||||
}
|
||||
$this->setEmpty($params['empty'] ?? null);
|
||||
$this->setGroup($params['group'] ?? 'blocks');
|
||||
$this->setMax($params['max'] ?? null);
|
||||
$this->setMin($params['min'] ?? null);
|
||||
$this->setPretty($params['pretty'] ?? false);
|
||||
}
|
||||
|
||||
public function blocksToValues($blocks, $to = 'values'): array
|
||||
{
|
||||
$result = [];
|
||||
$fields = [];
|
||||
public function blocksToValues($blocks, $to = 'values'): array
|
||||
{
|
||||
$result = [];
|
||||
$fields = [];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
try {
|
||||
$type = $block['type'];
|
||||
foreach ($blocks as $block) {
|
||||
try {
|
||||
$type = $block['type'];
|
||||
|
||||
// get and cache fields at the same time
|
||||
$fields[$type] ??= $this->fields($block['type']);
|
||||
// get and cache fields at the same time
|
||||
$fields[$type] ??= $this->fields($block['type']);
|
||||
|
||||
// overwrite the block content with form values
|
||||
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
|
||||
// overwrite the block content with form values
|
||||
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
|
||||
|
||||
$result[] = $block;
|
||||
} catch (Throwable $e) {
|
||||
$result[] = $block;
|
||||
$result[] = $block;
|
||||
} catch (Throwable $e) {
|
||||
$result[] = $block;
|
||||
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fields(string $type)
|
||||
{
|
||||
return $this->fieldset($type)->fields();
|
||||
}
|
||||
public function fields(string $type)
|
||||
{
|
||||
return $this->fieldset($type)->fields();
|
||||
}
|
||||
|
||||
public function fieldset(string $type)
|
||||
{
|
||||
if ($fieldset = $this->fieldsets->find($type)) {
|
||||
return $fieldset;
|
||||
}
|
||||
public function fieldset(string $type)
|
||||
{
|
||||
if ($fieldset = $this->fieldsets->find($type)) {
|
||||
return $fieldset;
|
||||
}
|
||||
|
||||
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
|
||||
}
|
||||
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
|
||||
}
|
||||
|
||||
public function fieldsets()
|
||||
{
|
||||
return $this->fieldsets;
|
||||
}
|
||||
public function fieldsets()
|
||||
{
|
||||
return $this->fieldsets;
|
||||
}
|
||||
|
||||
public function fieldsetGroups(): ?array
|
||||
{
|
||||
$fieldsetGroups = $this->fieldsets()->groups();
|
||||
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
|
||||
}
|
||||
public function fieldsetGroups(): ?array
|
||||
{
|
||||
$fieldsetGroups = $this->fieldsets()->groups();
|
||||
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
|
||||
}
|
||||
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = BlocksCollection::parse($value);
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
$this->value = $this->blocksToValues($blocks->toArray());
|
||||
}
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = BlocksCollection::parse($value);
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
$this->value = $this->blocksToValues($blocks->toArray());
|
||||
}
|
||||
|
||||
public function form(array $fields, array $input = [])
|
||||
{
|
||||
return new Form([
|
||||
'fields' => $fields,
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
public function form(array $fields, array $input = [])
|
||||
{
|
||||
return new Form([
|
||||
'fields' => $fields,
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return count($this->value()) === 0;
|
||||
}
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return count($this->value()) === 0;
|
||||
}
|
||||
|
||||
public function group(): string
|
||||
{
|
||||
return $this->group;
|
||||
}
|
||||
public function group(): string
|
||||
{
|
||||
return $this->group;
|
||||
}
|
||||
|
||||
public function pretty(): bool
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
public function pretty(): bool
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
'empty' => $this->empty(),
|
||||
'fieldsets' => $this->fieldsets()->toArray(),
|
||||
'fieldsetGroups' => $this->fieldsetGroups(),
|
||||
'group' => $this->group(),
|
||||
'max' => $this->max(),
|
||||
'min' => $this->min(),
|
||||
] + parent::props();
|
||||
}
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
'empty' => $this->empty(),
|
||||
'fieldsets' => $this->fieldsets()->toArray(),
|
||||
'fieldsetGroups' => $this->fieldsetGroups(),
|
||||
'group' => $this->group(),
|
||||
'max' => $this->max(),
|
||||
'min' => $this->min(),
|
||||
] + parent::props();
|
||||
}
|
||||
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
|
||||
return [
|
||||
[
|
||||
'pattern' => 'uuid',
|
||||
'action' => fn () => ['uuid' => uuid()]
|
||||
],
|
||||
[
|
||||
'pattern' => 'paste',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field) {
|
||||
$value = BlocksCollection::parse(get('html'));
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
return $field->blocksToValues($blocks->toArray());
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)',
|
||||
'method' => 'GET',
|
||||
'action' => function ($fieldsetType) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$defaults = $field->form($fields, [])->data(true);
|
||||
$content = $field->form($fields, $defaults)->values();
|
||||
return [
|
||||
[
|
||||
'pattern' => 'uuid',
|
||||
'action' => fn () => ['uuid' => Str::uuid()]
|
||||
],
|
||||
[
|
||||
'pattern' => 'paste',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field) {
|
||||
$request = App::instance()->request();
|
||||
|
||||
return Block::factory([
|
||||
'content' => $content,
|
||||
'type' => $fieldsetType
|
||||
])->toArray();
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$field = $field->form($fields)->field($fieldName);
|
||||
$value = BlocksCollection::parse($request->get('html'));
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
return $field->blocksToValues($blocks->toArray());
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)',
|
||||
'method' => 'GET',
|
||||
'action' => function ($fieldsetType) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$defaults = $field->form($fields, [])->data(true);
|
||||
$content = $field->form($fields, $defaults)->values();
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
return Block::factory([
|
||||
'content' => $content,
|
||||
'type' => $fieldsetType
|
||||
])->toArray();
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$field = $field->form($fields)->field($fieldName);
|
||||
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
],
|
||||
];
|
||||
}
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
|
||||
public function store($value)
|
||||
{
|
||||
$blocks = $this->blocksToValues((array)$value, 'content');
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if (empty($blocks) === true) {
|
||||
return '';
|
||||
}
|
||||
public function store($value)
|
||||
{
|
||||
$blocks = $this->blocksToValues((array)$value, 'content');
|
||||
|
||||
return $this->valueToJson($blocks, $this->pretty());
|
||||
}
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if (empty($blocks) === true) {
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function setFieldsets($fieldsets, $model)
|
||||
{
|
||||
if (is_string($fieldsets) === true) {
|
||||
$fieldsets = [];
|
||||
}
|
||||
return $this->valueToJson($blocks, $this->pretty());
|
||||
}
|
||||
|
||||
$this->fieldsets = Fieldsets::factory($fieldsets, [
|
||||
'parent' => $model
|
||||
]);
|
||||
}
|
||||
protected function setFieldsets($fieldsets, $model)
|
||||
{
|
||||
if (is_string($fieldsets) === true) {
|
||||
$fieldsets = [];
|
||||
}
|
||||
|
||||
protected function setGroup(string $group = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
$this->fieldsets = Fieldsets::factory($fieldsets, [
|
||||
'parent' => $model
|
||||
]);
|
||||
}
|
||||
|
||||
protected function setPretty(bool $pretty = false)
|
||||
{
|
||||
$this->pretty = $pretty;
|
||||
}
|
||||
protected function setGroup(string $group = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'blocks' => function ($value) {
|
||||
if ($this->min && count($value) < $this->min) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.min.' . ($this->min === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'min' => $this->min
|
||||
]
|
||||
]);
|
||||
}
|
||||
protected function setPretty(bool $pretty = false)
|
||||
{
|
||||
$this->pretty = $pretty;
|
||||
}
|
||||
|
||||
if ($this->max && count($value) > $this->max) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.max.' . ($this->max === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'max' => $this->max
|
||||
]
|
||||
]);
|
||||
}
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'blocks' => function ($value) {
|
||||
if ($this->min && count($value) < $this->min) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.min.' . ($this->min === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'min' => $this->min
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
$index = 0;
|
||||
if ($this->max && count($value) > $this->max) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.max.' . ($this->max === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'max' => $this->max
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($value as $block) {
|
||||
$index++;
|
||||
$blockType = $block['type'];
|
||||
$fields = [];
|
||||
$index = 0;
|
||||
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
foreach ($value as $block) {
|
||||
$index++;
|
||||
$blockType = $block['type'];
|
||||
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.validation',
|
||||
'data' => [
|
||||
'index' => $index,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.validation',
|
||||
'data' => [
|
||||
'index' => $index,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Blueprint;
|
||||
use Kirby\Cms\Fieldset;
|
||||
use Kirby\Cms\Layout;
|
||||
|
@ -13,220 +14,222 @@ use Throwable;
|
|||
|
||||
class LayoutField extends BlocksField
|
||||
{
|
||||
protected $layouts;
|
||||
protected $settings;
|
||||
protected $layouts;
|
||||
protected $settings;
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
$this->setModel($params['model'] ?? site());
|
||||
$this->setLayouts($params['layouts'] ?? ['1/1']);
|
||||
$this->setSettings($params['settings'] ?? null);
|
||||
public function __construct(array $params)
|
||||
{
|
||||
$this->setModel($params['model'] ?? App::instance()->site());
|
||||
$this->setLayouts($params['layouts'] ?? ['1/1']);
|
||||
$this->setSettings($params['settings'] ?? null);
|
||||
|
||||
parent::__construct($params);
|
||||
}
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = $this->valueFromJson($value);
|
||||
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = $this->valueFromJson($value);
|
||||
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
foreach ($layouts as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
|
||||
}
|
||||
foreach ($layouts as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
|
||||
}
|
||||
}
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->value = $layouts;
|
||||
}
|
||||
$this->value = $layouts;
|
||||
}
|
||||
|
||||
public function attrsForm(array $input = [])
|
||||
{
|
||||
$settings = $this->settings();
|
||||
public function attrsForm(array $input = [])
|
||||
{
|
||||
$settings = $this->settings();
|
||||
|
||||
return new Form([
|
||||
'fields' => $settings ? $settings->fields() : [],
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
return new Form([
|
||||
'fields' => $settings ? $settings->fields() : [],
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
|
||||
public function layouts(): ?array
|
||||
{
|
||||
return $this->layouts;
|
||||
}
|
||||
public function layouts(): ?array
|
||||
{
|
||||
return $this->layouts;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
$settings = $this->settings();
|
||||
public function props(): array
|
||||
{
|
||||
$settings = $this->settings();
|
||||
|
||||
return array_merge(parent::props(), [
|
||||
'settings' => $settings !== null ? $settings->toArray() : null,
|
||||
'layouts' => $this->layouts()
|
||||
]);
|
||||
}
|
||||
return array_merge(parent::props(), [
|
||||
'settings' => $settings !== null ? $settings->toArray() : null,
|
||||
'layouts' => $this->layouts()
|
||||
]);
|
||||
}
|
||||
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
$routes = parent::routes();
|
||||
$routes[] = [
|
||||
'pattern' => 'layout',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field) {
|
||||
$defaults = $field->attrsForm([])->data(true);
|
||||
$attrs = $field->attrsForm($defaults)->values();
|
||||
$columns = get('columns') ?? ['1/1'];
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
$routes = parent::routes();
|
||||
$routes[] = [
|
||||
'pattern' => 'layout',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field) {
|
||||
$request = App::instance()->request();
|
||||
|
||||
return Layout::factory([
|
||||
'attrs' => $attrs,
|
||||
'columns' => array_map(fn ($width) => [
|
||||
'blocks' => [],
|
||||
'id' => uuid(),
|
||||
'width' => $width,
|
||||
], $columns)
|
||||
])->toArray();
|
||||
},
|
||||
];
|
||||
$defaults = $field->attrsForm([])->data(true);
|
||||
$attrs = $field->attrsForm($defaults)->values();
|
||||
$columns = $request->get('columns') ?? ['1/1'];
|
||||
|
||||
$routes[] = [
|
||||
'pattern' => 'fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldName, string $path = null) use ($field) {
|
||||
$form = $field->attrsForm();
|
||||
$field = $form->field($fieldName);
|
||||
return Layout::factory([
|
||||
'attrs' => $attrs,
|
||||
'columns' => array_map(fn ($width) => [
|
||||
'blocks' => [],
|
||||
'id' => Str::uuid(),
|
||||
'width' => $width,
|
||||
], $columns)
|
||||
])->toArray();
|
||||
},
|
||||
];
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
$routes[] = [
|
||||
'pattern' => 'fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldName, string $path = null) use ($field) {
|
||||
$form = $field->attrsForm();
|
||||
$field = $form->field($fieldName);
|
||||
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
];
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
|
||||
return $routes;
|
||||
}
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
];
|
||||
|
||||
protected function setLayouts(array $layouts = [])
|
||||
{
|
||||
$this->layouts = array_map(
|
||||
fn ($layout) => Str::split($layout),
|
||||
$layouts
|
||||
);
|
||||
}
|
||||
return $routes;
|
||||
}
|
||||
|
||||
protected function setSettings($settings = null)
|
||||
{
|
||||
if (empty($settings) === true) {
|
||||
$this->settings = null;
|
||||
return;
|
||||
}
|
||||
protected function setLayouts(array $layouts = [])
|
||||
{
|
||||
$this->layouts = array_map(
|
||||
fn ($layout) => Str::split($layout),
|
||||
$layouts
|
||||
);
|
||||
}
|
||||
|
||||
$settings = Blueprint::extend($settings);
|
||||
protected function setSettings($settings = null)
|
||||
{
|
||||
if (empty($settings) === true) {
|
||||
$this->settings = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$settings['icon'] = 'dashboard';
|
||||
$settings['type'] = 'layout';
|
||||
$settings['parent'] = $this->model();
|
||||
$settings = Blueprint::extend($settings);
|
||||
|
||||
$this->settings = Fieldset::factory($settings);
|
||||
}
|
||||
$settings['icon'] = 'dashboard';
|
||||
$settings['type'] = 'layout';
|
||||
$settings['parent'] = $this->model();
|
||||
|
||||
public function settings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
$this->settings = Fieldset::factory($settings);
|
||||
}
|
||||
|
||||
public function store($value)
|
||||
{
|
||||
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
public function settings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if (empty($value) === true) {
|
||||
return '';
|
||||
}
|
||||
public function store($value)
|
||||
{
|
||||
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
foreach ($value as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
|
||||
}
|
||||
// returns empty string to avoid storing empty array as string `[]`
|
||||
// and to consistency work with `$field->isEmpty()`
|
||||
if (empty($value) === true) {
|
||||
return '';
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
|
||||
}
|
||||
}
|
||||
foreach ($value as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
|
||||
}
|
||||
|
||||
return $this->valueToJson($value, $this->pretty());
|
||||
}
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
|
||||
}
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'layout' => function ($value) {
|
||||
$fields = [];
|
||||
$layoutIndex = 0;
|
||||
return $this->valueToJson($value, $this->pretty());
|
||||
}
|
||||
|
||||
foreach ($value as $layout) {
|
||||
$layoutIndex++;
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'layout' => function ($value) {
|
||||
$fields = [];
|
||||
$layoutIndex = 0;
|
||||
|
||||
// validate settings form
|
||||
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
foreach ($value as $layout) {
|
||||
$layoutIndex++;
|
||||
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.settings',
|
||||
'data' => [
|
||||
'index' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
// validate settings form
|
||||
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
// validate blocks in the layout
|
||||
$blockIndex = 0;
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.settings',
|
||||
'data' => [
|
||||
'index' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] ?? [] as $column) {
|
||||
foreach ($column['blocks'] ?? [] as $block) {
|
||||
$blockIndex++;
|
||||
$blockType = $block['type'];
|
||||
// validate blocks in the layout
|
||||
$blockIndex = 0;
|
||||
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
foreach ($layout['columns'] ?? [] as $column) {
|
||||
foreach ($column['blocks'] ?? [] as $block) {
|
||||
$blockIndex++;
|
||||
$blockType = $block['type'];
|
||||
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.block',
|
||||
'data' => [
|
||||
'blockIndex' => $blockIndex,
|
||||
'layoutIndex' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.block',
|
||||
'data' => [
|
||||
'blockIndex' => $blockIndex,
|
||||
'layoutIndex' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -16,42 +16,42 @@ use Kirby\Toolkit\Collection;
|
|||
*/
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* 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 void
|
||||
*/
|
||||
public function __set(string $name, $field): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
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 = [];
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
foreach ($this as $field) {
|
||||
$array[$field->name()] = $field->toArray();
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,373 +23,371 @@ use Throwable;
|
|||
*/
|
||||
class Form
|
||||
{
|
||||
/**
|
||||
* An array of all found errors
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $errors;
|
||||
/**
|
||||
* An array of all found errors
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $errors;
|
||||
|
||||
/**
|
||||
* Fields in the form
|
||||
*
|
||||
* @var \Kirby\Form\Fields|null
|
||||
*/
|
||||
protected $fields;
|
||||
/**
|
||||
* Fields in the form
|
||||
*
|
||||
* @var \Kirby\Form\Fields|null
|
||||
*/
|
||||
protected $fields;
|
||||
|
||||
/**
|
||||
* All values of form
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $values = [];
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
// 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);
|
||||
// lowercase all value names
|
||||
$values = array_change_key_case($values);
|
||||
$input = array_change_key_case($input);
|
||||
|
||||
unset($inject['fields'], $inject['values'], $inject['input']);
|
||||
unset($inject['fields'], $inject['values'], $inject['input']);
|
||||
|
||||
$this->fields = new Fields();
|
||||
$this->values = [];
|
||||
$this->fields = new Fields();
|
||||
$this->values = [];
|
||||
|
||||
foreach ($fields as $name => $props) {
|
||||
foreach ($fields as $name => $props) {
|
||||
// inject stuff from the form constructor (model, etc.)
|
||||
$props = array_merge($inject, $props);
|
||||
|
||||
// inject stuff from the form constructor (model, etc.)
|
||||
$props = array_merge($inject, $props);
|
||||
// inject the name
|
||||
$props['name'] = $name = strtolower($name);
|
||||
|
||||
// inject the name
|
||||
$props['name'] = $name = strtolower($name);
|
||||
// check if the field is disabled
|
||||
$disabled = $props['disabled'] ?? false;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if ($field->save() !== false) {
|
||||
$this->values[$name] = $field->value();
|
||||
}
|
||||
$this->fields->append($name, $field);
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
if ($strict !== true) {
|
||||
foreach ($input as $key => $value) {
|
||||
if (isset($this->values[$key]) === false) {
|
||||
$this->values[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// use all given values, no matter
|
||||
// if there's a field or not.
|
||||
$input = array_merge($values, $input);
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
foreach ($input as $key => $value) {
|
||||
if (isset($this->values[$key]) === false) {
|
||||
$this->values[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
return $data;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* An array of all found errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors(): array
|
||||
{
|
||||
if ($this->errors !== null) {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
$this->errors = [];
|
||||
|
||||
/**
|
||||
* An array of all found errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors(): array
|
||||
{
|
||||
if ($this->errors !== null) {
|
||||
return $this->errors;
|
||||
}
|
||||
foreach ($this->fields as $field) {
|
||||
if (empty($field->errors()) === false) {
|
||||
$this->errors[$field->name()] = [
|
||||
'label' => $field->label(),
|
||||
'message' => $field->errors()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$this->errors = [];
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
foreach ($this->fields as $field) {
|
||||
if (empty($field->errors()) === false) {
|
||||
$this->errors[$field->name()] = [
|
||||
'label' => $field->label(),
|
||||
'message' => $field->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();
|
||||
|
||||
return $this->errors;
|
||||
}
|
||||
if (App::instance()->option('debug') === true) {
|
||||
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
$props = array_merge($props, [
|
||||
'label' => 'Error in "' . $props['name'] . '" field.',
|
||||
'theme' => 'negative',
|
||||
'text' => strip_tags($message),
|
||||
]);
|
||||
|
||||
if (App::instance()->option('debug') === true) {
|
||||
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
|
||||
}
|
||||
return Field::factory('info', $props);
|
||||
}
|
||||
|
||||
$props = array_merge($props, [
|
||||
'label' => 'Error in "' . $props['name'] . '" field.',
|
||||
'theme' => 'negative',
|
||||
'text' => strip_tags($message),
|
||||
]);
|
||||
/**
|
||||
* 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;
|
||||
|
||||
return Field::factory('info', $props);
|
||||
}
|
||||
foreach ($fieldNames as $fieldName) {
|
||||
$index++;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
if ($field = $form->fields()->get($fieldName)) {
|
||||
if ($count !== $index) {
|
||||
$form = $field->form();
|
||||
}
|
||||
} else {
|
||||
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($fieldNames as $fieldName) {
|
||||
$index++;
|
||||
// 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');
|
||||
}
|
||||
|
||||
if ($field = $form->fields()->get($fieldName)) {
|
||||
if ($count !== $index) {
|
||||
$form = $field->form();
|
||||
}
|
||||
} else {
|
||||
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
|
||||
}
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
|
||||
// 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');
|
||||
}
|
||||
/**
|
||||
* Returns form fields
|
||||
*
|
||||
* @return \Kirby\Form\Fields|null
|
||||
*/
|
||||
public function fields()
|
||||
{
|
||||
return $this->fields;
|
||||
}
|
||||
|
||||
return $field;
|
||||
}
|
||||
/**
|
||||
* @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'] ?? [];
|
||||
|
||||
/**
|
||||
* Returns form fields
|
||||
*
|
||||
* @return \Kirby\Form\Fields|null
|
||||
*/
|
||||
public function fields()
|
||||
{
|
||||
return $this->fields;
|
||||
}
|
||||
// convert closures to values
|
||||
foreach ($values as $key => $value) {
|
||||
if (is_a($value, 'Closure') === true) {
|
||||
$values[$key] = $value($original[$key] ?? null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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'] ?? [];
|
||||
// set a few defaults
|
||||
$props['values'] = array_merge($original, $values);
|
||||
$props['fields'] ??= [];
|
||||
$props['model'] = $model;
|
||||
|
||||
// convert closures to values
|
||||
foreach ($values as $key => $value) {
|
||||
if (is_a($value, 'Closure') === true) {
|
||||
$values[$key] = $value($original[$key] ?? null);
|
||||
}
|
||||
}
|
||||
// search for the blueprint
|
||||
if (method_exists($model, 'blueprint') === true && $blueprint = $model->blueprint()) {
|
||||
$props['fields'] = $blueprint->fields();
|
||||
}
|
||||
|
||||
// set a few defaults
|
||||
$props['values'] = array_merge($original, $values);
|
||||
$props['fields'] ??= [];
|
||||
$props['model'] = $model;
|
||||
$ignoreDisabled = $props['ignoreDisabled'] ?? false;
|
||||
|
||||
// search for the blueprint
|
||||
if (method_exists($model, 'blueprint') === true && $blueprint = $model->blueprint()) {
|
||||
$props['fields'] = $blueprint->fields();
|
||||
}
|
||||
// REFACTOR: this could be more elegant
|
||||
if ($ignoreDisabled === true) {
|
||||
$props['fields'] = array_map(function ($field) {
|
||||
$field['disabled'] = false;
|
||||
return $field;
|
||||
}, $props['fields']);
|
||||
}
|
||||
|
||||
$ignoreDisabled = $props['ignoreDisabled'] ?? false;
|
||||
return new static($props);
|
||||
}
|
||||
|
||||
// REFACTOR: this could be more elegant
|
||||
if ($ignoreDisabled === true) {
|
||||
$props['fields'] = array_map(function ($field) {
|
||||
$field['disabled'] = false;
|
||||
return $field;
|
||||
}, $props['fields']);
|
||||
}
|
||||
/**
|
||||
* Checks if the form is invalid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isInvalid(): bool
|
||||
{
|
||||
return empty($this->errors()) === false;
|
||||
}
|
||||
|
||||
return new static($props);
|
||||
}
|
||||
/**
|
||||
* Checks if the form is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return empty($this->errors()) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the form is invalid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isInvalid(): bool
|
||||
{
|
||||
return empty($this->errors()) === false;
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Checks if the form is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return empty($this->errors()) === true;
|
||||
}
|
||||
// only modify the fields if we have a valid Kirby multilang instance
|
||||
if (!$kirby || $kirby->multilang() === false) {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
if ($language === null) {
|
||||
$language = $kirby->language()->code();
|
||||
}
|
||||
|
||||
// only modify the fields if we have a valid Kirby multilang instance
|
||||
if (!$kirby || $kirby->multilang() === false) {
|
||||
return $fields;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($language === null) {
|
||||
$language = $kirby->language()->code();
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Converts the data of fields to strings
|
||||
*
|
||||
* @param false $defaults
|
||||
* @return array
|
||||
*/
|
||||
public function strings($defaults = false): array
|
||||
{
|
||||
$strings = [];
|
||||
|
||||
return $fields;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the data of fields to strings
|
||||
*
|
||||
* @param false $defaults
|
||||
* @return array
|
||||
*/
|
||||
public function strings($defaults = false): array
|
||||
{
|
||||
$strings = [];
|
||||
return $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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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 $strings;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* Returns form values
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function values(): array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@ namespace Kirby\Form\Mixin;
|
|||
|
||||
trait EmptyState
|
||||
{
|
||||
protected $empty;
|
||||
protected $empty;
|
||||
|
||||
protected function setEmpty($empty = null)
|
||||
{
|
||||
$this->empty = $this->i18n($empty);
|
||||
}
|
||||
protected function setEmpty($empty = null)
|
||||
{
|
||||
$this->empty = $this->i18n($empty);
|
||||
}
|
||||
|
||||
public function empty(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->empty);
|
||||
}
|
||||
public function empty(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->empty);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@ namespace Kirby\Form\Mixin;
|
|||
|
||||
trait Max
|
||||
{
|
||||
protected $max;
|
||||
protected $max;
|
||||
|
||||
public function max(): ?int
|
||||
{
|
||||
return $this->max;
|
||||
}
|
||||
public function max(): ?int
|
||||
{
|
||||
return $this->max;
|
||||
}
|
||||
|
||||
protected function setMax(int $max = null)
|
||||
{
|
||||
$this->max = $max;
|
||||
}
|
||||
protected function setMax(int $max = null)
|
||||
{
|
||||
$this->max = $max;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@ namespace Kirby\Form\Mixin;
|
|||
|
||||
trait Min
|
||||
{
|
||||
protected $min;
|
||||
protected $min;
|
||||
|
||||
public function min(): ?int
|
||||
{
|
||||
return $this->min;
|
||||
}
|
||||
public function min(): ?int
|
||||
{
|
||||
return $this->min;
|
||||
}
|
||||
|
||||
protected function setMin(int $min = null)
|
||||
{
|
||||
$this->min = $min;
|
||||
}
|
||||
protected function setMin(int $min = null)
|
||||
{
|
||||
$this->min = $min;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,189 +19,192 @@ use Kirby\Toolkit\I18n;
|
|||
*/
|
||||
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',
|
||||
];
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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
|
||||
]);
|
||||
$optionsApi = new OptionsApi([
|
||||
'data' => static::data($model),
|
||||
'fetch' => $fetch,
|
||||
'url' => $url,
|
||||
'text' => $text,
|
||||
'value' => $value
|
||||
]);
|
||||
|
||||
return $optionsApi->options();
|
||||
}
|
||||
return $optionsApi->options();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Kirby\Cms\Model $model
|
||||
* @return array
|
||||
*/
|
||||
protected static function data($model): array
|
||||
{
|
||||
$kirby = $model->kirby();
|
||||
/**
|
||||
* @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(),
|
||||
];
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
// add the model by the proper alias
|
||||
foreach (static::aliases() as $className => $alias) {
|
||||
if (is_a($model, $className) === true) {
|
||||
$data[$alias] = $model;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* 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 [];
|
||||
}
|
||||
if (is_array($options) === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
$result = [];
|
||||
|
||||
foreach ($options as $key => $option) {
|
||||
if (is_array($option) === false || isset($option['value']) === false) {
|
||||
$option = [
|
||||
'value' => is_int($key) ? $option : $key,
|
||||
'text' => $option
|
||||
];
|
||||
}
|
||||
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']);
|
||||
}
|
||||
// fallback for the text
|
||||
$option['text'] ??= $option['value'];
|
||||
|
||||
// add the option to the list
|
||||
$result[] = $option;
|
||||
}
|
||||
// translate the option text
|
||||
if (is_array($option['text']) === true) {
|
||||
$option['text'] = I18n::translate($option['text'], $option['text']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
// add the option to the list
|
||||
$result[] = $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
return $result;
|
||||
}
|
||||
|
||||
// default text setup
|
||||
$text = [
|
||||
'arrayItem' => '{{ arrayItem.value }}',
|
||||
'block' => '{{ block.type }}: {{ block.id }}',
|
||||
'file' => '{{ file.filename }}',
|
||||
'page' => '{{ page.title }}',
|
||||
'structureItem' => '{{ structureItem.title }}',
|
||||
'user' => '{{ user.username }}',
|
||||
];
|
||||
/**
|
||||
* 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 value setup
|
||||
$value = [
|
||||
'arrayItem' => '{{ arrayItem.value }}',
|
||||
'block' => '{{ block.id }}',
|
||||
'file' => '{{ file.id }}',
|
||||
'page' => '{{ page.id }}',
|
||||
'structureItem' => '{{ structureItem.id }}',
|
||||
'user' => '{{ user.email }}',
|
||||
];
|
||||
// default text setup
|
||||
$text = [
|
||||
'arrayItem' => '{{ arrayItem.value }}',
|
||||
'block' => '{{ block.type }}: {{ block.id }}',
|
||||
'file' => '{{ file.filename }}',
|
||||
'page' => '{{ page.title }}',
|
||||
'structureItem' => '{{ structureItem.title }}',
|
||||
'user' => '{{ user.username }}',
|
||||
];
|
||||
|
||||
// resolve array query setup
|
||||
if (is_array($query) === true) {
|
||||
$text = $query['text'] ?? $text;
|
||||
$value = $query['value'] ?? $value;
|
||||
$query = $query['fetch'] ?? null;
|
||||
}
|
||||
// default value setup
|
||||
$value = [
|
||||
'arrayItem' => '{{ arrayItem.value }}',
|
||||
'block' => '{{ block.id }}',
|
||||
'file' => '{{ file.id }}',
|
||||
'page' => '{{ page.id }}',
|
||||
'structureItem' => '{{ structureItem.id }}',
|
||||
'user' => '{{ user.email }}',
|
||||
];
|
||||
|
||||
$optionsQuery = new OptionsQuery([
|
||||
'aliases' => static::aliases(),
|
||||
'data' => static::data($model),
|
||||
'query' => $query,
|
||||
'text' => $text,
|
||||
'value' => $value
|
||||
]);
|
||||
// resolve array query setup
|
||||
if (is_array($query) === true) {
|
||||
$text = $query['text'] ?? $text;
|
||||
$value = $query['value'] ?? $value;
|
||||
$query = $query['fetch'] ?? null;
|
||||
}
|
||||
|
||||
return $optionsQuery->options();
|
||||
}
|
||||
$optionsQuery = new OptionsQuery([
|
||||
'aliases' => static::aliases(),
|
||||
'data' => static::data($model),
|
||||
'query' => $query,
|
||||
'text' => $text,
|
||||
'value' => $value
|
||||
]);
|
||||
|
||||
return $optionsQuery->options();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,220 +23,220 @@ use Kirby\Toolkit\Str;
|
|||
*/
|
||||
class OptionsApi
|
||||
{
|
||||
use Properties;
|
||||
use Properties;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $fetch;
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $fetch;
|
||||
|
||||
/**
|
||||
* @var array|string|null
|
||||
*/
|
||||
protected $options;
|
||||
/**
|
||||
* @var array|string|null
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $text = '{{ item.value }}';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $text = '{{ item.value }}';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $value = '{{ item.key }}';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $value = '{{ item.key }}';
|
||||
|
||||
/**
|
||||
* OptionsApi constructor
|
||||
*
|
||||
* @param array $props
|
||||
*/
|
||||
public function __construct(array $props)
|
||||
{
|
||||
$this->setProperties($props);
|
||||
}
|
||||
/**
|
||||
* OptionsApi constructor
|
||||
*
|
||||
* @param array $props
|
||||
*/
|
||||
public function __construct(array $props)
|
||||
{
|
||||
$this->setProperties($props);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
return $this->fetch;
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* @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
|
||||
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');
|
||||
}
|
||||
// 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());
|
||||
$content = @file_get_contents($this->url());
|
||||
|
||||
if (is_string($content) !== true) {
|
||||
throw new Exception('Unexpected read error'); // @codeCoverageIgnore
|
||||
}
|
||||
if (is_string($content) !== true) {
|
||||
throw new Exception('Unexpected read error'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (empty($content) === true) {
|
||||
return [];
|
||||
}
|
||||
if (empty($content) === true) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode($content, true);
|
||||
}
|
||||
$data = json_decode($content, true);
|
||||
}
|
||||
|
||||
if (is_array($data) === false) {
|
||||
throw new InvalidArgumentException('Invalid options format');
|
||||
}
|
||||
if (is_array($data) === false) {
|
||||
throw new InvalidArgumentException('Invalid options format');
|
||||
}
|
||||
|
||||
$result = (new Query($this->fetch(), Nest::create($data)))->result();
|
||||
$options = [];
|
||||
$result = (new Query($this->fetch(), Nest::create($data)))->result();
|
||||
$options = [];
|
||||
|
||||
foreach ($result as $item) {
|
||||
$data = array_merge($this->data(), ['item' => $item]);
|
||||
foreach ($result as $item) {
|
||||
$data = array_merge($this->data(), ['item' => $item]);
|
||||
|
||||
$options[] = [
|
||||
'text' => $this->field('text', $data),
|
||||
'value' => $this->field('value', $data),
|
||||
];
|
||||
}
|
||||
$options[] = [
|
||||
'text' => $this->field('text', $data),
|
||||
'value' => $this->field('value', $data),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return $this
|
||||
*/
|
||||
protected function setData(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @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 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 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 $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 $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;
|
||||
}
|
||||
/**
|
||||
* @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 string
|
||||
*/
|
||||
public function text(): string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->options();
|
||||
}
|
||||
/**
|
||||
* @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 url(): string
|
||||
{
|
||||
return Str::template($this->url, $this->data());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function value(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function value(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,247 +25,247 @@ use Kirby\Toolkit\Str;
|
|||
*/
|
||||
class OptionsQuery
|
||||
{
|
||||
use Properties;
|
||||
use Properties;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $aliases = [];
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $aliases = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var array|string|null
|
||||
*/
|
||||
protected $options;
|
||||
/**
|
||||
* @var array|string|null
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $query;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $text;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $text;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $value;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* OptionsQuery constructor
|
||||
*
|
||||
* @param array $props
|
||||
*/
|
||||
public function __construct(array $props)
|
||||
{
|
||||
$this->setProperties($props);
|
||||
}
|
||||
/**
|
||||
* 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 aliases(): array
|
||||
{
|
||||
return $this->aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
/**
|
||||
* @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();
|
||||
/**
|
||||
* @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');
|
||||
}
|
||||
if (is_array($value) === true) {
|
||||
if (isset($value[$object]) === false) {
|
||||
throw new NotFoundException('Missing "' . $field . '" definition');
|
||||
}
|
||||
|
||||
$value = $value[$object];
|
||||
}
|
||||
$value = $value[$object];
|
||||
}
|
||||
|
||||
return Str::safeTemplate($value, $data);
|
||||
}
|
||||
return Str::safeTemplate($value, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function options(): array
|
||||
{
|
||||
if (is_array($this->options) === true) {
|
||||
return $this->options;
|
||||
}
|
||||
/**
|
||||
* @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 = [];
|
||||
$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]);
|
||||
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)
|
||||
];
|
||||
}
|
||||
$options[] = [
|
||||
'text' => $this->template($alias, 'text', $data),
|
||||
'value' => $this->template($alias, 'value', $data)
|
||||
];
|
||||
}
|
||||
|
||||
return $this->options = $options;
|
||||
}
|
||||
return $this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function query(): string
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
// slow but precise resolving
|
||||
foreach ($this->aliases as $className => $alias) {
|
||||
if (is_a($object, $className) === true) {
|
||||
return $alias;
|
||||
}
|
||||
}
|
||||
|
||||
return 'item';
|
||||
}
|
||||
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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
$result = new Collection($result);
|
||||
}
|
||||
|
||||
if (is_a($result, 'Kirby\Toolkit\Collection') === false) {
|
||||
throw new InvalidArgumentException('Invalid query result data');
|
||||
}
|
||||
if (is_a($result, 'Kirby\Toolkit\Collection') === false) {
|
||||
throw new InvalidArgumentException('Invalid query result data');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $aliases
|
||||
* @return $this
|
||||
*/
|
||||
protected function setAliases(?array $aliases = null)
|
||||
{
|
||||
$this->aliases = $aliases;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @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 $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 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 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 $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;
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
protected function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function text()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function text()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->options();
|
||||
}
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->options();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function value()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function value()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,279 +16,279 @@ use Kirby\Toolkit\V;
|
|||
*/
|
||||
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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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())
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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())
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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())
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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())
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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')
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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'
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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');
|
||||
/**
|
||||
* 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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (in_array($value, $values, true) !== true) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'validation.option'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue