4 +-----------------------------------------------------------------------+
5 | program/include/html.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2009, 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();
32 protected $allowed = array();
35 public static $common_attrib = array('id','class','style','title','align');
36 public static $containers = array('iframe','div','span','p','h1','h2','h3','form','textarea','table','tr','th','td','style','script');
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, array_merge(self::$common_attrib, $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, array_merge(self::$common_attrib, array('onclick')));
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','onmousedown','onmouseup')));
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 to create <iframe></iframe>
186 * @param mixed Hash array with tag attributes or string with frame source (src)
187 * @return string HTML code
190 public static function iframe($attr = null, $cont = null)
192 if (is_string($attr)) {
193 $attr = array('src' => $attr);
195 return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib, array('src','name','width','height','border','frameborder')));
199 * Derrived method for line breaks
201 * @return string HTML code
204 public static function br()
206 return self::tag('br');
210 * Create string with attributes
212 * @param array Associative arry with tag attributes
213 * @param array List of allowed attributes
214 * @return string Valid attribute string
216 public static function attrib_string($attrib = array(), $allowed = null)
218 if (empty($attrib)) {
222 $allowed_f = array_flip((array)$allowed);
223 $attrib_arr = array();
224 foreach ($attrib as $key => $value) {
225 // skip size if not numeric
226 if (($key=='size' && !is_numeric($value))) {
230 // ignore "internal" or not allowed attributes
231 if ($key == 'nl' || ($allowed && !isset($allowed_f[$key])) || $value === null) {
235 // skip empty eventhandlers
236 if (preg_match('/^on[a-z]+/', $key) && !$value) {
240 // attributes with no value
241 if (in_array($key, array('checked', 'multiple', 'disabled', 'selected'))) {
243 $attrib_arr[] = sprintf('%s="%s"', $key, $key);
246 else if ($key=='value') {
247 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value, 'strict', false));
250 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value));
253 return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
258 * Class to create an HTML input field
262 class html_inputfield extends html
264 protected $tagname = 'input';
265 protected $type = 'text';
266 protected $allowed = array('type','name','value','size','tabindex','autocomplete','checked','onchange','onclick','disabled','readonly','spellcheck','results');
268 public function __construct($attrib = array())
270 if (is_array($attrib)) {
271 $this->attrib = $attrib;
274 if ($attrib['type']) {
275 $this->type = $attrib['type'];
278 if ($attrib['newline']) {
279 $this->newline = true;
286 * @param string Field value
287 * @param array Additional attributes to override
288 * @return string HTML output
290 public function show($value = null, $attrib = null)
292 // overwrite object attributes
293 if (is_array($attrib)) {
294 $this->attrib = array_merge($this->attrib, $attrib);
297 // set value attribute
298 if ($value !== null) {
299 $this->attrib['value'] = $value;
302 $this->attrib['type'] = $this->type;
303 return parent::show();
308 * Class to create an HTML password field
312 class html_passwordfield extends html_inputfield
314 protected $type = 'password';
318 * Class to create an hidden HTML input field
323 class html_hiddenfield extends html_inputfield
325 protected $type = 'hidden';
326 protected $fields_arr = array();
327 protected $newline = true;
332 * @param array Named tag attributes
334 public function __construct($attrib = null)
336 if (is_array($attrib)) {
342 * Add a hidden field to this instance
344 * @param array Named tag attributes
346 public function add($attrib)
348 $this->fields_arr[] = $attrib;
352 * Create HTML code for the hidden fields
354 * @return string Final HTML code
356 public function show()
359 foreach ($this->fields_arr as $attrib) {
360 $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
367 * Class to create HTML radio buttons
371 class html_radiobutton extends html_inputfield
373 protected $type = 'radio';
376 * Get HTML code for this object
378 * @param string Value of the checked field
379 * @param array Additional attributes to override
380 * @return string HTML output
382 public function show($value = '', $attrib = null)
384 // overwrite object attributes
385 if (is_array($attrib)) {
386 $this->attrib = array_merge($this->attrib, $attrib);
389 // set value attribute
390 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
392 return parent::show();
397 * Class to create HTML checkboxes
401 class html_checkbox extends html_inputfield
403 protected $type = 'checkbox';
406 * Get HTML code for this object
408 * @param string Value of the checked field
409 * @param array Additional attributes to override
410 * @return string HTML output
412 public function show($value = '', $attrib = null)
414 // overwrite object attributes
415 if (is_array($attrib)) {
416 $this->attrib = array_merge($this->attrib, $attrib);
419 // set value attribute
420 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
422 return parent::show();
427 * Class to create an HTML textarea
431 class html_textarea extends html
433 protected $tagname = 'textarea';
434 protected $allowed = array('name','rows','cols','wrap','tabindex','onchange','disabled','readonly','spellcheck');
437 * Get HTML code for this object
439 * @param string Textbox value
440 * @param array Additional attributes to override
441 * @return string HTML output
443 public function show($value = '', $attrib = null)
445 // overwrite object attributes
446 if (is_array($attrib)) {
447 $this->attrib = array_merge($this->attrib, $attrib);
450 // take value attribute as content
451 if (empty($value) && !empty($this->attrib['value'])) {
452 $value = $this->attrib['value'];
455 // make shure we don't print the value attribute
456 if (isset($this->attrib['value'])) {
457 unset($this->attrib['value']);
460 if (!empty($value) && !preg_match('/mce_editor/', $this->attrib['class'])) {
461 $value = Q($value, 'strict', false);
464 return self::tag($this->tagname, $this->attrib, $value, array_merge(self::$common_attrib, $this->allowed));
469 * Builder for HTML drop-down menus
471 * // create instance. arguments are used to set attributes of select-tag
472 * $select = new html_select(array('name' => 'fieldname'));
475 * $select->add('Switzerland', 'CH');
477 * // add multiple options
478 * $select->add(array('Switzerland','Germany'), array('CH','DE'));
480 * // generate pulldown with selection 'Switzerland' and return html-code
481 * // as second argument the same attributes available to instanciate can be used
482 * print $select->show('CH');
487 class html_select extends html
489 protected $tagname = 'select';
490 protected $options = array();
491 protected $allowed = array('name','size','tabindex','autocomplete','multiple','onchange','disabled');
494 * Add a new option to this drop-down
496 * @param mixed Option name or array with option names
497 * @param mixed Option value or array with option values
499 public function add($names, $values = null)
501 if (is_array($names)) {
502 foreach ($names as $i => $text) {
503 $this->options[] = array('text' => $text, 'value' => $values[$i]);
507 $this->options[] = array('text' => $names, 'value' => $values);
513 * Get HTML code for this object
515 * @param string Value of the selection option
516 * @param array Additional attributes to override
517 * @return string HTML output
519 public function show($select = array(), $attrib = null)
521 // overwrite object attributes
522 if (is_array($attrib)) {
523 $this->attrib = array_merge($this->attrib, $attrib);
526 $this->content = "\n";
527 $select = (array)$select;
528 foreach ($this->options as $option) {
530 'value' => $option['value'],
531 'selected' => (in_array($option['value'], $select, true) ||
532 in_array($option['text'], $select, true)) ? 1 : null);
534 $this->content .= self::tag('option', $attr, Q($option['text']));
536 return parent::show();
542 * Class to build an HTML table
546 class html_table extends html
548 protected $tagname = 'table';
549 protected $allowed = array('id','class','style','width','summary','cellpadding','cellspacing','border');
550 private $header = array();
551 private $rows = array();
552 private $rowindex = 0;
553 private $colindex = 0;
556 public function __construct($attrib = array())
558 $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
564 * @param array Cell attributes
565 * @param string Cell content
567 public function add($attr, $cont)
569 if (is_string($attr)) {
570 $attr = array('class' => $attr);
573 $cell = new stdClass;
574 $cell->attrib = $attr;
575 $cell->content = $cont;
577 $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
580 if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
586 * Add a table header cell
588 * @param array Cell attributes
589 * @param string Cell content
591 public function add_header($attr, $cont)
593 if (is_string($attr))
594 $attr = array('class' => $attr);
596 $cell = new stdClass;
597 $cell->attrib = $attr;
598 $cell->content = $cont;
599 $this->header[] = $cell;
603 * Remove a column from a table
604 * Useful for plugins making alterations
606 * @param string $class
608 public function remove_column($class)
611 foreach($this->header as $index=>$header){
612 if($header->attrib['class'] == $class){
613 unset($this->header[$index]);
618 // Remove cells from rows
619 foreach($this->rows as $i=>$row){
620 foreach($row->cells as $j=>$cell){
621 if($cell->attrib['class'] == $class){
622 unset($this->rows[$i]->cells[$j]);
633 * @param array Row attributes
635 public function add_row($attr = array())
639 $this->rows[$this->rowindex] = new stdClass;
640 $this->rows[$this->rowindex]->attrib = $attr;
641 $this->rows[$this->rowindex]->cells = array();
645 * Set current row attrib
647 * @param array Row attributes
649 public function set_row_attribs($attr = array())
651 if (is_string($attr))
652 $attr = array('class' => $attr);
654 $this->rows[$this->rowindex]->attrib = $attr;
658 * Build HTML output of the table data
660 * @param array Table attributes
661 * @return string The final table HTML code
663 public function show($attrib = null)
665 if (is_array($attrib))
666 $this->attrib = array_merge($this->attrib, $attrib);
668 $thead = $tbody = "";
671 if (!empty($this->header)) {
673 foreach ($this->header as $c => $col) {
674 $rowcontent .= self::tag('td', $col->attrib, $col->content);
676 $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent));
679 foreach ($this->rows as $r => $row) {
681 foreach ($row->cells as $c => $col) {
682 $rowcontent .= self::tag('td', $col->attrib, $col->content);
685 if ($r < $this->rowindex || count($row->cells)) {
686 $tbody .= self::tag('tr', $row->attrib, $rowcontent);
690 if ($this->attrib['rowsonly']) {
695 $this->content = $thead . self::tag('tbody', null, $tbody);
697 unset($this->attrib['cols'], $this->attrib['rowsonly']);
698 return parent::show();
702 * Count number of rows
704 * @return The number of rows
706 public function size()
708 return count($this->rows);