[BUGFIX] Remove usage of fluid legacy classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / DistributionImageViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Core\Utility\PathUtility;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
19
20 /**
21 * Renders the distribution image
22 *
23 * @internal
24 */
25 class DistributionImageViewHelper extends AbstractTagBasedViewHelper
26 {
27 /**
28 * @var string
29 */
30 protected $tagName = 'img';
31
32 /**
33 * Initialize arguments.
34 */
35 public function initializeArguments()
36 {
37 parent::initializeArguments();
38 $this->registerArgument('extensionkey', 'string', '', true);
39 $this->registerArgument('width', 'int', 'width of the image');
40 $this->registerArgument('height', 'int', 'height of the image');
41 $this->registerUniversalTagAttributes();
42 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
43 }
44
45 /**
46 * Renders the destribution preview image for the given extension
47 *
48 * @return string rendered tag.
49 */
50 public function render()
51 {
52 $extensionKey = $this->arguments['extensionkey'];
53 $width = $this->arguments['width'];
54 $height = $this->arguments['height'];
55 $content = '';
56
57 $src = $this->findImage($extensionKey);
58 $uri = $this->getImageUri($src);
59
60 if (!empty($uri)) {
61 if ($width) {
62 $this->tag->addAttribute('width', (int)$width);
63 }
64 if ($height) {
65 $this->tag->addAttribute('height', (int)$height);
66 }
67 $this->tag->addAttribute('src', $uri);
68 $content = $this->tag->render();
69 }
70
71 return $content;
72 }
73
74 /**
75 * Find the distrubution image
76 *
77 * @param string $extensionKey
78 * @return string
79 */
80 protected function findImage($extensionKey)
81 {
82 $paths = [];
83 $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.svg';
84 $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.png';
85 $paths[] = 'EXT:extensionmanager/Resources/Public/Images/Distribution.svg';
86
87 foreach ($paths as $path) {
88 $absFileName = GeneralUtility::getFileAbsFileName($path);
89 if (file_exists($absFileName)) {
90 return $absFileName;
91 }
92 }
93
94 return '';
95 }
96
97 /**
98 * Get image uri
99 *
100 * @param string $src
101 * @return string
102 */
103 protected function getImageUri($src)
104 {
105 $uri = GeneralUtility::getFileAbsFileName($src);
106 if ($uri !== '' && !file_exists($uri)) {
107 $uri = '';
108 }
109 if ($uri !== '') {
110 $uri = '../' . PathUtility::stripPathSitePrefix($uri);
111 }
112 return $uri;
113 }
114 }