1<?php
2	/**************************************************************************\
3	* phpGroupWare - HTML creation class                                       *
4	* http://www.phpgroupware.org                                              *
5	* Written by Ralf Becker <RalfBecker@outdoor-training.de>                  *
6	* --------------------------------------------                             *
7	*  This program is free software; you can redistribute it and/or modify it *
8	*  under the terms of the GNU General Public License as published by the   *
9	*  Free Software Foundation; either version 2 of the License, or (at your  *
10	*  option) any later version.                                              *
11	\**************************************************************************/
12
13	/* $Id: class.html.inc.php 21060 2010-03-25 22:45:41Z Caeies $ */
14
15class html
16{
17	var $user_agent,$ua_version;	// 'mozilla','msie','konqueror'
18	var $prefered_img_title;
19
20	function html()
21	{																// should be Ok for all HTML 4 compatible browsers
22		if (!eregi('compatible; ([a-z_]+)[/ ]+([0-9.]+)',$_SERVER['HTTP_USER_AGENT'],$parts))
23		{
24			eregi('^([a-z_]+)/([0-9.]+)',$_SERVER['HTTP_USER_AGENT'],$parts);
25		}
26		list(,$this->user_agent,$this->ua_version) = $parts;
27		$this->user_agent = strtolower($this->user_agent);
28
29		$this->prefered_img_title = $this->user_agent == 'mozilla' && $this->ua_version < 5 ? 'ALT' : 'TITLE';
30		//echo "<p>HTTP_USER_AGENT='$GLOBALS[HTTP_USER_AGENT]', UserAgent: '$this->user_agent', Version: '$this->ua_version', img_title: '$this->prefered_img_title'</p>\n";
31	}
32
33	/*
34	* Function:		Allows to show and select one item from an array
35	*	Parameters:		$name		string with name of the submitted var which holds the key of the selected item form array
36	*						$key		key(s) of already selected item(s) from $arr, eg. '1' or '1,2' or array with keys
37	*						$arr		array with items to select, eg. $arr = array ( 'y' => 'yes','n' => 'no','m' => 'maybe');
38	*						$no_lang	if !$no_lang send items through lang()
39	*						$options	additional options (e.g. 'multiple')
40	* On submit		$XXX		is the key of the selected item (XXX is the content of $name)
41	* Returns:			string to set for a template or to echo into html page
42	*/
43	function select($name, $key, $arr=0,$no_lang=0,$options='',$multiple=0)
44	{
45		// should be in class common.sbox
46		if (!is_array($arr))
47		{
48			$arr = array('no','yes');
49		}
50		if (intval($multiple) > 0)
51		{
52			$options .= ' MULTIPLE SIZE="'.intval($multiple).'"';
53			if (substr($name,-2) != '[]')
54			{
55				$name .= '[]';
56			}
57		}
58		$out = "<select name=\"$name\" $options>\n";
59
60		if (is_array($key))
61		{
62			$key = implode(',',$key);
63		}
64		foreach($arr as $k => $text)
65		{
66			$out .= '<option value="'.htmlspecialchars($k).'"';
67
68			if("$k" == "$key" || strstr(",$key,",",$k,"))
69			{
70				$out .= " SELECTED";
71			}
72			$out .= ">" . ($no_lang || $text == '' ? $text : lang($text)) . "</option>\n";
73		}
74		$out .= "</select>\n";
75
76		return $out;
77	}
78
79	function div($content,$options='')
80	{
81		return "<DIV $options>\n$content</DIV>\n";
82	}
83
84	function input_hidden($vars,$value='',$ignore_empty=True)
85	{
86		if (!is_array($vars))
87		{
88			$vars = array( $vars => $value );
89		}
90		foreach($vars as $name => $value)
91		{
92			if (is_array($value))
93			{
94				$value = serialize($value);
95			}
96			if (!$ignore_empty || $value && !($name == 'filter' && $value == 'none'))	// dont need to send all the empty vars
97			{
98				$html .= "<INPUT TYPE=\"HIDDEN\" NAME=\"$name\" VALUE=\"".htmlspecialchars($value)."\">\n";
99			}
100		}
101		return $html;
102	}
103
104	function textarea($name,$value='',$options='' )
105	{
106		return "<TEXTAREA name=\"$name\" $options>".htmlspecialchars($value)."</TEXTAREA>\n";
107	}
108
109	function input($name,$value='',$type='',$options='' )
110	{
111		if ($type)
112		{
113			$type = 'TYPE="'.$type.'"';
114		}
115		return "<INPUT $type NAME=\"$name\" VALUE=\"".htmlspecialchars($value)."\" $options>\n";
116	}
117
118	function submit_button($name,$lang,$onClick='',$no_lang=0,$options='',$image='',$app='')
119	{
120		if ($image != '')
121		{
122			if (strpos($image,'.'))
123			{
124				$image = substr($image,0,strpos($image,'.'));
125			}
126			if (!($path = $GLOBALS['phpgw']->common->image($app,$image)) &&
127			    !($path = $GLOBALS['phpgw']->common->image('phpgwapi',$image)))
128			{
129				$path = $image;		// name may already contain absolut path
130			}
131			$image = ' SRC="'.$path.'"';
132		}
133		if (!$no_lang)
134		{
135			$lang = lang($lang);
136		}
137		if (($accesskey = strstr($lang,'&')) && $accesskey[1] != ' ' &&
138			(($pos = strpos($accesskey,';')) === False || $pos > 5))
139		{
140			$lang_u = str_replace('&'.$accesskey[1],'<u>'.$accesskey[1].'</u>',$lang);
141			$lang = str_replace('&','',$lang);
142			$options = 'ACCESSKEY="'.$accesskey[1].'" '.$options;
143		}
144		else
145		{
146			$accesskey = '';
147			$lang_u = $lang;
148		}
149		if ($onClick) $options .= " onClick=\"$onClick\"";
150
151		// <button> is not working in all cases if ($this->user_agent == 'mozilla' && $this->ua_version < 5 || $image)
152		{
153			return $this->input($name,$lang,$image != '' ? 'IMAGE' : 'SUBMIT',$options.$image);
154		}
155		return '<button TYPE="submit" NAME="'.$name.'" VALUE="'.$lang.'" '.$options.'>'.
156			($image != '' ? "<img$image $this->prefered_img_title=\"$lang\"> " : '').
157			($image == '' || $accesskey ? $lang_u : '').'</button>';
158	}
159
160	/*!
161	@function link
162	@abstract creates an absolut link + the query / get-variables
163	@param $url phpgw-relative link, may include query / get-vars
164	@parm $vars query or array ('name' => 'value', ...) with query
165	@example link('/index.php?menuaction=infolog.uiinfolog.get_list',array('info_id' => 123))
166	@example  = 'http://domain/phpgw-path/index.php?menuaction=infolog.uiinfolog.get_list&info_id=123'
167	@result absolut link already run through $phpgw->link
168	*/
169	function link($url,$vars='')
170	{
171		if (!is_array($vars))
172		{
173			$vars = explode('&',$vars);
174		}
175		list($url,$v) = explode('?',$url);	// url may contain additional vars
176		if ($v)
177		{
178			$vars += explode('&',$v);
179		}
180		return $GLOBALS['phpgw']->link($url,$vars);
181	}
182
183	function checkbox($name,$value='')
184	{
185		return "<input type=\"checkbox\" name=\"$name\" value=\"True\"" .($value ? ' checked' : '') . ">\n";
186	}
187
188	function form($content,$hidden_vars,$url,$url_vars='',$name='',$options='',$method='POST')
189	{
190		$html = "<form method=\"$method\" ".($name != '' ? "name=\"$name\" " : '')."action=\"".$this->link($url,$url_vars)."\" $options>\n";
191		$html .= $this->input_hidden($hidden_vars);
192
193		if ($content)
194		{
195			$html .= $content;
196			$html .= "</form>\n";
197		}
198		return $html;
199	}
200
201	function form_1button($name,$lang,$hidden_vars,$url,$url_vars='',$form_name='',$method='POST')
202	{
203		return $this->form($this->submit_button($name,$lang),
204			$hidden_vars,$url,$url_vars,$form_name,'',$method);
205	}
206
207	/*!
208	@function table
209	@abstracts creates table from array with rows
210	@discussion abstract the html stuff
211	@param $rows array with rows, each row is an array of the cols
212	@param $options options for the table-tag
213	@example $rows = array ( '1'  => array( 1 => 'cell1', '.1' => 'colspan=3',
214	@example                                2 => 'cell2', 3 => 'cell3', '.3' => 'width="10%"' ),
215	@example                 '.1' => 'BGCOLOR="#0000FF"' );
216	@example table($rows,'WIDTH="100%"') = '<table WIDTH="100%"><tr><td colspan=3>cell1</td><td>cell2</td><td width="10%">cell3</td></tr></table>'
217	@result string with html-code of the table
218	*/
219	function table($rows,$options = '',$no_table_tr=False)
220	{
221		$html = $no_table_tr ? '' : "<TABLE $options>\n";
222
223		foreach($rows as $key => $row)
224		{
225			if (!is_array($row))
226			{
227				continue;					// parameter
228			}
229			$html .= $no_table_tr && $key == 1 ? '' : "\t<TR ".$rows['.'.$key].">\n";
230
231			foreach($row as $key => $cell)
232			{
233				if ($key[0] == '.')
234				{
235					continue;				// parameter
236				}
237				$table_pos = strpos($cell,'<TABLE');
238				$td_pos = strpos($cell,'<TD');
239				if ($td_pos !== False && ($table_pos === False || $td_pos < $table_pos))
240				{
241					$html .= $cell;
242				}
243				else
244				{
245					$html .= "\t\t<TD ".$row['.'.$key].">$cell</TD>\n";
246				}
247			}
248			$html .= "\t</TR>\n";
249		}
250		$html .= "</TABLE>\n";
251
252		if ($no_table_tr)
253		{
254			$html = substr($html,0,-16);
255		}
256		return $html;
257	}
258
259	function sbox_submit( $sbox,$no_script=0 )
260	{
261		$html = str_replace('<select','<select onChange="this.form.submit()" ',
262								  $sbox);
263		if ($no_script)
264		{
265			$html .= '<noscript>'.$this->submit_button('send','>').'</noscript>';
266		}
267		return $html;
268	}
269
270	function image( $app,$name,$title='',$options='' )
271	{
272		if (strstr($name,'.') === False)
273		{
274			$name .= '.gif';
275		}
276		if (!($path = $GLOBALS['phpgw']->common->image($app,$name)))
277		{
278			$path = $name;		// name may already contain absolut path
279		}
280		if (!@is_readable($_SERVER['DOCUMENT_ROOT'] . $path))
281		{
282			return $title;
283		}
284		if ($title)
285		{
286			$options .= " $this->prefered_img_title=\"".htmlspecialchars($title).'"';
287		}
288		return "<IMG SRC=\"$path\" $options>";
289	}
290
291	function a_href( $content,$url,$vars='',$options='')
292	{
293		if (!strstr($url,'/') && count(explode('.',$url)) == 3)
294		{
295			$url = "/index.php?menuaction=$url";
296		}
297		if (is_array($url))
298		{
299			$vars = $url;
300			$url = '/index.php';
301		}
302		return '<a href="'.$this->link($url,$vars).'" '.$options.'>'.$content.'</a>';
303	}
304
305	function bold($content)
306	{
307		return '<b>'.$content.'</b>';
308	}
309
310	function italic($content)
311	{
312		return '<i>'.$content.'</i>';
313	}
314
315	function hr($width,$options='')
316	{
317		if ($width)
318			$options .= " WIDTH=$width";
319		return "<hr $options>\n";
320	}
321
322	/*!
323	@function formatOptions
324	@abstract formats option-string for most of the above functions
325	@param $options String (or Array) with option-values eg. '100%,,1'
326	@param $names String (or Array) with the option-names eg. 'WIDTH,HEIGHT,BORDER'
327	@example formatOptions('100%,,1','WIDTH,HEIGHT,BORDER') = ' WIDTH="100%" BORDER="1"'
328	@result option string
329	*/
330	function formatOptions($options,$names)
331	{
332		if (!is_array($options)) $options = explode(',',$options);
333		if (!is_array($names))   $names   = explode(',',$names);
334
335		while (list($n,$val) = each($options))
336			if ($val != '' && $names[$n] != '')
337				$html .= ' '.$names[$n].'="'.$val.'"';
338
339		return $html;
340	}
341
342	/*!
343	@function themeStyles
344	@abstract returns simple stylesheet (incl. <STYLE> tags) for nextmatch row-colors
345	@result the classes 'th' = nextmatch header, 'row_on'+'row_off' = alternating rows
346	*/
347	function themeStyles()
348	{
349		return $this->style($this->theme2css());
350	}
351
352	/*!
353	@function theme2css
354	@abstract returns simple stylesheet for nextmatch row-colors
355	@result the classes 'th' = nextmatch header, 'row_on'+'row_off' = alternating rows
356	*/
357	function theme2css()
358	{
359		return
360			".th { background: ".$GLOBALS['phpgw_info']['theme']['th_bg']."; font-weight: bold; }\n".
361			".row_on,.th_bright { background: ".$GLOBALS['phpgw_info']['theme']['row_on']."; }\n".
362			".row_off { background: ".$GLOBALS['phpgw_info']['theme']['row_off']."; }\n";
363	}
364
365	function style($styles)
366	{
367		return $styles ? "<STYLE type=\"text/css\">\n<!--\n$styles\n-->\n</STYLE>" : '';
368	}
369
370	function label($content,$id='',$accesskey='',$options='')
371	{
372		if ($id != '')
373		{
374			$id = " FOR=\"$id\"";
375		}
376		if ($accesskey != '')
377		{
378			$accesskey = " ACCESSKEY=\"$accesskey\"";
379		}
380		return "<LABEL$id$accesskey $options>$content</LABEL>";
381	}
382}
383