1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class HtmlTable
21: {
22: 23: 24: 25: 26:
27: private $caption;
28: 29: 30: 31: 32:
33: private $properties;
34: 35: 36: 37: 38:
39: private $tbody;
40: 41: 42: 43: 44:
45: private $tfoot;
46: 47: 48: 49: 50:
51: private $thead;
52:
53: 54: 55: 56: 57:
58: public function __construct($properties = array())
59: {
60: $this->tbody = array();
61: $this->tfoot = array();
62: $this->thead = array();
63: $this->properties = $properties;
64: }
65:
66: 67: 68: 69: 70:
71: public function addCaption($text)
72: {
73: $this->caption = $text;
74: }
75:
76: 77: 78: 79: 80:
81: public function addFooter(TableRow $row)
82: {
83: $this->tfoot[] = $row;
84: }
85:
86: 87: 88: 89: 90:
91: public function addHeader($headers)
92: {
93: $this->thead[] = $headers;
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function addRow($rows, $attributes = array())
103: {
104: $tr = TableRow::create($attributes);
105: foreach ($rows as $td) {
106: $tr->addCell($td);
107: }
108: $this->tbody[] = $tr;
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: public static function create($properties = array())
119: {
120: $obj = new static($properties);
121:
122: return $obj;
123: }
124:
125: 126: 127: 128: 129:
130: public function render()
131: {
132: $prop = array();
133: foreach ($this->properties as $k => $v) {
134: $prop[] = $k.'="'.$v.'"';
135: }
136: $table = array("\n<table".(empty($prop) ? '' : ' '.implode(' ', $prop)).'>');
137: if (!empty($this->caption)) {
138: $table[] = '<caption>'.$this->caption.'</caption>';
139: }
140:
141: if (!empty($this->thead)) {
142: $thead = array();
143: foreach ($this->thead as $r => $rows) {
144: $tr = array();
145: foreach ($rows as $c => $th) {
146: $prop = array('id' => 'id="c'.$c.'"', 'scope' => 'scope="col"');
147: foreach ($th as $key => $value) {
148: if ($key != 'text') {
149: $prop[$key] = $key.'="'.$value.'"';
150: }
151: }
152: $tr[] = '<th '.implode(' ', $prop).'>'.(isset($th['text']) ? $th['text'] : '').'</th>';
153: }
154: if (!empty($tr)) {
155: $thead[] = "<tr>\n".implode("\n", $tr)."\n</tr>";
156: }
157: }
158: if (!empty($thead)) {
159: $table[] = "<thead>\n".implode("\n", $thead)."\n</thead>";
160: }
161: }
162:
163: if (!empty($this->tfoot)) {
164: $rows = array();
165: foreach ($this->tfoot as $tr) {
166: $rows[] = $tr->render();
167: }
168: if (!empty($rows)) {
169: $table[] = "<tfoot>\n".implode("\n", $rows)."\n</tfoot>";
170: }
171: }
172:
173: if (!empty($this->tbody)) {
174: $rows = array();
175: foreach ($this->tbody as $tr) {
176: $rows[] = $tr->render();
177: }
178: if (!empty($rows)) {
179: $table[] = "<tbody>\n".implode("\n", $rows)."\n</tbody>";
180: }
181: }
182: $table[] = "</table>\n";
183:
184: return implode("\n", $table);
185: }
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: class TableRow
196: {
197: 198: 199: 200: 201:
202: private $properties;
203: 204: 205: 206: 207:
208: private $tds;
209:
210: 211: 212: 213: 214:
215: public function __construct($properties = array())
216: {
217: $this->properties = $properties;
218: $this->tds = array();
219: }
220:
221: 222: 223: 224: 225:
226: public function addCell($td)
227: {
228: $this->tds[] = $td;
229: }
230:
231: 232: 233: 234: 235: 236: 237:
238: public static function create($properties = array())
239: {
240: $obj = new static($properties);
241:
242: return $obj;
243: }
244:
245: 246: 247: 248: 249:
250: public function render()
251: {
252: $prop = array();
253: foreach ($this->properties as $key => $value) {
254: $prop[$key] = $key.'="'.$value.'"';
255: }
256: $row = array('<tr '.implode(' ', $prop).'>');
257: foreach ($this->tds as $c => $td) {
258: $prop = array();
259: $tag = 'td';
260: foreach ($td as $key => $value) {
261: if ($key == 'scope') {
262: $tag = 'th';
263: $prop['scope'] = 'scope="'.$value.'"';
264: if (isset($this->properties['id'])) {
265: $prop['id'] = 'id="r'.$this->properties['id'].'"';
266: }
267: } elseif ($key != 'text') {
268: $prop[$key] = $key.'="'.$value.'"';
269: }
270: }
271: if (isset($this->properties['id'])) {
272: $prop['headers'] = $tag == 'th' ? 'headers="c'.$c.'"' : 'headers="r'.$this->properties['id'].' c'.$c.'"';
273: }
274: $tr[] = '<'.$tag.' '.implode(' ', $prop).'>'.(isset($th['text']) ? $th['text'] : '').'</'.$tag.'>';
275: $row[] = '<'.$tag.' '.implode(' ', $prop).'>'.(empty($td['text']) ? '' : $td['text']).'</'.$tag.'>';
276: }
277: $row[] = '</tr>';
278:
279: return implode("\n", $row);
280: }
281: }
282: