[BUGFIX] Do not use Fluid legacy classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Be / InfoboxViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Be;
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\MathUtility;
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20 /**
21 * View helper for rendering a styled content infobox markup.
22 *
23 * = States =
24 *
25 * The Infobox provides different context sensitive states that
26 * can be used to provide an additional visual feedback to the
27 * to the user to underline the meaning of the information.
28 *
29 * Possible values are in range from -2 to 2. Please choose a
30 * meaningful value from the following list.
31 *
32 * -2: Notices (Default)
33 * -1: Information
34 * 0: Positive feedback
35 * 1: Warnings
36 * 2: Error
37 *
38 * = Examples =
39 *
40 * <code title="Simple">
41 * <f:be.infobox title="Message title">your box content</f:be.infobox>
42 * </code>
43 *
44 * <code title="All options">
45 * <f:be.infobox title="Message title" message="your box content" state="-2" iconName="check" disableIcon="TRUE" />
46 * </code>
47 *
48 * @api
49 */
50 class InfoboxViewHelper extends AbstractViewHelper
51 {
52 const STATE_NOTICE = -2;
53 const STATE_INFO = -1;
54 const STATE_OK = 0;
55 const STATE_WARNING = 1;
56 const STATE_ERROR = 2;
57
58 /**
59 * @var bool
60 */
61 protected $escapeOutput = false;
62
63 /**
64 * @param string $title The title of the info box
65 * @param string $message The message of the info box, if NULL tag content is used
66 * @param int $state The state of the box, InfoboxViewHelper::STATE_*
67 * @param string $iconName The icon name from font awesome, NULL sets default icon
68 * @param bool $disableIcon If set to TRUE, the icon is not rendered.
69 *
70 * @return string
71 */
72 public function render($title = null, $message = null, $state = self::STATE_NOTICE, $iconName = null, $disableIcon = false)
73 {
74 return static::renderStatic(
75 [
76 'title' => $title,
77 'message' => $message,
78 'state' => $state,
79 'iconName' => $iconName,
80 'disableIcon' => $disableIcon
81 ],
82 $this->buildRenderChildrenClosure(),
83 $this->renderingContext
84 );
85 }
86
87 /**
88 * @param array $arguments
89 * @param \Closure $renderChildrenClosure
90 * @param RenderingContextInterface $renderingContext
91 *
92 * @return string
93 */
94 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
95 {
96 $title = $arguments['title'];
97 $message = $arguments['message'];
98 $state = $arguments['state'];
99 $isInRange = MathUtility::isIntegerInRange($state, -2, 2);
100 if (!$isInRange) {
101 $state = -2;
102 }
103
104 $iconName = $arguments['iconName'];
105 $disableIcon = $arguments['disableIcon'];
106 if ($message === null) {
107 $messageTemplate = $renderChildrenClosure();
108 } else {
109 $messageTemplate = htmlspecialchars($message);
110 }
111 $classes = [
112 self::STATE_NOTICE => 'notice',
113 self::STATE_INFO => 'info',
114 self::STATE_OK => 'success',
115 self::STATE_WARNING => 'warning',
116 self::STATE_ERROR => 'danger'
117 ];
118 $icons = [
119 self::STATE_NOTICE => 'lightbulb-o',
120 self::STATE_INFO => 'info',
121 self::STATE_OK => 'check',
122 self::STATE_WARNING => 'exclamation',
123 self::STATE_ERROR => 'times'
124 ];
125 $stateClass = $classes[$state];
126 $icon = $icons[$state];
127 if ($iconName !== null) {
128 $icon = $iconName;
129 }
130 $iconTemplate = '';
131 if (!$disableIcon) {
132 $iconTemplate = '' .
133 '<div class="media-left">' .
134 '<span class="fa-stack fa-lg callout-icon">' .
135 '<i class="fa fa-circle fa-stack-2x"></i>' .
136 '<i class="fa fa-' . htmlspecialchars($icon) . ' fa-stack-1x"></i>' .
137 '</span>' .
138 '</div>';
139 }
140 $titleTemplate = '';
141 if ($title !== null) {
142 $titleTemplate = '<h4 class="callout-title">' . htmlspecialchars($title) . '</h4>';
143 }
144 return '<div class="callout callout-' . htmlspecialchars($stateClass) . '">' .
145 '<div class="media">' .
146 $iconTemplate .
147 '<div class="media-body">' .
148 $titleTemplate .
149 '<div class="callout-body">' . $messageTemplate . '</div>' .
150 '</div>' .
151 '</div>' .
152 '</div>';
153 }
154 }