|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Panel\Controller\Dialog; |
| 4 | + |
| 5 | +use Kirby\Cms\Blueprint; |
| 6 | +use Kirby\Cms\ModelWithContent; |
| 7 | +use Kirby\Cms\Page; |
| 8 | +use Kirby\Cms\Section; |
| 9 | +use Kirby\Cms\Site; |
| 10 | +use Kirby\Cms\User; |
| 11 | +use Kirby\Exception\InvalidArgumentException; |
| 12 | +use Kirby\Form\Form; |
| 13 | +use Kirby\Panel\Controller\DialogController; |
| 14 | + |
| 15 | +/** |
| 16 | + * Controls a Panel dialog to create a new model |
| 17 | + * |
| 18 | + * @package Kirby Panel |
| 19 | + * @author Bastian Allgeier <[email protected]> |
| 20 | + * @link https://getkirby.com |
| 21 | + * @copyright Bastian Allgeier |
| 22 | + * @license https://getkirby.com/license |
| 23 | + * @since 6.0.0 |
| 24 | + */ |
| 25 | +abstract class ModelCreateDialogController extends DialogController |
| 26 | +{ |
| 27 | + public static array $fieldTypes = []; |
| 28 | + |
| 29 | + protected Blueprint $blueprint; |
| 30 | + public ModelWithContent $model; |
| 31 | + public Page|Site|User $parent; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + Page|Site|User|null $parent = null, |
| 35 | + public Section|null $section = null |
| 36 | + ) { |
| 37 | + parent::__construct(); |
| 38 | + |
| 39 | + $this->parent = $parent ?? $this->site; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Get the blueprint for the new page |
| 44 | + */ |
| 45 | + public function blueprint(): Blueprint |
| 46 | + { |
| 47 | + return $this->blueprint ??= $this->model()->blueprint(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * All the default fields for the dialog |
| 52 | + */ |
| 53 | + abstract public function coreFields(): array; |
| 54 | + |
| 55 | + /** |
| 56 | + * Loads custom fields for the page type |
| 57 | + */ |
| 58 | + public function customFields(): array |
| 59 | + { |
| 60 | + $custom = []; |
| 61 | + $fields = $this->blueprint()->fields(); |
| 62 | + $ignore = $this->customFieldsIgnore(); |
| 63 | + |
| 64 | + foreach ($this->blueprint()->create()['fields'] ?? [] as $name) { |
| 65 | + $field = $fields[$name] ?? null; |
| 66 | + |
| 67 | + if ($field === null) { |
| 68 | + throw new InvalidArgumentException( |
| 69 | + message: 'Unknown field "' . $name . '" in create dialog' |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (in_array($field['type'], static::$fieldTypes, true) === false) { |
| 74 | + throw new InvalidArgumentException( |
| 75 | + message: 'Field type "' . $field['type'] . '" not supported in create dialog' |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + if (in_array($name, $ignore, true) === true) { |
| 80 | + throw new InvalidArgumentException( |
| 81 | + message: 'Field name "' . $name . '" not allowed as custom field in create dialog' |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // switch all fields to 1/1 |
| 86 | + $field['width'] = '1/1'; |
| 87 | + |
| 88 | + // add the field to the form |
| 89 | + $custom[$name] = $field; |
| 90 | + } |
| 91 | + |
| 92 | + // create form so that field props, options etc. |
| 93 | + // can be properly resolved |
| 94 | + $form = new Form( |
| 95 | + fields: $custom, |
| 96 | + model: $this->model() |
| 97 | + ); |
| 98 | + |
| 99 | + return $form->fields()->toProps(); |
| 100 | + } |
| 101 | + |
| 102 | + protected function customFieldsIgnore(): array |
| 103 | + { |
| 104 | + return array_keys($this->coreFields()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Loads all the fields for the dialog |
| 109 | + */ |
| 110 | + public function fields(): array |
| 111 | + { |
| 112 | + return [ |
| 113 | + ...$this->coreFields(), |
| 114 | + ...$this->customFields() |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Temporary model to be created, |
| 120 | + * used to properly render the blueprint for fields |
| 121 | + */ |
| 122 | + abstract public function model(): ModelWithContent; |
| 123 | + |
| 124 | + /** |
| 125 | + * Generates values for title and slug |
| 126 | + * from template strings from the blueprint |
| 127 | + */ |
| 128 | + public function resolveFieldTemplates(array $input, array $fields): array |
| 129 | + { |
| 130 | + // create temporary page object to resolve the template strings |
| 131 | + $page = $this->model()->clone(['content' => $input]); |
| 132 | + |
| 133 | + foreach ($fields as $field) { |
| 134 | + $template = $this->blueprint()->create()[$field] ?? null; |
| 135 | + |
| 136 | + if (is_string($template) === true) { |
| 137 | + $input[$field] = $page->toSafeString($template); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return $input; |
| 142 | + } |
| 143 | + |
| 144 | + public function value(): array |
| 145 | + { |
| 146 | + $value = []; |
| 147 | + |
| 148 | + // add default values for custom fields |
| 149 | + foreach ($this->customFields() as $name => $field) { |
| 150 | + if ($default = $field['default'] ?? null) { |
| 151 | + $value[$name] = $default; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return $value; |
| 156 | + } |
| 157 | +} |
0 commit comments