first version

This commit is contained in:
Bastian Allgeier
2019-01-13 23:17:34 +01:00
commit 01277f79f2
595 changed files with 82913 additions and 0 deletions

276
kirby/src/Form/Field.php Executable file
View File

@@ -0,0 +1,276 @@
<?php
namespace Kirby\Form;
use Exception;
use Kirby\Data\Yaml;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Http\Router;
use Kirby\Toolkit\Component;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\V;
/**
* Form Field object that takes a Vue component style
* array of properties and methods and converts them
* to a usable field option array for the API.
*/
class Field extends Component
{
/**
* Registry for all component mixins
*
* @var array
*/
public static $mixins = [];
/**
* Registry for all component types
*
* @var array
*/
public static $types = [];
/**
* An array of all found errors
*
* @var array
*/
protected $errors = [];
public function __construct(string $type, array $attrs = [])
{
if (isset(static::$types[$type]) === false) {
throw new InvalidArgumentException('The field type "' . $type . '" does not exist');
}
// use the type as fallback for the name
$attrs['name'] = $attrs['name'] ?? $type;
$attrs['type'] = $type;
parent::__construct($type, $attrs);
$this->validate();
}
public function api()
{
if (isset($this->options['api']) === true && is_callable($this->options['api']) === true) {
return $this->options['api']->call($this);
}
}
public function data($default = false)
{
$save = $this->options['save'] ?? true;
if ($default === true && $this->isEmpty($this->value)) {
$value = $this->default();
} else {
$value = $this->value;
}
if ($save === false) {
return null;
} elseif (is_callable($save) === true) {
return $save->call($this, $value);
} else {
return $value;
}
}
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;
},
/**
* 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;
}
]
];
}
public function errors(): array
{
return $this->errors;
}
public function isEmpty(...$args): bool
{
if (count($args) === 0) {
$value = $this->value();
} else {
$value = $args[0];
}
if (isset($this->options['isEmpty']) === true) {
return $this->options['isEmpty']->call($this, $value);
}
return in_array($value, [null, '', []], true);
}
public function isInvalid(): bool
{
return empty($this->errors) === false;
}
public function isRequired(): bool
{
return $this->required ?? false;
}
public function isValid(): bool
{
return empty($this->errors) === true;
}
public function kirby()
{
return $this->model->kirby();
}
public function model()
{
return $this->model;
}
public function save(): bool
{
return ($this->options['save'] ?? true) !== false;
}
public function toArray(): array
{
$array = parent::toArray();
unset($array['model']);
$array['invalid'] = $this->isInvalid();
$array['errors'] = $this->errors();
$array['signature'] = md5(json_encode($array));
ksort($array);
return array_filter($array, function ($item) {
return $item !== null;
});
}
protected function validate()
{
$validations = $this->options['validations'] ?? [];
$this->errors = [];
// validate required values
if ($this->isRequired() === true && $this->save() === true && $this->isEmpty() === true) {
$this->errors['required'] = I18n::translate('error.validation.required');
}
foreach ($validations as $key => $validation) {
if (is_int($key) === true) {
// predefined validation
try {
Validations::$validation($this, $this->value());
} catch (Exception $e) {
$this->errors[$validation] = $e->getMessage();
}
continue;
}
if (is_a($validation, 'Closure') === true) {
try {
$validation->call($this, $this->value());
} catch (Exception $e) {
$this->errors[$key] = $e->getMessage();
}
}
}
if (empty($this->validate) === false) {
$errors = V::errors($this->value(), $this->validate);
if (empty($errors) === false) {
$this->errors = array_merge($this->errors, $errors);
}
}
}
public function value()
{
return $this->save() ? $this->value : null;
}
}

52
kirby/src/Form/Fields.php Executable file
View File

@@ -0,0 +1,52 @@
<?php
namespace Kirby\Form;
use Closure;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Collection;
/**
* A collection of Field objects
*/
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 $id
* @param object $object
*/
public function __set(string $name, $field)
{
if (is_array($field)) {
// use the array key as name if the name is not set
$field['name'] = $field['name'] ?? $name;
$field = new Field($field['type'], $field);
}
return parent::__set($field->name(), $field);
}
/**
* Converts the fields collection to an
* array and also does that for every
* included field.
*
* @param Closure $map
* @return array
*/
public function toArray(Closure $map = null): array
{
$array = [];
foreach ($this as $field) {
$array[$field->name()] = $field->toArray();
}
return $array;
}
}

174
kirby/src/Form/Form.php Executable file
View File

