[TASK] Extend markup of icons
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / Icon.php
1 <?php
2 namespace TYPO3\CMS\Core\Imaging;
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\Type\Icon\IconState;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Icon object, holds all information for one icon, identified by the "identifier" property.
22 * Is available to render itself as string.
23 */
24 class Icon
25 {
26 /**
27 * @var string the small size
28 */
29 const SIZE_SMALL = 'small'; // 16
30
31 /**
32 * @var string the default size
33 */
34 const SIZE_DEFAULT = 'default'; // 32
35
36 /**
37 * @var string the large size
38 */
39 const SIZE_LARGE = 'large'; // 48
40
41 /**
42 * @internal
43 * @var string the overlay size, which depends on icon size
44 */
45 const SIZE_OVERLAY = 'overlay';
46
47 /**
48 * The identifier which the PHP code that calls the IconFactory hands over
49 *
50 * @var string
51 */
52 protected $identifier;
53
54 /**
55 * The identifier for a possible overlay icon
56 *
57 * @var Icon
58 */
59 protected $overlayIcon = null;
60
61 /**
62 * Contains the size string ("large", "small" or "default")
63 *
64 * @var string
65 */
66 protected $size = '';
67
68 /**
69 * Flag to indicate if the icon has a spinning animation
70 *
71 * @var bool
72 */
73 protected $spinning = false;
74
75 /**
76 * Contains the state information
77 *
78 * @var IconState
79 */
80 protected $state;
81
82 /**
83 * @var Dimension
84 */
85 protected $dimension;
86
87 /**
88 * @var string
89 */
90 protected $markup;
91
92 /**
93 * @var array
94 */
95 protected $alternativeMarkups = array();
96
97 /**
98 * @internal this method is used for internal processing, to get the prepared and final markup use render()
99 *
100 * @param string $alternativeMarkupIdentifier
101 *
102 * @return string
103 */
104 public function getMarkup($alternativeMarkupIdentifier = null)
105 {
106 if ($alternativeMarkupIdentifier !== null && isset($this->alternativeMarkups[$alternativeMarkupIdentifier])) {
107 return $this->alternativeMarkups[$alternativeMarkupIdentifier];
108 }
109 return $this->markup;
110 }
111
112 /**
113 * @param string $markup
114 */
115 public function setMarkup($markup)
116 {
117 $this->markup = $markup;
118 }
119
120 /**
121 * @param string $markupIdentifier
122 *
123 * @return string
124 */
125 public function getAlternativeMarkup($markupIdentifier)
126 {
127 return $this->alternativeMarkups[$markupIdentifier];
128 }
129
130 /**
131 * @param string $markupIdentifier
132 * @param string $markup
133 */
134 public function setAlternativeMarkup($markupIdentifier, $markup)
135 {
136 $this->alternativeMarkups[$markupIdentifier] = $markup;
137 }
138
139 /**
140 * @return string
141 */
142 public function getIdentifier()
143 {
144 return $this->identifier;
145 }
146
147 /**
148 * @param string $identifier
149 */
150 public function setIdentifier($identifier)
151 {
152 $this->identifier = $identifier;
153 }
154
155 /**
156 * @return Icon
157 */
158 public function getOverlayIcon()
159 {
160 return $this->overlayIcon;
161 }
162
163 /**
164 * @param Icon $overlayIcon
165 */
166 public function setOverlayIcon($overlayIcon)
167 {
168 $this->overlayIcon = $overlayIcon;
169 }
170
171 /**
172 * @return string
173 */
174 public function getSize()
175 {
176 return $this->size;
177 }
178
179 /**
180 * Sets the size and creates the new dimension
181 *
182 * @param string $size
183 */
184 public function setSize($size)
185 {
186 $this->size = $size;
187 $this->dimension = GeneralUtility::makeInstance(Dimension::class, $size);
188 }
189
190 /**
191 * @return bool
192 */
193 public function isSpinning()
194 {
195 return $this->spinning;
196 }
197
198 /**
199 * @param bool $spinning
200 */
201 public function setSpinning($spinning)
202 {
203 $this->spinning = $spinning;
204 }
205
206 /**
207 * @return IconState
208 */
209 public function getState()
210 {
211 return $this->state;
212 }
213
214 /**
215 * Sets the state of the icon
216 *
217 * @param IconState $state
218 */
219 public function setState(IconState $state)
220 {
221 $this->state = $state;
222 }
223
224 /**
225 * @return Dimension
226 */
227 public function getDimension()
228 {
229 return $this->dimension;
230 }
231
232 /**
233 * Render the icon as HTML code
234 *
235 * @param string $alternativeMarkupIdentifier
236 *
237 * @return string
238 */
239 public function render($alternativeMarkupIdentifier = null)
240 {
241 $overlayIconMarkup = '';
242 if ($this->overlayIcon !== null) {
243 $overlayIconMarkup = '<span class="icon-overlay icon-' . htmlspecialchars($this->overlayIcon->getIdentifier()) . '">' . $this->overlayIcon->getMarkup() . '</span>';
244 }
245 return str_replace('{overlayMarkup}', $overlayIconMarkup, $this->wrappedIcon($alternativeMarkupIdentifier));
246 }
247
248 /**
249 * Render the icon as HTML code
250 *
251 * @return string
252 */
253 public function __toString()
254 {
255 return $this->render();
256 }
257
258 /**
259 * Wrap icon markup in unified HTML code
260 *
261 * @param string $alternativeMarkupIdentifier
262 *
263 * @return string
264 */
265 protected function wrappedIcon($alternativeMarkupIdentifier = null)
266 {
267 $classes = array();
268 $classes[] = 't3js-icon';
269 $classes[] = 'icon';
270 $classes[] = 'icon-size-' . $this->size;
271 $classes[] = 'icon-state-' . htmlspecialchars((string)$this->state);
272 $classes[] = 'icon-' . $this->getIdentifier();
273 if ($this->isSpinning()) {
274 $classes[] = 'icon-spin';
275 }
276
277 $markup = array();
278 $markup[] = '<span class="' . htmlspecialchars(implode(' ', $classes)) . '" data-identifier="' . htmlspecialchars($this->getIdentifier()) . '">';
279 $markup[] = ' <span class="icon-markup">';
280 $markup[] = $this->getMarkup($alternativeMarkupIdentifier);
281 $markup[] = ' </span>';
282 $markup[] = ' {overlayMarkup}';
283 $markup[] = '</span>';
284
285 return implode(LF, $markup);
286 }
287 }