Add name attribute to hyperlink template
[squirrelmail.git] / functions / html.php
1 <?php
2
3 /**
4 * html.php
5 *
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 *
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
14 * @since 1.3.0
15 */
16
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 * @param string $name The anchor name (OPTIONAL; default not used)
31 *
32 * @return string The desired hyperlink tag.
33 *
34 * @since 1.5.2
35 *
36 */
37 function create_hyperlink($uri, $text, $target='', $onclick='',
38 $class='', $id='', $name='') {
39
40 global $oTemplate;
41
42 $oTemplate->assign('uri', $uri);
43 $oTemplate->assign('text', $text);
44 $oTemplate->assign('target', $target);
45 $oTemplate->assign('onclick', $onclick);
46 $oTemplate->assign('class', $class);
47 $oTemplate->assign('id', $id);
48 $oTemplate->assign('name', $name);
49
50 return $oTemplate->fetch('hyperlink.tpl');
51
52 }
53
54
55 /**
56 * Generates an image tag
57 *
58 * @param string $src The image source path
59 * @param string $alt Alternate link text (OPTIONAL; default
60 * not used)
61 * @param string $width The width the image should be shown in
62 * (OPTIONAL; default not used)
63 * @param string $height The height the image should be shown in
64 * (OPTIONAL; default not used)
65 * @param string $border The image's border attribute value
66 * (OPTIONAL; default not used)
67 * @param string $class The CSS class name (OPTIONAL; default
68 * not used)
69 * @param string $id The ID name (OPTIONAL; default not used)
70 * @param string $onclick The onClick JavaScript handler (OPTIONAL;
71 * default not used)
72 * @param string $title The image's title attribute value
73 * (OPTIONAL; default not used)
74 * @param string $align The image's alignment attribute value
75 * (OPTIONAL; default not used)
76 * @param string $hspace The image's hspace attribute value
77 * (OPTIONAL; default not used)
78 * @param string $vspace The image's vspace attribute value
79 * (OPTIONAL; default not used)
80 * @param string $text_alternative A text replacement for the entire
81 * image tag, to be used at the
82 * discretion of the template set,
83 * if for some reason the image tag
84 * cannot or should not be produced
85 * (OPTIONAL; default not used)
86 *
87 * @return string The desired hyperlink tag.
88 *
89 * @since 1.5.2
90 *
91 */
92 function create_image($src, $alt='', $width='', $height='',
93 $border='', $class='', $id='', $onclick='',
94 $title='', $align='', $hspace='', $vspace='',
95 $text_alternative='') {
96
97 global $oTemplate;
98
99 $oTemplate->assign('src', $src);
100 $oTemplate->assign('alt', $alt);
101 $oTemplate->assign('width', $width);
102 $oTemplate->assign('height', $height);
103 $oTemplate->assign('border', $border);
104 $oTemplate->assign('class', $class);
105 $oTemplate->assign('id', $id);
106 $oTemplate->assign('onclick', $onclick);
107 $oTemplate->assign('title', $title);
108 $oTemplate->assign('align', $align);
109 $oTemplate->assign('hspace', $hspace);
110 $oTemplate->assign('vspace', $vspace);
111 $oTemplate->assign('text_alternative', $text_alternative);
112
113 return $oTemplate->fetch('image.tpl');
114
115 }
116
117
118 /**
119 * Generates a span tag
120 *
121 * @param string $value The contents that belong inside the span
122 * @param string $class The CSS class name (OPTIONAL; default
123 * not used)
124 * @param string $id The ID name (OPTIONAL; default not used)
125 *
126 * @return string The desired span tag.
127 *
128 * @since 1.5.2
129 *
130 */
131 function create_span($value, $class='', $id='') {
132
133 global $oTemplate;
134
135 $oTemplate->assign('value', $value);
136 $oTemplate->assign('class', $class);
137 $oTemplate->assign('id', $id);
138
139 return $oTemplate->fetch('span.tpl');
140
141 }
142
143
144 /**
145 * Generates html tags
146 //FIXME: this should not be used anywhere in the core, or we should
147 // convert this to use templates. We sould not be assuming HTML output.
148 *
149 * @param string $tag Tag to output
150 * @param string $val Value between tags
151 * @param string $align Alignment (left, center, etc)
152 * @param string $bgcolor Back color in hexadecimal
153 * @param string $xtra Extra options
154 * @return string HTML ready for output
155 * @since 1.3.0
156 */
157 function html_tag( $tag, // Tag to output
158 $val = '', // Value between tags
159 $align = '', // Alignment
160 $bgcolor = '', // Back color
161 $xtra = '' ) { // Extra options
162
163 GLOBAL $languages, $squirrelmail_language;
164
165 $align = strtolower( $align );
166 $bgc = '';
167 $tag = strtolower( $tag );
168
169 if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
170 $dir = $languages[$squirrelmail_language]['DIR'];
171 } else {
172 $dir = 'ltr';
173 }
174
175 if ( $dir == 'ltr' ) {
176 $rgt = 'right';
177 $lft = 'left';
178 } else {
179 $rgt = 'left';
180 $lft = 'right';
181 }
182
183 if ( $bgcolor <> '' ) {
184 $bgc = " bgcolor=\"$bgcolor\"";
185 }
186
187 switch ( $align ) {
188 case '':
189 $alg = '';
190 break;
191 case 'right':
192 $alg = " align=\"$rgt\"";
193 break;
194 case 'left':
195 $alg = " align=\"$lft\"";
196 break;
197 default:
198 $alg = " align=\"$align\"";
199 break;
200 }
201
202 $ret = "<$tag";
203
204 if ( $dir <> 'ltr' ) {
205 $ret .= " dir=\"$dir\"";
206 }
207 $ret .= $bgc . $alg;
208
209 if ( $xtra <> '' ) {
210 $ret .= " $xtra";
211 }
212
213 if ( $val <> '' ) {
214 $ret .= ">$val</$tag>\n";
215 } else {
216 $ret .= '>'. "\n";
217 }
218
219 return( $ret );
220 }
221
222
223 /**
224 * handy function to set url vars
225 *
226 * especially useful when $url = $PHP_SELF
227 * @param string $url url that must be modified
228 * @param string $var variable name
229 * @param string $val variable value
230 * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
231 * @return string $url modified url
232 * @since 1.3.0
233 */
234 function set_url_var($url, $var, $val=0, $link=true) {
235 $k = '';
236 $pat_a = array (
237 '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
238 '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
239 '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
240 '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
241 );
242 $url = str_replace('&amp;','&',$url);
243
244 // FIXME: why switch is used instead of if () or one preg_match()
245 switch (true) {
246 case (preg_match($pat_a[0],$url,$regs)):
247 $k = $regs[1];
248 $v = $regs[2];
249 break;
250 case (preg_match($pat_a[1],$url,$regs)):
251 $k = $regs[1];
252 $v = $regs[2];
253 break;
254 case (preg_match($pat_a[2],$url,$regs)):
255 $k = $regs[1];
256 $v = $regs[2];
257 break;
258 case (preg_match($pat_a[3],$url,$regs)):
259 $k = $regs[1];
260 $v = $regs[2];
261 break;
262 default:
263 if ($val) {
264 if (strpos($url,'?')) {
265 $url .= "&$var=$val";
266 } else {
267 $url .= "?$var=$val";
268 }
269 }
270 break;
271 }
272
273 if ($k) {
274 if ($val) {
275 $rpl = "$k=$val";
276 } else {
277 $rpl = '';
278 }
279 if( substr($v,-1)=='&' ) {
280 $rpl .= '&';
281 }
282 $pat = "/$k=$v/";
283 $url = preg_replace($pat,$rpl,$url);
284 }
285 if ($link) {
286 $url = str_replace('&','&amp;',$url);
287 }
288 return $url;
289 }
290
291