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