1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2023 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace App\Form; |
15
|
|
|
|
16
|
|
|
use Cake\Utility\Hash; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Return a custom component to handle input for a property |
20
|
|
|
*/ |
21
|
|
|
class CustomComponentControl implements CustomHandlerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public function control(string $name, $value, array $options): array |
27
|
|
|
{ |
28
|
|
|
// you can define a custom component via `tag` option, default is <key-value-list> |
29
|
|
|
$tag = Hash::get($options, 'tag', 'key-value-list'); |
30
|
|
|
$label = (string)Hash::get($options, 'label', $name); |
31
|
|
|
$readonly = (bool)Hash::get($options, 'readonly', false); |
32
|
|
|
$type = (string)Hash::get($options, 'type'); |
33
|
|
|
if ($type === 'json' || is_array($value) || is_object($value)) { |
34
|
|
|
$value = htmlspecialchars($this->jsonValue($value)); |
35
|
|
|
} |
36
|
|
|
$html = sprintf( |
37
|
|
|
'<%s label="%s" name="%s" value="%s" :readonly=%s />', |
38
|
|
|
$tag, |
39
|
|
|
$label, |
40
|
|
|
$name, |
41
|
|
|
$value, |
42
|
|
|
$readonly ? 'true' : 'false', |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return compact('type', 'html'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return a string representing the json value. |
50
|
|
|
* |
51
|
|
|
* @param mixed|null $value The value |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function jsonValue($value): string |
55
|
|
|
{ |
56
|
|
|
if (empty($value)) { |
57
|
|
|
return ''; |
58
|
|
|
} |
59
|
|
|
if (is_array($value) || is_object($value)) { |
60
|
|
|
return json_encode($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return json_encode(json_decode($value, true)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|