|
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 playerTranslations |
|
43
|
|
|
$allowedKeys = ['shareButtons', 'screenshotCaptions', 'constants', 'equalizer']; |
|
44
|
|
|
$result = array_intersect_key($inputSettings, array_flip($allowedKeys)); |
|
45
|
|
|
|
|
46
|
|
|
// Add translations |
|
47
|
|
|
$translationBaseFiles = $inputSettings['playerTranslations']['baseFile'] ?? 'EXT:dlf/Resources/Private/Language/locallang_media.xlf'; |
|
48
|
|
|
if (!is_array($translationBaseFiles)) { |
|
49
|
|
|
$translationBaseFiles = [$translationBaseFiles]; |
|
50
|
|
|
} |
|
51
|
|
|
$result['lang'] = self::getTranslations($translationBaseFiles); |
|
52
|
|
|
|
|
53
|
|
|
// Resolve paths |
|
54
|
|
|
foreach ($result['shareButtons'] ?? [] as $key => $button) { |
|
55
|
|
|
// For Flexforms-configured button |
|
56
|
|
|
if (isset($button['singleButton'])) { |
|
57
|
|
|
$button = $button['singleButton']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($button['type'] === 'image') { |
|
61
|
|
|
$filePath = GeneralUtility::getFileAbsFileName($button['src']); |
|
62
|
|
|
$webPath = PathUtility::getAbsoluteWebPath($filePath); |
|
63
|
|
|
|
|
64
|
|
|
$button['src'] = $webPath; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$result['shareButtons'][$key] = $button; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Allow using (and overriding) non-numeric keys for shareButtons |
|
71
|
|
|
$result['shareButtons'] = array_values($result['shareButtons'] ?? []); |
|
72
|
|
|
|
|
73
|
|
|
// Equalizer configuration |
|
74
|
|
|
foreach ($result['equalizer']['presets'] ?? [] as $key => &$preset) { |
|
|
|
|
|
|
75
|
|
|
$result['equalizer']['presets'][$key]['key'] = $key; |
|
76
|
|
|
} |
|
77
|
|
|
$result['equalizer']['presets'] = array_values($result['equalizer']['presets'] ?? []); |
|
78
|
|
|
|
|
79
|
|
|
$idJson = json_encode($id); |
|
80
|
|
|
$resultJson = json_encode($result); |
|
81
|
|
|
|
|
82
|
|
|
return <<<CONFIG |
|
83
|
|
|
<script> |
|
84
|
|
|
window[$idJson] = $resultJson; |
|
85
|
|
|
</script> |
|
86
|
|
|
CONFIG; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Collect translation keys from the specified XLIFF file and translate them |
|
91
|
|
|
* using the current language. |
|
92
|
|
|
* |
|
93
|
|
|
* Keys of the result array: |
|
94
|
|
|
* - locale: Locale identifier of current site language |
|
95
|
|
|
* - twoLetterIsoCode: Two-letter ISO code of current site language |
|
96
|
|
|
* - phrases: Map from translation keys to their translations |
|
97
|
|
|
*/ |
|
98
|
|
|
private static function getTranslations(array $translationBaseFiles) |
|
99
|
|
|
{ |
|
100
|
|
|
$language = $GLOBALS['TYPO3_REQUEST']->getAttribute('language'); |
|
101
|
|
|
$languageKey = $language->getTypo3Language(); |
|
102
|
|
|
$phrases = []; |
|
103
|
|
|
|
|
104
|
|
|
$localizationFactory = GeneralUtility::makeInstance(LocalizationFactory::class); |
|
105
|
|
|
|
|
106
|
|
|
// TODO: Wouldn't there be a TypoScript utility? |
|
107
|
|
|
ksort($translationBaseFiles); |
|
108
|
|
|
foreach ($translationBaseFiles as $filename) { |
|
109
|
|
|
// Grab available translation keys from the specified file |
|
110
|
|
|
$translationKeys = array_keys($localizationFactory->getParsedData($filename, 'default')['default']); |
|
111
|
|
|
|
|
112
|
|
|
// Translate each available key as per the current language. |
|
113
|
|
|
// - This falls back to default language if a key isn't translated |
|
114
|
|
|
// - Pass $languageKey to ensure that translation matches ISO code |
|
115
|
|
|
foreach ($translationKeys as $transKey) { |
|
116
|
|
|
$phrases[$transKey] = LocalizationUtility::translate( |
|
117
|
|
|
"LLL:$filename:$transKey", |
|
118
|
|
|
/* extensionName= */null, |
|
119
|
|
|
/* arguments= */null, |
|
120
|
|
|
$languageKey |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return [ |
|
126
|
|
|
'locale' => $language->getLocale(), |
|
127
|
|
|
'twoLetterIsoCode' => $language->getTwoLetterIsoCode(), |
|
128
|
|
|
'phrases' => $phrases, |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Let?s assume that you have the following
foreachstatement:$itemValueis assigned by reference. This is possible because the expression (in the example$array) can be used as a reference target.However, if we were to replace
$arraywith something different like the result of a function call as inthen assigning by reference is not possible anymore as there is no target that could be modified.
Available Fixes
1. Do not assign by reference
2. Assign to a local variable first
3. Return a reference