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