1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2021 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
|
|
|
namespace App\View\Helper; |
14
|
|
|
|
15
|
|
|
use Cake\Utility\Hash; |
16
|
|
|
use Cake\Utility\Inflector; |
17
|
|
|
use Cake\View\Helper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Helper class to generate properties html for admin pages |
21
|
|
|
* |
22
|
|
|
* @property \Cake\View\Helper\FormHelper $Form The form helper |
23
|
|
|
* @property \App\View\Helper\PropertyHelper $Property The properties helper |
24
|
|
|
* @property \App\View\Helper\SchemaHelper $Schema The schema helper |
25
|
|
|
*/ |
26
|
|
|
class AdminHelper extends Helper |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* List of helpers used by this helper |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $helpers = ['Form', 'Property', 'Schema']; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Object containing i18n strings |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $dictionary = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Options |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $options = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function initialize(array $config): void |
53
|
|
|
{ |
54
|
|
|
parent::initialize($config); |
55
|
|
|
|
56
|
|
|
$this->options = [ |
57
|
|
|
'text' => [ |
58
|
|
|
'label' => false, |
59
|
|
|
'size' => 25, |
60
|
|
|
], |
61
|
|
|
'bool' => [ |
62
|
|
|
'label' => false, |
63
|
|
|
'type' => 'radio', |
64
|
|
|
'options' => [ |
65
|
|
|
['value' => 1, 'text' => __('Yes')], |
66
|
|
|
['value' => 0, 'text' => __('No')], |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
'json' => [ |
70
|
|
|
'label' => false, |
71
|
|
|
'type' => 'textarea', |
72
|
|
|
'v-jsoneditor' => 'true', |
73
|
|
|
'class' => 'json', |
74
|
|
|
], |
75
|
|
|
'combo' => [ |
76
|
|
|
'label' => false, |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
if (empty($this->dictionary)) { |
80
|
|
|
$this->setDictionary(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Control by type, property, value. |
86
|
|
|
* |
87
|
|
|
* @param string $type The type |
88
|
|
|
* @param string $property The property |
89
|
|
|
* @param mixed $value The value |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function control(string $type, string $property, $value): string |
93
|
|
|
{ |
94
|
|
|
$readonly = (bool)$this->_View->get('readonly'); |
95
|
|
|
$resource = (array)$this->_View->get('resource'); |
96
|
|
|
$unchangeable = (bool)Hash::get($resource, 'meta.unchangeable', false); |
97
|
|
|
if ($readonly === true || $unchangeable === true) { |
98
|
|
|
$schema = (array)$this->_View->get('schema'); |
99
|
|
|
|
100
|
|
|
return $this->Schema->format($value, (array)Hash::get($schema, sprintf('properties.%s', $property))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (in_array($type, ['bool', 'json', 'text'])) { |
104
|
|
|
if ($type === 'json' && is_array($value)) { |
105
|
|
|
$value = json_encode($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->Form->control($property, $this->options[$type] + compact('value')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (in_array($type, ['applications', 'auth_providers', 'endpoints', 'roles'])) { |
112
|
|
|
$options = (array)$this->_View->get($type); |
113
|
|
|
$value = $value ?? '-'; |
114
|
|
|
|
115
|
|
|
return $this->Form->control($property, $this->options['combo'] + compact('options', 'value')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->Property->control($property, $value, $this->options['text']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get dictionary object as json string |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getDictionary(): string |
127
|
|
|
{ |
128
|
|
|
return json_encode($this->dictionary); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set dictionary object |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function setDictionary(): void |
137
|
|
|
{ |
138
|
|
|
$modules = (array)$this->_View->get('modules'); |
139
|
|
|
foreach ($modules as $name => $module) { |
140
|
|
|
$moduleName = Inflector::humanize((string)Hash::get($module, 'name', $name)); |
141
|
|
|
$this->dictionary[$name] = Hash::get($module, 'label', __($moduleName)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|