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