4ca27bee4ba411015f6f8582181c86712d2b6d7b
[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\Rendering\RenderingContextInterface;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
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 {
41 return static::renderStatic(
42 array(
43 'documentTranslation' => $documentTranslation,
44 ),
45 $this->buildRenderChildrenClosure(),
46 $this->renderingContext
47 );
48 }
49
50 /**
51 * Statically renders all format download links.
52 *
53 * @param array $arguments
54 * @param callable $renderChildrenClosure
55 * @param RenderingContextInterface $renderingContext
56 *
57 * @return string
58 */
59 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
60 {
61 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation $documentTranslation */
62 $documentTranslation = $arguments['documentTranslation'];
63
64 /** @var IconFactory $iconFactory */
65 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
66 $emptyIcon = $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render();
67 $icons = array(
68 'html' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>',
69 'pdf' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>',
70 'sxw' => '<a class="btn btn-default disabled">' . $emptyIcon . '</a>'
71 );
72 $formats = $documentTranslation->getFormats();
73
74 foreach ($formats as $format) {
75 $output = '';
76 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentFormat $format */
77 $output .= '<a ';
78
79 $uri = '../' . $format->getPath();
80 $documentFormat = $format->getFormat();
81 $extension = substr($uri, strrpos($uri, '.') + 1);
82 if (strlen($extension) < 5) {
83 // This is direct link to a file
84 $output .= 'href="' . $uri . '" class="btn btn-default"';
85 $iconHtml = static::getIconForFileExtension($extension, $iconFactory);
86 } else {
87 $output .= 'href="#" onclick="top.TYPO3.Backend.ContentContainer.setUrl(' . GeneralUtility::quoteJSvalue($uri) . ')" class="btn btn-default"';
88 $iconHtml = static::getIconForFileExtension($documentFormat, $iconFactory);
89 }
90
91 $xliff = 'LLL:EXT:documentation/Resources/Private/Language/locallang.xlf';
92 $title = sprintf(
93 $GLOBALS['LANG']->sL($xliff . ':tx_documentation_domain_model_documentformat.format.title'),
94 $documentFormat
95 );
96 $output .= ' title="' . htmlspecialchars($title) . '">';
97 $output .= $iconHtml . '</a>' . LF;
98 if ($documentFormat === 'json') {
99 // It should take over the place of sxw which will then never be used
100 $documentFormat = 'sxw';
101 }
102 $icons[$documentFormat] = $output;
103 }
104 return implode('', array_values($icons));
105 }
106
107 /**
108 * Returns the icon associated to a given file extension (privileging black and white).
109 *
110 * @param IconFactory $iconFactory
111 * @param string $extension
112 * @return string
113 */
114 protected static function getIconForFileExtension($extension, IconFactory $iconFactory)
115 {
116 switch ($extension) {
117 case 'html':
118 case 'pdf':
119 $iconHtml = $iconFactory->getIcon('actions-file-' . $extension, Icon::SIZE_SMALL)->render();
120 break;
121 case 'sxw':
122 $iconHtml = $iconFactory->getIcon('actions-file-openoffice', Icon::SIZE_SMALL)->render();
123 break;
124 case 'json':
125 $iconHtml = $iconFactory->getIcon('actions-system-extension-documentation', Icon::SIZE_SMALL)->render();
126 break;
127 default:
128 $iconHtml = $iconFactory->getIconForFileExtension($extension, Icon::SIZE_SMALL)->render();;
129 }
130 return $iconHtml;
131 }
132 }