This repository was archived by the owner on Mar 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseElement.php
More file actions
362 lines (327 loc) · 12.1 KB
/
Copy pathBaseElement.php
File metadata and controls
362 lines (327 loc) · 12.1 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace Mapbender\DataSourceBundle\Element;
use Doctrine\DBAL\Connection;
use Mapbender\CoreBundle\Component\Element;
use Symfony\Component\HttpFoundation\Response;
use Zumba\Util\JsonSerializer;
/**
* Class BaseElement
*/
abstract class BaseElement extends Element
{
/**
* Legacy mechanism to provide Element description for backend display and filtering via static attribute.
* Mapbender will remove support for this mechanism.
* Preferred method is to override getClassDescription.
* Class descriptions are subject to translation.
*
* @var string
* @deprecated
*/
protected static $description = '';
/**
* Legacy mechanism to provide Element title for backend display and filtering via static attribute.
* Mapbender will remove support for this mechanism.
* Preferred method is to override getClassTitle.
* Class titles are subject to translation.
*
* @var string
* @deprecated
*/
protected static $title = '';
/**
* Returns the element class title for backend Element selection and filtering.
* Subject to translation.
*
* @return string
*/
public static function getClassTitle()
{
return static::$title;
}
/**
* Returns the element class description for backend Element selection and filtering.
* Subject to translation.
*
* @return string
*/
public static function getClassDescription()
{
return static::$description;
}
/**
* Returns the JavaScript widget constructor name, magically auto-calculated from the component
* class name.
*
* @return string
* @deprecated every Element component should return its widget constructor name explicitly
* unless it wants to inherit a parent value.
*/
public function getWidgetName()
{
@trigger_error("Deprecated: " . get_called_class() . " relies on automatically calculated widget constructor name. Please implement getWidgetName in your Element class", E_USER_DEPRECATED);
$classNameParts = explode('\\', get_called_class());
return 'mapbender.mb' . end($classNameParts);
}
/**
* Prepare elements recursive.
*
* @param $items
* @return array
*/
public function prepareItems($items)
{
if (!is_array($items)) {
return $items;
} elseif (self::isAssoc($items)) {
$items = $this->prepareItem($items);
} else {
foreach ($items as $key => $item) {
$items[ $key ] = $this->prepareItem($item);
}
}
return $items;
}
/**
* Handles requests (API)
*
* Get request "action" variable and run defined action method.
*
* Example: if $action="feature/get", then convert name
* and run $this->getFeatureAction($request);
*
* @inheritdoc
*/
public function httpAction($action)
{
$request = $this->getRequestData();
$names = array_reverse(explode('/', $action));
$namesLength = count($names);
for ($i = 1; $i < $namesLength; $i++) {
$names[ $i ][0] = strtoupper($names[ $i ][0]);
}
$action = implode($names);
$methodName = preg_replace('/[^a-z]+/si', null, $action) . 'Action';
$result = $this->{$methodName}($request);
if (is_array($result)) {
$serializer = new JsonSerializer();
$responseBody = $serializer->serialize($result);
$result = new Response($responseBody, 200, array('Content-Type' => 'application/json'));
}
return $result;
}
/**
* Prepare element by type
*
* @param $item
* @return mixed
* @internal param $type
*/
protected function prepareItem($item)
{
if (!isset($item["type"])) {
return $item;
}
if (isset($item["children"])) {
$item["children"] = $this->prepareItems($item["children"]);
}
switch ($item['type']) {
case 'select':
if (isset($item['sql'])) {
$connectionName = isset($item['connection']) ? $item['connection'] : 'default';
$sql = $item['sql'];
$options = isset($item["options"]) ? $item["options"] : array();
unset($item['sql']);
unset($item['connection']);
/** @var Connection $connection */
$connection = $this->container->get("doctrine.dbal.{$connectionName}_connection");
$all = $connection->fetchAll($sql);
foreach ($all as $option) {
$options[] = array(reset($option), end($option));
}
$item["options"] = $options;
}
if (isset($item['service'])) {
$serviceInfo = $item['service'];
$serviceName = isset($serviceInfo['serviceName']) ? $serviceInfo['serviceName'] : 'default';
$method = isset($serviceInfo['method']) ? $serviceInfo['method'] : 'get';
$args = isset($serviceInfo['args']) ? $item['args'] : '';
$service = $this->container->get($serviceName);
$options = $service->$method($args);
$item['options'] = $options;
}
if (isset($item['dataStore'])) {
$dataStoreInfo = $item['dataStore'];
$dataStore = $this->container->get('data.source')->get($dataStoreInfo["id"]);
$options = array();
foreach ($dataStore->search() as $dataItem) {
$options[ $dataItem->getId() ] = $dataItem->getAttribute($dataStoreInfo["text"]);
}
if (isset($item['dataStore']['popupItems'])) {
$item['dataStore']['popupItems'] = $this->prepareItems($item['dataStore']['popupItems']);
}
$item['options'] = $options;
}
break;
}
return $item;
}
/**
* @return array|mixed
* @throws \LogicException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
protected function getRequestData()
{
$content = $this->container->get('request')->getContent();
$request = array_merge($_POST, $_GET);
if (!empty($content)) {
$request = array_merge($request, json_decode($content, true));
}
return $this->decodeRequest($request);
}
/**
* @return int
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
protected function getUserId()
{
return $this->container->get('security.context')->getUser()->getId();
}
/**
* Decode request array variables
*
* @param array $request
* @return mixed
*/
public function decodeRequest(array $request)
{
foreach ($request as $key => $value) {
if (is_array($value)) {
$request[ $key ] = $this->decodeRequest($value);
} elseif (strpos($key, '[')) {
preg_match('/(.+?)\[(.+?)\]/', $key, $matches);
list($match, $name, $subKey) = $matches;
if (!isset($request[ $name ])) {
$request[ $name ] = array();
}
$request[ $name ][ $subKey ] = $value;
unset($request[ $key ]);
}
}
return $request;
}
/**
* Auto-calculation of AdminType class from Element class name.
* Bare-bones reimplementation of deprecated upstream method.
*
* @return string fully qualified class name
*/
public static function getType()
{
$clsInfo = explode('\\', get_called_class());
$namespaceParts = array_slice($clsInfo, 0, -1);
// convention: AdminType classes are placed into the "<bundle>\Element\Type" namespace
$namespaceParts[] = "Type";
$bareClassName = implode('', array_slice($clsInfo, -1));
// convention: AdminType class name is the same as the element class name suffixed with AdminType
return implode('\\', $namespaceParts) . '\\' . $bareClassName . 'AdminType';
}
/**
* Auto-calculation of template reference from class name.
* Bare-bones reimplementation of deprecated upstream method.
*
* @param string $section 'Element' or 'ElementAdmin'
* @param string $suffix '.html.twig' (default) or '.json.twig'
* @return string twig-style template resource reference
*/
private static function autoTemplate($section, $suffix = '.html.twig')
{
$cls = get_called_class();
$bundleName = str_replace('\\', '', preg_replace('/^([\w]+\\\\)*?(\w+\\\\\w+Bundle).*$/', '\2', $cls));
$elementName = implode('', array_slice(explode('\\', $cls), -1));
$elementSnakeCase = strtolower(preg_replace('/([^A-Z])([A-Z])/', '\\1_\\2', $elementName));
return "{$bundleName}:{$section}:{$elementSnakeCase}{$suffix}";
}
/**
* Auto-calculation of admin template reference from class name.
* Bare-bones reimplementation of deprecated upstream method.
*
* @return string twig-style template resource reference
*/
public static function getFormTemplate()
{
return static::autoTemplate('ElementAdmin');
}
/**
* Auto-calculation of frontend template reference from class name.
* Bare-bones reimplementation of deprecated upstream method.
*
* @param string $suffix '.html.twig' (default) or '.json.twig'
* @return string twig-style template resource reference
*/
public function getFrontendTemplatePath($suffix = '.html.twig')
{
return static::autoTemplate('Element', $suffix);
}
/**
* @param array $arr
* @return bool
*/
protected static function isAssoc(&$arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
public function getAssets()
{
return $this->listAssets();
}
public static function listAssets()
{
return array(
'js' => array(
'/bundles/mapbenderdatasource/mapbender.element.datasource.base.js',
),
'css' => array(
'/bundles/mapbendercore/sass/element/htmlelement.scss',
),
);
}
/**
* @inheritdoc
*/
public static function getFormAssets()
{
return array(
'js' => array(
'components/codemirror/lib/codemirror.js',
'components/codemirror/mode/xml/xml.js',
'components/codemirror/keymap/sublime.js',
'components/codemirror/addon/selection/active-line.js',
'bundles/mapbendercore/mapbender.admin.htmlelement.js',
),
'css' => array(
'components/codemirror/lib/codemirror.css',
'components/codemirror/theme/neo.css',
)
);
}
public function getFrontendTemplateVars()
{
// The default fallback getConfiguration call (see below) can be outrageously expensive.
// This can make a default inherited render() call very slow. BaseElement child classes
// generally have pretty trivial templates, accessing only id and title of the Element
// entity, so this is completely appropriate here.
return $this->entity->getConfiguration();
}
public function getConfiguration()
{
$configuration = $this->entity->getConfiguration();
if (isset($configuration['children'])) {
$configuration['children'] = $this->prepareItems($configuration['children']);
}
return $configuration;
}
}