-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathFieldset.php
More file actions
321 lines (256 loc) · 8.03 KB
/
Fieldset.php
File metadata and controls
321 lines (256 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace Statamic\Fields;
use Statamic\Contracts\Query\ContainsQueryableValues;
use Statamic\Events\FieldsetCreated;
use Statamic\Events\FieldsetCreating;
use Statamic\Events\FieldsetDeleted;
use Statamic\Events\FieldsetDeleting;
use Statamic\Events\FieldsetReset;
use Statamic\Events\FieldsetSaved;
use Statamic\Events\FieldsetSaving;
use Statamic\Exceptions\FieldsetRecursionException;
use Statamic\Facades;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Collection;
use Statamic\Facades\Fieldset as FieldsetRepository;
use Statamic\Facades\File;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Path;
use Statamic\Facades\Taxonomy;
use Statamic\Support\Arr;
use Statamic\Support\Str;
class Fieldset implements ContainsQueryableValues
{
protected $handle;
protected $contents = [];
protected $afterSaveCallbacks = [];
protected $withEvents = true;
protected $initialPath;
public function setHandle(string $handle)
{
$this->handle = $handle;
return $this;
}
public function handle(): ?string
{
return $this->handle;
}
public function path()
{
return Path::tidy(vsprintf('%s/%s.yaml', [
Facades\Fieldset::directory(),
str_replace('.', '/', $this->handle()),
]));
}
public function initialPath($path = null)
{
if (func_num_args() === 0) {
return $this->initialPath;
}
$this->initialPath = $path;
return $this;
}
public function setContents(array $contents)
{
$fields = Arr::get($contents, 'fields', []);
// Support legacy syntax
if (! empty($fields) && array_keys($fields)[0] !== 0) {
$fields = collect($fields)->map(function ($field, $handle) {
return compact('handle', 'field');
})->values()->all();
}
$contents['fields'] = $fields;
$this->contents = $contents;
return $this;
}
public function contents(): array
{
return $this->contents;
}
public function title()
{
return $this->contents['title'] ?? Str::humanize(Str::of($this->handle)->after('::')->afterLast('.'));
}
/**
* @throws FieldsetRecursionException
*/
public function validateRecursion()
{
$this->fields();
}
public function fields(): Fields
{
$fields = Arr::get($this->contents, 'fields', []);
return new Fields($fields);
}
public function field(string $handle): ?Field
{
return $this->fields()->get($handle);
}
public function hasField($field)
{
return $this->fields()->has($field);
}
public function isNamespaced(): bool
{
return Str::contains($this->handle(), '::');
}
public function namespace()
{
return $this->isNamespaced() ? Str::before($this->handle, '::') : null;
}
public function editUrl()
{
return cp_route('fieldsets.edit', $this->handle());
}
public function deleteUrl()
{
return cp_route('fieldsets.destroy', $this->handle());
}
public function resetUrl()
{
return cp_route('fieldsets.reset', $this->handle());
}
public function importedBy(): array
{
$blueprints = collect([
...Collection::all()->flatMap->entryBlueprints(),
...Taxonomy::all()->flatMap->termBlueprints(),
...GlobalSet::all()->map->blueprint(),
...AssetContainer::all()->map->blueprint(),
...Blueprint::in('')->values(),
])->filter()->filter(function (Blueprint $blueprint) {
return collect($blueprint->contents()['tabs'])
->pluck('sections')
->flatten(1)
->pluck('fields')
->flatten(1)
->filter(fn ($field) => $field && $this->fieldImportsFieldset($field))
->isNotEmpty();
})->values();
$fieldsets = \Statamic\Facades\Fieldset::all()
->filter(fn (Fieldset $fieldset) => isset($fieldset->contents()['fields']))
->filter(function (Fieldset $fieldset) {
return collect($fieldset->contents()['fields'])
->filter(fn ($field) => $this->fieldImportsFieldset($field))
->isNotEmpty();
})
->values();
return ['blueprints' => $blueprints, 'fieldsets' => $fieldsets];
}
private function fieldImportsFieldset(array $field): bool
{
if (isset($field['import'])) {
return $field['import'] === $this->handle();
}
if (is_string($field['field'])) {
return Str::before($field['field'], '.') === $this->handle();
}
if (isset($field['field']['fields'])) {
return collect($field['field']['fields'])
->filter(fn ($field) => $this->fieldImportsFieldset($field))
->isNotEmpty();
}
if (isset($field['field']['sets'])) {
return collect($field['field']['sets'])
->filter(fn ($setGroup) => isset($setGroup['sets']))
->filter(function ($setGroup) {
return collect($setGroup['sets'])->filter(function ($set) {
return collect($set['fields'])
->filter(fn ($field) => $this->fieldImportsFieldset($field))
->isNotEmpty();
})->isNotEmpty();
})
->isNotEmpty();
}
return false;
}
public function isDeletable()
{
return ! $this->isNamespaced();
}
public function isResettable()
{
return $this->isNamespaced()
&& File::exists(FieldsetRepository::overriddenNamespacedFieldsetPath($this->handle));
}
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
return $this;
}
public function saveQuietly()
{
$this->withEvents = false;
return $this->save();
}
public function save()
{
$isNew = is_null(Facades\Fieldset::find($this->handle()));
$withEvents = $this->withEvents;
$this->withEvents = true;
$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];
if ($withEvents) {
if ($isNew && FieldsetCreating::dispatch($this) === false) {
return false;
}
if (FieldsetSaving::dispatch($this) === false) {
return false;
}
}
FieldsetRepository::save($this);
foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
if ($withEvents) {
if ($isNew) {
FieldsetCreated::dispatch($this);
}
FieldsetSaved::dispatch($this);
}
return $this;
}
public function deleteQuietly()
{
$this->withEvents = false;
return $this->delete();
}
public function delete()
{
$withEvents = $this->withEvents;
$this->withEvents = true;
if ($withEvents && FieldsetDeleting::dispatch($this) === false) {
return false;
}
FieldsetRepository::delete($this);
if ($withEvents) {
FieldsetDeleted::dispatch($this);
}
return true;
}
public function reset()
{
FieldsetRepository::reset($this);
FieldsetReset::dispatch($this);
return true;
}
public function getQueryableValue(string $field)
{
if (in_array($method = Str::camel($field), $this->queryableMethods())) {
return $this->{$method}();
}
return null;
}
private function queryableMethods(): array
{
return [
'editUrl', 'fields', 'handle', 'isDeletable', 'isResettable',
'namespace', 'path', 'title',
];
}
public static function __callStatic($method, $parameters)
{
return Facades\Fieldset::{$method}(...$parameters);
}
}