|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Yaro\Jarboe\Table\CRUD; |
|
8
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Translatable; |
|
9
|
|
|
|
|
10
|
|
|
class Repeater extends AbstractField |
|
11
|
|
|
{ |
|
12
|
|
|
use Translatable; |
|
13
|
|
|
|
|
14
|
|
|
private $fields = []; |
|
15
|
|
|
private $sortable = false; |
|
16
|
|
|
private $modelObject; |
|
17
|
|
|
private $headingName = ''; |
|
18
|
|
|
|
|
19
|
|
|
public function sortable(bool $enable = true) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->sortable = $enable; |
|
22
|
|
|
|
|
23
|
|
|
return $this; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function isSortable(): bool |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->sortable; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function fields(array $fields) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($fields as $field) { |
|
34
|
|
|
$this->addField($field); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function addField(AbstractField $field) |
|
41
|
|
|
{ |
|
42
|
|
|
$adapter = $field::$repeaterAdapterClass; |
|
43
|
|
|
if ($adapter) { |
|
44
|
|
|
$field = new $adapter($field); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->fields[] = $field; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function beforeUpdate($model) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->modelObject = $model; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function value(Request $request) |
|
58
|
|
|
{ |
|
59
|
|
|
$repeaterItems = Arr::get($request->all($this->name()), $this->name()); |
|
60
|
|
|
$repeaterItems = is_array($repeaterItems) ? $repeaterItems : []; |
|
61
|
|
|
|
|
62
|
|
|
$data = []; |
|
63
|
|
|
if ($this->isTranslatable()) { |
|
64
|
|
|
foreach ($repeaterItems as $locale => $items) { |
|
65
|
|
|
$localeData = []; |
|
66
|
|
|
foreach ($items as $item) { |
|
67
|
|
|
$itemData = []; |
|
68
|
|
|
/** @var AbstractField $field */ |
|
69
|
|
|
foreach ($this->getFields() as $field) { |
|
70
|
|
|
$itemData[$field->name()] = $field->value( |
|
71
|
|
|
(new Request([], [], [], [], array_filter($item, function ($item) { |
|
72
|
|
|
return is_object($item); |
|
73
|
|
|
})))->replace($item) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
$localeData[] = $itemData; |
|
77
|
|
|
} |
|
78
|
|
|
$data[$locale] = $localeData; |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
$index = 0; |
|
82
|
|
|
foreach ($repeaterItems as $item) { |
|
83
|
|
|
$itemData = []; |
|
84
|
|
|
/** @var AbstractField $field */ |
|
85
|
|
|
foreach ($this->getFields() as $field) { |
|
86
|
|
|
$itemData[$field->name()] = $field->value( |
|
87
|
|
|
(new Request())->replace($item) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
$data[] = $itemData; |
|
91
|
|
|
$index++; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $data; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getFields(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->fields; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getListView($model) |
|
104
|
|
|
{ |
|
105
|
|
|
return view('jarboe::crud.fields.repeater.list', [ |
|
106
|
|
|
'crud' => $this->crud(), |
|
107
|
|
|
'repeater' => $this, |
|
108
|
|
|
'model' => $model, |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getEditFormView($model) |
|
113
|
|
|
{ |
|
114
|
|
|
return view('jarboe::crud.fields.repeater.edit', [ |
|
115
|
|
|
'crud' => $this->crud(), |
|
116
|
|
|
'repeater' => $this, |
|
117
|
|
|
'model' => $model, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getCreateFormView() |
|
122
|
|
|
{ |
|
123
|
|
|
return view('jarboe::crud.fields.repeater.create', [ |
|
124
|
|
|
'crud' => $this->crud(), |
|
125
|
|
|
'repeater' => $this, |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function errors(array $messages): array |
|
130
|
|
|
{ |
|
131
|
|
|
$errors = []; |
|
132
|
|
|
foreach ($messages as $key => $value) { |
|
133
|
|
|
foreach ($value as &$error) { |
|
134
|
|
|
$parts = explode('.', $key); |
|
135
|
|
|
$name = array_pop($parts); |
|
136
|
|
|
$error = str_replace($key, $name, $error); |
|
137
|
|
|
} |
|
138
|
|
|
array_set($errors, $key, $value); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $errors[$this->name()] ?? []; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getRepeaterItemFormView(array $data) |
|
145
|
|
|
{ |
|
146
|
|
|
$model = new class($data, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS) extends \ArrayObject { |
|
147
|
|
|
public function offsetGet($index) |
|
148
|
|
|
{ |
|
149
|
|
|
if (!parent::offsetExists($index)) { |
|
|
|
|
|
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
return parent::offsetGet($index); |
|
153
|
|
|
} |
|
154
|
|
|
}; |
|
155
|
|
|
|
|
156
|
|
|
return view('jarboe::crud.fields.repeater.inc.item_edit', [ |
|
157
|
|
|
'repeater' => $this, |
|
158
|
|
|
'model' => $model, |
|
159
|
|
|
'rowsLeft' => 12, |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function heading(string $fieldName) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->headingName = $fieldName; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getHeadingName(): string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->headingName; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function prepare(CRUD $crud) |
|
176
|
|
|
{ |
|
177
|
|
|
parent::prepare($crud); |
|
178
|
|
|
|
|
179
|
|
|
/** @var AbstractField $field */ |
|
180
|
|
|
foreach ($this->getFields() as $field) { |
|
181
|
|
|
$field->prepare($crud); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.