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