e3efd9cd63b5da779e1522d526e7920f9c10ea83
[Packages/TYPO3.CMS.git] / typo3 / sysext / documentation / Classes / ViewHelpers / FormatsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Documentation\ViewHelpers;
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\Imaging\Icon;
18 use TYPO3\CMS\Core\Imaging\IconFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
23
24 /**
25 * ViewHelper to display all download links for a document
26 *
27 * Example: <doc:formats document="{document}" />
28 *
29 * @internal
30 */
31 class FormatsViewHelper extends AbstractViewHelper implements CompilableInterface {
32
33 /**
34 * Renders all format download links.
35 *
36 * @param \TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation $documentTranslation
37 * @return string
38 */
39 public function render(\TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation $documentTranslation) {
40 return static::renderStatic(
41 array(
42 'documentTranslation' => $documentTranslation,
43 ),
44 $this->buildRenderChildrenClosure(),
45 $this->renderingContext
46 );
47 }
48
49 /**
50 * @param array $arguments
51 * @param callable $renderChildrenClosure
52 * @param RenderingContextInterface $renderingContext
53 *
54 * @return string
55 */
56 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
57 $documentTranslation = $arguments['documentTranslation'];
58
59 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
60 $emptyIcon = $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render();
61 $icons = array(
62 'html' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>',
63 'pdf' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>',
64 'sxw' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>'
65 );
66 $formats = $documentTranslation->getFormats();
67
68 foreach ($formats as $format) {
69 $output = '';
70 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentFormat $format */
71 $output .= '<a ';
72
73 $uri = '../' . $format->getPath();
74 $extension = substr($uri, strrpos($uri, '.') + 1);
75 if (strlen($extension) < 5) {
76 // This is direct link to a file
77 $output .= 'href="' . $uri . '" class="btn btn-default"';
78 } else {
79 $extension = $format->getFormat();
80 if ($extension === 'json') {
81 $extension = 'js';
82 }
83 $output .= 'href="#" onclick="top.TYPO3.Backend.ContentContainer.setUrl(' . GeneralUtility::quoteJSvalue($uri) . ')" class="btn btn-default"';
84 }
85
86 $xliff = 'LLL:EXT:documentation/Resources/Private/Language/locallang.xlf';
87 $title = sprintf(
88 $GLOBALS['LANG']->sL($xliff . ':tx_documentation_domain_model_documentformat.format.title'),
89 $format->getFormat()
90 );
91 $output .= ' title="' . htmlspecialchars($title) . '">';
92 $spriteIconHtml = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile($extension);
93 $output .= $spriteIconHtml . '</a>' . LF;
94 $keyFormat = $format->getFormat();
95 if ($keyFormat === 'json') {
96 // It should take over the place of sxw which will then never be used
97 $keyFormat = 'sxw';
98 }
99 $icons[$keyFormat] = $output;
100 }
101 return implode('', array_values($icons));
102 }
103
104 }