ae17fbc4a466352987edca9e9b5ecaaeb020a6eb
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Utility / IconUtility.php
1 <?php
2 namespace TYPO3\CMS\Backend\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Core\Versioning\VersionState;
32
33 /**
34 * Contains class for icon generation in the backend
35 * This library has functions that returns - and if necessary creates - the icon for an element in TYPO3
36 *
37 * Expects global vars:
38 * - $BACK_PATH
39 * - PATH_typo3
40 * - $TCA, $PAGES_TYPES
41 *
42 *
43 * Notes:
44 * These functions are strongly related to the interface of TYPO3.
45 * The class is included in eg. init.php
46 * Static class, functions called without making a class instance.
47 *
48 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
49 */
50 class IconUtility {
51
52 static public $fileSpriteIconNames = array(
53 'htm' => 'mimetypes-text-html',
54 'html' => 'mimetypes-text-html',
55 'css' => 'mimetypes-text-css',
56 'js' => 'mimetypes-text-js',
57 'csv' => 'mimetypes-text-csv',
58 'php' => 'mimetypes-text-php',
59 'php6' => 'mimetypes-text-php',
60 'php5' => 'mimetypes-text-php',
61 'php4' => 'mimetypes-text-php',
62 'php3' => 'mimetypes-text-php',
63 'inc' => 'mimetypes-text-php',
64 'ts' => 'mimetypes-text-ts',
65 'txt' => 'mimetypes-text-text',
66 'class' => 'mimetypes-text-text',
67 'tmpl' => 'mimetypes-text-text',
68 'jpg' => 'mimetypes-media-image',
69 'jpeg' => 'mimetypes-media-image',
70 'gif' => 'mimetypes-media-image',
71 'png' => 'mimetypes-media-image',
72 'bmp' => 'mimetypes-media-image',
73 'tif' => 'mimetypes-media-image',
74 'tga' => 'mimetypes-media-image',
75 'psd' => 'mimetypes-media-image',
76 'eps' => 'mimetypes-media-image',
77 'avi' => 'mimetypes-media-video',
78 'mpg' => 'mimetypes-media-video',
79 'mpeg' => 'mimetypes-media-video',
80 'mov' => 'mimetypes-media-video',
81 'wav' => 'mimetypes-media-audio',
82 'mp3' => 'mimetypes-media-audio',
83 'mid' => 'mimetypes-media-audio',
84 'swf' => 'mimetypes-media-flash',
85 'swa' => 'mimetypes-media-flash',
86 'exe' => 'mimetypes-executable-executable',
87 'com' => 'mimetypes-executable-executable',
88 't3x' => 'mimetypes-compressed',
89 't3d' => 'mimetypes-compressed',
90 'zip' => 'mimetypes-compressed',
91 'tgz' => 'mimetypes-compressed',
92 'gz' => 'mimetypes-compressed',
93 'pdf' => 'mimetypes-pdf',
94 'doc' => 'mimetypes-word',
95 'dot' => 'mimetypes-word',
96 'docm' => 'mimetypes-word',
97 'docx' => 'mimetypes-word',
98 'dotm' => 'mimetypes-word',
99 'dotx' => 'mimetypes-word',
100 'sxw' => 'mimetypes-word',
101 'rtf' => 'mimetypes-word',
102 'xls' => 'mimetypes-excel',
103 'xlsm' => 'mimetypes-excel',
104 'xlsx' => 'mimetypes-excel',
105 'xltm' => 'mimetypes-excel',
106 'xltx' => 'mimetypes-excel',
107 'sxc' => 'mimetypes-excel',
108 'ppt' => 'mimetypes-powerpoint',
109 'pptm' => 'mimetypes-powerpoint',
110 'pptx' => 'mimetypes-powerpoint',
111 'potm' => 'mimetypes-powerpoint',
112 'potx' => 'mimetypes-powerpoint',
113 'mount' => 'apps-filetree-mount',
114 'folder' => 'apps-filetree-folder-default',
115 'default' => 'mimetypes-other-other'
116 );
117
118 /**
119 * Returns an icon image tag, 18x16 pixels, based on input information.
120 * This function is recommended to use in your backend modules.
121 *
122 * @param string $table The table name
123 * @param array $row The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
124 * @param string $backPath The backpath to the main TYPO3 directory (relative path back to PATH_typo3)
125 * @param string $params Additional attributes for the image tag
126 * @param boolean $shaded If set, the icon will be grayed/shaded
127 * @return string <img>-tag
128 * @see getIcon()
129 * @deprecated since TYPO3 6.1 will be removed in 7.0, should not be used anymore as only sprite icons are used since TYPO3 4.4
130 */
131 static public function getIconImage($table, $row = array(), $backPath, $params = '', $shaded = FALSE) {
132 GeneralUtility::logDeprecatedFunction();
133
134 $str = '<img' . self::skinImg($backPath, self::getIcon($table, $row, $shaded), 'width="18" height="16"') . (trim($params) ? ' ' . trim($params) : '');
135 if (!stristr($str, 'alt="')) {
136 $str .= ' alt=""';
137 }
138 $str .= ' />';
139 return $str;
140 }
141
142 /**
143 * Creates the icon for input table/row
144 * Returns filename for the image icon, relative to PATH_typo3
145 *
146 * @param string $table The table name
147 * @param array $row The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
148 * @param boolean $shaded If set, the icon will be grayed/shaded
149 * @return string Icon filename
150 * @see getIconImage()
151 */
152 static public function getIcon($table, $row = array(), $shaded = FALSE) {
153 // Flags
154 // If set, then the usergroup number will NOT be printed unto the icon. NOTICE.
155 // The icon is generated only if a default icon for groups is not found... So effectively this is ineffective.
156 $doNotRenderUserGroupNumber = TRUE;
157 // Shadow
158 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
159 switch (VersionState::cast($row['t3ver_state'])) {
160 case new VersionState(VersionState::NEW_PLACEHOLDER):
161 return 'gfx/i/shadow_hide.png';
162 break;
163 case new VersionState(VersionState::DELETE_PLACEHOLDER):
164 return 'gfx/i/shadow_delete.png';
165 break;
166 case new VersionState(VersionState::MOVE_PLACEHOLDER):
167 return 'gfx/i/shadow_moveto_plh.png';
168 break;
169 case new VersionState(VersionState::MOVE_POINTER):
170 return 'gfx/i/shadow_moveto_pointer.png';
171 break;
172 }
173 }
174 // First, find the icon file name. This can depend on configuration in TCA, field values and more:
175 if ($table == 'pages') {
176 $iconfile = $GLOBALS['PAGES_TYPES'][$row['doktype']]['icon'];
177 if (!$iconfile) {
178 $iconfile = $GLOBALS['PAGES_TYPES']['default']['icon'];
179 }
180 } else {
181 if (!($iconfile = $GLOBALS['TCA'][$table]['ctrl']['typeicons'][$row[$GLOBALS['TCA'][$table]['ctrl']['typeicon_column']]])) {
182 $iconfile = $GLOBALS['TCA'][$table]['ctrl']['iconfile'] ?: $table . '.gif';
183 }
184 }
185 // Setting path of iconfile if not already set. Default is "gfx/i/"
186 if (!strstr($iconfile, '/')) {
187 $iconfile = 'gfx/i/' . $iconfile;
188 }
189 // Setting the absolute path where the icon should be found as a file:
190 if (substr($iconfile, 0, 3) == '../') {
191 $absfile = PATH_site . substr($iconfile, 3);
192 } else {
193 $absfile = PATH_typo3 . $iconfile;
194 }
195 // Initializing variables, all booleans except otherwise stated:
196 $hidden = FALSE;
197 $timing = FALSE;
198 $futuretiming = FALSE;
199 // In fact an integer value
200 $user = FALSE;
201 $deleted = FALSE;
202 // Set, if a page-record (only pages!) has the extend-to-subpages flag set.
203 $protectSection = FALSE;
204 $noIconFound = $row['_NO_ICON_FOUND'] ? TRUE : FALSE;
205 // + $shaded which is also boolean!
206 // Icon state based on "enableFields":
207 if (is_array($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])) {
208 $enCols = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'];
209 // If "hidden" is enabled:
210 if ($enCols['disabled']) {
211 if ($row[$enCols['disabled']]) {
212 $hidden = TRUE;
213 }
214 }
215 // If a "starttime" is set and higher than current time:
216 if ($enCols['starttime']) {
217 if ($GLOBALS['EXEC_TIME'] < (int)$row[$enCols['starttime']]) {
218 $timing = TRUE;
219 // And if "endtime" is NOT set:
220 if ((int)$row[$enCols['endtime']] === 0) {
221 $futuretiming = TRUE;
222 }
223 }
224 }
225 // If an "endtime" is set:
226 if ($enCols['endtime']) {
227 if ((int)$row[$enCols['endtime']] > 0) {
228 if ((int)$row[$enCols['endtime']] < $GLOBALS['EXEC_TIME']) {
229 // End-timing applies at this point.
230 $timing = TRUE;
231 } else {
232 // End-timing WILL apply in the future for this element.
233 $futuretiming = TRUE;
234 }
235 }
236 }
237 // If a user-group field is set:
238 if ($enCols['fe_group']) {
239 $user = $row[$enCols['fe_group']];
240 if ($user && $doNotRenderUserGroupNumber) {
241 $user = 100;
242 }
243 }
244 }
245 // If "deleted" flag is set (only when listing records which are also deleted!)
246 if ($col = $row[$GLOBALS['TCA'][$table]['ctrl']['delete']]) {
247 $deleted = TRUE;
248 }
249 // Detecting extendToSubpages (for pages only)
250 if ($table == 'pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
251 $protectSection = TRUE;
252 }
253 // If ANY of the booleans are set it means we have to alter the icon:
254 if ($hidden || $timing || $futuretiming || $user || $deleted || $shaded || $noIconFound) {
255 $flags = '';
256 $string = '';
257 if ($deleted) {
258 $string = 'deleted';
259 $flags = 'd';
260 } elseif ($noIconFound) {
261 // This is ONLY for creating icons with "?" on easily...
262 $string = 'no_icon_found';
263 $flags = 'x';
264 } else {
265 if ($hidden) {
266 $string .= 'hidden';
267 }
268 if ($timing) {
269 $string .= 'timing';
270 }
271 if (!$string && $futuretiming) {
272 $string = 'futuretiming';
273 }
274 $flags .= ($hidden ? 'h' : '') . ($timing ? 't' : '') . ($futuretiming ? 'f' : '') . ($user ? 'u' : '') . ($protectSection ? 'p' : '') . ($shaded ? 's' : '');
275 }
276 // Create tagged icon file name:
277 $iconFileName_stateTagged = preg_replace('/.([[:alnum:]]+)$/', '__' . $flags . '.\\1', basename($iconfile));
278 // Check if tagged icon file name exists (a tagged icon means the icon base name with the flags added between body and extension of the filename, prefixed with underscore)
279 if (@is_file((dirname($absfile) . '/' . $iconFileName_stateTagged)) || @is_file(($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . '/' . dirname($iconfile) . '/' . $iconFileName_stateTagged))) {
280 // Look for [iconname]_xxxx.[ext]
281 return dirname($iconfile) . '/' . $iconFileName_stateTagged;
282 } else {
283 // Otherwise, create the icon:
284 $theRes = self::makeIcon($GLOBALS['BACK_PATH'] . $iconfile, $string, $user, $protectSection, $absfile, $iconFileName_stateTagged);
285 return $theRes;
286 }
287 } else {
288 return $iconfile;
289 }
290 }
291
292 /**
293 * Returns the src=... for the input $src value OR any alternative found in $TBE_STYLES['skinImg']
294 * Used for skinning the TYPO3 backend with an alternative set of icons
295 *
296 * @param string $backPath Current backpath to PATH_typo3 folder
297 * @param string $src Icon file name relative to PATH_typo3 folder
298 * @param string $wHattribs Default width/height, defined like 'width="12" height="14"'
299 * @param integer $outputMode Mode: 0 (zero) is default and returns src/width/height. 1 returns value of src+backpath, 2 returns value of w/h.
300 * @return string Returns ' src="[backPath][src]" [wHattribs]'
301 * @see skinImgFile()
302 */
303 static public function skinImg($backPath, $src, $wHattribs = '', $outputMode = 0) {
304 static $cachedSkinImages = array();
305 $imageId = md5($backPath . $src . $wHattribs . $outputMode);
306 if (isset($cachedSkinImages[$imageId])) {
307 return $cachedSkinImages[$imageId];
308 }
309 // Setting source key. If the icon is refered to inside an extension, we homogenize the prefix to "ext/":
310 $srcKey = preg_replace('/^(\\.\\.\\/typo3conf\\/ext|sysext|ext)\\//', 'ext/', $src);
311 // LOOKING for alternative icons:
312 if ($GLOBALS['TBE_STYLES']['skinImg'][$srcKey]) {
313 // Slower or faster with is_array()? Could be used.
314 list($src, $wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$srcKey];
315 } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg']) {
316 // Otherwise, test if auto-detection is enabled:
317 // Search for alternative icon automatically:
318 $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
319 $scaleFactor = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ?: 1;
320 // Scaling factor
321 $lookUpName = $fExt ? preg_replace('/\\.[[:alnum:]]+$/', '', $srcKey) . '.' . $fExt : $srcKey;
322 // Set filename to look for
323 if ($fExt && !@is_file(($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName))) {
324 // Fallback to original filename if icon with forced extension doesn't exists
325 $lookUpName = $srcKey;
326 }
327 // If file is found:
328 if (@is_file(($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName))) {
329 // If there is a file...
330 $iInfo = @getimagesize(($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'] . $lookUpName));
331 // Get width/height:
332 // Set $src and $wHattribs:
333 $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'] . $lookUpName;
334 $wHattribs = 'width="' . round($iInfo[0] * $scaleFactor) . '" height="' . round($iInfo[1] * $scaleFactor) . '"';
335 }
336 // In any case, set currect src / wHattrib - this way we make sure that an entry IS found next time we hit the function,
337 // regardless of whether it points to a alternative icon or just the current.
338 $GLOBALS['TBE_STYLES']['skinImg'][$srcKey] = array($src, $wHattribs);
339 }
340 // Rendering disabled (greyed) icons using _i (inactive) as name suffix ("_d" is already used)
341 $matches = array();
342 $srcBasename = basename($src);
343 if (preg_match('/(.*)_i(\\....)$/', $srcBasename, $matches)) {
344 $temp_path = dirname(PATH_thisScript) . '/';
345 if (!@is_file(($temp_path . $backPath . $src))) {
346 $srcOrg = preg_replace('/_i' . preg_quote($matches[2], '/') . '$/', $matches[2], $src);
347 $src = self::makeIcon($backPath . $srcOrg, 'disabled', 0, FALSE, $temp_path . $backPath . $srcOrg, $srcBasename);
348 }
349 }
350 // Return icon source/wHattributes:
351 $output = '';
352 switch ($outputMode) {
353 case 0:
354 $output = ' src="' . $backPath . $src . '" ' . $wHattribs;
355 break;
356 case 1:
357 $output = $backPath . $src;
358 break;
359 case 2:
360 $output = $wHattribs;
361 break;
362 }
363 $cachedSkinImages[$imageId] = $output;
364 return $output;
365 }
366
367 /***********************************
368 *
369 * Other functions
370 *
371 ***********************************/
372 /**
373 * Creates the icon file for the function getIcon()
374 *
375 * @param string $iconfile Original unprocessed Icon file, relative path to PATH_typo3
376 * @param string $mode Mode string, eg. "deleted" or "futuretiming" determining how the icon will look
377 * @param integer $user The number of the fe_group record uid if applicable
378 * @param boolean $protectSection Flag determines if the protected-section icon should be applied.
379 * @param string $absFile Absolute path to file from which to create the icon.
380 * @param string $iconFileName_stateTagged The filename that this icon should have had, basically [icon base name]_[flags].[extension] - used for part of temporary filename
381 * @return string Filename relative to PATH_typo3
382 * @access private
383 */
384 static public function makeIcon($iconfile, $mode, $user, $protectSection, $absFile, $iconFileName_stateTagged) {
385 $iconFileName = 'icon_' . GeneralUtility::shortMD5(($iconfile . '|' . $mode . '|-' . $user . '|' . $protectSection)) . '_' . $iconFileName_stateTagged . '.' . ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif');
386 $mainpath = '../typo3temp/' . $iconFileName;
387 $path = PATH_site . 'typo3temp/' . $iconFileName;
388 if (file_exists(PATH_typo3 . 'icons/' . $iconFileName)) {
389 // Returns if found in typo3/icons/
390 return 'icons/' . $iconFileName;
391 } elseif (file_exists($path)) {
392 // Returns if found in ../typo3temp/icons/
393 return $mainpath;
394 } else {
395 // Makes icon:
396 if (file_exists($absFile)) {
397 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
398 // Create image pointer, if possible
399 $im = self::imagecreatefrom($absFile);
400 if ($im < 0) {
401 return $iconfile;
402 }
403 // Converting to gray scale, dimming the icon:
404 if ($mode == 'disabled' or $mode != 'futuretiming' && $mode != 'no_icon_found' && !(!$mode && $user)) {
405 $totalImageColors = ImageColorsTotal($im);
406 for ($c = 0; $c < $totalImageColors; $c++) {
407 $cols = ImageColorsForIndex($im, $c);
408 $newcol = round(($cols['red'] + $cols['green'] + $cols['blue']) / 3);
409 $lighten = $mode == 'disabled' ? 2.5 : 2;
410 $newcol = round(255 - (255 - $newcol) / $lighten);
411 ImageColorSet($im, $c, $newcol, $newcol, $newcol);
412 }
413 }
414 // Applying user icon, if there are access control on the item:
415 if ($user) {
416 if ($user < 100) {
417 // Apply user number only if lower than 100
418 $black = ImageColorAllocate($im, 0, 0, 0);
419 imagefilledrectangle($im, 0, 0, $user > 10 ? 9 : 5, 8, $black);
420 $white = ImageColorAllocate($im, 255, 255, 255);
421 imagestring($im, 1, 1, 1, $user, $white);
422 }
423 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_group.gif');
424 if ($ol_im < 0) {
425 return $iconfile;
426 }
427 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
428 }
429 // Applying overlay based on mode:
430 if ($mode) {
431 unset($ol_im);
432 switch ($mode) {
433 case 'deleted':
434 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_deleted.gif');
435 break;
436 case 'futuretiming':
437 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif');
438 break;
439 case 'timing':
440 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif');
441 break;
442 case 'hiddentiming':
443 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden_timing.gif');
444 break;
445 case 'no_icon_found':
446 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_no_icon_found.gif');
447 break;
448 case 'disabled':
449 // is already greyed - nothing more
450 $ol_im = 0;
451 break;
452 case 'hidden':
453
454 default:
455 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden.gif');
456 }
457 if ($ol_im < 0) {
458 return $iconfile;
459 }
460 if ($ol_im) {
461 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
462 }
463 }
464 // Protect-section icon:
465 if ($protectSection) {
466 $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_sub5.gif');
467 if ($ol_im < 0) {
468 return $iconfile;
469 }
470 self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
471 }
472 // Create the image as file, destroy GD image and return:
473 @self::imagemake($im, $path);
474 GeneralUtility::gif_compress($path, 'IM');
475 ImageDestroy($im);
476 return $mainpath;
477 } else {
478 return $iconfile;
479 }
480 } else {
481 return $GLOBALS['BACK_PATH'] . 'gfx/fileicons/default.gif';
482 }
483 }
484 }
485
486 /**
487 * The necessity of using this function for combining two images if GD is version 2 is that
488 * GD2 cannot manage to combine two indexed-color images without totally spoiling everything.
489 * In class \TYPO3\CMS\Core\Imaging\GraphicalFunctions this was solved by combining the images
490 * onto a first created true color image.
491 * However it has turned out that this method will not work if the indexed png-files contains transparency.
492 * So I had to turn my attention to ImageMagick - my 'enemy of death'.
493 * And so it happend - ImageMagick is now used to combine my two indexed-color images with transparency. And that works.
494 * Of course it works only if ImageMagick is able to create valid png-images - which you cannot be sure of with older versions (still 5+)
495 * The only drawback is (apparently) that IM creates true-color png's. The transparency of these will not be shown by MSIE on windows at this time (although it's straight 0%/100% transparency!) and the file size may be larger.
496 *
497 * @param resource $destinationImage Destination image
498 * @param resource $sourceImage Source image
499 * @param integer $destinationX Destination x-coordinate
500 * @param integer $destinationY Destination y-coordinate
501 * @param integer $sourceX Source x-coordinate
502 * @param integer $sourceY Source y-coordinate
503 * @param integer $destinationWidth Destination width
504 * @param integer $destinationHeight Destination height
505 * @param integer $sourceWidth Source width
506 * @param integer $sourceHeight Source height
507 * @return void
508 * @access private
509 * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions::imagecopyresized()
510 */
511 static public function imagecopyresized(&$destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight) {
512 imagecopyresized($destinationImage, $sourceImage, $destinationX, $destinationY, $sourceX, $sourceY, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
513 }
514
515 /**
516 * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by \TYPO3\CMS\Core\Utility\GeneralUtility::read_png_gif())
517 *
518 * @param string $file Absolute filename of the image file from which to start the icon creation.
519 * @return mixed If success, image pointer, otherwise "-1
520 * @access private
521 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::read_png_gif
522 */
523 static public function imagecreatefrom($file) {
524 $file = GeneralUtility::read_png_gif($file, $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
525 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
526 return $file ? imagecreatefrompng($file) : -1;
527 } else {
528 return $file ? imagecreatefromgif($file) : -1;
529 }
530 }
531
532 /**
533 * Write the icon in $im pointer to $path
534 *
535 * @param pointer $im Pointer to GDlib image resource
536 * @param string $path Absolute path to the filename in which to write the icon.
537 * @return void
538 * @access private
539 */
540 static public function imagemake($im, $path) {
541 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
542 @ImagePng($im, $path);
543 } else {
544 @ImageGif($im, $path);
545 }
546 if (@is_file($path)) {
547 GeneralUtility::fixPermissions($path);
548 }
549 }
550
551 /**********************************************
552 * SPRITE ICON API
553 *
554 * The Sprite Icon API helps you to quickly get the HTML for any icon you want
555 * this is typically wrapped in a <span> tag with corresponding CSS classes that
556 * will be responsible for the
557 *
558 * There are three ways to use this API:
559 *
560 * 1) for any given TCA record
561 * $spriteIconHtml = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('pages', $row);
562 *
563 * 2) for any given file
564 * $spriteIconHtml = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('myimage.png');
565 *
566 * 3) for any other icon you know the name
567 * $spriteIconHtml = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-open');
568 *
569 **********************************************/
570 /**
571 * This generic method is used throughout the TYPO3 Backend to show icons in any variation which are not
572 * bound to any file type (see getSpriteIconForFile) or database record (see getSpriteIconForRecord)
573 *
574 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
575 * already to have a pre-defined background image, and the correct background-position to show
576 * the necessary icon.
577 *
578 * @param string $iconName The name of the icon to fetch
579 * @param array $options An associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
580 * @param array $overlays An associative array with the icon-name as key, and the options for this overlay as an array again (see the parameter $options again)
581 * @return string The full HTML tag (usually a <span>)
582 * @access public
583 */
584 static public function getSpriteIcon($iconName, array $options = array(), array $overlays = array()) {
585 $innerHtml = isset($options['html']) ? $options['html'] : NULL;
586 $tagName = isset($options['tagName']) ? $options['tagName'] : NULL;
587 // Deal with the overlays
588 if (count($overlays)) {
589 foreach ($overlays as $overlayIconName => $overlayOptions) {
590 $overlayOptions['html'] = $innerHtml;
591 $overlayOptions['class'] = (isset($overlayOptions['class']) ? $overlayOptions['class'] . ' ' : '') . 't3-icon-overlay';
592 $innerHtml = self::getSpriteIcon($overlayIconName, $overlayOptions);
593 }
594 }
595 // Check if whished icon is available
596 $iconName = in_array($iconName, $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']) || $iconName == 'empty-empty' ? $iconName : 'status-status-icon-missing';
597 // Create the CSS class
598 $options['class'] = self::getSpriteIconClasses($iconName) . (isset($options['class']) ? ' ' . $options['class'] : '');
599 unset($options['html']);
600 unset($options['tagName']);
601 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
602 }
603
604 /**
605 * This method is used throughout the TYPO3 Backend to show icons for a file type
606 *
607 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
608 * already to have a pre-defined background image, and the correct background-position to show
609 * the necessary icon.
610 *
611 * @param string $fileExtension The name of the icon to fetch, can be a file extension, full file path or one of the special keywords "folder" or "mount
612 * @param array $options An associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
613 * @return string The full HTML tag (usually a <span>)
614 * @access public
615 */
616 static public function getSpriteIconForFile($fileExtension, array $options = array()) {
617 $innerHtml = isset($options['html']) ? $options['html'] : NULL;
618 $tagName = isset($options['tagName']) ? $options['tagName'] : NULL;
619 // Create the CSS class
620 $options['class'] = self::mapFileExtensionToSpriteIconClass($fileExtension) . (isset($options['class']) ? ' ' . $options['class'] : '');
621 unset($options['html']);
622 unset($options['tagName']);
623 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
624 }
625
626 /**
627 * Generates the spriteicon css classes name for a given path or fileExtension
628 * usually called from getSpriteIconForFile or ExtJs Provider
629 *
630 * @param string $fileExtension FileExtension can be jpg, gif etc, but also be 'mount' or 'folder', but can also be a full path which will be resolved then
631 * @return string The string of the CSS class, see \TYPO3\CMS\Backend\Utility\IconUtility::$fileSpriteIconNames
632 * @access private
633 */
634 static public function mapFileExtensionToSpriteIconClass($fileExtension) {
635 return self::getSpriteIconClasses(self::mapFileExtensionToSpriteIconName($fileExtension));
636 }
637
638 /**
639 * Generates the spriteicon name for a given path or fileExtension
640 * usually called from mapFileExtensionToSpriteIconClass and tceforms
641 *
642 * @param string $fileExtension FileExtension can be jpg, gif etc, but also be 'mount' or 'folder', but can also be a full path which will be resolved then
643 * @return string The string of the CSS class, see \TYPO3\CMS\Backend\Utility\IconUtility::$fileSpriteIconNames
644 * @access private
645 */
646 static public function mapFileExtensionToSpriteIconName($fileExtension) {
647 // If the file is a whole file with name etc (mainly, if it has a "." or a "/"),
648 // then it is checked whether it is a valid directory
649 if (strpos($fileExtension, '.') !== FALSE || strpos($fileExtension, '/') !== FALSE) {
650 // Check if it is a directory
651 $filePath = dirname(GeneralUtility::getIndpEnv('SCRIPT_FILENAME')) . '/' . $GLOBALS['BACK_PATH'] . $fileExtension;
652 $path = GeneralUtility::resolveBackPath($filePath);
653 if (is_dir($path) || substr($fileExtension, -1) === '/' || substr($fileExtension, -1) === '\\') {
654 $fileExtension = 'folder';
655 } else {
656 if (($pos = strrpos($fileExtension, '.')) !== FALSE) {
657 $fileExtension = strtolower(substr($fileExtension, $pos + 1));
658 } else {
659 $fileExtension = 'default';
660 }
661 }
662 }
663 // If the file extension is not valid
664 // then use the default one
665 if (!isset(self::$fileSpriteIconNames[$fileExtension])) {
666 $fileExtension = 'default';
667 }
668 $iconName = self::$fileSpriteIconNames[$fileExtension];
669 return $iconName;
670 }
671
672 /**
673 * This method is used throughout the TYPO3 Backend to show icons for a DB record
674 *
675 * Generates a HTML tag with proper CSS classes. The TYPO3 skin has defined these CSS classes
676 * already to have a pre-defined background image, and the correct background-position to show
677 * the necessary icon.
678 *
679 * @param string $table The TCA table name
680 * @param array $row The DB record of the TCA table
681 * @param array $options An associative array with additional options and attributes for the tag. by default, the key is the name of the attribute, and the value is the parameter string that is set. However, there are some additional special reserved keywords that can be used as keys: "html" (which is the HTML that will be inside the icon HTML tag), "tagName" (which is an alternative tagName than "span"), and "class" (additional class names that will be merged with the sprite icon CSS classes)
682 * @return string The full HTML tag (usually a <span>)
683 * @access public
684 */
685 static public function getSpriteIconForRecord($table, array $row, array $options = array()) {
686 $innerHtml = isset($options['html']) ? $options['html'] : NULL;
687 $tagName = isset($options['tagName']) ? $options['tagName'] : NULL;
688 // Overlay this record icon with the status of the row
689 $overlaySpriteIconName = self::mapRecordOverlayToSpriteIconName($table, $row);
690 if ($overlaySpriteIconName) {
691 $overlayOptions = array(
692 'html' => $innerHtml,
693 'class' => 't3-icon-overlay'
694 );
695 $innerHtml = self::getSpriteIcon($overlaySpriteIconName, $overlayOptions);
696 }
697 // Fetch the name for the CSS class, based on the $row
698 $options['class'] = self::mapRecordTypeToSpriteIconClass($table, $row) . (isset($options['class']) ? ' ' . $options['class'] : '');
699 unset($options['html']);
700 unset($options['tagName']);
701 return self::buildSpriteHtmlIconTag($options, $innerHtml, $tagName);
702 }
703
704 /**
705 * this helper functions looks up the column that is used for the type of
706 * the chosen TCA table. And then fetches the corresponding class
707 * based on the chosen iconsprite class in this TCA
708 * The TCA looks up
709 * - [ctrl][typeicon_column]
710 * -
711 * This method solely takes care of the type of this record, not any
712 * statuses, used for overlays.
713 * You should not use this directly besides if you need classes for ExtJS iconCls.
714 *
715 * see ext:core/Configuration/TCA/pages.php for an example with the TCA table "pages"
716 *
717 * @param string $table The TCA table
718 * @param array $row The selected record
719 * @return string The CSS class for the sprite icon of that DB record
720 * @access private
721 */
722 static public function mapRecordTypeToSpriteIconClass($table, array $row) {
723 return self::getSpriteIconClasses(self::mapRecordTypeToSpriteIconName($table, $row));
724 }
725
726 /**
727 * this helper functions looks up the column that is used for the type of
728 * the chosen TCA table. And then fetches the corresponding iconname
729 * based on the chosen iconsprite class in this TCA
730 * The TCA looks up
731 * - [ctrl][typeicon_column]
732 * -
733 * This method solely takes care of the type of this record, not any
734 * statuses, used for overlays.
735 * You should not use this directly besides if you need it in tceforms/core classes
736 *
737 * see ext:core/Configuration/TCA/pages.php for an example with the TCA table "pages"
738 *
739 * @param string $tableThe TCA table
740 * @param array $row The selected record
741 * @return string The CSS class for the sprite icon of that DB record
742 * @access private
743 */
744 static public function mapRecordTypeToSpriteIconName($table, array $row) {
745 $recordType = array();
746 $ref = NULL;
747 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_column'])) {
748 $column = $GLOBALS['TCA'][$table]['ctrl']['typeicon_column'];
749 if (isset($row[$column])) {
750 $recordType[1] = $row[$column];
751 } else {
752 $recordType[1] = 'default';
753 }
754 // Workaround to give nav_hide pages a complete different icon
755 // Although it's not a separate doctype
756 // and to give root-pages an own icon
757 if ($table === 'pages') {
758 if ($row['nav_hide']) {
759 $recordType[2] = $recordType[1] . '-hideinmenu';
760 }
761 if ($row['is_siteroot']) {
762 $recordType[3] = $recordType[1] . '-root';
763 }
764 if ($row['module']) {
765 $recordType[4] = 'contains-' . $row['module'];
766 }
767 }
768 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
769 foreach ($recordType as $key => $type) {
770 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type])) {
771 $recordType[$key] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type];
772 } else {
773 unset($recordType[$key]);
774 }
775 }
776 $recordType[0] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
777 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['mask'])) {
778 $recordType[5] = str_replace('###TYPE###', $row[$column], $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['mask']);
779 }
780 if (isset($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['userFunc'])) {
781 $parameters = array('row' => $row);
782 $recordType[6] = GeneralUtility::callUserFunction($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['userFunc'], $parameters, $ref);
783 }
784 } else {
785 foreach ($recordType as &$type) {
786 $type = 'tcarecords-' . $table . '-' . $type;
787 }
788 unset($type);
789 $recordType[0] = 'tcarecords-' . $table . '-default';
790 }
791 } else {
792 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
793 $recordType[0] = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
794 } else {
795 $recordType[0] = 'tcarecords-' . $table . '-default';
796 }
797 }
798 krsort($recordType);
799 if (is_array($GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable'])) {
800 foreach ($recordType as $iconName) {
801 if (in_array($iconName, $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable'])) {
802 return $iconName;
803 }
804 }
805 }
806 return 'status-status-icon-missing';
807 }
808
809 /**
810 * this helper functions checks if the DB record ($row) has any special status
811 * based on the TCA settings like hidden, starttime etc, and then returns a specific
812 * Sprite icon class for the overlay of this DB record
813 * This method solely takes care of the overlay of this record, not any type
814 *
815 * Please note that this only returns one overlay, one status, that is prioritized
816 * by $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconRecordOverlayPriorities']
817 * We wanted to not have these icons blown over by tons of overlays, so this is limited
818 * to just one.
819 *
820 * see ext:core/Configuration/DefaultConfiguration.php for the default options, you will find
821 * $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconRecordOverlayNames'] that shows
822 * the list of CSS classes that will be used for the sprites, mapped to the statuses here
823 *
824 * @param string $table The TCA table
825 * @param array $row The selected record
826 * @return string The CSS class for the sprite icon of that DB record
827 * @access private
828 */
829 static public function mapRecordOverlayToSpriteIconName($table, array $row) {
830 $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
831 // Calculate for a given record the actual visibility at the moment
832 $status = array(
833 'hidden' => FALSE,
834 'starttime' => FALSE,
835 'endtime' => FALSE,
836 'futureendtime' => FALSE,
837 'fe_group' => FALSE,
838 'deleted' => FALSE,
839 'protectedSection' => FALSE,
840 'nav_hide' => $row['nav_hide'] ? TRUE : FALSE,
841 'noIconFound' => $row['_NO_ICON_FOUND'] ? TRUE : FALSE
842 );
843 // Icon state based on "enableFields":
844 if (is_array($tcaCtrl['enablecolumns'])) {
845 $enCols = $tcaCtrl['enablecolumns'];
846 // If "hidden" is enabled:
847 if ($tcaCtrl['enablecolumns']['disabled'] && $row[$tcaCtrl['enablecolumns']['disabled']]) {
848 $status['hidden'] = TRUE;
849 }
850 // If a "starttime" is set and higher than current time:
851 if ($tcaCtrl['enablecolumns']['starttime'] && $GLOBALS['EXEC_TIME'] < (int)$row[$tcaCtrl['enablecolumns']['starttime']]) {
852 $status['starttime'] = TRUE;
853 }
854 // If an "endtime" is set
855 if ($tcaCtrl['enablecolumns']['endtime']) {
856 if ((int)$row[$tcaCtrl['enablecolumns']['endtime']] > 0) {
857 if ((int)$row[$tcaCtrl['enablecolumns']['endtime']] < $GLOBALS['EXEC_TIME']) {
858 // End-timing applies at this point.
859 $status['endtime'] = TRUE;
860 } else {
861 // End-timing WILL apply in the future for this element.
862 $status['futureendtime'] = TRUE;
863 }
864 }
865 }
866 // If a user-group field is set
867 if ($tcaCtrl['enablecolumns']['fe_group'] && $row[$tcaCtrl['enablecolumns']['fe_group']]) {
868 $status['fe_group'] = TRUE;
869 }
870 }
871 // If "deleted" flag is set (only when listing records which are also deleted!)
872 if ($row[$tcaCtrl['delete']]) {
873 $status['deleted'] = TRUE;
874 }
875 // Detecting extendToSubpages (for pages only)
876 if ($table == 'pages' && $row['extendToSubpages']) {
877 $status['protectedSection'] = TRUE;
878 }
879 // Hook: allow some other process to influence the choice of icon overlay
880 // The method called receives the table name, the current row and the current status array as parameters
881 // The status array should be passed as a reference and in order to be modified within the hook
882 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'])) {
883 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'] as $classRef) {
884 $hookObject = GeneralUtility::getUserObj($classRef);
885 if (method_exists($hookObject, 'overrideIconOverlay')) {
886 $hookObject->overrideIconOverlay($table, $row, $status);
887 }
888 }
889 }
890 // Now only show the status with the highest priority
891 $priorities = $GLOBALS['TBE_STYLES']['spriteIconApi']['spriteIconRecordOverlayPriorities'];
892 $iconName = '';
893 if (is_array($priorities)) {
894 foreach ($priorities as $priority) {
895 if ($status[$priority]) {
896 $iconName = $GLOBALS['TBE_STYLES']['spriteIconApi']['spriteIconRecordOverlayNames'][$priority];
897 break;
898 }
899 }
900 }
901 return $iconName;
902 }
903
904 /**
905 * generic method to create the final CSS classes based on the sprite icon name
906 * with the base class and splits the name into parts
907 * is usually called by the methods that are responsible for fetching the names
908 * out of the file name, or the record type
909 *
910 * @param string $name Iconname like 'actions-document-new'
911 * @return string A list of all CSS classes needed for the HTML tag
912 */
913 static public function getSpriteIconClasses($iconName) {
914 $cssClasses = ($baseCssClass = 't3-icon');
915 $parts = explode('-', $iconName);
916 if (count($parts) > 1) {
917 // Will be something like "t3-icon-actions"
918 $cssClasses .= ' ' . ($baseCssClass . '-' . $parts[0]);
919 // Will be something like "t3-icon-actions-document"
920 $cssClasses .= ' ' . ($baseCssClass . '-' . $parts[0] . '-' . $parts[1]);
921 // Will be something like "t3-icon-document-new"
922 $cssClasses .= ' ' . ($baseCssClass . '-' . substr($iconName, (strlen($parts[0]) + 1)));
923 }
924 return $cssClasses;
925 }
926
927 /**
928 * low level function that generates the HTML tag for the sprite icon
929 * is usually called by the three API classes (getSpriteIcon, getSpriteIconForFile, getSpriteIconForRecord)
930 * it does not care about classes or anything else, but just plainly builds the HTML tag
931 *
932 * @param array $tagAttributes An associative array of additional tagAttributes for the HTML tag
933 * @param string $innerHtml The content within the tag, a "&nbsp;" by default
934 * @param string $tagName The name of the HTML element that should be used (span by default)
935 * @return string The sprite html icon tag
936 */
937 static protected function buildSpriteHtmlIconTag(array $tagAttributes, $innerHtml = NULL, $tagName = NULL) {
938 $innerHtml = $innerHtml === NULL ? '&nbsp;' : $innerHtml;
939 $tagName = $tagName === NULL ? 'span' : $tagName;
940 $attributes = '';
941 foreach ($tagAttributes as $attribute => $value) {
942 $attributes .= ' ' . htmlspecialchars($attribute) . '="' . htmlspecialchars($value) . '"';
943 }
944 return '<' . $tagName . $attributes . '>' . $innerHtml . '</' . $tagName . '>';
945 }
946
947 }