|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
trait HandleBrowsers |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param \A17\Twill\Models\Model $object |
|
9
|
|
|
* @param array $fields |
|
10
|
|
|
* @return void |
|
11
|
|
|
*/ |
|
12
|
|
|
public function afterSaveHandleBrowsers($object, $fields) |
|
13
|
|
|
{ |
|
14
|
|
|
if (property_exists($this, 'browsers')) { |
|
15
|
|
|
foreach ($this->browsers as $module) { |
|
16
|
|
|
if (is_string($module)) { |
|
17
|
|
|
$this->updateBrowser($object, $fields, $module, 'position', $module); |
|
18
|
|
|
} elseif (is_array($module)) { |
|
19
|
|
|
$browserName = !empty($module['browserName']) ? $module['browserName'] : key($module); |
|
20
|
|
|
$relation = !empty($module['relation']) ? $module['relation'] : key($module); |
|
21
|
|
|
$positionAttribute = !empty($module['positionAttribute']) ? $module['positionAttribute'] : 'position'; |
|
22
|
|
|
$this->updateBrowser($object, $fields, $relation, $positionAttribute, $browserName); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \A17\Twill\Models\Model $object |
|
30
|
|
|
* @param array $fields |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFormFieldsHandleBrowsers($object, $fields) |
|
34
|
|
|
{ |
|
35
|
|
|
if (property_exists($this, 'browsers')) { |
|
36
|
|
|
foreach ($this->browsers as $module) { |
|
37
|
|
|
if (is_string($module)) { |
|
38
|
|
|
$fields['browsers'][$module] = $this->getFormFieldsForBrowser($object, $module, null, 'title', null); |
|
39
|
|
|
} elseif (is_array($module)) { |
|
40
|
|
|
$relation = !empty($module['relation']) ? $module['relation'] : key($module); |
|
41
|
|
|
$routePrefix = isset($module['routePrefix']) ? $module['routePrefix'] : null; |
|
42
|
|
|
$titleKey = !empty($module['titleKey']) ? $module['titleKey'] : 'title'; |
|
43
|
|
|
$moduleName = isset($module['moduleName']) ? $module['moduleName'] : null; |
|
44
|
|
|
$browserName = !empty($module['browserName']) ? $module['browserName'] : key($module); |
|
45
|
|
|
$fields['browsers'][$browserName] = $this->getFormFieldsForBrowser($object, $relation, $routePrefix, $titleKey, $moduleName); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $fields; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \A17\Twill\Models\Model $object |
|
55
|
|
|
* @param array $fields |
|
56
|
|
|
* @param string $relationship |
|
57
|
|
|
* @param string $positionAttribute |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function updateBrowser($object, $fields, $relationship, $positionAttribute = 'position', $browserName = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$browserName = $browserName ?? $relationship; |
|
63
|
|
|
$fieldsHasElements = isset($fields['browsers'][$browserName]) && !empty($fields['browsers'][$browserName]); |
|
64
|
|
|
$relatedElements = $fieldsHasElements ? $fields['browsers'][$browserName] : []; |
|
65
|
|
|
$relatedElementsWithPosition = []; |
|
66
|
|
|
$position = 1; |
|
67
|
|
|
foreach ($relatedElements as $relatedElement) { |
|
68
|
|
|
$relatedElementsWithPosition[$relatedElement['id']] = [$positionAttribute => $position++]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$object->$relationship()->sync($relatedElementsWithPosition); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param \A17\Twill\Models\Model $object |
|
76
|
|
|
* @param array $fields |
|
77
|
|
|
* @param string $relationship |
|
78
|
|
|
* @param string $positionAttribute |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function updateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position') { |
|
82
|
|
|
$this->updateBrowser($object, $fields, $relationship, $positionAttribute); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $object |
|
87
|
|
|
* @param array $fields |
|
88
|
|
|
* @param string $browserName |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function updateRelatedBrowser($object, $fields, $browserName) |
|
92
|
|
|
{ |
|
93
|
|
|
$object->saveRelated($fields['browsers'][$browserName] ?? [], $browserName); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param \A17\Twill\Models\Model $object |
|
98
|
|
|
* @param string $relation |
|
99
|
|
|
* @param string|null $routePrefix |
|
100
|
|
|
* @param string $titleKey |
|
101
|
|
|
* @param string|null $moduleName |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getFormFieldsForBrowser($object, $relation, $routePrefix = null, $titleKey = 'title', $moduleName = null) |
|
105
|
|
|
{ |
|
106
|
|
|
return $object->$relation->map(function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName) { |
|
107
|
|
|
return [ |
|
108
|
|
|
'id' => $relatedElement->id, |
|
109
|
|
|
'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey, |
|
110
|
|
|
'edit' => moduleRoute($moduleName ?? $relation, $routePrefix ?? '', 'edit', $relatedElement->id), |
|
111
|
|
|
'endpointType' => $relatedElement->getMorphClass(), |
|
112
|
|
|
] + (classHasTrait($relatedElement, HasMedias::class) ? [ |
|
|
|
|
|
|
113
|
|
|
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]), |
|
114
|
|
|
] : []); |
|
115
|
|
|
})->toArray(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param \A17\Twill\Models\Model $object |
|
120
|
|
|
* @param string $relation |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getFormFieldsForRelatedBrowser($object, $relation) |
|
124
|
|
|
{ |
|
125
|
|
|
return $object->getRelated($relation)->map(function ($relatedElement) { |
|
|
|
|
|
|
126
|
|
|
return ($relatedElement != null) ? [ |
|
127
|
|
|
'id' => $relatedElement->id, |
|
128
|
|
|
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title, |
|
129
|
|
|
'endpointType' => $relatedElement->getMorphClass(), |
|
130
|
|
|
] + (($relatedElement->adminEditUrl ?? null) ? [] : [ |
|
131
|
|
|
'edit' => $relatedElement->adminEditUrl, |
|
132
|
|
|
]) + (classHasTrait($relatedElement, HasMedias::class) ? [ |
|
133
|
|
|
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]), |
|
134
|
|
|
] : []) : []; |
|
135
|
|
|
})->reject(function ($item) { |
|
136
|
|
|
return empty($item); |
|
137
|
|
|
})->values()->toArray(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths