|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kitodo\Dlf\ViewHelpers; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU General Public License version 3 or later. |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
|
14
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
15
|
|
|
use TYPO3\CMS\Core\Localization\LocalizationFactory; |
|
16
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
17
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
|
18
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This view helper serializes media player configuration to JSON and makes it |
|
22
|
|
|
* available as a variable to be passed to the player. |
|
23
|
|
|
*/ |
|
24
|
|
|
class MediaPlayerConfigViewHelper extends AbstractViewHelper |
|
25
|
|
|
{ |
|
26
|
|
|
protected $escapeOutput = false; |
|
27
|
|
|
|
|
28
|
|
|
public function initializeArguments() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->registerArgument('id', 'string', 'ID of the generated player configuration', true); |
|
31
|
|
|
$this->registerArgument('settings', 'array', 'the settings array that is converted to JSON', true); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function renderStatic( |
|
35
|
|
|
array $arguments, |
|
36
|
|
|
\Closure $renderChildrenClosure, |
|
37
|
|
|
RenderingContextInterface $renderingContext |
|
38
|
|
|
) { |
|
39
|
|
|
$id = $arguments['id']; |
|
40
|
|
|
$inputSettings = $arguments['settings']; |
|
41
|
|
|
|
|
42
|
|
|
// Whitelist keys to keep out stuff such as playerTranslationsFile |
|
43
|
|
|
$allowedKeys = ['shareButtons', 'screenshotCaptions', 'constants']; |
|
44
|
|
|
$result = array_intersect_key($inputSettings, array_flip($allowedKeys)); |
|
45
|
|
|
|
|
46
|
|
|
// Add translations |
|
47
|
|
|
$result['lang'] = self::getTranslations($inputSettings['playerTranslations']['baseFile'] ?? 'EXT:dlf/Resources/Private/Language/locallang_media.xlf'); |
|
48
|
|
|
|
|
49
|
|
|
// Resolve paths |
|
50
|
|
|
foreach ($result['shareButtons'] ?? [] as $key => $button) { |
|
51
|
|
|
// For Flexforms-configured button |
|
52
|
|
|
if (isset($button['singleButton'])) { |
|
53
|
|
|
$button = $button['singleButton']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($button['type'] === 'image') { |
|
57
|
|
|
$filePath = GeneralUtility::getFileAbsFileName($button['src']); |
|
58
|
|
|
$webPath = PathUtility::getAbsoluteWebPath($filePath); |
|
59
|
|
|
|
|
60
|
|
|
$button['src'] = $webPath; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$result['shareButtons'][$key] = $button; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Allow using (and overriding) non-numeric keys for shareButtons |
|
67
|
|
|
$result['shareButtons'] = array_values($result['shareButtons'] ?? []); |
|
68
|
|
|
|
|
69
|
|
|
$idJson = json_encode($id); |
|
70
|
|
|
$resultJson = json_encode($result); |
|
71
|
|
|
|
|
72
|
|
|
return <<<CONFIG |
|
73
|
|
|
<script> |
|
74
|
|
|
window[$idJson] = $resultJson; |
|
75
|
|
|
</script> |
|
76
|
|
|
CONFIG; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Collect translation keys from the specified XLIFF file and translate them |
|
81
|
|
|
* using the current language. |
|
82
|
|
|
* |
|
83
|
|
|
* Keys of the result array: |
|
84
|
|
|
* - locale: Locale identifier of current site language |
|
85
|
|
|
* - twoLetterIsoCode: Two-letter ISO code of current site language |
|
86
|
|
|
* - phrases: Map from translation keys to their translations |
|
87
|
|
|
*/ |
|
88
|
|
|
private static function getTranslations(string $baseTranslationFile) |
|
89
|
|
|
{ |
|
90
|
|
|
$language = $GLOBALS['TYPO3_REQUEST']->getAttribute('language'); |
|
91
|
|
|
$languageKey = $language->getTypo3Language(); |
|
92
|
|
|
|
|
93
|
|
|
// Grab available translation keys from the specified file |
|
94
|
|
|
$localizationFactory = GeneralUtility::makeInstance(LocalizationFactory::class); |
|
95
|
|
|
$translationKeys = array_keys($localizationFactory->getParsedData($baseTranslationFile, 'default')['default']); |
|
96
|
|
|
|
|
97
|
|
|
// Translate each available key as per the current language. |
|
98
|
|
|
// - This falls back to default language if a key isn't translated |
|
99
|
|
|
// - Pass $languageKey to ensure that translation matches ISO code |
|
100
|
|
|
$phrases = []; |
|
101
|
|
|
foreach ($translationKeys as $transKey) { |
|
102
|
|
|
$phrases[$transKey] = LocalizationUtility::translate( |
|
103
|
|
|
"LLL:$baseTranslationFile:$transKey", |
|
104
|
|
|
/* extensionName= */null, |
|
105
|
|
|
/* arguments= */null, |
|
106
|
|
|
$languageKey |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return [ |
|
111
|
|
|
'locale' => $language->getLocale(), |
|
112
|
|
|
'twoLetterIsoCode' => $language->getTwoLetterIsoCode(), |
|
113
|
|
|
'phrases' => $phrases, |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|