-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElfinderTemplate.php
582 lines (498 loc) · 15 KB
/
ElfinderTemplate.php
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
namespace Charcoal\Admin\Template;
use ArrayIterator;
use RuntimeException;
use InvalidArgumentException;
// From PSR-7
use Psr\Http\Message\RequestInterface;
// From Pimple
use Pimple\Container;
// From Mustache
use Mustache_LambdaHelper as LambdaHelper;
// From 'charcoal-factory'
use Charcoal\Factory\FactoryInterface;
// From 'charcoal-translator'
use Charcoal\Translator\Translation;
// From 'charcoal-property'
use Charcoal\Property\FileProperty;
// From 'charcoal-admin'
use Charcoal\Admin\AdminTemplate;
/**
*
*/
class ElfinderTemplate extends AdminTemplate
{
public const ADMIN_ASSETS_REL_PATH = 'assets/admin/';
public const ELFINDER_ASSETS_REL_PATH = 'assets/admin/elfinder/';
/**
* Store the elFinder configuration from the admin configuration.
*
* @var \Charcoal\Config\ConfigInterface
*/
protected $elfinderConfig;
/**
* Store the current property instance for the current class.
*
* @var \Charcoal\Property\PropertyInterface
*/
protected $formProperty;
/**
* The related object type.
*
* @var string
*/
private $objType;
/**
* The related object ID.
*
* @var string
*/
private $objId;
/**
* The related property identifier.
*
* @var string
*/
private $propertyIdent;
/**
* Whether to output JS/CSS assets for initializing elFinder.
*
* @var boolean
*/
private $showAssets = true;
/**
* Custom localization messages.
*
* @var array<string, (Translation|string|null)>|null
*/
private $localizations;
/**
* The related JS callback ID.
*
* @var string
*/
private $callbackIdent = '';
/**
* URL for the elFinder connector.
*
* @var string
*/
private $elfinderConnectorUrl;
/**
* Sets the template data from a PSR Request object.
*
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @return self
*/
protected function setDataFromRequest(RequestInterface $request)
{
$keys = $this->validDataFromRequest();
$data = $request->getParams($keys);
if (isset($data['obj_type'])) {
$this->objType = filter_var($data['obj_type'], FILTER_SANITIZE_STRING);
}
if (isset($data['obj_id'])) {
$this->objId = filter_var($data['obj_id'], FILTER_SANITIZE_STRING);
}
if (isset($data['property'])) {
$this->propertyIdent = filter_var($data['property'], FILTER_SANITIZE_STRING);
}
if (isset($data['assets'])) {
$this->showAssets = !!$data['assets'];
}
if (isset($data['callback'])) {
$this->callbackIdent = filter_var($data['callback'], FILTER_SANITIZE_STRING);
}
if (isset($this->elfinderConfig['translations'])) {
$this->setLocalizations(array_replace_recursive(
$this->defaultLocalizations(),
$this->elfinderConfig['translations']
));
}
return true;
}
/**
* Retrieve the list of parameters to extract from the HTTP request.
*
* @return string[]
*/
protected function validDataFromRequest()
{
return array_merge([
// Current object
'obj_type', 'obj_id', 'property',
// elFinder instance
'assets', 'callback'
], parent::validDataFromRequest());
}
/**
* Retrieve the title of the page.
*
* @return Translation
*/
public function title()
{
if ($this->title === null) {
$this->title = $this->translator()->translation('filesystem.library.media');
}
return $this->title;
}
/**
* Set the custom localization messages.
*
* @param array $localizations An associative array of localizations.
* @return self
*/
public function setLocalizations(array $localizations)
{
$this->localizations = new ArrayIterator();
foreach ($localizations as $ident => $translations) {
$this->addLocalization($ident, $translations);
}
return $this;
}
/**
* Add a custom localization message.
*
* @param string $ident The message ID.
* @param mixed $translations The message translations.
* @throws InvalidArgumentException If the message ID is not a string or the translations are invalid.
* @return self
*/
public function addLocalization($ident, $translations)
{
if (!is_string($ident)) {
throw new InvalidArgumentException(sprintf(
'Translation key must be a string, received %s',
(is_object($ident) ? get_class($ident) : gettype($ident))
));
}
$this->localizations[$ident] = $this->translator()->translation($translations);
return $this;
}
/**
* Remove the translations for the given message ID.
*
* @param string $ident The message ID to remove.
* @throws InvalidArgumentException If the message ID is not a string.
* @return self
*/
public function removeLocalization($ident)
{
if (!is_string($ident)) {
throw new InvalidArgumentException(sprintf(
'Translation key must be a string, received %s',
(is_object($ident) ? get_class($ident) : gettype($ident))
));
}
unset($this->localizations[$ident]);
return $this;
}
/**
* Count the number of localizations.
*
* @return integer
*/
public function numLocalizations()
{
return count($this->localizations());
}
/**
* Determine if there are any localizations.
*
* @return boolean
*/
public function hasLocalizations()
{
return !!$this->numLocalizations();
}
/**
* Retrieve the localizations.
*
* @return array<string, (Translation|string|null)>
*/
public function localizations()
{
if ($this->localizations === null) {
$this->setLocalizations($this->defaultLocalizations());
}
return $this->localizations;
}
/**
* Retrieve the translations for the given message ID.
*
* @param string $ident The message ID to lookup.
* @throws InvalidArgumentException If the message ID is not a string.
* @return Translation|string|null
*/
public function localization($ident)
{
if (!is_string($ident)) {
throw new InvalidArgumentException(sprintf(
'Translation key must be a string, received %s',
(is_object($ident) ? get_class($ident) : gettype($ident))
));
}
if (isset($this->localizations[$ident])) {
return $this->localizations[$ident];
}
return $ident;
}
/**
* Retrieve the custom localizations for elFinder.
*
* @return array<string, array<string, (string|null)>>
*/
public function elfinderLocalizations()
{
$i18n = [];
foreach ($this->localizations() as $id => $translations) {
if ($translations instanceof Translation) {
foreach ($translations->data() as $language => $message) {
$i18n[$language][$id] = $message;
}
} else {
$i18n[$language][$id] = $translations;
}
}
return $i18n;
}
/**
* Converts the elFinder {@see self::elfinderLocalizations() localizations} as a JSON string.
*
* @return string Returns data serialized with {@see json_encode()}.
*/
public function elfinderLocalizationsAsJson()
{
$options = (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($this->debug()) {
$options = ($options | JSON_PRETTY_PRINT);
}
return json_encode($this->elfinderLocalizations(), $options);
}
/**
* Converts the elFinder {@see self::elfinderLocalizations() localizations} as a JSON string, protected from Mustache.
*
* @return string Returns a stringified JSON object, protected from Mustache rendering.
*/
final public function escapedElfinderLocalizationsAsJson()
{
return '{{=<% %>=}}' . $this->elfinderLocalizationsAsJson() . '<%={{ }}=%>';
}
/**
* @return string
*/
public function adminAssetsUrl()
{
return $this->baseUrl(static::ADMIN_ASSETS_REL_PATH);
}
/**
* @return string
*/
public function elfinderAssetsUrl()
{
return $this->baseUrl(static::ELFINDER_ASSETS_REL_PATH);
}
/**
* @return string
*/
public function elfinderAssets()
{
return $this->showAssets;
}
/**
* Retrieve the current elFinder callback ID from the GET parameters.
*
* @return string|null
*/
public function elfinderCallback()
{
return $this->callbackIdent;
}
/**
* @param string $url The elFinder connector AJAX URL.
* @return self
*/
public function setElfinderConnectorUrl($url)
{
$this->elfinderConnectorUrl = $url;
return $this;
}
/**
* @return string|null
*/
public function elfinderConnectorUrl()
{
return $this->elfinderConnectorUrl;
}
/**
* Render the elFinder connector URL with the correct object model context.
*
* This method (a necessary evil) allows one to customize the URL
* without duplicating the template view.
*
* @see \Charcoal\Admin\Property\Input\FileInput::prepareFilePickerUrl()
*
* @return callable|null
*/
public function prepareElfinderConnectorUrl()
{
$uri = $this->getElfinderConnectorUrlTemplate();
return function ($noop, LambdaHelper $helper) use ($uri) {
$uri = $helper->render($uri);
$this->setElfinderConnectorUrl($uri);
return null;
};
}
/**
* Retrieve the elFinder connector URL template for rendering.
*
* @return string
*/
protected function getElfinderConnectorUrlTemplate()
{
$uri = 'obj_type={{ objType }}&obj_id={{ objId }}&property={{ propertyIdent }}';
$uri = '{{# withAdminUrl }}elfinder-connector?' . $uri . '{{/ withAdminUrl }}';
return $uri;
}
/**
* Retrieve the current object type from the GET parameters.
*
* @return string|null
*/
public function objType()
{
return $this->objType;
}
/**
* Retrieve the current object ID from the GET parameters.
*
* @return string|null
*/
public function objId()
{
return $this->objId;
}
/**
* Retrieve the current object's property identifier from the GET parameters.
*
* @return string|null
*/
public function propertyIdent()
{
return $this->propertyIdent;
}
/**
* Retrieve the current property.
*
* @return \Charcoal\Property\PropertyInterface|boolean A Form Property instance
* or FALSE if a property can not be resolved.
*/
public function formProperty()
{
if ($this->formProperty === null) {
$this->formProperty = false;
$objType = $this->objType();
$propertyIdent = $this->propertyIdent();
if ($objType && $propertyIdent) {
$model = $this->modelFactory()->get($objType);
if ($model->hasProperty($propertyIdent)) {
$this->formProperty = $model->property($propertyIdent);
}
}
}
return $this->formProperty;
}
/**
* Retrieve the elFinder client-side settings.
*
* @return array
*/
public function elfinderClientConfig()
{
if (empty($this->elfinderConfig['client'])) {
$settings = [];
} else {
$settings = $this->elfinderConfig['client'];
}
$settings['lang'] = $this->translator()->getLocale();
$property = $this->formProperty();
if ($property) {
$mimeTypes = filter_input(INPUT_GET, 'filetype', FILTER_SANITIZE_STRING);
if ($mimeTypes) {
if ($mimeTypes === 'file') {
$mimeTypes = [];
} elseif (!is_array($mimeTypes)) {
$mimeTypes = explode(',', $mimeTypes);
$mimeTypes = array_filter($mimeTypes, 'strlen');
}
$settings['onlyMimes'] = $mimeTypes;
} elseif ($property instanceof FileProperty) {
$settings['onlyMimes'] = $property['acceptedMimetypes'];
}
$settings['rememberLastDir'] = !($property instanceof FileProperty);
}
return $settings;
}
/**
* Converts the elFinder client-side {@see self::elfinderClientConfig() options} as a JSON string.
*
* @return string Returns data serialized with {@see json_encode()}.
*/
public function elfinderClientConfigAsJson()
{
$options = (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($this->debug()) {
$options = ($options | JSON_PRETTY_PRINT);
}
return json_encode($this->elfinderClientConfig(), $options);
}
/**
* Converts the elFinder client-side {@see self::elfinderClientConfig() options} as a JSON string, protected from Mustache.
*
* @return string Returns a stringified JSON object, protected from Mustache rendering.
*/
final public function escapedElfinderClientConfigAsJson()
{
return '{{=<% %>=}}' . $this->elfinderClientConfigAsJson() . '<%={{ }}=%>';
}
/**
* Inject dependencies from a DI Container.
*
* @param Container $container A dependencies container instance.
* @return void
*/
protected function setDependencies(Container $container)
{
parent::setDependencies($container);
$this->elfinderConfig = $container['elfinder/config'];
}
/**
* Retrieve the default custom localizations.
*
* @return array<string, (Translation|string|null)>
*/
protected function defaultLocalizations()
{
$t = $this->translator();
return [
'volume_default' => $t->translation('filesystem.volume.default'),
'volume_library' => $t->translation('filesystem.volume.library'),
'volume_storage' => $t->translation('filesystem.volume.storage'),
'volume_uploads' => $t->translation('filesystem.volume.uploads'),
'volume_public' => $t->translation('filesystem.volume.public'),
'volume_private' => $t->translation('filesystem.volume.private'),
];
}
// Templating
// =========================================================================
/**
* Determine if main & secondary menu should appear as mobile in a desktop resolution.
*
* @return boolean
*/
public function isFullscreenTemplate()
{
return false;
}
}