[FEATURE] Add spinning feature for 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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * Icon object, holds all information for one icon, identified by the "identifier" property.
20 * Is available to render itself as string.
21 */
22 class Icon {
23
24 /**
25 * @var string the small size
26 */
27 const SIZE_SMALL = 'small'; // 16
28
29 /**
30 * @var string the default size
31 */
32 const SIZE_DEFAULT = 'default'; // 32
33
34 /**
35 * @var string the large size
36 */
37 const SIZE_LARGE = 'large'; // 48
38
39 /**
40 * @internal
41 * @var string the overlay size, which depends on icon size
42 */
43 const SIZE_OVERLAY = 'overlay';
44
45 /**
46 * The identifier which the PHP code that calls the IconFactory hands over
47 * @var string
48 */
49 protected $identifier;
50
51 /**
52 * The identifier for a possible overlay icon
53 * @var Icon
54 */
55 protected $overlayIcon = NULL;
56
57 /**
58 * Contains the size string ("large", "small" or "default")
59 * @var string
60 */
61 protected $size = '';
62
63 /**
64 * Flag to indicate if the icon has a spinning animation
65 * @var bool
66 */
67 protected $spinning = FALSE;
68
69 /**
70 * @var Dimension
71 */
72 protected $dimension;
73
74 /**
75 * @var string
76 */
77 protected $markup;
78
79 /**
80 * @internal this method is used for internal processing, to get the prepared and final markup use render()
81 * @return string
82 */
83 public function getMarkup() {
84 return $this->markup;
85 }
86
87 /**
88 * @param string $markup
89 */
90 public function setMarkup($markup) {
91 $this->markup = $markup;
92 }
93
94 /**
95 * @return string
96 */
97 public function getIdentifier() {
98 return $this->identifier;
99 }
100
101 /**
102 * @param string $identifier
103 */
104 public function setIdentifier($identifier) {
105 $this->identifier = $identifier;
106 }
107
108 /**
109 * @return Icon
110 */
111 public function getOverlayIcon() {
112 return $this->overlayIcon;
113 }
114
115 /**
116 * @param Icon $overlayIcon
117 */
118 public function setOverlayIcon($overlayIcon) {
119 $this->overlayIcon = $overlayIcon;
120 }
121
122 /**
123 * @return string
124 */
125 public function getSize() {
126 return $this->size;
127 }
128
129 /**
130 * Sets the size and creates the new dimension
131 * @param string $size
132 */
133 public function setSize($size) {
134 $this->size = $size;
135 $this->dimension = GeneralUtility::makeInstance(Dimension::class, $size);
136 }
137
138 /**
139 * @return boolean
140 */
141 public function isSpinning() {
142 return $this->spinning;
143 }
144
145 /**
146 * @param boolean $spinning
147 */
148 public function setSpinning($spinning) {
149 $this->spinning = $spinning;
150 }
151
152 /**
153 * @return Dimension
154 */
155 public function getDimension() {
156 return $this->dimension;
157 }
158
159 /**
160 * Render the icon as HTML code
161 *
162 * @return string
163 */
164 public function render() {
165 return $this->__toString();
166 }
167
168 /**
169 * Render the icon as HTML code
170 *
171 * @return string
172 */
173 public function __toString() {
174 $overlayIconMarkup = '';
175 if ($this->overlayIcon !== NULL) {
176 $overlayIconMarkup = '<span class="icon-overlay icon-' . htmlspecialchars($this->overlayIcon->getIdentifier()) . '">' . $this->overlayIcon->getMarkup() . '</span>';
177 }
178 return str_replace('{overlayMarkup}', $overlayIconMarkup, $this->wrappedIcon());
179 }
180
181 /**
182 * Wrap icon markup in unified HTML code
183 *
184 * @return string
185 */
186 protected function wrappedIcon() {
187 $classes = array();
188 $classes[] = 'icon';
189 $classes[] = 'icon-size-' . $this->size;
190 $classes[] = 'icon-' . $this->getIdentifier();
191 if ($this->isSpinning()) {
192 $classes[] = 'icon-spin';
193 }
194
195 $markup = array();
196 $markup[] = '<span class="' . htmlspecialchars(implode(' ', $classes)) . '">';
197 $markup[] = ' <span class="icon-markup">';
198 $markup[] = $this->getMarkup();
199 $markup[] = ' </span>';
200 $markup[] = ' {overlayMarkup}';
201 $markup[] = '</span>';
202
203 return implode(LF, $markup);
204 }
205 }