From 9dffa7af76dd2d1c67e0dabed965c5e888a5eef2 Mon Sep 17 00:00:00 2001 From: samhemelryk Date: Tue, 7 Jul 2009 03:56:15 +0000 Subject: [PATCH] lib-filelib MDL-19707 Added file_extension_icon() and file_mimetype_icon() After talking with Tim we decided that this would be a better solution than adding more methods to $OUTPUT --- lib/filelib.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/lib/filelib.php b/lib/filelib.php index bceb262450..1f9cf600a0 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1130,6 +1130,81 @@ function mimeinfo_from_icon($element, $icon, $all=false) { return array_pop($info); // Return last match (mimicking behaviour/comment inside foreach loop) } +/** + * Returns the relative icon path for a given mime type + * + * This function should be used in conjuction with $OUTPUT->old_icon_url to produce + * a return the full path to an icon. + * + * + * $mimetype = 'image/jpg'; + * $icon = $OUTPUT->old_icon_url(file_mimetype_icon($mimetype)); + * echo ''.$mimetype.''; + * + * + * @todo When an $OUTPUT->icon method is available this function should be altered + * to conform with that. + * + * @param string $mimetype The mimetype to fetch an icon for + * @param int $size The size of the icon. Not yet implemented + * @return string The relative path to the icon + */ +function file_mimetype_icon($mimetype, $size=null) { + $icon = mimeinfo_from_type('icon', $mimetype); + $icon = substr($icon, 0, strrpos($icon, '.')); + if ($size!=null && is_int($size)) { + $icon .= '-'.$size; + } + return 'f/'.$icon; +} + +/** + * Returns the relative icon path for a given file extension + * + * This function should be used in conjuction with $OUTPUT->old_icon_url to produce + * a return the full path to an icon. + * + * The extension that you should pass should be just the extension, not the whole filename. + * A preceeding . is tolerated. + * + * + * $extension = 'jpg'; + * $icon = $OUTPUT->old_icon_url(file_extension_icon($extension)); + * echo ''.$extension.''; + * + * + * @todo When an $OUTPUT->icon method is available this function should be altered + * to conform with that. + * @todo Implement $size + * + * @param string $extension The extension of the file to get an icon for + * @param int $size The size of the icon. Not yet implemented + * @return string + */ +function file_extension_icon($extension, $size=null) { + // Get rid of any preceeding . + $extension = trim($extension, '. '); + $mimeinfo = get_mimetypes_array(); + foreach ($mimeinfo as $ext=>$mime) { + // Check each till we find an exact match for extension + if ($ext === $extension) { + $icon = $mime['icon']; + $icon = substr($icon, 0, strrpos($icon, '.')); + if ($size!=null && is_int($size)) { + $icon .= '-'.$size; + } + return 'f/'.$icon; + } + } + // Didn't find a match return the default + $icon = $mimeinfo['xxx']['icon']; + $icon = substr($icon, 0, strrpos($icon, '.')); + if ($size!=null && is_int($size)) { + $icon .= '-'.$size; + } + return 'f/'.$icon; +} + /** * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the * mimetypes.php language file. -- 2.39.5