1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Form\Collection; |
4
|
|
|
|
5
|
|
|
use Terranet\Administrator\Collection\Mutable as BaseMutableCollection; |
6
|
|
|
use Terranet\Administrator\Columns\MediaElement; |
7
|
|
|
use Terranet\Administrator\Exception; |
8
|
|
|
use Terranet\Administrator\Field\Textarea; |
9
|
|
|
use Terranet\Administrator\Form\FormElement; |
10
|
|
|
use Terranet\Administrator\Form\FormSection; |
|
|
|
|
11
|
|
|
use Terranet\Administrator\Form\InputFactory; |
|
|
|
|
12
|
|
|
use Terranet\Administrator\Form\Type\Ckeditor; |
|
|
|
|
13
|
|
|
use Terranet\Administrator\Form\Type\Markdown; |
|
|
|
|
14
|
|
|
use Terranet\Administrator\Form\Type\Medium; |
|
|
|
|
15
|
|
|
use Terranet\Administrator\Form\Type\Tinymce; |
|
|
|
|
16
|
|
|
|
17
|
|
|
class Mutable extends BaseMutableCollection |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Insert a new form element. |
21
|
|
|
* |
22
|
|
|
* @param $element |
23
|
|
|
* @param mixed string|Closure $inputType |
24
|
|
|
* @param mixed null|int|string $position |
25
|
|
|
* |
26
|
|
|
* @throws Exception |
27
|
|
|
* |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
public function create($element, $inputType = null, $position = null) |
31
|
|
|
{ |
32
|
|
|
if (!(is_string($element) || $element instanceof FormElement)) { |
33
|
|
|
throw new Exception('$element must be string or FormElement instance.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Create new element from string declaration ("title"). |
37
|
|
|
if (is_string($element)) { |
38
|
|
|
$element = (new FormElement($element)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Create Form Input Element from string declaration ("textarea") |
42
|
|
|
if (is_string($inputType)) { |
43
|
|
|
$oldInput = $element->getInput(); |
44
|
|
|
$newInput = InputFactory::make($element->id(), $inputType); |
45
|
|
|
|
46
|
|
|
$newInput->setRelation( |
47
|
|
|
$oldInput->getRelation() |
|
|
|
|
48
|
|
|
)->setTranslatable( |
49
|
|
|
$oldInput->getTranslatable() |
|
|
|
|
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$element->setInput( |
53
|
|
|
$newInput |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Allow a callable input type. |
58
|
|
|
if (is_callable($inputType)) { |
59
|
|
|
call_user_func_array($inputType, [$element]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_numeric($position)) { |
63
|
|
|
return $this->insert($element, $position); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Push element |
67
|
|
|
$this->push($element); |
68
|
|
|
|
69
|
|
|
if (null !== $position) { |
70
|
|
|
return $this->move($element->id(), $position); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create a section. |
78
|
|
|
* |
79
|
|
|
* @param $section |
80
|
|
|
* @param null $position |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function section($section, $position = null) |
85
|
|
|
{ |
86
|
|
|
if (is_string($section)) { |
87
|
|
|
$section = new FormSection($section); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null !== $position ? $this->insert($section, $position) : $this->push($section); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Whether the collection has active editor of specific type. |
95
|
|
|
* |
96
|
|
|
* @param $editor |
97
|
|
|
* @return bool |
98
|
|
|
* @throws Exception |
99
|
|
|
*/ |
100
|
|
|
public function hasEditors($editor) |
101
|
|
|
{ |
102
|
|
|
$this->validateEditor($editor); |
103
|
|
|
|
104
|
|
|
return (bool) $this->filter(function ($field) use ($editor) { |
105
|
|
|
return $field instanceof Textarea && $field->editorEnabled($editor); |
106
|
|
|
})->count(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set rich editors. |
111
|
|
|
* |
112
|
|
|
* @param mixed string|array $fields |
113
|
|
|
*/ |
114
|
|
|
public function editors($fields, string $editor = null) |
115
|
|
|
{ |
116
|
|
|
if (is_array($fields)) { |
117
|
|
|
foreach ($fields as $field => $editor) { |
118
|
|
|
$this->editors($field, $editor); |
119
|
|
|
} |
120
|
|
|
} elseif (is_string($fields) && $editor) { |
121
|
|
|
$item = $this->find($fields); |
122
|
|
|
if ($item instanceof Textarea) { |
123
|
|
|
if (method_exists($item, $editor)) { |
124
|
|
|
$item->$editor(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set fields descriptions. |
134
|
|
|
* |
135
|
|
|
* @param mixed string|array $fields |
136
|
|
|
*/ |
137
|
|
|
public function hints($fields, string $hint = null) |
138
|
|
|
{ |
139
|
|
|
if (is_array($fields)) { |
140
|
|
|
foreach ($fields as $field => $hint) { |
141
|
|
|
$this->hints($field, $hint); |
142
|
|
|
} |
143
|
|
|
} elseif (is_string($fields) && $hint) { |
144
|
|
|
$item = $this->find($fields); |
145
|
|
|
$item->setDescription($hint); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $editor |
153
|
|
|
* |
154
|
|
|
* @throws Exception |
155
|
|
|
*/ |
156
|
|
|
protected function validateEditor($editor) |
157
|
|
|
{ |
158
|
|
|
if (!in_array($editor, ['ckeditor', 'tinymce', 'medium', 'markdown'], true)) { |
159
|
|
|
throw new Exception(sprintf('Unknown editor %s', $editor)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Create element object from string. |
165
|
|
|
* |
166
|
|
|
* @param $element |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
protected function createElement($element) |
171
|
|
|
{ |
172
|
|
|
if (is_string($element)) { |
173
|
|
|
$element = new FormElement($element); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $element; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $collection |
181
|
|
|
* |
182
|
|
|
* @return FormElement|\Terranet\Administrator\Columns\MediaElement |
183
|
|
|
*/ |
184
|
|
|
protected function createMediaElement($collection): MediaElement |
185
|
|
|
{ |
186
|
|
|
return FormElement::media($collection); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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