1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22class CTable extends CTag { 23 24 protected $colgroup; 25 protected $header; 26 protected $footer; 27 protected $colnum; 28 protected $rownum; 29 protected $heading_column; 30 31 public function __construct() { 32 parent::__construct('table', true); 33 $this->rownum = 0; 34 $this->colgroup = ''; 35 $this->header = ''; 36 $this->footer = ''; 37 $this->colnum = 1; 38 $this->heading_column = null; 39 } 40 41 public function setCellPadding($value) { 42 $this->attributes['cellpadding'] = strval($value); 43 return $this; 44 } 45 46 public function setCellSpacing($value) { 47 $this->attributes['cellspacing'] = strval($value); 48 return $this; 49 } 50 51 public function prepareRow($item, $class = null, $id = null) { 52 if ($item === null) { 53 return null; 54 } 55 56 if ($item instanceof CCol) { 57 if (isset($this->header) && !isset($item->attributes['colspan'])) { 58 $item->attributes['colspan'] = $this->colnum; 59 } 60 } 61 62 if (!($item instanceof CRow)) { 63 $item = new CRow($item, $this->heading_column); 64 if ($id !== null) { 65 $item->setId($id); 66 } 67 } 68 69 if ($class !== null) { 70 $item->addClass($class); 71 } 72 73 return $item; 74 } 75 76 /** 77 * Setup table column styles by colgroup and setup table headers. 78 * Note: should not be used together with setHeader() function. 79 * 80 * @param array $columns Array with CTableColumn elements. 81 * 82 * @return CTable 83 */ 84 public function setColumns(array $columns = []): self { 85 $headers = []; 86 $cols = []; 87 88 foreach ($columns as $col) { 89 if ($col instanceof CTableColumn) { 90 $headers[] = $col->getHeader(); 91 $cols[] = $col; 92 } 93 } 94 95 $this->colgroup = new CTag('colgroup', true, $cols); 96 $this->setHeader($headers); 97 98 return $this; 99 } 100 101 /** 102 * Setup table header row. 103 * Note: should not be used together with setColumns() function. 104 * 105 * @param mixed $value Table header row or array with table header cells. 106 * 107 * @return CTable 108 */ 109 public function setHeader($value = null) { 110 if (!($value instanceof CRow)) { 111 $value = new CRowHeader($value); 112 } 113 114 $this->colnum = $value->itemsCount(); 115 116 $value = new CTag('thead', true, $value); 117 $this->header = $value->toString(); 118 119 return $this; 120 } 121 122 /** 123 * Format given column as row header. 124 * 125 * @param int|null $heading_column Column index for heading column. Starts from 0. 'null' if no heading column. 126 * 127 * @return CTable 128 */ 129 public function setHeadingColumn($heading_column) { 130 $this->heading_column = $heading_column; 131 132 return $this; 133 } 134 135 public function setFooter($value = null, $class = null) { 136 $this->footer = $this->prepareRow($value, $class); 137 $this->footer = $this->footer->toString(); 138 return $this; 139 } 140 141 public function addRow($item, $class = null, $id = null) { 142 $this->addItem($this->prepareRow($item, $class, $id)); 143 ++$this->rownum; 144 return $this; 145 } 146 147 public function getNumCols() { 148 return $this->colnum; 149 } 150 151 public function getNumRows() { 152 return $this->rownum; 153 } 154 155 protected function startToString() { 156 $ret = parent::startToString(); 157 $ret .= $this->colgroup; 158 $ret .= $this->header; 159 $ret .= '<tbody>'; 160 return $ret; 161 } 162 163 protected function endToString() { 164 $ret = $this->footer; 165 $ret .= '</tbody>'; 166 $ret .= parent::endToString(); 167 return $ret; 168 } 169} 170