@@ -0,0 +1,174 @@
<?php
namespace Kirby\Form;
use Throwable;
use Kirby\Toolkit\Collection;
use Kirby\Data\Yaml;
/**
* The main form class, that is being
* used to create a list of form fields
* and handles global form validation
* and submission
*/
class Form
{
protected $errors;
protected $fields;
protected $values = [];
public function __construct(array $props)
{
$fields = $props['fields'] ?? [];
$values = $props['values'] ?? [];
$input = $props['input'] ?? [];
$strict = $props['strict'] ?? false;
$inject = $props;
// lowercase all value names
$values = array_change_key_case($values);
$input = array_change_key_case($input);
unset($inject['fields'], $inject['values'], $inject['input']);
$this->fields = new Fields;
$this->values = [];
foreach ($fields as $name => $props) {
// inject stuff from the form constructor (model, etc.)
$props = array_merge($inject, $props);
// inject the name
$props['name'] = $name = strtolower($name);
// check if the field is disabled
$disabled = $props['disabled'] ?? false;
// overwrite the field value if not set
if ($disabled === true) {
$props['value'] = $values[$name] ?? null;
} else {
$props['value'] = $input[$name] ?? $values[$name] ?? null;
}
try {
$field = new Field($props['type'], $props);
} catch (Throwable $e) {
$props = array_merge($props, [
'name' => $props['name'],
'label' => 'Error in "' . $props['name'] . '" field',
'theme' => 'negative',
'text' => $e->getMessage(),
]);
$field = new Field('info', $props);
}
if ($field->save() !== false) {
$this->values[$name] = $field->value();
}
$this->fields->append($name, $field);
}
if ($strict !== true) {
// use all given values, no matter
// if there's a field or not.
$input = array_merge($values, $input);
foreach ($input as $key => $value) {
if (isset($this->values[$key]) === false) {
$this->values[$key] = $value;
}
}
}
}
public function data($defaults = false): array
{
$data = $this->values;
foreach ($this->fields as $field) {
if ($field->save() === false || $field->unset() === true) {
$data[$field->name()] = null;
} else {
$data[$field->name()] = $field->data($defaults);
}
}
return $data;
}
public function errors(): array
{
if ($this->errors !== null) {
return $this->errors;
}
$this->errors = [];
foreach ($this->fields as $field) {
if (empty($field->errors()) === false) {
$this->errors[$field->name()] = [
'label' => $field->label(),
'message' => $field->errors()
];
}
}
return $this->errors;
}
public function fields()
{
return $this->fields;
}
public function isInvalid(): bool
{
return empty($this->errors()) === false;
}
public function isValid(): bool
{
return empty($this->errors()) === true;
}
public function strings($defaults = false): array
{
$strings = [];
foreach ($this->data($defaults) as $key => $value) {
if ($value === null) {
$strings[$key] = null;
} elseif (is_array($value) === true) {
$strings[$key] = Yaml::encode($value);
} else {
$strings[$key] = (string)$value;
}
}
return $strings;
}
public function toArray()
{
$array = [
'errors' => $this->errors(),
'fields' => $this->fields->toArray(function ($item) {
return $item->toArray();
}),
'invalid' => $this->isInvalid()
];
return $array;
}
public function values(): array
{
return $this->values;
}
}

172
kirby/src/Form/Options.php Executable file
View File

@@ -0,0 +1,172 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Cms\StructureObject;
use Kirby\Cms\User;
use Kirby\Toolkit\A;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Obj;
/**
* Foundation for the Options query
* classes, that are used to generate
* options arrays for select fiels,
* radio boxes, checkboxes and more.
*/
class Options
{
protected static function aliases(): array
{
return [
'Kirby\Cms\File' => 'file',
'Kirby\Toolkit\Obj' => 'arrayItem',
'Kirby\Cms\Page' => 'page',
'Kirby\Cms\StructureObject' => 'structureItem',
'Kirby\Cms\User' => 'user',
];
}
public static function api($api, $model = null): array
{
$model = $model ?? App::instance()->site();
$fetch = null;
$text = null;
$value = null;
if (is_array($api) === true) {
$fetch = $api['fetch'] ?? null;
$text = $api['text'] ?? null;
$value = $api['value'] ?? null;
$url = $api['url'] ?? null;
} else {
$url = $api;
}
$optionsApi = new OptionsApi([
'data' => static::data($model),
'fetch' => $fetch,
'url' => $url,
'text' => $text,
'value' => $value
]);
return $optionsApi->options();
}
protected static function data($model): array
{
$kirby = $model->kirby();
// default data setup
$data = [
'kirby' => $kirby,
'site' => $kirby->site(),
'users' => $kirby->users(),
];
// add the model by the proper alias
foreach (static::aliases() as $className => $alias) {
if (is_a($model, $className) === true) {
$data[$alias] = $model;
}
}
return $data;
}
public static function factory($options, array $props = [], $model = null): array
{
switch ($options) {
case 'api':
$options = static::api($props['api']);
break;
case 'query':
$options = static::query($props['query'], $model);
break;
case 'children':
case 'grandChildren':
case 'siblings':
case 'index':
case 'files':
case 'images':
case 'documents':
case 'videos':
case 'audio':
case 'code':
case 'archives':
$options = static::query('page.' . $options, $model);
break;
case 'pages':
$options = static::query('site.index', $model);
break;
}
if (is_array($options) === false) {
return [];
}
$result = [];
foreach ($options as $key => $option) {
if (is_array($option) === false || isset($option['value']) === false) {
$option = [
'value' => is_int($key) ? $option : $key,
'text' => $option
];
}
// translate the option text
$option['text'] = I18n::translate($option['text'], $option['text']);
// add the option to the list
$result[] = $option;
}
return $result;
}
public static function query($query, $model = null): array
{
$model = $model ?? App::instance()->site();
// default text setup
$text = [
'arrayItem' => '{{ arrayItem.value }}',
'file' => '{{ file.filename }}',
'page' => '{{ page.title }}',
'structureItem' => '{{ structureItem.title }}',
'user' => '{{ user.username }}',
];
// default value setup
$value = [
'arrayItem' => '{{ arrayItem.value }}',
'file' => '{{ file.id }}',
'page' => '{{ page.id }}',
'structureItem' => '{{ structureItem.id }}',
'user' => '{{ user.email }}',
];
// resolve array query setup
if (is_array($query) === true) {
$text = $query['text'] ?? $text;
$value = $query['value'] ?? $value;
$query = $query['fetch'] ?? null;
}
$optionsQuery = new OptionsQuery([
'aliases' => static::aliases(),
'data' => static::data($model),
'query' => $query,
'text' => $text,
'value' => $value
]);
return $optionsQuery->options();
}
}

