|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Services\Blocks\BlockCollection; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* Save repeaters in a json column instead of a new model. |
|
12
|
|
|
* |
|
13
|
|
|
* This trait is not intended to replace main repeaters but to give a quick |
|
14
|
|
|
* and easy alternative for simple elements where creating a new table might be an overkill. |
|
15
|
|
|
* |
|
16
|
|
|
* Simply define an array with the repeater names on your repository: |
|
17
|
|
|
* protected $jsonRepeaters = [ 'REPEATER_NAME_1', 'REPEATER_NAME_2', ... ] |
|
18
|
|
|
* |
|
19
|
|
|
* Names must be the same as the ones you added in your `repeaters` attribute on `config\twill.php` |
|
20
|
|
|
* or the actual filename for self-contained repeaters introduced in 2.1. |
|
21
|
|
|
* |
|
22
|
|
|
* Supported: Input, WYSIWYG, textarea, browsers. |
|
23
|
|
|
* Not supported: Medias, Files, repeaters. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
trait HandleJsonRepeaters |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \A17\Twill\Models\Model $object |
|
32
|
|
|
* @param array $fields |
|
33
|
|
|
* @return string[] |
|
34
|
|
|
*/ |
|
35
|
|
|
public function prepareFieldsBeforeSaveHandleJsonRepeaters($object, $fields) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
foreach ($this->jsonRepeaters as $repeater) { |
|
38
|
|
|
if (isset($fields['repeaters'][$repeater])) { |
|
39
|
|
|
$fields[$repeater] = $fields['repeaters'][$repeater]; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $fields; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \A17\Twill\Models\Model $object |
|
48
|
|
|
* @param array $fields |
|
49
|
|
|
* @return string[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getFormFieldsHandleJsonRepeaters($object, $fields) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->jsonRepeaters as $repeater) { |
|
55
|
|
|
if (isset($fields[$repeater]) && !empty($fields[$repeater])) { |
|
56
|
|
|
$fields = $this->getJsonRepeater($fields, $repeater, $fields[$repeater]); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $fields; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getJsonRepeater($fields, $repeaterName, $serializedData) |
|
64
|
|
|
{ |
|
65
|
|
|
$repeatersFields = []; |
|
66
|
|
|
$repeatersBrowsers = []; |
|
67
|
|
|
$repeatersList = app(BlockCollection::class)->getRepeaterList()->keyBy('name'); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($serializedData as $index => $repeaterItem) { |
|
70
|
|
|
$id = $repeaterItem['id'] ?? $index; |
|
71
|
|
|
|
|
72
|
|
|
$repeaters[] = [ |
|
73
|
|
|
'id' => $id, |
|
74
|
|
|
'type' => $repeatersList[$repeaterName]['component'], |
|
75
|
|
|
'title' => $repeatersList[$repeaterName]['title'], |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
if (isset($repeaterItem['browsers'])) { |
|
79
|
|
|
foreach ($repeaterItem['browsers'] as $key => $values) { |
|
80
|
|
|
$repeatersBrowsers["blocks[$id][$key]"] = $values; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$itemFields = Arr::except($repeaterItem, ['id', 'repeaters', 'files', 'medias', 'browsers', 'blocks']); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($itemFields as $index => $value) { |
|
|
|
|
|
|
87
|
|
|
$repeatersFields[] = [ |
|
88
|
|
|
'name' => "blocks[$id][$index]", |
|
89
|
|
|
'value' => $value, |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$fields['repeaters'][$repeaterName] = $repeaters; |
|
|
|
|
|
|
95
|
|
|
$fields['repeaterFields'][$repeaterName] = $repeatersFields; |
|
96
|
|
|
$fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers; |
|
97
|
|
|
|
|
98
|
|
|
return $fields; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.