Refactor hyperlink template code
[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 *
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 *
31 * @return string The desired hyperlink tag.
32 *
33 * @since 1.5.2
34 *
35 */
36function create_hyperlink($uri, $text, $target='', $onclick='', $class='', $id='') {
37
38 global $oTemplate;
39
40 $oTemplate->assign('uri', $uri);
41 $oTemplate->assign('text', $text);
42 $oTemplate->assign('target', $target);
43 $oTemplate->assign('onclick', $onclick);
44 $oTemplate->assign('class', $class);
45 $oTemplate->assign('id', $id);
46
47 return $oTemplate->fetch('hyperlink.tpl');
48
49}
50
51
d6c32258 52/**
792f911e 53 * Generates html tags
d6c32258 54 *
55 * @param string $tag Tag to output
56 * @param string $val Value between tags
57 * @param string $align Alignment (left, center, etc)
58 * @param string $bgcolor Back color in hexadecimal
59 * @param string $xtra Extra options
60 * @return string HTML ready for output
792f911e 61 * @since 1.3.0
d6c32258 62 */
63function html_tag( $tag, // Tag to output
d68323ff 64 $val = '', // Value between tags
8008456a 65 $align = '', // Alignment
66 $bgcolor = '', // Back color
67 $xtra = '' ) { // Extra options
892b98c9 68
792f911e 69 GLOBAL $languages, $squirrelmail_language;
892b98c9 70
792f911e 71 $align = strtolower( $align );
72 $bgc = '';
73 $tag = strtolower( $tag );
892b98c9 74
792f911e 75 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
76 $dir = $languages[$squirrelmail_language]['DIR'];
77 } else {
78 $dir = 'ltr';
79 }
892b98c9 80
792f911e 81 if ( $dir == 'ltr' ) {
82 $rgt = 'right';
83 $lft = 'left';
84 } else {
85 $rgt = 'left';
86 $lft = 'right';
87 }
892b98c9 88
792f911e 89 if ( $bgcolor <> '' ) {
90 $bgc = " bgcolor=\"$bgcolor\"";
91 }
892b98c9 92
792f911e 93 switch ( $align ) {
94 case '':
95 $alg = '';
96 break;
97 case 'right':
98 $alg = " align=\"$rgt\"";
99 break;
100 case 'left':
101 $alg = " align=\"$lft\"";
102 break;
103 default:
104 $alg = " align=\"$align\"";
105 break;
106 }
892b98c9 107
792f911e 108 $ret = "<$tag";
892b98c9 109
792f911e 110 if ( $dir <> 'ltr' ) {
111 $ret .= " dir=\"$dir\"";
112 }
113 $ret .= $bgc . $alg;
892b98c9 114
792f911e 115 if ( $xtra <> '' ) {
116 $ret .= " $xtra";
117 }
892b98c9 118
792f911e 119 if ( $val <> '' ) {
120 $ret .= ">$val</$tag>\n";
121 } else {
122 $ret .= '>'. "\n";
94ac35c6 123 }
124
792f911e 125 return( $ret );
126}
892b98c9 127
769a819d 128
792f911e 129/**
130 * handy function to set url vars
131 *
132 * especially useful when $url = $PHP_SELF
133 * @param string $url url that must be modified
134 * @param string $var variable name
135 * @param string $val variable value
136 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
137 * @return string $url modified url
138 * @since 1.3.0
139 */
140function set_url_var($url, $var, $val=0, $link=true) {
141 $k = '';
142 $pat_a = array (
143 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
144 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
145 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
146 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
147 );
4f113ce5 148 $url = str_replace('&amp;','&',$url);
792f911e 149
150 // FIXME: why switch is used instead of if () or one preg_match()
151 switch (true) {
152 case (preg_match($pat_a[0],$url,$regs)):
153 $k = $regs[1];
154 $v = $regs[2];
155 break;
156 case (preg_match($pat_a[1],$url,$regs)):
157 $k = $regs[1];
158 $v = $regs[2];
159 break;
160 case (preg_match($pat_a[2],$url,$regs)):
161 $k = $regs[1];
162 $v = $regs[2];
163 break;
164 case (preg_match($pat_a[3],$url,$regs)):
165 $k = $regs[1];
166 $v = $regs[2];
167 break;
168 default:
169 if ($val) {
170 if (strpos($url,'?')) {
171 $url .= "&$var=$val";
892b98c9 172 } else {
792f911e 173 $url .= "?$var=$val";
892b98c9 174 }
792f911e 175 }
176 break;
177 }
178
179 if ($k) {
180 if ($val) {
181 $rpl = "$k=$val";
792f911e 182 } else {
183 $rpl = '';
184 }
185 if( substr($v,-1)=='&' ) {
186 $rpl .= '&';
02725e0b 187 }
792f911e 188 $pat = "/$k=$v/";
189 $url = preg_replace($pat,$rpl,$url);
892b98c9 190 }
4f113ce5 191 if ($link) {
192 $url = str_replace('&','&amp;',$url);
193 }
792f911e 194 return $url;
195}
769a819d 196
197