131
kirby/src/Form/OptionsApi.php Executable file
View File

@@ -0,0 +1,131 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\Nest;
use Kirby\Cms\Structure;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Toolkit\Query;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Str;
/**
* The OptionsApi class handles fetching options
* from any REST API with valid JSON data.
*/
class OptionsApi
{
use Properties;
protected $data;
protected $fetch;
protected $options;
protected $text = '{{ item.value }}';
protected $url;
protected $value = '{{ item.key }}';
public function __construct(array $props)
{
$this->setProperties($props);
}
public function data(): array
{
return $this->data;
}
public function fetch()
{
return $this->fetch;
}
protected function field(string $field, array $data)
{
$value = $this->$field();
return Str::template($value, $data);
}
public function options(): array
{
if (is_array($this->options) === true) {
return $this->options;
}
$content = @file_get_contents($this->url());
if (empty($content) === true) {
return [];
}
$data = json_decode($content, true);
if (is_array($data) === false) {
throw new InvalidArgumentException('Invalid options format');
}
$result = (new Query($this->fetch(), Nest::create($data)))->result();
$options = [];
foreach ($result as $item) {
$data = array_merge($this->data(), ['item' => $item]);
$options[] = [
'text' => $this->field('text', $data),
'value' => $this->field('value', $data),
];
}
return $options;
}
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
protected function setFetch(string $fetch = null)
{
$this->fetch = $fetch;
return $this;
}
protected function setText($text = null)
{
$this->text = $text;
return $this;
}
protected function setUrl($url)
{
$this->url = $url;
return $this;
}
protected function setValue($value = null)
{
$this->value = $value;
return $this;
}
public function text()
{
return $this->text;
}
public function toArray(): array
{
return $this->options();
}
public function url(): string
{
return Str::template($this->url, $this->data());
}
public function value()
{
return $this->value;
}
}

174
kirby/src/Form/OptionsQuery.php Executable file
View File

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

171
kirby/src/Form/Validations.php Executable file
View File

@@ -0,0 +1,171 @@
<?php
namespace Kirby\Form;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\V;
/**
* Often used validation rules for fields
*/
class Validations
{
public static function boolean(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (is_bool($value) === false) {
throw new InvalidArgumentException([
'key' => 'validation.boolean'
]);
}
}
return true;
}
public static function date(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::date($value) !== true) {
throw new InvalidArgumentException(
V::message('date', $value)
);
}
}
return true;
}
public static function email(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::email($value) === false) {
throw new InvalidArgumentException(
V::message('email', $value)
);
}
}
return true;
}
public static function max(Field $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;
}
public static function maxlength(Field $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;
}
public static function min(Field $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;
}
public static function minlength(Field $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;
}
public static function required(Field $field, $value): bool
{
if ($field->isRequired() === true && $field->save() === true && $field->isEmpty($value) === true) {
throw new InvalidArgumentException([
'key' => 'validation.required'
]);
}
return true;
}
public static function option(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
if (in_array($value, $values, true) !== true) {
throw new InvalidArgumentException([
'key' => 'validation.option'
]);
}
}
return true;
}
public static function options(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
foreach ($value as $key => $val) {
if (in_array($val, $values, true) === false) {
throw new InvalidArgumentException([
'key' => 'validation.option'
]);
}
}
}
return true;
}
public static function time(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::time($value) !== true) {
throw new InvalidArgumentException(
V::message('time', $value)
);
}
}
return true;
}
public static function url(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::url($value) === false) {
throw new InvalidArgumentException(
V::message('url', $value)
);
}
}
return true;
}
}