|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Docs\Template\Object; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
// From Pimple |
|
8
|
|
|
use Pimple\Container; |
|
9
|
|
|
|
|
10
|
|
|
// From 'charcoal-admin' |
|
11
|
|
|
use Charcoal\Admin\AdminTemplate; |
|
12
|
|
|
use Charcoal\Admin\Ui\DashboardContainerInterface; |
|
13
|
|
|
use Charcoal\Admin\Ui\DashboardContainerTrait; |
|
14
|
|
|
use Charcoal\Admin\Ui\ObjectContainerInterface; |
|
15
|
|
|
use Charcoal\Admin\Ui\ObjectContainerTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Object Edit Template |
|
19
|
|
|
*/ |
|
20
|
|
|
class DocTemplate extends AdminTemplate implements |
|
21
|
|
|
DashboardContainerInterface, |
|
22
|
|
|
ObjectContainerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use DashboardContainerTrait; |
|
25
|
|
|
use ObjectContainerTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retrieve the list of parameters to extract from the HTTP request. |
|
29
|
|
|
* |
|
30
|
|
|
* @return string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function validDataFromRequest() |
|
33
|
|
|
{ |
|
34
|
|
|
return array_merge([ |
|
35
|
|
|
'obj_type' |
|
36
|
|
|
], parent::validDataFromRequest()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieve the header menu. |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function headerMenu() |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->headerMenu === null) { |
|
47
|
|
|
$dashboardConfig = $this->dashboardConfig(); |
|
48
|
|
|
|
|
49
|
|
|
if (isset($dashboardConfig['secondary_menu'])) { |
|
50
|
|
|
$this->headerMenu = $this->createHeaderMenu($dashboardConfig['secondary_menu']); |
|
|
|
|
|
|
51
|
|
|
} else { |
|
52
|
|
|
$this->headerMenu = $this->createHeaderMenu(); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->headerMenu; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retrieve the title of the page. |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Charcoal\Translator\Translation |
|
63
|
|
|
*/ |
|
64
|
|
|
public function title() |
|
65
|
|
|
{ |
|
66
|
|
|
if (isset($this->title)) { |
|
67
|
|
|
return $this->title; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
|
|
$config = $this->dashboardConfig(); |
|
72
|
|
|
|
|
73
|
|
|
if (isset($config['title'])) { |
|
74
|
|
|
$this->title = $this->translator()->translation($config['title']); |
|
75
|
|
|
|
|
76
|
|
|
return $this->title; |
|
77
|
|
|
} |
|
78
|
|
|
} catch (Exception $e) { |
|
79
|
|
|
$this->logger->error($e->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$obj = $this->obj(); |
|
83
|
|
|
$metadata = $obj->metadata(); |
|
84
|
|
|
$objLabel = null; |
|
85
|
|
|
|
|
86
|
|
|
if (!$objLabel && isset($metadata['admin']['forms'])) { |
|
|
|
|
|
|
87
|
|
|
$adminMetadata = $metadata['admin']; |
|
88
|
|
|
|
|
89
|
|
|
$formIdent = filter_input(INPUT_GET, 'form_ident', FILTER_SANITIZE_STRING); |
|
90
|
|
|
if (!$formIdent) { |
|
91
|
|
|
$formIdent = (isset($adminMetadata['default_form']) ? $adminMetadata['default_form'] : ''); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (isset($adminMetadata['forms'][$formIdent]['label'])) { |
|
95
|
|
|
$objLabel = $this->translator()->translation($adminMetadata['forms'][$formIdent]['label']); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!$objLabel) { |
|
100
|
|
|
$objType = (isset($metadata['labels']['singular_name']) ? |
|
101
|
|
|
$this->translator()->translation($metadata['labels']['singular_name']) : null); |
|
102
|
|
|
|
|
103
|
|
|
$objLabel = $this->translator()->translation('Documentation: {{ objType }}'); |
|
104
|
|
|
|
|
105
|
|
|
if ($objType) { |
|
106
|
|
|
$objLabel = strtr($objLabel, [ |
|
|
|
|
|
|
107
|
|
|
'{{ objType }}' => $objType |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($obj->view()) { |
|
|
|
|
|
|
113
|
|
|
$this->title = $obj->render((string)$objLabel, $obj); |
|
|
|
|
|
|
114
|
|
|
} else { |
|
115
|
|
|
$this->title = (string)$objLabel; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->title; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Container $container DI container. |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function setDependencies(Container $container) |
|
126
|
|
|
{ |
|
127
|
|
|
parent::setDependencies($container); |
|
128
|
|
|
|
|
129
|
|
|
// Required ObjectContainerInterface dependencies |
|
130
|
|
|
$this->setModelFactory($container['model/factory']); |
|
131
|
|
|
|
|
132
|
|
|
// Required dependencies. |
|
133
|
|
|
$this->dashboardBuilder = $container['dashboard/builder']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @throws Exception If the object's dashboard config can not be loaded. |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function createDashboardConfig() |
|
141
|
|
|
{ |
|
142
|
|
|
$adminMetadata = $this->objAdminMetadata(); |
|
143
|
|
|
$dashboardIdent = $this->dashboardIdent(); |
|
144
|
|
|
|
|
145
|
|
|
if (empty($dashboardIdent)) { |
|
146
|
|
|
$dashboardIdent = filter_input(INPUT_GET, 'dashboard_ident', FILTER_SANITIZE_STRING); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (empty($dashboardIdent)) { |
|
150
|
|
|
if (isset($adminMetadata['default_doc_dashboard'])) { |
|
151
|
|
|
$dashboardIdent = $adminMetadata['default_doc_dashboard']; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$overrideType = false; |
|
156
|
|
|
|
|
157
|
|
|
if (empty($dashboardIdent)) { |
|
158
|
|
|
if (!isset($adminMetadata['default_edit_dashboard'])) { |
|
159
|
|
|
throw new Exception(sprintf( |
|
160
|
|
|
'No default doc dashboard defined in admin metadata for %s', |
|
161
|
|
|
get_class($this->obj()) |
|
162
|
|
|
)); |
|
163
|
|
|
} |
|
164
|
|
|
$overrideType = true; |
|
165
|
|
|
$dashboardIdent = $adminMetadata['default_edit_dashboard']; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (!isset($adminMetadata['dashboards']) || !isset($adminMetadata['dashboards'][$dashboardIdent])) { |
|
169
|
|
|
throw new Exception( |
|
170
|
|
|
'Dashboard config is not defined.' |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$dashboardConfig = $adminMetadata['dashboards'][$dashboardIdent]; |
|
175
|
|
|
|
|
176
|
|
|
if ($overrideType) { |
|
177
|
|
|
$widgets = $dashboardConfig['widgets']; |
|
178
|
|
|
foreach ($widgets as $ident => $widget) { |
|
179
|
|
|
$dashboardConfig['widgets'][$ident]['type'] = 'charcoal/admin/widget/doc'; |
|
180
|
|
|
$dashboardConfig['widgets'][$ident]['show_header'] = true; |
|
181
|
|
|
$dashboardConfig['widgets'][$ident]['show_title'] = true; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $dashboardConfig; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @throws Exception If the object's admin metadata is not set. |
|
190
|
|
|
* @return \ArrayAccess |
|
191
|
|
|
*/ |
|
192
|
|
|
private function objAdminMetadata() |
|
193
|
|
|
{ |
|
194
|
|
|
$obj = $this->obj(); |
|
195
|
|
|
|
|
196
|
|
|
$objMetadata = $obj->metadata(); |
|
197
|
|
|
|
|
198
|
|
|
$adminMetadata = isset($objMetadata['admin']) ? $objMetadata['admin'] : null; |
|
199
|
|
|
if ($adminMetadata === null) { |
|
200
|
|
|
throw new Exception(sprintf( |
|
201
|
|
|
'The object %s does not have an admin metadata.', |
|
202
|
|
|
get_class($obj) |
|
203
|
|
|
)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $adminMetadata; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|