Moving template util file
[squirrelmail.git] / functions / html.php
CommitLineData
9cd67a0a 1<?php
2
3/**
8008456a 4 * html.php
9cd67a0a 5 *
9cd67a0a 6 * The idea is to inlcude here some functions to make easier
7 * the right to left implementation by "functionize" some
8 * html outputs.
9 *
47ccfad4 10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
792f911e 14 * @since 1.3.0
9cd67a0a 15 */
16
769a819d 17
18/**
19 * Generates a hyperlink
20 *
6edb574e 21 * @param string $uri The target link location
22 * @param string $text The link text
23 * @param string $target The location where the link should
24 * be opened (OPTIONAL; default not used)
25 * @param string $onclick The onClick JavaScript handler (OPTIONAL;
26 * default not used)
27 * @param string $class The CSS class name (OPTIONAL; default
28 * not used)
29 * @param string $id The ID name (OPTIONAL; default not used)
30 * @param string $name The anchor name (OPTIONAL; default not used)
31 * @param array $aAttribs Any extra attributes: this must be an
32 * associative array, where keys will be
33 * added as the attribute name, and values
34 * (which are optional - should be null if
35 * none should be used) will be placed in
36 * double quotes (pending template implementation)
37 * as the attribute value (OPTIONAL; default empty).
769a819d 38 *
39 * @return string The desired hyperlink tag.
40 *
41 * @since 1.5.2
42 *
43 */
ca11c6f1 44function create_hyperlink($uri, $text, $target='', $onclick='',
6edb574e 45 $class='', $id='', $name='', $aAttribs=array()) {
769a819d 46
47 global $oTemplate;
48
49 $oTemplate->assign('uri', $uri);
50 $oTemplate->assign('text', $text);
51 $oTemplate->assign('target', $target);
52 $oTemplate->assign('onclick', $onclick);
53 $oTemplate->assign('class', $class);
54 $oTemplate->assign('id', $id);
ca11c6f1 55 $oTemplate->assign('name', $name);
769a819d 56
6edb574e 57 $oTemplate->assign('aAttribs', $aAttribs);
58
769a819d 59 return $oTemplate->fetch('hyperlink.tpl');
60
61}
62
63
0173ad29 64/**
65 * Generates an image tag
66 *
67 * @param string $src The image source path
68 * @param string $alt Alternate link text (OPTIONAL; default
69 * not used)
70 * @param string $width The width the image should be shown in
71 * (OPTIONAL; default not used)
72 * @param string $height The height the image should be shown in
73 * (OPTIONAL; default not used)
74 * @param string $border The image's border attribute value
75 * (OPTIONAL; default not used)
76 * @param string $class The CSS class name (OPTIONAL; default
77 * not used)
78 * @param string $id The ID name (OPTIONAL; default not used)
79 * @param string $onclick The onClick JavaScript handler (OPTIONAL;
80 * default not used)
81 * @param string $title The image's title attribute value
82 * (OPTIONAL; default not used)
83 * @param string $align The image's alignment attribute value
84 * (OPTIONAL; default not used)
85 * @param string $hspace The image's hspace attribute value
86 * (OPTIONAL; default not used)
87 * @param string $vspace The image's vspace attribute value
88 * (OPTIONAL; default not used)
bcc55e4b 89 * @param string $text_alternative A text replacement for the entire
90 * image tag, to be used at the
91 * discretion of the template set,
92 * if for some reason the image tag
93 * cannot or should not be produced
94 * (OPTIONAL; default not used)
6edb574e 95 * @param array $aAttribs Any extra attributes: this must be an
96 * associative array, where keys will be
97 * added as the attribute name, and values
98 * (which are optional - should be null if
99 * none should be used) will be placed in
100 * double quotes (pending template implementation)
101 * as the attribute value (OPTIONAL; default empty).
0173ad29 102 *
103 * @return string The desired hyperlink tag.
104 *
105 * @since 1.5.2
106 *
107 */
108function create_image($src, $alt='', $width='', $height='',
109 $border='', $class='', $id='', $onclick='',
bcc55e4b 110 $title='', $align='', $hspace='', $vspace='',
6edb574e 111 $text_alternative='', $aAttribs=array()) {
0173ad29 112
113 global $oTemplate;
114
115 $oTemplate->assign('src', $src);
116 $oTemplate->assign('alt', $alt);
117 $oTemplate->assign('width', $width);
118 $oTemplate->assign('height', $height);
119 $oTemplate->assign('border', $border);
120 $oTemplate->assign('class', $class);
121 $oTemplate->assign('id', $id);
122 $oTemplate->assign('onclick', $onclick);
123 $oTemplate->assign('title', $title);
124 $oTemplate->assign('align', $align);
125 $oTemplate->assign('hspace', $hspace);
126 $oTemplate->assign('vspace', $vspace);
bcc55e4b 127 $oTemplate->assign('text_alternative', $text_alternative);
0173ad29 128
6edb574e 129 $oTemplate->assign('aAttribs', $aAttribs);
130
0173ad29 131 return $oTemplate->fetch('image.tpl');
132
133}
134
135
b0215f91 136/**
137 * Generates a span tag
138 *
139 * @param string $value The contents that belong inside the span
140 * @param string $class The CSS class name (OPTIONAL; default
141 * not used)
142 * @param string $id The ID name (OPTIONAL; default not used)
6edb574e 143 * @param array $aAttribs Any extra attributes: this must be an
144 * associative array, where keys will be
145 * added as the attribute name, and values
146 * (which are optional - should be null if
147 * none should be used) will be placed in
148 * double quotes (pending template implementation)
149 * as the attribute value (OPTIONAL; default empty).
b0215f91 150 *
151 * @return string The desired span tag.
152 *
153 * @since 1.5.2
154 *
155 */
6edb574e 156function create_span($value, $class='', $id='', $aAttribs=array()) {
b0215f91 157
158 global $oTemplate;
159
160 $oTemplate->assign('value', $value);
161 $oTemplate->assign('class', $class);
162 $oTemplate->assign('id', $id);
163
6edb574e 164 $oTemplate->assign('aAttribs', $aAttribs);
165
b0215f91 166 return $oTemplate->fetch('span.tpl');
167
168}
169
170
d6c32258 171/**
792f911e 172 * Generates html tags
b0215f91 173//FIXME: this should not be used anywhere in the core, or we should
174// convert this to use templates. We sould not be assuming HTML output.
d6c32258 175 *
176 * @param string $tag Tag to output
177 * @param string $val Value between tags
178 * @param string $align Alignment (left, center, etc)
179 * @param string $bgcolor Back color in hexadecimal
180 * @param string $xtra Extra options
181 * @return string HTML ready for output
792f911e 182 * @since 1.3.0
d6c32258 183 */
184function html_tag( $tag, // Tag to output
d68323ff 185 $val = '', // Value between tags
8008456a 186 $align = '', // Alignment
187 $bgcolor = '', // Back color
188 $xtra = '' ) { // Extra options
892b98c9 189
792f911e 190 GLOBAL $languages, $squirrelmail_language;
892b98c9 191
792f911e 192 $align = strtolower( $align );
193 $bgc = '';
194 $tag = strtolower( $tag );
892b98c9 195
792f911e 196 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
197 $dir = $languages[$squirrelmail_language]['DIR'];
198 } else {
199 $dir = 'ltr';
200 }
892b98c9 201
792f911e 202 if ( $dir == 'ltr' ) {
203 $rgt = 'right';
204 $lft = 'left';
205 } else {
206 $rgt = 'left';
207 $lft = 'right';
208 }
892b98c9 209
792f911e 210 if ( $bgcolor <> '' ) {
211 $bgc = " bgcolor=\"$bgcolor\"";
212 }
892b98c9 213
792f911e 214 switch ( $align ) {
215 case '':
216 $alg = '';
217 break;
218 case 'right':
219 $alg = " align=\"$rgt\"";
220 break;
221 case 'left':
222 $alg = " align=\"$lft\"";
223 break;
224 default:
225 $alg = " align=\"$align\"";
226 break;
227 }
892b98c9 228
792f911e 229 $ret = "<$tag";
892b98c9 230
792f911e 231 if ( $dir <> 'ltr' ) {
232 $ret .= " dir=\"$dir\"";
233 }
234 $ret .= $bgc . $alg;
892b98c9 235
792f911e 236 if ( $xtra <> '' ) {
237 $ret .= " $xtra";
238 }
892b98c9 239
792f911e 240 if ( $val <> '' ) {
241 $ret .= ">$val</$tag>\n";
242 } else {
243 $ret .= '>'. "\n";
94ac35c6 244 }
245
792f911e 246 return( $ret );
247}
892b98c9 248
769a819d 249
792f911e 250/**
251 * handy function to set url vars
252 *
253 * especially useful when $url = $PHP_SELF
254 * @param string $url url that must be modified
255 * @param string $var variable name
256 * @param string $val variable value
257 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
258 * @return string $url modified url
259 * @since 1.3.0
260 */
261function set_url_var($url, $var, $val=0, $link=true) {
262 $k = '';
263 $pat_a = array (
264 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
265 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
266 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
267 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
268 );
4f113ce5 269 $url = str_replace('&amp;','&',$url);
792f911e 270
271 // FIXME: why switch is used instead of if () or one preg_match()
272 switch (true) {
273 case (preg_match($pat_a[0],$url,$regs)):
274 $k = $regs[1];
275 $v = $regs[2];
276 break;
277 case (preg_match($pat_a[1],$url,$regs)):
278 $k = $regs[1];
279 $v = $regs[2];
280 break;
281 case (preg_match($pat_a[2],$url,$regs)):
282 $k = $regs[1];
283 $v = $regs[2];
284 break;
285 case (preg_match($pat_a[3],$url,$regs)):
286 $k = $regs[1];
287 $v = $regs[2];
288 break;
289 default:
290 if ($val) {
291 if (strpos($url,'?')) {
292 $url .= "&$var=$val";
892b98c9 293 } else {
792f911e 294 $url .= "?$var=$val";
892b98c9 295 }
792f911e 296 }
297 break;
298 }
299
300 if ($k) {
301 if ($val) {
302 $rpl = "$k=$val";
792f911e 303 } else {
304 $rpl = '';
305 }
306 if( substr($v,-1)=='&' ) {
307 $rpl .= '&';
02725e0b 308 }
792f911e 309 $pat = "/$k=$v/";
310 $url = preg_replace($pat,$rpl,$url);
892b98c9 311 }
4f113ce5 312 if ($link) {
313 $url = str_replace('&','&amp;',$url);
314 }
792f911e 315 return $url;
316}
769a819d 317
318