|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Template\Object; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
// From PSR-7 |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
// From Pimple |
|
12
|
|
|
use Pimple\Container; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
// From 'charcoal-admin' |
|
16
|
|
|
use Charcoal\Admin\AdminTemplate; |
|
17
|
|
|
use Charcoal\Admin\Ui\CollectionContainerInterface; |
|
18
|
|
|
use Charcoal\Admin\Ui\CollectionContainerTrait; |
|
19
|
|
|
use Charcoal\Admin\Ui\DashboardContainerInterface; |
|
20
|
|
|
use Charcoal\Admin\Ui\DashboardContainerTrait; |
|
21
|
|
|
use Charcoal\Admin\Widget\SearchWidget; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Object collection template (table with a list of objects). |
|
25
|
|
|
*/ |
|
26
|
|
|
class CollectionTemplate extends AdminTemplate implements |
|
27
|
|
|
CollectionContainerInterface, |
|
28
|
|
|
DashboardContainerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use CollectionContainerTrait; |
|
31
|
|
|
use DashboardContainerTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param RequestInterface $request PSR-7 request. |
|
35
|
|
|
* @return boolean |
|
36
|
|
|
*/ |
|
37
|
|
|
public function init(RequestInterface $request) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::init($request); |
|
40
|
|
|
$this->createObjTable(); |
|
41
|
|
|
|
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Retrieve the list of parameters to extract from the HTTP request. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string[] |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function validDataFromRequest() |
|
51
|
|
|
{ |
|
52
|
|
|
return array_merge([ |
|
53
|
|
|
'obj_type' |
|
54
|
|
|
], parent::validDataFromRequest()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Sets the search widget accodingly |
|
59
|
|
|
* Uses the "default_search_list" ident that should point |
|
60
|
|
|
* on ident in the "lists" |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Charcoal\Admin\Widget\SearchWidget |
|
63
|
|
|
*/ |
|
64
|
|
|
public function searchWidget() |
|
65
|
|
|
{ |
|
66
|
|
|
$widget = $this->widgetFactory()->create(SearchWidget::class); |
|
67
|
|
|
$widget->setObjType($this->objType()); |
|
68
|
|
|
|
|
69
|
|
|
$listIdent = $this->metadataListIdent(); |
|
70
|
|
|
|
|
71
|
|
|
// Note that if the ident doesn't match a list, |
|
72
|
|
|
// it will return basicly every properties of the object |
|
73
|
|
|
$widget->setCollectionIdent($listIdent); |
|
74
|
|
|
|
|
75
|
|
|
return $widget; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Retrieve the title of the page. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Charcoal\Translator\Translation |
|
82
|
|
|
*/ |
|
83
|
|
|
public function title() |
|
84
|
|
|
{ |
|
85
|
|
|
if (isset($this->title)) { |
|
86
|
|
|
return $this->title; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$translator = $this->translator(); |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
|
|
$config = $this->dashboardConfig(); |
|
93
|
|
|
|
|
94
|
|
|
if (isset($config['title'])) { |
|
95
|
|
|
$this->title = $translator->translation($config['title']); |
|
96
|
|
|
return $this->title; |
|
97
|
|
|
} |
|
98
|
|
|
} catch (Exception $e) { |
|
99
|
|
|
$this->logger->error($e->getMessage()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$model = $this->proto(); |
|
103
|
|
|
$metadata = $model->metadata(); |
|
104
|
|
|
$objLabel = null; |
|
105
|
|
|
|
|
106
|
|
|
if (!$objLabel && isset($metadata['admin']['lists'])) { |
|
|
|
|
|
|
107
|
|
|
$adminMetadata = $metadata['admin']; |
|
108
|
|
|
|
|
109
|
|
|
$listIdent = filter_input(INPUT_GET, 'collection_ident', FILTER_SANITIZE_STRING); |
|
110
|
|
|
if (!$listIdent) { |
|
111
|
|
|
$listIdent = $this->collectionIdent(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!$listIdent) { |
|
115
|
|
|
$listIdent = $this->collectionIdentFallback(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($listIdent && $model->view()) { |
|
119
|
|
|
$listIdent = $model->render($listIdent); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (isset($adminMetadata['lists'][$listIdent]['label'])) { |
|
123
|
|
|
$objLabel = $translator->translation($adminMetadata['lists'][$listIdent]['label']); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (!$objLabel && isset($metadata['labels']['all_items'])) { |
|
128
|
|
|
$objLabel = $translator->translation($metadata['labels']['all_items']); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (!$objLabel) { |
|
132
|
|
|
$objType = (isset($metadata['labels']['name']) |
|
133
|
|
|
? $translator->translation($metadata['labels']['name']) |
|
134
|
|
|
: null); |
|
135
|
|
|
|
|
136
|
|
|
$objLabel = $translator->translation('Collection: {{ objType }}'); |
|
137
|
|
|
|
|
138
|
|
|
if ($objType) { |
|
139
|
|
|
$objLabel = strtr($objLabel, [ |
|
|
|
|
|
|
140
|
|
|
'{{ objType }}' => $objType |
|
141
|
|
|
]); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ($model->view()) { |
|
146
|
|
|
$this->title = $model->render((string)$objLabel, $model); |
|
147
|
|
|
} else { |
|
148
|
|
|
$this->title = (string)$objLabel; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this->title; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Container $container DI Container. |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function setDependencies(Container $container) |
|
159
|
|
|
{ |
|
160
|
|
|
parent::setDependencies($container); |
|
161
|
|
|
|
|
162
|
|
|
// Required collection dependencies |
|
163
|
|
|
$this->setModelFactory($container['model/factory']); |
|
164
|
|
|
$this->setCollectionLoader($container['model/collection/loader']); |
|
165
|
|
|
|
|
166
|
|
|
// Required dashboard dependencies. |
|
167
|
|
|
$this->setDashboardBuilder($container['dashboard/builder']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @throws Exception If the dashboard config can not be loaded. |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function createDashboardConfig() |
|
175
|
|
|
{ |
|
176
|
|
|
$adminMetadata = $this->objAdminMetadata(); |
|
177
|
|
|
$dashboardIdent = $this->dashboardIdent(); |
|
178
|
|
|
|
|
179
|
|
|
if (!$dashboardIdent) { |
|
180
|
|
|
$dashboardIdent = $this->metadataDashboardIdent(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (!isset($adminMetadata['dashboards']) || !isset($adminMetadata['dashboards'][$dashboardIdent])) { |
|
184
|
|
|
throw new Exception( |
|
185
|
|
|
'Dashboard config is not defined.' |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$dashboardConfig = $adminMetadata['dashboards'][$dashboardIdent]; |
|
190
|
|
|
|
|
191
|
|
|
return $dashboardConfig; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
private function createObjTable() |
|
198
|
|
|
{ |
|
199
|
|
|
$obj = $this->proto(); |
|
200
|
|
|
if (!$obj) { |
|
|
|
|
|
|
201
|
|
|
return; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if ($obj->source()->tableExists() === false) { |
|
205
|
|
|
$obj->source()->createTable(); |
|
206
|
|
|
$msg = $this->translator()->translate('Database table created for "{{ objType }}".', [ |
|
207
|
|
|
'{{ objType }}' => $obj->objType() |
|
208
|
|
|
]); |
|
209
|
|
|
$this->addFeedback( |
|
210
|
|
|
'notice', |
|
211
|
|
|
'<span class="fa fa-asterisk" aria-hidden="true"></span><span> '.$msg.'</span>' |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
private function metadataListIdent() |
|
220
|
|
|
{ |
|
221
|
|
|
$adminMetadata = $this->objAdminMetadata(); |
|
222
|
|
|
|
|
223
|
|
|
if (isset($adminMetadata['default_search_list'])) { |
|
224
|
|
|
$listIdent = $adminMetadata['default_search_list']; |
|
225
|
|
|
} elseif (isset($adminMetadata['default_list'])) { |
|
226
|
|
|
$listIdent = $adminMetadata['default_list']; |
|
227
|
|
|
} else { |
|
228
|
|
|
$listIdent = 'default'; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $listIdent; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @throws Exception If no default collection is defined. |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
private function metadataDashboardIdent() |
|
239
|
|
|
{ |
|
240
|
|
|
$dashboardIdent = filter_input(INPUT_GET, 'dashboard_ident', FILTER_SANITIZE_STRING); |
|
241
|
|
|
if ($dashboardIdent) { |
|
242
|
|
|
return $dashboardIdent; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
$adminMetadata = $this->objAdminMetadata(); |
|
246
|
|
|
if (!isset($adminMetadata['default_collection_dashboard'])) { |
|
247
|
|
|
throw new Exception(sprintf( |
|
248
|
|
|
'No default collection dashboard defined in admin metadata for %s.', |
|
249
|
|
|
get_class($this->proto()) |
|
250
|
|
|
)); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $adminMetadata['default_collection_dashboard']; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @throws Exception If the object's admin metadata is not set. |
|
258
|
|
|
* @return \ArrayAccess |
|
259
|
|
|
*/ |
|
260
|
|
|
private function objAdminMetadata() |
|
261
|
|
|
{ |
|
262
|
|
|
$obj = $this->proto(); |
|
263
|
|
|
|
|
264
|
|
|
$objMetadata = $obj->metadata(); |
|
265
|
|
|
|
|
266
|
|
|
$adminMetadata = isset($objMetadata['admin']) ? $objMetadata['admin'] : []; |
|
267
|
|
|
|
|
268
|
|
|
return $adminMetadata; |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|