[TASK] Replace icon "status-status-permission-denied" with the new IconFactory
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / ViewHelpers / PermissionsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Beuser\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\Backend\Utility\IconUtility;
18 use TYPO3\CMS\Core\Imaging\IconFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
21 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
24
25 /**
26 * Render permission icon group (user / group / others) of the "Access" module.
27 *
28 * Most of that could be done in fluid directly, but this view helper
29 * is much better performance wise.
30 */
31 class PermissionsViewHelper extends AbstractViewHelper implements CompilableInterface {
32
33 /**
34 * @var string Cached Css classes for a "granted" icon
35 */
36 static protected $grantedCssClasses = '';
37
38 /**
39 * @var string Cached Css classes for a "denied" icon
40 */
41 static protected $deniedCssClasses = '';
42
43 /**
44 * @var array Cached labels for a single permission mask like "Delete page"
45 */
46 static protected $permissionLabels = array();
47
48 /**
49 * Return permissions.
50 *
51 * @param int $permission Current permission
52 * @param string $scope "user" / "group" / "everybody"
53 * @param int $pageId
54 * @return string
55 */
56 public function render($permission, $scope, $pageId) {
57 return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
58 }
59
60 /**
61 * Implementing CompilableInterface suppresses object instantiation of this view helper
62 *
63 * @param array $arguments
64 * @param \Closure $renderChildrenClosure
65 * @param RenderingContextInterface $renderingContext
66 * @return string
67 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
68 */
69 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
70 // The two main icon classes are static during one call. They trigger relatively expensive
71 // calculation with a signal and object creation and thus make sense to have them cached.
72 /** @var IconFactory $iconFactory */
73 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
74 if (!static::$grantedCssClasses) {
75 static::$grantedCssClasses = IconUtility::getSpriteIconClasses('status-status-permission-granted');
76 }
77 if (!static::$deniedCssClasses) {
78 static::$deniedCssClasses = $iconFactory->getIcon('status-status-permission-denied')->render();
79 }
80
81 $masks = array(1, 16, 2, 4, 8);
82
83 if (empty(static::$permissionLabels)) {
84 foreach ($masks as $mask) {
85 static::$permissionLabels[$mask] = LocalizationUtility::translate(
86 'LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $mask,
87 'be_user'
88 );
89 }
90 }
91
92 $icon = '';
93 foreach ($masks as $mask) {
94 if ($arguments['permission'] & $mask) {
95 $icon .= '<span' .
96 ' title="' . static::$permissionLabels[$mask] . '"' .
97 ' class="' . static::$grantedCssClasses . ' change-permission text-success"' .
98 ' data-page="' . $arguments['pageId'] . '"' .
99 ' data-permissions="' . $arguments['permission'] . '"' .
100 ' data-mode="delete"' .
101 ' data-who="' . $arguments['scope'] . '"' .
102 ' data-bits="' . $mask . '"' .
103 ' style="cursor:pointer"' .
104 '></span>';
105 } else {
106 $icon .= '<span' .
107 ' title="' . static::$permissionLabels[$mask] . '"' .
108 ' class="' . static::$deniedCssClasses . ' change-permission text-danger"' .
109 ' data-page="' . $arguments['pageId'] . '"' .
110 ' data-permissions="' . $arguments['permission'] . '"' .
111 ' data-mode="add"' .
112 ' data-who="' . $arguments['scope'] . '"' .
113 ' data-bits="' . $mask . '"' .
114 ' style="cursor:pointer"' .
115 '></span>';
116 }
117 }
118
119 return '<span id="' . $arguments['pageId'] . '_' . $arguments['scope'] . '">' . $icon . '</span>';
120 }
121
122 }