4 +-----------------------------------------------------------------------+
5 | program/include/html.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2008, RoundCube Dev, - Switzerland |
9 | Licensed under the GNU GPL |
12 | Helper class to create valid XHTML code |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
24 * Class for HTML code creation
31 protected $attrib = array();
35 public static $common_attrib = array('id','class','style','title','align');
36 public static $containers = array('div','span','p','h1','h2','h3','form','textarea');
37 public static $lc_tags = true;
42 * @param array Hash array with tag attributes
44 public function __construct($attrib = array())
46 if (is_array($attrib)) {
47 $this->attrib = $attrib;
54 * @return string The finally composed HTML tag
56 public function show()
58 return self::tag($this->tagname, $this->attrib, $this->content, $this->allowed);
61 /****** STATIC METHODS *******/
64 * Generic method to create a HTML tag
66 * @param string Tag name
67 * @param array Tag attributes as key/value pairs
68 * @param string Optinal Tag content (creates a container tag)
69 * @param array List with allowed attributes, omit to allow all
70 * @return string The XHTML tag
72 public static function tag($tagname, $attrib = array(), $content = null, $allowed_attrib = null)
74 $inline_tags = array('a','span','img');
75 $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : '';
77 $tagname = self::$lc_tags ? strtolower($tagname) : $tagname;
78 if ($content || in_array($tagname, self::$containers)) {
79 $templ = $attrib['noclose'] ? "<%s%s>%s" : "<%s%s>%s</%s>%s";
80 unset($attrib['noclose']);
81 return sprintf($templ, $tagname, self::attrib_string($attrib, $allowed_attrib), $content, $tagname, $suffix);
84 return sprintf("<%s%s />%s", $tagname, self::attrib_string($attrib, $allowed_attrib), $suffix);
89 * Derrived method for <div> containers
91 * @param mixed Hash array with tag attributes or string with class name
92 * @param string Div content
93 * @return string HTML code
96 public static function div($attr = null, $cont = null)
98 if (is_string($attr)) {
99 $attr = array('class' => $attr);
101 return self::tag('div', $attr, $cont, self::$common_attrib);
105 * Derrived method for <p> blocks
107 * @param mixed Hash array with tag attributes or string with class name
108 * @param string Paragraph content
109 * @return string HTML code
112 public static function p($attr = null, $cont = null)
114 if (is_string($attr)) {
115 $attr = array('class' => $attr);
117 return self::tag('p', $attr, $cont, self::$common_attrib);
121 * Derrived method to create <img />
123 * @param mixed Hash array with tag attributes or string with image source (src)
124 * @return string HTML code
127 public static function img($attr = null)
129 if (is_string($attr)) {
130 $attr = array('src' => $attr);
132 return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib, array('src','alt','width','height','border','usemap')));
136 * Derrived method for link tags
138 * @param mixed Hash array with tag attributes or string with link location (href)
139 * @param string Link content
140 * @return string HTML code
143 public static function a($attr, $cont)
145 if (is_string($attr)) {
146 $attr = array('href' => $attr);
148 return self::tag('a', $attr, $cont, array_merge(self::$common_attrib, array('href','target','name','onclick','onmouseover','onmouseout')));
152 * Derrived method for inline span tags
154 * @param mixed Hash array with tag attributes or string with class name
155 * @param string Tag content
156 * @return string HTML code
159 public static function span($attr, $cont)
161 if (is_string($attr)) {
162 $attr = array('class' => $attr);
164 return self::tag('span', $attr, $cont, self::$common_attrib);
168 * Derrived method for form element labels
170 * @param mixed Hash array with tag attributes or string with 'for' attrib
171 * @param string Tag content
172 * @return string HTML code
175 public static function label($attr, $cont)
177 if (is_string($attr)) {
178 $attr = array('for' => $attr);
180 return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for')));
184 * Derrived method for line breaks
186 * @return string HTML code
189 public static function br()
191 return self::tag('br');
195 * Create string with attributes
197 * @param array Associative arry with tag attributes
198 * @param array List of allowed attributes
199 * @return string Valid attribute string
201 public static function attrib_string($attrib = array(), $allowed = null)
203 if (empty($attrib)) {
207 $allowed_f = array_flip((array)$allowed);
208 $attrib_arr = array();
209 foreach ($attrib as $key => $value) {
210 // skip size if not numeric
211 if (($key=='size' && !is_numeric($value))) {
215 // ignore "internal" or not allowed attributes
216 if ($key == 'nl' || ($allowed && !isset($allowed_f[$key])) || $value === null) {
220 // skip empty eventhandlers
221 if (preg_match('/^on[a-z]+/', $key) && !$value) {
225 // attributes with no value
226 if (in_array($key, array('checked', 'multiple', 'disabled', 'selected'))) {
228 $attrib_arr[] = sprintf('%s="%s"', $key, $key);
231 else if ($key=='value') {
232 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value, 'strict', false));
235 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value));
238 return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
243 * Class to create an HTML input field
247 class html_inputfield extends html
249 protected $tagname = 'input';
250 protected $type = 'text';
252 public function __construct($attrib = array())
254 if (is_array($attrib)) {
255 $this->attrib = $attrib;
258 if ($attrib['type']) {
259 $this->type = $attrib['type'];
262 if ($attrib['newline']) {
263 $this->newline = true;
270 * @param string Field value
271 * @param array Additional attributes to override
272 * @return string HTML output
274 public function show($value = null, $attrib = null)
276 // overwrite object attributes
277 if (is_array($attrib)) {
278 $this->attrib = array_merge($this->attrib, $attrib);
281 // set value attribute
282 if ($value !== null) {
283 $this->attrib['value'] = $value;
286 $this->attrib['type'] = $this->type;
287 return parent::show();
292 * Class to create an HTML password field
296 class html_passwordfield extends html_inputfield
298 protected $type = 'password';
302 * Class to create an hidden HTML input field
307 class html_hiddenfield extends html_inputfield
309 protected $type = 'hidden';
310 protected $fields_arr = array();
311 protected $newline = true;
316 * @param array Named tag attributes
318 public function __construct($attrib = null)
320 if (is_array($attrib)) {
326 * Add a hidden field to this instance
328 * @param array Named tag attributes
330 public function add($attrib)
332 $this->fields_arr[] = $attrib;
336 * Create HTML code for the hidden fields
338 * @return string Final HTML code
340 public function show()
343 foreach ($this->fields_arr as $attrib) {
344 $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
351 * Class to create HTML radio buttons
355 class html_radiobutton extends html_inputfield
357 protected $type = 'radio';
360 * Get HTML code for this object
362 * @param string Value of the checked field
363 * @param array Additional attributes to override
364 * @return string HTML output
366 public function show($value = '', $attrib = null)
368 // overwrite object attributes
369 if (is_array($attrib)) {
370 $this->attrib = array_merge($this->attrib, $attrib);
373 // set value attribute
374 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
376 return parent::show();
381 * Class to create HTML checkboxes
385 class html_checkbox extends html_inputfield
387 protected $type = 'checkbox';
390 * Get HTML code for this object
392 * @param string Value of the checked field
393 * @param array Additional attributes to override
394 * @return string HTML output
396 public function show($value = '', $attrib = null)
398 // overwrite object attributes
399 if (is_array($attrib)) {
400 $this->attrib = array_merge($this->attrib, $attrib);
403 // set value attribute
404 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
406 return parent::show();
411 * Class to create an HTML textarea
415 class html_textarea extends html
417 protected $tagname = 'textarea';
418 protected $allowed_attrib = array('name','rows','cols','wrap','tabindex');
421 * Get HTML code for this object
423 * @param string Textbox value
424 * @param array Additional attributes to override
425 * @return string HTML output
427 public function show($value = '', $attrib = null)
429 // overwrite object attributes
430 if (is_array($attrib)) {
431 $this->attrib = array_merge($this->attrib, $attrib);
434 // take value attribute as content
435 if (empty($value) && !empty($this->attrib['value'])) {
436 $value = $this->attrib['value'];
439 // make shure we don't print the value attribute
440 if (isset($this->attrib['value'])) {
441 unset($this->attrib['value']);
444 if (!empty($value) && !isset($this->attrib['mce_editable'])) {
445 $value = Q($value, 'strict', false);
447 return self::tag($this->tagname, $this->attrib, $value, array_merge(self::$common_attrib, $this->allowed_attrib));
452 * Builder for HTML drop-down menus
454 * // create instance. arguments are used to set attributes of select-tag
455 * $select = new html_select(array('name' => 'fieldname'));
458 * $select->add('Switzerland', 'CH');
460 * // add multiple options
461 * $select->add(array('Switzerland','Germany'), array('CH','DE'));
463 * // generate pulldown with selection 'Switzerland' and return html-code
464 * // as second argument the same attributes available to instanciate can be used
465 * print $select->show('CH');
470 class html_select extends html
472 protected $tagname = 'select';
473 protected $options = array();
476 * Add a new option to this drop-down
478 * @param mixed Option name or array with option names
479 * @param mixed Option value or array with option values
481 public function add($names, $values = null)
483 if (is_array($names)) {
484 foreach ($names as $i => $text) {
485 $this->options[] = array('text' => $text, 'value' => $values[$i]);
489 $this->options[] = array('text' => $names, 'value' => $values);
495 * Get HTML code for this object
497 * @param string Value of the selection option
498 * @param array Additional attributes to override
499 * @return string HTML output
501 public function show($select = array(), $attrib = null)
503 // overwrite object attributes
504 if (is_array($attrib)) {
505 $this->attrib = array_merge($this->attrib, $attrib);
508 $this->content = "\n";
509 $select = (array)$select;
510 foreach ($this->options as $option) {
512 'value' => $option['value'],
513 'selected' => (in_array($option['value'], $select, true) ||
514 in_array($option['text'], $select, true)) ? 1 : null);
516 $this->content .= self::tag('option', $attr, Q($option['text']));
518 return parent::show();
524 * Class to build an HTML table
528 class html_table extends html
530 protected $tagname = 'table';
531 protected $allowed = array('id','class','style','width','summary','cellpadding','cellspacing','border');
532 private $header = array();
533 private $rows = array();
534 private $rowindex = 0;
535 private $colindex = 0;
538 public function __construct($attrib = array())
540 $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
546 * @param array Cell attributes
547 * @param string Cell content
549 public function add($attr, $cont)
551 if (is_string($attr)) {
552 $attr = array('class' => $attr);
555 $cell = new stdClass;
556 $cell->attrib = $attr;
557 $cell->content = $cont;
559 $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
562 if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
568 * Add a table header cell
570 * @param array Cell attributes
571 * @param string Cell content
573 private function add_header($attr, $cont)
575 if (is_string($attr))
576 $attr = array('class' => $attr);
578 $cell = new stdClass;
579 $cell->attrib = $attr;
580 $cell->content = $cont;
581 $this->header[] = $cell;
587 * @param array Row attributes
589 private function add_row($attr = array())
593 $this->rows[$this->rowindex] = new stdClass;
594 $this->rows[$this->rowindex]->attrib = $attr;
595 $this->rows[$this->rowindex]->cells = array();
600 * Build HTML output of the table data
602 * @param array Table attributes
603 * @return string The final table HTML code
605 public function show($attr = array())
607 $this->attrib = array_merge($this->attrib, $attr);
608 $thead = $tbody = "";
611 if (!empty($this->header)) {
613 foreach ($this->header as $c => $col) {
614 $rowcontent .= self::tag('th', $col->attrib, $col->content);
616 $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent));
619 foreach ($this->rows as $r => $row) {
621 foreach ($row->cells as $c => $col) {
622 $rowcontent .= self::tag('td', $col->attrib, $col->content);
625 if ($r < $this->rowindex || count($row->cells)) {
626 $tbody .= self::tag('tr', $rows->attrib, $rowcontent);
630 if ($this->attrib['rowsonly']) {
635 $this->content = $thead . self::tag('tbody', null, $tbody);
637 unset($this->attrib['cols'], $this->attrib['rowsonly']);
638 return parent::show();