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