0d5ba40a6f0f4362622323b4c62f587c19e2413d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / AudioTagRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Rendering;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\FileReference;
19
20 /**
21 * Class AudioTagRenderer
22 */
23 class AudioTagRenderer implements FileRendererInterface
24 {
25 /**
26 * Mime types that can be used in the HTML Video tag
27 *
28 * @var array
29 */
30 protected $possibleMimeTypes = ['audio/mpeg', 'audio/wav', 'audio/ogg'];
31
32 /**
33 * Returns the priority of the renderer
34 * This way it is possible to define/overrule a renderer
35 * for a specific file type/context.
36 * For example create a video renderer for a certain storage/driver type.
37 * Should be between 1 and 100, 100 is more important than 1
38 *
39 * @return int
40 */
41 public function getPriority()
42 {
43 return 1;
44 }
45
46 /**
47 * Check if given File(Reference) can be rendered
48 *
49 * @param FileInterface $file File or FileReference to render
50 * @return bool
51 */
52 public function canRender(FileInterface $file)
53 {
54 return in_array($file->getMimeType(), $this->possibleMimeTypes, true);
55 }
56
57 /**
58 * Render for given File(Reference) HTML output
59 *
60 * @param FileInterface $file
61 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
62 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
63 * @param array $options controls = TRUE/FALSE (default TRUE), autoplay = TRUE/FALSE (default FALSE), loop = TRUE/FALSE (default FALSE)
64 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
65 * @return string
66 */
67 public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
68 {
69
70 // If autoplay isn't set manually check if $file is a FileReference take autoplay from there
71 if (!isset($options['autoplay']) && $file instanceof FileReference) {
72 $autoplay = $file->getProperty('autoplay');
73 if ($autoplay !== null) {
74 $options['autoplay'] = $autoplay;
75 }
76 }
77
78 $additionalAttributes = [];
79 if (!isset($options['controls']) || !empty($options['controls'])) {
80 $additionalAttributes[] = 'controls';
81 }
82 if (!empty($options['autoplay'])) {
83 $additionalAttributes[] = 'autoplay';
84 }
85 if (!empty($options['muted'])) {
86 $additionalAttributes[] = 'muted';
87 }
88 if (!empty($options['loop'])) {
89 $additionalAttributes[] = 'loop';
90 }
91 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'preload', 'controlsList'] as $key) {
92 if (!empty($options[$key])) {
93 $additionalAttributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
94 }
95 }
96
97 return sprintf(
98 '<audio%s><source src="%s" type="%s"></audio>',
99 empty($additionalAttributes) ? '' : ' ' . implode(' ', $additionalAttributes),
100 htmlspecialchars($file->getPublicUrl($usedPathsRelativeToCurrentScript)),
101 $file->getMimeType()
102 );
103 }
104 }