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