1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\FormElements; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader; |
7
|
|
|
use SilverStripe\Core\Manifest\ModuleResource; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* If you want to have a default_config, it's up to you to set |
11
|
|
|
* it in the constructor of your classes (by calling mergeDefaultConfig) |
12
|
|
|
*/ |
13
|
|
|
trait BaseElement |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Config array |
17
|
|
|
* |
18
|
|
|
* @var array<string,mixed> |
19
|
|
|
*/ |
20
|
|
|
protected $config = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array<string,mixed> |
24
|
|
|
*/ |
25
|
|
|
protected $elementAttributes = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get a config key value |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getConfig($key) |
34
|
|
|
{ |
35
|
|
|
if (isset($this->config[$key])) { |
36
|
|
|
return $this->config[$key]; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the config (always as json object) |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getConfigAsJson() |
45
|
|
|
{ |
46
|
|
|
if (empty($this->config)) { |
47
|
|
|
return '{}'; |
48
|
|
|
} |
49
|
|
|
$json = json_encode($this->config); |
50
|
|
|
return $json; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set a config value |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setConfig($key, $value) |
61
|
|
|
{ |
62
|
|
|
if ($value !== null) { |
63
|
|
|
$this->config[$key] = $value; |
64
|
|
|
} else { |
65
|
|
|
unset($this->config[$key]); |
66
|
|
|
} |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array<string,mixed> |
72
|
|
|
*/ |
73
|
|
|
public function readConfig() |
74
|
|
|
{ |
75
|
|
|
return $this->config; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Merge default_config into config |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function mergeDefaultConfig() |
83
|
|
|
{ |
84
|
|
|
$this->config = array_merge(self::config()->default_config, $this->config); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Merge an array of settings with the default/current |
89
|
|
|
* configuration. |
90
|
|
|
* |
91
|
|
|
* @param array $config |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function mergeConfig(array $config) |
95
|
|
|
{ |
96
|
|
|
$this->config = array_merge($this->config, $config); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function clearConfig() |
105
|
|
|
{ |
106
|
|
|
$this->config = []; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $config |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function replaceConfig($config) |
115
|
|
|
{ |
116
|
|
|
$this->config = $config; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array<string,mixed> |
122
|
|
|
*/ |
123
|
|
|
public function getElementAttributes() |
124
|
|
|
{ |
125
|
|
|
return $this->elementAttributes; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getElementAttributesHTML() |
132
|
|
|
{ |
133
|
|
|
$attributes = $this->getElementAttributes(); |
134
|
|
|
$parts = []; |
135
|
|
|
|
136
|
|
|
foreach ($attributes as $name => $value) { |
137
|
|
|
if (is_bool($value)) { |
138
|
|
|
$value = $value ? "true" : "false"; |
139
|
|
|
} else { |
140
|
|
|
if (is_scalar($value)) { |
141
|
|
|
$value = (string) $value; |
142
|
|
|
} else { |
143
|
|
|
$value = json_encode($value); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$parts[] = sprintf('%s="%s"', Convert::raw2att($name), Convert::raw2att($value)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return implode(' ', $parts); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function getElementAttribute(string $k) |
157
|
|
|
{ |
158
|
|
|
return $this->elementAttributes[$k] ?? null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $v |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setElementAttribute(string $k, $v) |
166
|
|
|
{ |
167
|
|
|
$this->elementAttributes[$k] = $v; |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $el |
173
|
|
|
* @param array<string,mixed> $properties |
174
|
|
|
* @param string $extraHtml |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function wrapInElement($el, $properties = [], string $extraHtml = '') |
178
|
|
|
{ |
179
|
|
|
if (static::config()->enable_requirements) { |
180
|
|
|
static::requirements(); |
181
|
|
|
} |
182
|
|
|
$html = parent::Field($properties); |
183
|
|
|
$config = $this->getConfigAsJson(); |
184
|
|
|
|
185
|
|
|
$attrsHTML = $this->getElementAttributesHTML(); |
186
|
|
|
|
187
|
|
|
// Simply wrap with custom element and set config |
188
|
|
|
$html = "<$el data-config='$config' $attrsHTML>{$html}{$extraHtml}</$el>"; |
189
|
|
|
return $html; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Helper to access this module resources |
194
|
|
|
* |
195
|
|
|
* @param string $path |
196
|
|
|
* @return ModuleResource |
197
|
|
|
*/ |
198
|
|
|
protected static function moduleResource($path) |
199
|
|
|
{ |
200
|
|
|
return ModuleLoader::getModule('lekoala/silverstripe-form-elements')->getResource($path); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|