-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandleJsonRepeaters.php
121 lines (103 loc) · 3.96 KB
/
HandleJsonRepeaters.php
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
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Facades\TwillBlocks;
use Illuminate\Support\Arr;
/**
* Save repeaters in a json column instead of a new model.
*
* This trait is not intended to replace main repeaters but to give a quick
* and easy alternative for simple elements where creating a new table might be an overkill.
*
* Simply define an array with the repeater names on your repository:
* protected $jsonRepeaters = [ 'REPEATER_NAME_1', 'REPEATER_NAME_2', ... ]
*
* Names must be the same as the ones you added in your `repeaters` attribute on `config\twill.php`
* or the actual filename for self-contained repeaters introduced in 2.1.
*
* Supported: Input, WYSIWYG, textarea, browsers.
* Not supported: Medias, Files, repeaters.
*/
trait HandleJsonRepeaters
{
/**
* @param array $fields
* @return array
*/
public function prepareFieldsBeforeCreateHandleJsonRepeaters($fields)
{
foreach ($this->jsonRepeaters as $repeater) {
if (isset($fields['repeaters'][$repeater])) {
$fields[$repeater] = $fields['repeaters'][$repeater];
}
}
return $fields;
}
/**
* @param \A17\Twill\Models\Model|null $object
* @param array $fields
* @return array
*/
public function prepareFieldsBeforeSaveHandleJsonRepeaters($object, $fields)
{
foreach ($this->jsonRepeaters as $repeater) {
if (isset($fields['repeaters'][$repeater])) {
$fields[$repeater] = $fields['repeaters'][$repeater];
}
}
return $fields;
}
/**
* @param \A17\Twill\Models\Model|null $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleJsonRepeaters($object, $fields)
{
foreach ($this->jsonRepeaters as $repeater) {
if (isset($fields[$repeater]) && ! empty($fields[$repeater])) {
$fields = $this->getJsonRepeater($fields, $repeater, $fields[$repeater]);
}
}
return $fields;
}
public function getJsonRepeater(array $fields, string $repeaterName, array $serializedData): array
{
$repeatersFields = [];
$repeatersBrowsers = [];
/** @var \A17\Twill\Services\Blocks\Block[] $repeatersList */
$repeatersList = TwillBlocks::getRepeaters()->keyBy('name');
$repeaters = [];
foreach ($serializedData as $index => $repeaterItem) {
$id = $repeaterItem['id'] ?? $index;
$repeater = $repeatersList[$repeaterName] ?? $repeatersList['dynamic-repeater-' . $repeaterName] ?? null;
if (!$repeater) {
// There is no repeater found. This can be due to code removal but a database left-over.
// In that case, we cannot do anything so we simply return the fields.
return $fields;
}
$repeaters[] = [
'id' => $id,
'type' => $repeater->component,
'title' => $repeater->title,
'titleField' => $repeater->titleField,
'hideTitlePrefix' => $repeater->hideTitlePrefix,
];
if (isset($repeaterItem['browsers'])) {
foreach ($repeaterItem['browsers'] as $key => $values) {
$repeatersBrowsers["blocks[$id][$key]"] = $values;
}
}
$itemFields = Arr::except($repeaterItem, ['id', 'repeaters', 'files', 'medias', 'browsers', 'blocks']);
foreach ($itemFields as $itemFieldIndex => $value) {
$repeatersFields[] = [
'name' => "blocks[$id][$itemFieldIndex]",
'value' => $value,
];
}
}
$fields['repeaters'][$repeaterName] = $repeaters;
$fields['repeaterFields'][$repeaterName] = $repeatersFields;
$fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers;
return $fields;
}
}