Aleksander Machniak
2014-05-08 7079110c61d41020912baab91cef8b3a3acdb577
commit | author | age
47124c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
e019f2 5  | This file is part of the Roundcube Webmail client                     |
0933d6 6  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
7fe381 7  |                                                                       |
T 8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
47124c 11  |                                                                       |
T 12  | PURPOSE:                                                              |
13  |   Helper class to create valid XHTML code                             |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
041c93 17 */
47124c 18
T 19
20 /**
21  * Class for HTML code creation
22  *
9ab346 23  * @package    Framework
a6fd15 24  * @subpackage View
47124c 25  */
T 26 class html
27 {
28     protected $tagname;
29     protected $attrib = array();
e3e597 30     protected $allowed = array();
47124c 31     protected $content;
T 32
f23073 33     public static $doctype = 'xhtml';
47124c 34     public static $lc_tags = true;
0933d6 35     public static $common_attrib = array('id','class','style','title','align','unselectable');
0b1de8 36     public static $containers = array('iframe','div','span','p','h1','h2','h3','ul','form','textarea','table','thead','tbody','tr','th','td','style','script');
47124c 37
4fdaa0 38
47124c 39     /**
T 40      * Constructor
41      *
5c461b 42      * @param array $attrib Hash array with tag attributes
47124c 43      */
T 44     public function __construct($attrib = array())
45     {
46         if (is_array($attrib)) {
47             $this->attrib = $attrib;
48         }
49     }
50
51     /**
52      * Return the tag code
53      *
54      * @return string The finally composed HTML tag
55      */
56     public function show()
57     {
e3e597 58         return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed));
47124c 59     }
T 60
61     /****** STATIC METHODS *******/
62
63     /**
64      * Generic method to create a HTML tag
65      *
5c461b 66      * @param string $tagname Tag name
A 67      * @param array  $attrib  Tag attributes as key/value pairs
68      * @param string $content Optinal Tag content (creates a container tag)
69      * @param array  $allowed_attrib List with allowed attributes, omit to allow all
47124c 70      * @return string The XHTML tag
T 71      */
72     public static function tag($tagname, $attrib = array(), $content = null, $allowed_attrib = null)
73     {
0501b6 74         if (is_string($attrib))
T 75             $attrib = array('class' => $attrib);
76
47124c 77         $inline_tags = array('a','span','img');
T 78         $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : '';
79
80         $tagname = self::$lc_tags ? strtolower($tagname) : $tagname;
448409 81         if (isset($content) || in_array($tagname, self::$containers)) {
011e80 82             $suffix = $attrib['noclose'] ? $suffix : '</' . $tagname . '>' . $suffix;
T 83             unset($attrib['noclose'], $attrib['nl']);
84             return '<' . $tagname  . self::attrib_string($attrib, $allowed_attrib) . '>' . $content . $suffix;
47124c 85         }
T 86         else {
011e80 87             return '<' . $tagname  . self::attrib_string($attrib, $allowed_attrib) . '>' . $suffix;
47124c 88         }
f23073 89     }
T 90
91     /**
92      *
93      */
94     public static function doctype($type)
95     {
96         $doctypes = array(
97             'html5'        => '<!DOCTYPE html>',
98             'xhtml'        => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
99             'xhtml-trans'  => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
100             'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
101         );
102
103         if ($doctypes[$type]) {
104             self::$doctype = preg_replace('/-\w+$/', '', $type);
105             return $doctypes[$type];
106         }
107
108         return '';
47124c 109     }
T 110
111     /**
112      * Derrived method for <div> containers
113      *
5c461b 114      * @param mixed  $attr Hash array with tag attributes or string with class name
A 115      * @param string $cont Div content
47124c 116      * @return string HTML code
T 117      * @see html::tag()
118      */
119     public static function div($attr = null, $cont = null)
120     {
121         if (is_string($attr)) {
122             $attr = array('class' => $attr);
123         }
f5aa16 124         return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick')));
47124c 125     }
T 126
127     /**
128      * Derrived method for <p> blocks
129      *
5c461b 130      * @param mixed  $attr Hash array with tag attributes or string with class name
A 131      * @param string $cont Paragraph content
47124c 132      * @return string HTML code
T 133      * @see html::tag()
134      */
135     public static function p($attr = null, $cont = null)
136     {
137         if (is_string($attr)) {
138             $attr = array('class' => $attr);
139         }
140         return self::tag('p', $attr, $cont, self::$common_attrib);
141     }
142
143     /**
144      * Derrived method to create <img />
145      *
5c461b 146      * @param mixed $attr Hash array with tag attributes or string with image source (src)
47124c 147      * @return string HTML code
T 148      * @see html::tag()
149      */
150     public static function img($attr = null)
151     {
152         if (is_string($attr)) {
153             $attr = array('src' => $attr);
154         }
878030 155         return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib,
315418 156             array('src','alt','width','height','border','usemap','onclick')));
47124c 157     }
T 158
159     /**
160      * Derrived method for link tags
161      *
5c461b 162      * @param mixed  $attr Hash array with tag attributes or string with link location (href)
A 163      * @param string $cont Link content
47124c 164      * @return string HTML code
T 165      * @see html::tag()
166      */
167     public static function a($attr, $cont)
168     {
169         if (is_string($attr)) {
170             $attr = array('href' => $attr);
171         }
878030 172         return self::tag('a', $attr, $cont, array_merge(self::$common_attrib,
0d2144 173             array('href','target','name','rel','onclick','onmouseover','onmouseout','onmousedown','onmouseup')));
47124c 174     }
T 175
176     /**
177      * Derrived method for inline span tags
178      *
5c461b 179      * @param mixed  $attr Hash array with tag attributes or string with class name
A 180      * @param string $cont Tag content
47124c 181      * @return string HTML code
T 182      * @see html::tag()
183      */
184     public static function span($attr, $cont)
185     {
186         if (is_string($attr)) {
187             $attr = array('class' => $attr);
188         }
189         return self::tag('span', $attr, $cont, self::$common_attrib);
190     }
191
192     /**
193      * Derrived method for form element labels
194      *
5c461b 195      * @param mixed  $attr Hash array with tag attributes or string with 'for' attrib
A 196      * @param string $cont Tag content
47124c 197      * @return string HTML code
T 198      * @see html::tag()
199      */
200     public static function label($attr, $cont)
201     {
202         if (is_string($attr)) {
203             $attr = array('for' => $attr);
204         }
205         return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for')));
206     }
207
208     /**
95fcc3 209      * Derrived method to create <iframe></iframe>
T 210      *
5c461b 211      * @param mixed $attr Hash array with tag attributes or string with frame source (src)
95fcc3 212      * @return string HTML code
T 213      * @see html::tag()
214      */
215     public static function iframe($attr = null, $cont = null)
216     {
217         if (is_string($attr)) {
218             $attr = array('src' => $attr);
219         }
878030 220         return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib,
fe6268 221             array('src','name','width','height','border','frameborder','onload')));
011e80 222     }
T 223
224     /**
225      * Derrived method to create <script> tags
226      *
227      * @param mixed $attr Hash array with tag attributes or string with script source (src)
3a7dec 228      * @param string $cont Javascript code to be placed as tag content
011e80 229      * @return string HTML code
T 230      * @see html::tag()
231      */
232     public static function script($attr, $cont = null)
233     {
234         if (is_string($attr)) {
235             $attr = array('src' => $attr);
236         }
237         if ($cont) {
238             if (self::$doctype == 'xhtml')
239                 $cont = "\n/* <![CDATA[ */\n" . $cont . "\n/* ]]> */\n";
240             else
241                 $cont = "\n" . $cont . "\n";
242         }
243
244         return self::tag('script', $attr + array('type' => 'text/javascript', 'nl' => true),
245             $cont, array_merge(self::$common_attrib, array('src','type','charset')));
95fcc3 246     }
T 247
248     /**
47124c 249      * Derrived method for line breaks
T 250      *
251      * @return string HTML code
252      * @see html::tag()
253      */
031491 254     public static function br($attrib = array())
47124c 255     {
031491 256         return self::tag('br', $attrib);
47124c 257     }
T 258
259     /**
260      * Create string with attributes
261      *
5c461b 262      * @param array $attrib Associative arry with tag attributes
A 263      * @param array $allowed List of allowed attributes
47124c 264      * @return string Valid attribute string
T 265      */
266     public static function attrib_string($attrib = array(), $allowed = null)
267     {
268         if (empty($attrib)) {
269             return '';
270         }
271
5e8da2 272         $allowed_f  = array_flip((array)$allowed);
47124c 273         $attrib_arr = array();
5e8da2 274
47124c 275         foreach ($attrib as $key => $value) {
T 276             // skip size if not numeric
0c2596 277             if ($key == 'size' && !is_numeric($value)) {
47124c 278                 continue;
T 279             }
280
5e8da2 281             // ignore "internal" or empty attributes
AM 282             if ($key == 'nl' || $value === null) {
47124c 283                 continue;
T 284             }
285
707911 286             // ignore not allowed attributes, except data-*
5e8da2 287             if (!empty($allowed)) {
707911 288                 if (!isset($allowed_f[$key]) && @substr_compare($key, 'data-', 0, 5) !== 0) {
5e8da2 289                     continue;
AM 290                 }
291             }
292
47124c 293             // skip empty eventhandlers
T 294             if (preg_match('/^on[a-z]+/', $key) && !$value) {
295                 continue;
296             }
297
298             // attributes with no value
60753b 299             if (in_array($key, array('checked', 'multiple', 'disabled', 'selected', 'autofocus'))) {
47124c 300                 if ($value) {
011e80 301                     $attrib_arr[] = $key . '="' . $key . '"';
47124c 302                 }
T 303             }
304             else {
d66e50 305                 $attrib_arr[] = $key . '="' . self::quote($value) . '"';
47124c 306             }
T 307         }
0c2596 308
47124c 309         return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
T 310     }
0c2596 311
A 312     /**
313      * Convert a HTML attribute string attributes to an associative array (name => value)
314      *
315      * @param string Input string
316      * @return array Key-value pairs of parsed attributes
317      */
318     public static function parse_attrib_string($str)
319     {
320         $attrib = array();
321         $regexp = '/\s*([-_a-z]+)=(["\'])??(?(2)([^\2]*)\2|(\S+?))/Ui';
322
323         preg_match_all($regexp, stripslashes($str), $regs, PREG_SET_ORDER);
324
325         // convert attributes to an associative array (name => value)
326         if ($regs) {
327             foreach ($regs as $attr) {
328                 $attrib[strtolower($attr[1])] = html_entity_decode($attr[3] . $attr[4]);
329             }
330         }
331
332         return $attrib;
333     }
334
335     /**
336      * Replacing specials characters in html attribute value
337      *
d66e50 338      * @param string $str Input string
0c2596 339      *
d66e50 340      * @return string The quoted string
0c2596 341      */
d66e50 342     public static function quote($str)
0c2596 343     {
4fdaa0 344         static $flags;
AM 345
346         if (!$flags) {
347             $flags = ENT_COMPAT;
348             if (defined('ENT_SUBSTITUTE')) {
349                 $flags |= ENT_SUBSTITUTE;
350             }
351         }
352
353         return @htmlspecialchars($str, $flags, RCUBE_CHARSET);
0c2596 354     }
47124c 355 }
0c2596 356
47124c 357
T 358 /**
359  * Class to create an HTML input field
360  *
a6fd15 361  * @package    Framework
AM 362  * @subpackage View
47124c 363  */
T 364 class html_inputfield extends html
365 {
366     protected $tagname = 'input';
367     protected $type = 'text';
0c2596 368     protected $allowed = array(
ec031a 369         'type','name','value','size','tabindex','autocapitalize','required',
fd6f6e 370         'autocomplete','checked','onchange','onclick','disabled','readonly',
4f53ab 371         'spellcheck','results','maxlength','src','multiple','accept',
TB 372         'placeholder','autofocus',
0c2596 373     );
47124c 374
5c461b 375     /**
A 376      * Object constructor
377      *
378      * @param array $attrib Associative array with tag attributes
379      */
47124c 380     public function __construct($attrib = array())
T 381     {
382         if (is_array($attrib)) {
383             $this->attrib = $attrib;
384         }
385
386         if ($attrib['type']) {
387             $this->type = $attrib['type'];
388         }
389     }
390
391     /**
392      * Compose input tag
393      *
5c461b 394      * @param string $value Field value
A 395      * @param array  $attrib Additional attributes to override
47124c 396      * @return string HTML output
T 397      */
398     public function show($value = null, $attrib = null)
399     {
400         // overwrite object attributes
401         if (is_array($attrib)) {
402             $this->attrib = array_merge($this->attrib, $attrib);
403         }
404
405         // set value attribute
406         if ($value !== null) {
407             $this->attrib['value'] = $value;
408         }
409         // set type
410         $this->attrib['type'] = $this->type;
411         return parent::show();
412     }
413 }
414
415 /**
416  * Class to create an HTML password field
417  *
a6fd15 418  * @package    Framework
AM 419  * @subpackage View
47124c 420  */
T 421 class html_passwordfield extends html_inputfield
422 {
423     protected $type = 'password';
424 }
425
426 /**
427  * Class to create an hidden HTML input field
428  *
a6fd15 429  * @package    Framework
AM 430  * @subpackage View
47124c 431  */
66d215 432 class html_hiddenfield extends html
47124c 433 {
66d215 434     protected $tagname = 'input';
47124c 435     protected $type = 'hidden';
T 436     protected $fields_arr = array();
66d215 437     protected $allowed = array('type','name','value','onchange','disabled','readonly');
47124c 438
T 439     /**
440      * Constructor
441      *
5c461b 442      * @param array $attrib Named tag attributes
47124c 443      */
T 444     public function __construct($attrib = null)
445     {
446         if (is_array($attrib)) {
447             $this->add($attrib);
448         }
449     }
450
451     /**
452      * Add a hidden field to this instance
453      *
5c461b 454      * @param array $attrib Named tag attributes
47124c 455      */
T 456     public function add($attrib)
457     {
458         $this->fields_arr[] = $attrib;
459     }
460
461     /**
462      * Create HTML code for the hidden fields
463      *
464      * @return string Final HTML code
465      */
466     public function show()
467     {
468         $out = '';
469         foreach ($this->fields_arr as $attrib) {
470             $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
471         }
472         return $out;
473     }
474 }
475
476 /**
477  * Class to create HTML radio buttons
478  *
a6fd15 479  * @package    Framework
AM 480  * @subpackage View
47124c 481  */
T 482 class html_radiobutton extends html_inputfield
483 {
484     protected $type = 'radio';
485
486     /**
487      * Get HTML code for this object
488      *
5c461b 489      * @param string $value  Value of the checked field
A 490      * @param array  $attrib Additional attributes to override
47124c 491      * @return string HTML output
T 492      */
493     public function show($value = '', $attrib = null)
494     {
495         // overwrite object attributes
496         if (is_array($attrib)) {
497             $this->attrib = array_merge($this->attrib, $attrib);
498         }
499
500         // set value attribute
ff6def 501         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 502
T 503         return parent::show();
504     }
505 }
506
507 /**
508  * Class to create HTML checkboxes
509  *
a6fd15 510  * @package    Framework
AM 511  * @subpackage View
47124c 512  */
T 513 class html_checkbox extends html_inputfield
514 {
515     protected $type = 'checkbox';
516
517     /**
518      * Get HTML code for this object
519      *
5c461b 520      * @param string $value  Value of the checked field
A 521      * @param array  $attrib Additional attributes to override
47124c 522      * @return string HTML output
T 523      */
524     public function show($value = '', $attrib = null)
525     {
526         // overwrite object attributes
527         if (is_array($attrib)) {
528             $this->attrib = array_merge($this->attrib, $attrib);
529         }
530
531         // set value attribute
ff6def 532         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 533
T 534         return parent::show();
535     }
536 }
537
538 /**
539  * Class to create an HTML textarea
540  *
a6fd15 541  * @package    Framework
AM 542  * @subpackage View
47124c 543  */
T 544 class html_textarea extends html
545 {
546     protected $tagname = 'textarea';
878030 547     protected $allowed = array('name','rows','cols','wrap','tabindex',
413df0 548         'onchange','disabled','readonly','spellcheck');
47124c 549
T 550     /**
551      * Get HTML code for this object
552      *
5c461b 553      * @param string $value  Textbox value
A 554      * @param array  $attrib Additional attributes to override
47124c 555      * @return string HTML output
T 556      */
557     public function show($value = '', $attrib = null)
558     {
559         // overwrite object attributes
560         if (is_array($attrib)) {
561             $this->attrib = array_merge($this->attrib, $attrib);
562         }
563
564         // take value attribute as content
54d830 565         if (empty($value) && !empty($this->attrib['value'])) {
47124c 566             $value = $this->attrib['value'];
T 567         }
568
569         // make shure we don't print the value attribute
570         if (isset($this->attrib['value'])) {
571             unset($this->attrib['value']);
572         }
573
0a1dd5 574         if (!empty($value) && empty($this->attrib['is_escaped'])) {
d66e50 575             $value = self::quote($value);
47124c 576         }
6025c8 577
878030 578         return self::tag($this->tagname, $this->attrib, $value,
413df0 579             array_merge(self::$common_attrib, $this->allowed));
47124c 580     }
T 581 }
582
583 /**
584  * Builder for HTML drop-down menus
585  * Syntax:<pre>
586  * // create instance. arguments are used to set attributes of select-tag
587  * $select = new html_select(array('name' => 'fieldname'));
588  *
589  * // add one option
590  * $select->add('Switzerland', 'CH');
591  *
592  * // add multiple options
593  * $select->add(array('Switzerland','Germany'), array('CH','DE'));
594  *
595  * // generate pulldown with selection 'Switzerland'  and return html-code
596  * // as second argument the same attributes available to instanciate can be used
597  * print $select->show('CH');
598  * </pre>
599  *
a6fd15 600  * @package    Framework
AM 601  * @subpackage View
47124c 602  */
T 603 class html_select extends html
604 {
605     protected $tagname = 'select';
606     protected $options = array();
878030 607     protected $allowed = array('name','size','tabindex','autocomplete',
413df0 608         'multiple','onchange','disabled','rel');
0c2596 609
47124c 610     /**
T 611      * Add a new option to this drop-down
612      *
5c461b 613      * @param mixed $names  Option name or array with option names
A 614      * @param mixed $values Option value or array with option values
b60857 615      * @param array $attrib Additional attributes for the option entry
47124c 616      */
b60857 617     public function add($names, $values = null, $attrib = array())
47124c 618     {
T 619         if (is_array($names)) {
620             foreach ($names as $i => $text) {
b60857 621                 $this->options[] = array('text' => $text, 'value' => $values[$i]) + $attrib;
47124c 622             }
T 623         }
624         else {
b60857 625             $this->options[] = array('text' => $names, 'value' => $values) + $attrib;
47124c 626         }
T 627     }
628
629     /**
630      * Get HTML code for this object
631      *
5c461b 632      * @param string $select Value of the selection option
A 633      * @param array  $attrib Additional attributes to override
47124c 634      * @return string HTML output
T 635      */
636     public function show($select = array(), $attrib = null)
637     {
638         // overwrite object attributes
639         if (is_array($attrib)) {
640             $this->attrib = array_merge($this->attrib, $attrib);
641         }
642
643         $this->content = "\n";
644         $select = (array)$select;
645         foreach ($this->options as $option) {
646             $attr = array(
647                 'value' => $option['value'],
ff6def 648                 'selected' => (in_array($option['value'], $select, true) ||
5b3dd4 649                   in_array($option['text'], $select, true)) ? 1 : null);
47124c 650
0a1dd5 651             $option_content = $option['text'];
AM 652             if (empty($this->attrib['is_escaped'])) {
d66e50 653                 $option_content = self::quote($option_content);
0a1dd5 654             }
AM 655
26830d 656             $this->content .= self::tag('option', $attr + $option, $option_content, array('value','label','class','style','title','disabled','selected'));
47124c 657         }
0c2596 658
47124c 659         return parent::show();
T 660     }
661 }
662
663
664 /**
665  * Class to build an HTML table
666  *
a6fd15 667  * @package    Framework
AM 668  * @subpackage View
47124c 669  */
T 670 class html_table extends html
671 {
672     protected $tagname = 'table';
878030 673     protected $allowed = array('id','class','style','width','summary',
413df0 674         'cellpadding','cellspacing','border');
878030 675
47124c 676     private $header = array();
T 677     private $rows = array();
678     private $rowindex = 0;
679     private $colindex = 0;
680
5c461b 681     /**
A 682      * Constructor
683      *
684      * @param array $attrib Named tag attributes
685      */
47124c 686     public function __construct($attrib = array())
T 687     {
7a3c0c 688         $default_attrib = self::$doctype == 'xhtml' ? array('summary' => '', 'border' => '0') : array();
AM 689         $this->attrib   = array_merge($attrib, $default_attrib);
517dae 690
TB 691         if (!empty($attrib['tagname']) && $attrib['tagname'] != 'table') {
692           $this->tagname = $attrib['tagname'];
693           $this->allowed = self::$common_attrib;
694         }
47124c 695     }
T 696
697     /**
698      * Add a table cell
699      *
5c461b 700      * @param array  $attr Cell attributes
A 701      * @param string $cont Cell content
47124c 702      */
T 703     public function add($attr, $cont)
704     {
705         if (is_string($attr)) {
706             $attr = array('class' => $attr);
707         }
708
709         $cell = new stdClass;
0d2144 710         $cell->attrib  = $attr;
47124c 711         $cell->content = $cont;
T 712
713         $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
ae44bf 714         $this->colindex += max(1, intval($attr['colspan']));
47124c 715
ae44bf 716         if ($this->attrib['cols'] && $this->colindex >= $this->attrib['cols']) {
47124c 717             $this->add_row();
T 718         }
719     }
720
721     /**
722      * Add a table header cell
723      *
5c461b 724      * @param array  $attr Cell attributes
A 725      * @param string $cont Cell content
47124c 726      */
83a763 727     public function add_header($attr, $cont)
47124c 728     {
413df0 729         if (is_string($attr)) {
AM 730             $attr = array('class' => $attr);
731         }
47124c 732
T 733         $cell = new stdClass;
0d2144 734         $cell->attrib   = $attr;
AM 735         $cell->content  = $cont;
47124c 736         $this->header[] = $cell;
T 737     }
738
0d2144 739     /**
cc97ea 740      * Remove a column from a table
T 741      * Useful for plugins making alterations
0d2144 742      *
AM 743      * @param string $class
cc97ea 744      */
T 745     public function remove_column($class)
746     {
747         // Remove the header
feac48 748         foreach ($this->header as $index=>$header){
A 749             if ($header->attrib['class'] == $class){
cc97ea 750                 unset($this->header[$index]);
T 751                 break;
752             }
753         }
754
755         // Remove cells from rows
feac48 756         foreach ($this->rows as $i=>$row){
A 757             foreach ($row->cells as $j=>$cell){
758                 if ($cell->attrib['class'] == $class){
cc97ea 759                     unset($this->rows[$i]->cells[$j]);
T 760                     break;
761                 }
762             }
763         }
764     }
765
47124c 766     /**
T 767      * Jump to next row
768      *
5c461b 769      * @param array $attr Row attributes
47124c 770      */
83a763 771     public function add_row($attr = array())
47124c 772     {
T 773         $this->rowindex++;
774         $this->colindex = 0;
775         $this->rows[$this->rowindex] = new stdClass;
776         $this->rows[$this->rowindex]->attrib = $attr;
777         $this->rows[$this->rowindex]->cells = array();
778     }
779
ffae15 780     /**
feac48 781      * Set row attributes
ffae15 782      *
feac48 783      * @param array $attr  Row attributes
A 784      * @param int   $index Optional row index (default current row index)
ffae15 785      */
feac48 786     public function set_row_attribs($attr = array(), $index = null)
ffae15 787     {
413df0 788         if (is_string($attr)) {
AM 789             $attr = array('class' => $attr);
790         }
ffae15 791
413df0 792         if ($index === null) {
feac48 793             $index = $this->rowindex;
413df0 794         }
feac48 795
cb34c1 796         // make sure row object exists (#1489094)
AM 797         if (!$this->rows[$index]) {
798             $this->rows[$index] = new stdClass;
799         }
800
24201d 801         $this->rows[$index]->attrib = $attr;
feac48 802     }
A 803
804     /**
805      * Get row attributes
806      *
807      * @param int $index Row index
808      *
809      * @return array Row attributes
810      */
811     public function get_row_attribs($index = null)
812     {
413df0 813         if ($index === null) {
feac48 814             $index = $this->rowindex;
413df0 815         }
feac48 816
A 817         return $this->rows[$index] ? $this->rows[$index]->attrib : null;
ffae15 818     }
47124c 819
T 820     /**
821      * Build HTML output of the table data
822      *
5c461b 823      * @param array $attrib Table attributes
47124c 824      * @return string The final table HTML code
T 825      */
46290a 826     public function show($attrib = null)
47124c 827     {
0d2144 828         if (is_array($attrib)) {
f92aba 829             $this->attrib = array_merge($this->attrib, $attrib);
0d2144 830         }
feac48 831
f92aba 832         $thead = $tbody = "";
47124c 833
T 834         // include <thead>
835         if (!empty($this->header)) {
836             $rowcontent = '';
837             foreach ($this->header as $c => $col) {
517dae 838                 $rowcontent .= self::tag($this->_col_tagname(), $col->attrib, $col->content);
47124c 839             }
517dae 840             $thead = $this->tagname == 'table' ? self::tag('thead', null, self::tag('tr', null, $rowcontent, parent::$common_attrib)) :
TB 841                 self::tag($this->_row_tagname(), array('class' => 'thead'), $rowcontent, parent::$common_attrib);
47124c 842         }
T 843
844         foreach ($this->rows as $r => $row) {
845             $rowcontent = '';
846             foreach ($row->cells as $c => $col) {
517dae 847                 $rowcontent .= self::tag($this->_col_tagname(), $col->attrib, $col->content);
47124c 848             }
T 849
850             if ($r < $this->rowindex || count($row->cells)) {
517dae 851                 $tbody .= self::tag($this->_row_tagname(), $row->attrib, $rowcontent, parent::$common_attrib);
47124c 852             }
T 853         }
854
855         if ($this->attrib['rowsonly']) {
856             return $tbody;
857         }
858
859         // add <tbody>
517dae 860         $this->content = $thead . ($this->tagname == 'table' ? self::tag('tbody', null, $tbody) : $tbody);
47124c 861
T 862         unset($this->attrib['cols'], $this->attrib['rowsonly']);
863         return parent::show();
864     }
feac48 865
35c31e 866     /**
T 867      * Count number of rows
868      *
869      * @return The number of rows
870      */
871     public function size()
872     {
0d2144 873         return count($this->rows);
35c31e 874     }
6f6efa 875
A 876     /**
877      * Remove table body (all rows)
878      */
879     public function remove_body()
880     {
881         $this->rows     = array();
882         $this->rowindex = 0;
883     }
884
517dae 885     /**
TB 886      * Getter for the corresponding tag name for table row elements
887      */
888     private function _row_tagname()
889     {
890         static $row_tagnames = array('table' => 'tr', 'ul' => 'li', '*' => 'div');
d924eb 891         return $row_tagnames[$this->tagname] ? $row_tagnames[$this->tagname] : $row_tagnames['*'];
517dae 892     }
TB 893
894     /**
895      * Getter for the corresponding tag name for table cell elements
896      */
897     private function _col_tagname()
898     {
899         static $col_tagnames = array('table' => 'td', '*' => 'span');
d924eb 900         return $col_tagnames[$this->tagname] ? $col_tagnames[$this->tagname] : $col_tagnames['*'];
517dae 901     }
TB 902
47124c 903 }