1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Fragments; |
5
|
|
|
|
6
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
7
|
|
|
use Thinktomorrow\Chief\Fields\Types\AbstractField; |
8
|
|
|
use Thinktomorrow\Chief\Fields\Types\Field; |
9
|
|
|
use Thinktomorrow\Chief\Fields\Types\FieldType; |
10
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
11
|
|
|
|
12
|
|
|
class FragmentField extends AbstractField implements Field |
13
|
|
|
{ |
14
|
|
|
/** @var Fields */ |
15
|
|
|
private $fields; |
16
|
|
|
|
17
|
|
|
public static function make(string $key, Fields $fields): Field |
18
|
|
|
{ |
19
|
|
|
return (new static(new FieldType(FieldType::FRAGMENT), $key)) |
20
|
|
|
->fields($fields); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function fields(Fields $fields) |
24
|
|
|
{ |
25
|
|
|
$this->fields = $fields; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getFields(): Fields |
31
|
|
|
{ |
32
|
|
|
return $this->fields; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|null $locale |
37
|
|
|
* @return Fields[] |
38
|
|
|
*/ |
39
|
|
|
public function getFragments(?string $locale = null): array |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
// By default there is one empty Fragment provided to the user |
42
|
|
|
$fragments = [Fragment::empty($this->getKey())]; |
43
|
|
|
|
44
|
|
|
// Model is auto-injected by Manager::editFields() method. |
45
|
|
|
if(($this->model)) { |
46
|
|
|
|
47
|
|
|
if(!method_exists($this->model, 'getFragments')) { |
48
|
|
|
throw new \RuntimeException(get_class($this->model) . ' is missing the ' . HasFragments::class . ' trait.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if(count($modelFragments = $this->model->getFragments($this->getKey())) > 0) { |
52
|
|
|
$fragments = $modelFragments->map(function (FragmentModel $fragmentModel) { |
53
|
|
|
return Fragment::fromModel($fragmentModel); |
54
|
|
|
})->all(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach($fragments as $k => $fragment) { |
59
|
|
|
$fragments[$k] = $fragments[$k] |
60
|
|
|
->setModelIdInputName($this->name.'[' . $k . '][modelId]') |
61
|
|
|
->setFields($this->fields->clone()->map(function(Field $field) use($k, $fragment){ |
62
|
|
|
return $field->name($this->name.'.' . $k . '.' . $field->getName()) |
|
|
|
|
63
|
|
|
->localizedFormat(':name.:locale') |
64
|
|
|
->valueResolver(function($model = null, $locale = null, $field) use($fragment){ |
65
|
|
|
|
66
|
|
|
if(isset($field->value)) return $field->value; |
67
|
|
|
|
68
|
|
|
if($field instanceof MediaField){ |
69
|
|
|
if(!$fragment->hasModelId()){ |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $field->getMedia(FragmentModel::find($fragment->getModelId()), $locale); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $fragment->getValue($field->getColumn(), $locale); |
77
|
|
|
}); |
78
|
|
|
})); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $fragments; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getDuplicatableFields(): array |
85
|
|
|
{ |
86
|
|
|
if(count($this->getFragments()) < 1) return []; |
87
|
|
|
|
88
|
|
|
// Take the fields from the first fragment as a starting point for duplication |
89
|
|
|
return array_map(function(\Thinktomorrow\Chief\Fields\Types\Field $field){ |
90
|
|
|
return $field->valueResolver(function($model = null, $locale = null, $field){ |
|
|
|
|
91
|
|
|
if($field instanceof \Thinktomorrow\Chief\Fields\Types\MediaField) { return []; } |
92
|
|
|
return null; |
93
|
|
|
})->render(); |
94
|
|
|
}, $this->getFragments()[0]->getFields()->clone()->all()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.