julienmonnerie/kirby/src/Form/Options.php

206 lines
4.5 KiB
PHP
Raw Normal View History

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