|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Blueprint; |
| 4 | + |
| 5 | +use Kirby\Cms\ModelWithContent; |
| 6 | +use Kirby\Form\Fields; |
| 7 | +use Kirby\Toolkit\I18n; |
| 8 | +use Kirby\Toolkit\Str; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents a single tab of a blueprint. Tabs are |
| 12 | + * the top level of the blueprint layout. They hold |
| 13 | + * columns, which hold fields. |
| 14 | + * |
| 15 | + * @copyright Bastian Allgeier |
| 16 | + * @license https://getkirby.com/license |
| 17 | + * @since 6.0.0 |
| 18 | + */ |
| 19 | +class Tab |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + protected ModelWithContent $model, |
| 23 | + protected string $name, |
| 24 | + protected array $columns = [], |
| 25 | + protected string|null $icon = null, |
| 26 | + protected array|string|null $label = null, |
| 27 | + protected array $props = [] |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns all normalized columns of the tab. |
| 33 | + * |
| 34 | + * If a `Fields` collection is passed, the blueprint field |
| 35 | + * definitions of each column are replaced by the resolved |
| 36 | + * field props for the Panel. |
| 37 | + */ |
| 38 | + public function columns(Fields|null $fields = null): array |
| 39 | + { |
| 40 | + if ($fields === null) { |
| 41 | + return $this->columns; |
| 42 | + } |
| 43 | + |
| 44 | + $columns = $this->columns; |
| 45 | + |
| 46 | + foreach ($columns as $key => $column) { |
| 47 | + $names = array_map( |
| 48 | + 'strtolower', |
| 49 | + array_keys($column['fields'] ?? []) |
| 50 | + ); |
| 51 | + |
| 52 | + // only resolve the props of the fields in this tab, |
| 53 | + // as that can be expensive for list fields |
| 54 | + $columns[$key]['fields'] = $fields |
| 55 | + ->filter(fn ($field) => in_array($field->name(), $names, true)) |
| 56 | + ->toProps(); |
| 57 | + } |
| 58 | + |
| 59 | + return $columns; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns a flat list of the lowercase names of all |
| 64 | + * fields in the tab. The Panel uses this list to count |
| 65 | + * the unsaved changes per tab. |
| 66 | + */ |
| 67 | + public function fieldNames(): array |
| 68 | + { |
| 69 | + $names = []; |
| 70 | + |
| 71 | + foreach ($this->columns as $column) { |
| 72 | + foreach (array_keys($column['fields'] ?? []) as $name) { |
| 73 | + $names[] = Str::lower($name); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return $names; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the icon of the tab |
| 82 | + */ |
| 83 | + public function icon(): string|null |
| 84 | + { |
| 85 | + return $this->icon; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the translated label of the tab and |
| 90 | + * falls back to an automatic label |
| 91 | + */ |
| 92 | + public function label(): string |
| 93 | + { |
| 94 | + $label = I18n::translate($this->label, $this->label); |
| 95 | + |
| 96 | + if (is_string($label) === true) { |
| 97 | + return $label; |
| 98 | + } |
| 99 | + |
| 100 | + return Str::label($this->name); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the Panel link to the tab |
| 105 | + */ |
| 106 | + public function link(): string |
| 107 | + { |
| 108 | + return $this->model->panel()->url(true) . '/?tab=' . $this->name; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the parent model |
| 113 | + */ |
| 114 | + public function model(): ModelWithContent |
| 115 | + { |
| 116 | + return $this->model; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the name of the tab |
| 121 | + */ |
| 122 | + public function name(): string |
| 123 | + { |
| 124 | + return $this->name; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the reduced props for the Panel tab bar. |
| 129 | + * The columns are not included here, as the tab bar |
| 130 | + * only needs to render a button per tab. |
| 131 | + */ |
| 132 | + public function toButtonProps(): array |
| 133 | + { |
| 134 | + return [ |
| 135 | + 'fields' => $this->fieldNames(), |
| 136 | + 'icon' => $this->icon(), |
| 137 | + 'label' => $this->label(), |
| 138 | + 'link' => $this->link(), |
| 139 | + 'name' => $this->name(), |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Converts the tab to a plain array with all |
| 145 | + * columns and fields. |
| 146 | + * |
| 147 | + * If a `Fields` collection is passed, the columns |
| 148 | + * carry the resolved field props for the Panel. |
| 149 | + */ |
| 150 | + public function toViewProps(Fields|null $fields = null): array |
| 151 | + { |
| 152 | + return [ |
| 153 | + ...$this->props, |
| 154 | + 'columns' => $this->columns($fields), |
| 155 | + 'icon' => $this->icon(), |
| 156 | + 'label' => $this->label(), |
| 157 | + 'link' => $this->link(), |
| 158 | + 'name' => $this->name(), |
| 159 | + ]; |
| 160 | + } |
| 161 | +} |
0 commit comments