Aleksander Machniak
2014-09-15 83a64265a7d8865f0381e53a0cc47d6ef53217b6
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;
e8bcf0 35     public static $common_attrib = array('id','class','style','title','align','unselectable','tabindex','role');
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,
fcb7d4 156             array('src','alt','width','height','border','usemap','onclick','onerror')));
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,
0ee2db 221             array('src','name','width','height','border','frameborder','onload','allowfullscreen')));
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
ebfdc0 286             // ignore not allowed attributes, except aria-* and data-*
5e8da2 287             if (!empty($allowed)) {
bc088f 288                 $is_data_attr = @substr_compare($key, 'data-', 0, 5) === 0;
e8bcf0 289                 $is_aria_attr = @substr_compare($key, 'aria-', 0, 5) === 0;
ebfdc0 290                 if (!$is_aria_attr && !$is_data_attr && !isset($allowed_f[$key])) {
5e8da2 291                     continue;
AM 292                 }
293             }
294
47124c 295             // skip empty eventhandlers
T 296             if (preg_match('/^on[a-z]+/', $key) && !$value) {
297                 continue;
298             }
299
300             // attributes with no value
60753b 301             if (in_array($key, array('checked', 'multiple', 'disabled', 'selected', 'autofocus'))) {
47124c 302                 if ($value) {
011e80 303                     $attrib_arr[] = $key . '="' . $key . '"';
47124c 304                 }
T 305             }
306             else {
d66e50 307                 $attrib_arr[] = $key . '="' . self::quote($value) . '"';
47124c 308             }
T 309         }
0c2596 310
47124c 311         return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
T 312     }
0c2596 313
A 314     /**
315      * Convert a HTML attribute string attributes to an associative array (name => value)
316      *
317      * @param string Input string
318      * @return array Key-value pairs of parsed attributes
319      */
320     public static function parse_attrib_string($str)
321     {
322         $attrib = array();
323         $regexp = '/\s*([-_a-z]+)=(["\'])??(?(2)([^\2]*)\2|(\S+?))/Ui';
324
325         preg_match_all($regexp, stripslashes($str), $regs, PREG_SET_ORDER);
326
327         // convert attributes to an associative array (name => value)
328         if ($regs) {
329             foreach ($regs as $attr) {
330                 $attrib[strtolower($attr[1])] = html_entity_decode($attr[3] . $attr[4]);
331             }
332         }
333
334         return $attrib;
335     }
336
337     /**
338      * Replacing specials characters in html attribute value
339      *
d66e50 340      * @param string $str Input string
0c2596 341      *
d66e50 342      * @return string The quoted string
0c2596 343      */
d66e50 344     public static function quote($str)
0c2596 345     {
4fdaa0 346         static $flags;
AM 347
348         if (!$flags) {
349             $flags = ENT_COMPAT;
350             if (defined('ENT_SUBSTITUTE')) {
351                 $flags |= ENT_SUBSTITUTE;
352             }
353         }
354
355         return @htmlspecialchars($str, $flags, RCUBE_CHARSET);
0c2596 356     }
47124c 357 }
0c2596 358
47124c 359
T 360 /**
361  * Class to create an HTML input field
362  *
a6fd15 363  * @package    Framework
AM 364  * @subpackage View
47124c 365  */
T 366 class html_inputfield extends html
367 {
368     protected $tagname = 'input';
369     protected $type = 'text';
0c2596 370     protected $allowed = array(
ec031a 371         'type','name','value','size','tabindex','autocapitalize','required',
fd6f6e 372         'autocomplete','checked','onchange','onclick','disabled','readonly',
4f53ab 373         'spellcheck','results','maxlength','src','multiple','accept',
TB 374         'placeholder','autofocus',
0c2596 375     );
47124c 376
5c461b 377     /**
A 378      * Object constructor
379      *
380      * @param array $attrib Associative array with tag attributes
381      */
47124c 382     public function __construct($attrib = array())
T 383     {
384         if (is_array($attrib)) {
385             $this->attrib = $attrib;
386         }
387
388         if ($attrib['type']) {
389             $this->type = $attrib['type'];
390         }
391     }
392
393     /**
394      * Compose input tag
395      *
5c461b 396      * @param string $value Field value
A 397      * @param array  $attrib Additional attributes to override
47124c 398      * @return string HTML output
T 399      */
400     public function show($value = null, $attrib = null)
401     {
402         // overwrite object attributes
403         if (is_array($attrib)) {
404             $this->attrib = array_merge($this->attrib, $attrib);
405         }
406
407         // set value attribute
408         if ($value !== null) {
409             $this->attrib['value'] = $value;
410         }
411         // set type
412         $this->attrib['type'] = $this->type;
413         return parent::show();
414     }
415 }
416
417 /**
418  * Class to create an HTML password field
419  *
a6fd15 420  * @package    Framework
AM 421  * @subpackage View
47124c 422  */
T 423 class html_passwordfield extends html_inputfield
424 {
425     protected $type = 'password';
426 }
427
428 /**
429  * Class to create an hidden HTML input field
430  *
a6fd15 431  * @package    Framework
AM 432  * @subpackage View
47124c 433  */
66d215 434 class html_hiddenfield extends html
47124c 435 {
66d215 436     protected $tagname = 'input';
47124c 437     protected $type = 'hidden';
T 438     protected $fields_arr = array();
66d215 439     protected $allowed = array('type','name','value','onchange','disabled','readonly');
47124c 440
T 441     /**
442      * Constructor
443      *
5c461b 444      * @param array $attrib Named tag attributes
47124c 445      */
T 446     public function __construct($attrib = null)
447     {
448         if (is_array($attrib)) {
449             $this->add($attrib);
450         }
451     }
452
453     /**
454      * Add a hidden field to this instance
455      *
5c461b 456      * @param array $attrib Named tag attributes
47124c 457      */
T 458     public function add($attrib)
459     {
460         $this->fields_arr[] = $attrib;
461     }
462
463     /**
464      * Create HTML code for the hidden fields
465      *
466      * @return string Final HTML code
467      */
468     public function show()
469     {
470         $out = '';
471         foreach ($this->fields_arr as $attrib) {
472             $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
473         }
474         return $out;
475     }
476 }
477
478 /**
479  * Class to create HTML radio buttons
480  *
a6fd15 481  * @package    Framework
AM 482  * @subpackage View
47124c 483  */
T 484 class html_radiobutton extends html_inputfield
485 {
486     protected $type = 'radio';
487
488     /**
489      * Get HTML code for this object
490      *
5c461b 491      * @param string $value  Value of the checked field
A 492      * @param array  $attrib Additional attributes to override
47124c 493      * @return string HTML output
T 494      */
495     public function show($value = '', $attrib = null)
496     {
497         // overwrite object attributes
498         if (is_array($attrib)) {
499             $this->attrib = array_merge($this->attrib, $attrib);
500         }
501
502         // set value attribute
ff6def 503         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 504
T 505         return parent::show();
506     }
507 }
508
509 /**
510  * Class to create HTML checkboxes
511  *
a6fd15 512  * @package    Framework
AM 513  * @subpackage View
47124c 514  */
T 515 class html_checkbox extends html_inputfield
516 {
517     protected $type = 'checkbox';
518
519     /**
520      * Get HTML code for this object
521      *
5c461b 522      * @param string $value  Value of the checked field
A 523      * @param array  $attrib Additional attributes to override
47124c 524      * @return string HTML output
T 525      */
526     public function show($value = '', $attrib = null)
527     {
528         // overwrite object attributes
529         if (is_array($attrib)) {
530             $this->attrib = array_merge($this->attrib, $attrib);
531         }
532
533         // set value attribute
ff6def 534         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 535
T 536         return parent::show();
537     }
538 }
539
540 /**
541  * Class to create an HTML textarea
542  *
a6fd15 543  * @package    Framework
AM 544  * @subpackage View
47124c 545  */
T 546 class html_textarea extends html
547 {
548     protected $tagname = 'textarea';
878030 549     protected $allowed = array('name','rows','cols','wrap','tabindex',
413df0 550         'onchange','disabled','readonly','spellcheck');
47124c 551
T 552     /**
553      * Get HTML code for this object
554      *
5c461b 555      * @param string $value  Textbox value
A 556      * @param array  $attrib Additional attributes to override
47124c 557      * @return string HTML output
T 558      */
559     public function show($value = '', $attrib = null)
560     {
561         // overwrite object attributes
562         if (is_array($attrib)) {
563             $this->attrib = array_merge($this->attrib, $attrib);
564         }
565
566         // take value attribute as content
54d830 567         if (empty($value) && !empty($this->attrib['value'])) {
47124c 568             $value = $this->attrib['value'];
T 569         }
570
571         // make shure we don't print the value attribute
572         if (isset($this->attrib['value'])) {
573             unset($this->attrib['value']);
574         }
575
0a1dd5 576         if (!empty($value) && empty($this->attrib['is_escaped'])) {
d66e50 577             $value = self::quote($value);
47124c 578         }
6025c8 579
878030 580         return self::tag($this->tagname, $this->attrib, $value,
413df0 581             array_merge(self::$common_attrib, $this->allowed));
47124c 582     }
T 583 }
584
585 /**
586  * Builder for HTML drop-down menus
587  * Syntax:<pre>
588  * // create instance. arguments are used to set attributes of select-tag
589  * $select = new html_select(array('name' => 'fieldname'));
590  *
591  * // add one option
592  * $select->add('Switzerland', 'CH');
593  *
594  * // add multiple options
595  * $select->add(array('Switzerland','Germany'), array('CH','DE'));
596  *
597  * // generate pulldown with selection 'Switzerland'  and return html-code
598  * // as second argument the same attributes available to instanciate can be used
599  * print $select->show('CH');
600  * </pre>
601  *
a6fd15 602  * @package    Framework
AM 603  * @subpackage View
47124c 604  */
T 605 class html_select extends html
606 {
607     protected $tagname = 'select';
608     protected $options = array();
878030 609     protected $allowed = array('name','size','tabindex','autocomplete',
413df0 610         'multiple','onchange','disabled','rel');
0c2596 611
47124c 612     /**
T 613      * Add a new option to this drop-down
614      *
5c461b 615      * @param mixed $names  Option name or array with option names
A 616      * @param mixed $values Option value or array with option values
b60857 617      * @param array $attrib Additional attributes for the option entry
47124c 618      */
b60857 619     public function add($names, $values = null, $attrib = array())
47124c 620     {
T 621         if (is_array($names)) {
622             foreach ($names as $i => $text) {
b60857 623                 $this->options[] = array('text' => $text, 'value' => $values[$i]) + $attrib;
47124c 624             }
T 625         }
626         else {
b60857 627             $this->options[] = array('text' => $names, 'value' => $values) + $attrib;
47124c 628         }
T 629     }
630
631     /**
632      * Get HTML code for this object
633      *
5c461b 634      * @param string $select Value of the selection option
A 635      * @param array  $attrib Additional attributes to override
47124c 636      * @return string HTML output
T 637      */
638     public function show($select = array(), $attrib = null)
639     {
640         // overwrite object attributes
641         if (is_array($attrib)) {
642             $this->attrib = array_merge($this->attrib, $attrib);
643         }
644
645         $this->content = "\n";
646         $select = (array)$select;
647         foreach ($this->options as $option) {
648             $attr = array(
649                 'value' => $option['value'],
ff6def 650                 'selected' => (in_array($option['value'], $select, true) ||
5b3dd4 651                   in_array($option['text'], $select, true)) ? 1 : null);
47124c 652
0a1dd5 653             $option_content = $option['text'];
AM 654             if (empty($this->attrib['is_escaped'])) {
d66e50 655                 $option_content = self::quote($option_content);
0a1dd5 656             }
AM 657
26830d 658             $this->content .= self::tag('option', $attr + $option, $option_content, array('value','label','class','style','title','disabled','selected'));
47124c 659         }
0c2596 660
47124c 661         return parent::show();
T 662     }
663 }
664
665
666 /**
667  * Class to build an HTML table
668  *
a6fd15 669  * @package    Framework
AM 670  * @subpackage View
47124c 671  */
T 672 class html_table extends html
673 {
674     protected $tagname = 'table';
878030 675     protected $allowed = array('id','class','style','width','summary',
413df0 676         'cellpadding','cellspacing','border');
878030 677
47124c 678     private $header = array();
T 679     private $rows = array();
680     private $rowindex = 0;
681     private $colindex = 0;
682
5c461b 683     /**
A 684      * Constructor
685      *
686      * @param array $attrib Named tag attributes
687      */
47124c 688     public function __construct($attrib = array())
T 689     {
7a3c0c 690         $default_attrib = self::$doctype == 'xhtml' ? array('summary' => '', 'border' => '0') : array();
AM 691         $this->attrib   = array_merge($attrib, $default_attrib);
517dae 692
TB 693         if (!empty($attrib['tagname']) && $attrib['tagname'] != 'table') {
694           $this->tagname = $attrib['tagname'];
695           $this->allowed = self::$common_attrib;
696         }
47124c 697     }
T 698
699     /**
700      * Add a table cell
701      *
5c461b 702      * @param array  $attr Cell attributes
A 703      * @param string $cont Cell content
47124c 704      */
T 705     public function add($attr, $cont)
706     {
707         if (is_string($attr)) {
708             $attr = array('class' => $attr);
709         }
710
711         $cell = new stdClass;
0d2144 712         $cell->attrib  = $attr;
47124c 713         $cell->content = $cont;
T 714
715         $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
ae44bf 716         $this->colindex += max(1, intval($attr['colspan']));
47124c 717
ae44bf 718         if ($this->attrib['cols'] && $this->colindex >= $this->attrib['cols']) {
47124c 719             $this->add_row();
T 720         }
721     }
722
723     /**
724      * Add a table header cell
725      *
5c461b 726      * @param array  $attr Cell attributes
A 727      * @param string $cont Cell content
47124c 728      */
83a763 729     public function add_header($attr, $cont)
47124c 730     {
413df0 731         if (is_string($attr)) {
AM 732             $attr = array('class' => $attr);
733         }
47124c 734
T 735         $cell = new stdClass;
0d2144 736         $cell->attrib   = $attr;
AM 737         $cell->content  = $cont;
47124c 738         $this->header[] = $cell;
T 739     }
740
0d2144 741     /**
cc97ea 742      * Remove a column from a table
T 743      * Useful for plugins making alterations
0d2144 744      *
AM 745      * @param string $class
cc97ea 746      */
T 747     public function remove_column($class)
748     {
749         // Remove the header
feac48 750         foreach ($this->header as $index=>$header){
A 751             if ($header->attrib['class'] == $class){
cc97ea 752                 unset($this->header[$index]);
T 753                 break;
754             }
755         }
756
757         // Remove cells from rows
feac48 758         foreach ($this->rows as $i=>$row){
A 759             foreach ($row->cells as $j=>$cell){
760                 if ($cell->attrib['class'] == $class){
cc97ea 761                     unset($this->rows[$i]->cells[$j]);
T 762                     break;
763                 }
764             }
765         }
766     }
767
47124c 768     /**
T 769      * Jump to next row
770      *
5c461b 771      * @param array $attr Row attributes
47124c 772      */
83a763 773     public function add_row($attr = array())
47124c 774     {
T 775         $this->rowindex++;
776         $this->colindex = 0;
777         $this->rows[$this->rowindex] = new stdClass;
778         $this->rows[$this->rowindex]->attrib = $attr;
779         $this->rows[$this->rowindex]->cells = array();
780     }
781
ffae15 782     /**
feac48 783      * Set row attributes
ffae15 784      *
feac48 785      * @param array $attr  Row attributes
A 786      * @param int   $index Optional row index (default current row index)
ffae15 787      */
feac48 788     public function set_row_attribs($attr = array(), $index = null)
ffae15 789     {
413df0 790         if (is_string($attr)) {
AM 791             $attr = array('class' => $attr);
792         }
ffae15 793
413df0 794         if ($index === null) {
feac48 795             $index = $this->rowindex;
413df0 796         }
feac48 797
cb34c1 798         // make sure row object exists (#1489094)
AM 799         if (!$this->rows[$index]) {
800             $this->rows[$index] = new stdClass;
801         }
802
24201d 803         $this->rows[$index]->attrib = $attr;
feac48 804     }
A 805
806     /**
807      * Get row attributes
808      *
809      * @param int $index Row index
810      *
811      * @return array Row attributes
812      */
813     public function get_row_attribs($index = null)
814     {
413df0 815         if ($index === null) {
feac48 816             $index = $this->rowindex;
413df0 817         }
feac48 818
A 819         return $this->rows[$index] ? $this->rows[$index]->attrib : null;
ffae15 820     }
47124c 821
T 822     /**
823      * Build HTML output of the table data
824      *
5c461b 825      * @param array $attrib Table attributes
47124c 826      * @return string The final table HTML code
T 827      */
46290a 828     public function show($attrib = null)
47124c 829     {
0d2144 830         if (is_array($attrib)) {
f92aba 831             $this->attrib = array_merge($this->attrib, $attrib);
0d2144 832         }
feac48 833
f92aba 834         $thead = $tbody = "";
47124c 835
T 836         // include <thead>
837         if (!empty($this->header)) {
838             $rowcontent = '';
839             foreach ($this->header as $c => $col) {
72afe3 840                 $rowcontent .= self::tag($this->_head_tagname(), $col->attrib, $col->content);
47124c 841             }
517dae 842             $thead = $this->tagname == 'table' ? self::tag('thead', null, self::tag('tr', null, $rowcontent, parent::$common_attrib)) :
TB 843                 self::tag($this->_row_tagname(), array('class' => 'thead'), $rowcontent, parent::$common_attrib);
47124c 844         }
T 845
846         foreach ($this->rows as $r => $row) {
847             $rowcontent = '';
848             foreach ($row->cells as $c => $col) {
517dae 849                 $rowcontent .= self::tag($this->_col_tagname(), $col->attrib, $col->content);
47124c 850             }
T 851
852             if ($r < $this->rowindex || count($row->cells)) {
517dae 853                 $tbody .= self::tag($this->_row_tagname(), $row->attrib, $rowcontent, parent::$common_attrib);
47124c 854             }
T 855         }
856
857         if ($this->attrib['rowsonly']) {
858             return $tbody;
859         }
860
861         // add <tbody>
517dae 862         $this->content = $thead . ($this->tagname == 'table' ? self::tag('tbody', null, $tbody) : $tbody);
47124c 863
T 864         unset($this->attrib['cols'], $this->attrib['rowsonly']);
865         return parent::show();
866     }
feac48 867
35c31e 868     /**
T 869      * Count number of rows
870      *
871      * @return The number of rows
872      */
873     public function size()
874     {
0d2144 875         return count($this->rows);
35c31e 876     }
6f6efa 877
A 878     /**
879      * Remove table body (all rows)
880      */
881     public function remove_body()
882     {
883         $this->rows     = array();
884         $this->rowindex = 0;
885     }
886
517dae 887     /**
TB 888      * Getter for the corresponding tag name for table row elements
889      */
890     private function _row_tagname()
891     {
892         static $row_tagnames = array('table' => 'tr', 'ul' => 'li', '*' => 'div');
72afe3 893         return $row_tagnames[$this->tagname] ?: $row_tagnames['*'];
TB 894     }
895
896     /**
897      * Getter for the corresponding tag name for table row elements
898      */
899     private function _head_tagname()
900     {
901         static $head_tagnames = array('table' => 'th', '*' => 'span');
902         return $head_tagnames[$this->tagname] ?: $head_tagnames['*'];
517dae 903     }
TB 904
905     /**
906      * Getter for the corresponding tag name for table cell elements
907      */
908     private function _col_tagname()
909     {
910         static $col_tagnames = array('table' => 'td', '*' => 'span');
72afe3 911         return $col_tagnames[$this->tagname] ?: $col_tagnames['*'];
517dae 912     }
TB 913
47124c 914 }