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
|
|
|
/** @var null|string */ |
18
|
|
|
private $fragmentLabel; |
19
|
|
|
|
20
|
|
|
public static function make(string $key, Fields $fields): Field |
21
|
|
|
{ |
22
|
|
|
return (new static(new FieldType(FieldType::FRAGMENT), $key)) |
23
|
|
|
->fields($fields); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function fields(Fields $fields) |
27
|
|
|
{ |
28
|
|
|
$this->fields = $fields; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getFields(): Fields |
34
|
|
|
{ |
35
|
|
|
return $this->fields; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string|null $locale |
40
|
|
|
* @return Fields[] |
41
|
|
|
*/ |
42
|
|
|
public function getFragments(?string $locale = null): array |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
// By default there is one empty Fragment provided to the user |
45
|
|
|
$fragments = [Fragment::empty($this->getKey())]; |
46
|
|
|
|
47
|
|
|
// Model is auto-injected by Manager::editFields() method. |
48
|
|
|
if(($this->model)) { |
49
|
|
|
|
50
|
|
|
if(!method_exists($this->model, 'getFragments')) { |
51
|
|
|
throw new \RuntimeException(get_class($this->model) . ' is missing the ' . HasFragments::class . ' trait.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if(count($modelFragments = $this->model->getFragments($this->getKey())) > 0) { |
55
|
|
|
$fragments = $modelFragments->map(function (FragmentModel $fragmentModel) { |
56
|
|
|
return Fragment::fromModel($fragmentModel); |
57
|
|
|
})->all(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach($fragments as $k => $fragment) { |
62
|
|
|
$fragments[$k] = $fragments[$k] |
63
|
|
|
->setModelIdInputName($this->name.'[' . $k . '][modelId]') |
64
|
|
|
->setFields($this->fields->clone()->map(function(Field $field) use($k, $fragment){ |
65
|
|
|
return $field->name($this->name.'.' . $k . '.' . $field->getName()) |
|
|
|
|
66
|
|
|
->localizedFormat(':name.:locale') |
67
|
|
|
->valueResolver(function($model = null, $locale = null, $field) use($fragment){ |
68
|
|
|
|
69
|
|
|
if(isset($field->value)) return $field->value; |
70
|
|
|
|
71
|
|
|
if($field instanceof MediaField){ |
72
|
|
|
if(!$fragment->hasModelId()){ |
73
|
|
|
return []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $field->getMedia(FragmentModel::find($fragment->getModelId()), $locale); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $fragment->getValue($field->getColumn(), $locale); |
80
|
|
|
}); |
81
|
|
|
})); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $fragments; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* A fragmentlabel is shown in admin as the label above each fragment. |
89
|
|
|
* |
90
|
|
|
* @param string $fragmentLabel |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function fragmentLabel(string $fragmentLabel): self |
94
|
|
|
{ |
95
|
|
|
$this->fragmentLabel = $fragmentLabel; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getFragmentLabel(): string |
101
|
|
|
{ |
102
|
|
|
return $this->fragmentLabel ?? 'fragment'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getDuplicatableFields(): array |
106
|
|
|
{ |
107
|
|
|
if(count($this->getFragments()) < 1) return []; |
108
|
|
|
|
109
|
|
|
// Take the fields from the first fragment as a starting point for duplication |
110
|
|
|
return array_map(function(\Thinktomorrow\Chief\Fields\Types\Field $field){ |
111
|
|
|
return $field->valueResolver(function($model = null, $locale = null, $field){ |
|
|
|
|
112
|
|
|
if($field instanceof \Thinktomorrow\Chief\Fields\Types\MediaField) { return []; } |
113
|
|
|
return null; |
114
|
|
|
})->render(); |
115
|
|
|
}, $this->getFragments()[0]->getFields()->clone()->all()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.