Moving template utilities... help appreciated moving the other util files too
[squirrelmail.git] / functions / template / template.php
CommitLineData
29997535 1<?php
62f9cbcc 2
29997535 3/**
62f9cbcc 4 * template.php
29997535 5 *
62f9cbcc 6 * This file is intended to contain helper functions for template sets
7 * that would like to use them.
8FIXME: potentially create a separate directory and separate functions into different files?
29997535 9 *
10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
29997535 14 */
62f9cbcc 15
16
17/**
18 * Create stylesheet links that will work for multiple browsers
19 *
20 * @param string $uri The URI to the linked stylesheet.
21 * @param string $name The title of the stylesheet (optional; default empty).
22 * @param boolean $alt Whether or not this is an alternate
23 * stylesheet (optional; default TRUE).
24 * @param string $mtype The target media display type (optional; default "screen").
25 * @param string $xhtml_end The XHTML-compliant close tag syntax to
26 * use (optional; default "/")
27 *
28 * @return string The full text of the stylesheet link.
29 *
30 */
31function create_css_link($uri, $name='', $alt=TRUE, $mtype='screen', $xhtml_end='/') {
32// FIXME: Add closing / to link and meta elements only after
33// switching to xhtml 1.0 Transitional.
34// It is not compatible with html 4.01 Transitional
35$xhtml_end='';
36
37 if (empty($uri)) {
38 return '';
39 }
40
41 // set to lower case to avoid errors
42 //
43 sqGetGlobalVar('HTTP_USER_AGENT', $browser_user_agent, SQ_SERVER);
44 $browser_user_agent = strtolower($browser_user_agent);
45
46 if (stristr($browser_user_agent, "msie 4")) {
47 $browser = 'msie4';
48 $dom_browser = false;
49 $is_IE = true;
50 } elseif (stristr($browser_user_agent, "msie")
51 && stristr($browser_user_agent, 'opera') === FALSE) {
52 $browser = 'msie';
53 $dom_browser = true;
54 $is_IE = true;
55 }
56
57 if ((strpos($uri, '-ie')!== false) and !$is_IE) {
58 //not IE, so don't render this sheet
59 return;
60 }
61
62 if ( strpos($uri, 'print') !== false )
63 $mtype = 'print';
64
65 $href = 'href="'.$uri.'" ';
66 $media = 'media="'.$mtype.'" ';
67
68 if ( empty($name) ) {
69 $title = '';
70 $rel = 'rel="stylesheet" ';
71 } else {
72 $title = 'title="'.$name.'" ';
73 $rel = 'rel="'.( $alt ? 'alternate ' : '' ).'stylesheet" ';
74 }
75
76 return '<link '.$media.$title.$rel.'type="text/css" '.$href." $xhtml_end>\n";
77}
78
79
29997535 80/**
81 * Checks for an image icon and returns a complete HTML img tag or a text
82 * string with the text icon based on what is found and user prefs.
62f9cbcc 83 *
29997535 84 * @param string $icon_theme_path User's chosen icon set
85 * @param string $icon_name File name of the desired icon
86 * @param string $text_icon Text-based icon to display if desired
87 * @param string $alt_text Optional. Text for alt/title attribute of image
88 * @param integer $w Optional. Width of requested image.
89 * @param integer $h Optional. Height of requested image.
90 * @return string $icon String containing icon that can be echo'ed
91 * @author Steve Brown
92 * @since 1.5.2
93 */
94function getIcon($icon_theme_path, $icon_name, $text_icon, $alt_text='', $w=NULL, $h=NULL) {
95 $icon = '';
96 if (is_null($icon_theme_path)) {
97 $icon = $text_icon;
98 } else {
7c8f16f3 99 $icon_path = getIconPath($icon_theme_path, $icon_name);
29997535 100
29997535 101 // If we found an icon, build an img tag to display it. If we didn't
102 // find an image, we will revert back to the text icon.
7c8f16f3 103 if (!is_null($icon_path)) {
29997535 104 $icon = '<img src="'.$icon_path.'" ' .
38469948 105 'alt="'.$alt_text.'" '.
106 (!empty($alt_text) ? 'title="'.$alt_text.'" ' : '') .
29997535 107 (!is_null($w) ? 'width="'.$w.'" ' : '') .
108 (!is_null($h) ? 'height="'.$h.'" ' : '') .
109 ' />';
110 } else {
111 $icon = $text_icon;
112 }
113 }
62f9cbcc 114 return $icon;
29997535 115}
7c8f16f3 116
62f9cbcc 117
7c8f16f3 118/**
119 * Gets the path to the specified icon or returns NULL if the image is not
120 * found. This has been separated from getIcon to allow the path to be fetched
121 * for use w/ third party packages, e.g. dTree.
62f9cbcc 122 *
7c8f16f3 123 * @param string $icon_theme_path User's chosen icon set
124 * @param string $icon_name File name of the desired icon
125 * @return string $icon String containing path to icon that can be used in
126 * an IMG tag, or NULL if the image is not found.
127 * @author Steve Brown
128 * @since 1.5.2
129 */
130function getIconPath ($icon_theme_path, $icon_name) {
341fd984 131 global $fallback_icon_theme_path;
74db4ed2 132
7c8f16f3 133 if (is_null($icon_theme_path))
134 return NULL;
62f9cbcc 135
7c8f16f3 136 // Desired icon exists in the current theme?
137 if (is_file($icon_theme_path . $icon_name)) {
138 return $icon_theme_path . $icon_name;
62f9cbcc 139
341fd984 140 // Icon not found, check for the admin-specified fallback
141 } elseif (!is_null($fallback_icon_theme_path) && is_file($fallback_icon_theme_path . $icon_name)) {
142 return $fallback_icon_theme_path . $icon_name;
62f9cbcc 143
7c8f16f3 144 // Icon not found, return the SQM default icon
145 } elseif (is_file(SM_PATH . 'images/themes/default/'.$icon_name)) {
146 return SM_PATH . 'images/themes/default/'.$icon_name;
62f9cbcc 147 }
148
7c8f16f3 149 return NULL;
150}
f6dccc95 151
62f9cbcc 152
f6dccc95 153/**
154 * Display error messages for use in footer.tpl
62f9cbcc 155 *
f6dccc95 156 * @author Steve Brown
157 * @since 1.5.2
158 **/
159function displayErrors () {
160 global $oErrorHandler;
62f9cbcc 161
e1603e71 162 if ($oErrorHandler) {
163 $oErrorHandler->displayErrors();
164 }
165}
166
62f9cbcc 167
e1603e71 168/**
169 * Make the internal show_readable_size() function available to templates.
62f9cbcc 170//FIXME: I think this is needless since there is no reason templates cannot just call directly to show_readable_size
171 *
e1603e71 172 * @param int size to be converted to human-readable
173 * @return string human-readable form
174 * @since 1.5.2
175 **/
176function humanReadableSize ($size) {
177 return show_readable_size($size);
f6dccc95 178}
e1603e71 179
62f9cbcc 180