1: <?php
2:
3: /**
4: * Document for class GeoTable
5: *
6: * PHP Version 5.6
7: *
8: * @category Class
9: * @package Geolib
10: * @author Peter Pitchford <peter@geotonics.com>
11: * @license GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
12: * @link http://geotonics.com/#geolib
13: */
14:
15: /**
16: * Geo - Class to create html table tags
17: *
18: * @category Html_Tag
19: * @package Geolib
20: * @author Peter Pitchford <peter@geotonics.com>
21: * @license GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
22: * @version Release: .1
23: * @link http://geotonics.com/#geolib
24: * @since Class available since Release .1
25: */
26: class GeoTable extends GeoTag
27: {
28: protected $rows = array();
29: private $colAligns = array();
30: private $rowAligns = array();
31: private $colValigns = array();
32: private $rowClasses = array();
33: private $titles = array();
34: private $rowValigns = array();
35: private $isRows = array();
36:
37: /**
38: * Class constructor
39: *
40: * @param array $rows table content
41: * @param string $class class of table
42: * @param array $rowClasses classes for table rows
43: * @param string $colClasses classes for table columns
44: * @param string $id id of table
45: * @param int $cellspacing cellspacing of table
46: * @param int $cellpadding cellpadding of table
47: * @param int $border html width of table border
48: * @param int $width width of table
49: * @param string $colValigns valigns for table columns (each cell in the column will have this valign value)
50: * @param string $rowValigns valigns for table rows
51: * @param string $colAligns aligns for table columns (each cell in the column will have this align value)
52: * @param string $rowAligns aligns for table rows
53: */
54: public function __construct(
55: $rows = null,
56: $class = null,
57: $rowClasses = null,
58: $colClasses = null,
59: $id = null,
60: $cellspacing = 0,
61: $cellpadding = 0,
62: $border = 0,
63: $width = null,
64: $colValigns = null,
65: $rowValigns = null,
66: $colAligns = null,
67: $rowAligns = null
68: ) {
69: $this->setAtt("width", $width);
70:
71: if ($cellspacing) {
72: $this->setAtt("cellspacing", $cellspacing);
73: }
74:
75: $this->setAtt("cellpadding", $cellpadding);
76:
77: if ($border) {
78: $this->setAtt("border", $border);
79: }
80:
81: $this->setRowClasses($rowClasses);
82: $this->setColClasses($colClasses);
83: $this->colAligns=$colAligns;
84: $this->rowAligns=$rowAligns;
85: $this->colValigns=$colValigns;
86: $this->rowValigns=$rowValigns;
87:
88: $this->init('table', $rows, $class, $id);
89: }
90:
91: /**
92: * Set table content
93: *
94: * @param array $rows 1 or 2 dimensional array for row content
95: *
96: * @return void
97: */
98: public function setText($rows)
99: {
100: if ($rows) {
101: $rows = Geo::arr($rows);
102: $this->rows = $rows;
103: }
104: }
105:
106: /**
107: * Set table titles
108: *
109: * @param array $titles Set Alternative method for setting first row of content.
110: *
111: * @return void
112: */
113: public function setTitles($titles)
114: {
115: $this->titles = $titles;
116: }
117:
118: /**
119: * Set table column classes
120: *
121: * @param array $colClasses column classes
122: * Each row in this array becomes the class foir each cell in the table row
123: *
124: * @return void
125: */
126: public function setColClasses($colClasses = null)
127: {
128: $this->colClasses = $colClasses;
129: }
130:
131: /**
132: * Set table row classes
133: *
134: * @param array $rowClasses row classes
135: *
136: * @return void
137: */
138: public function setRowClasses($rowClasses = null)
139: {
140: $this->rowClasses = $rowClasses;
141: }
142:
143: /**
144: * Set table cell classes
145: *
146: * @param array $cellClasses 2 d array of cell classes
147: *
148: * @return void
149: */
150: public function setCellClasses($cellClasses = null)
151: {
152: $this->cellClasses = $cellClasses;
153: }
154:
155: /**
156: * Set table row styles
157: *
158: * @param array $rowStyles Styles for each row
159: *
160: * @return void
161: */
162: public function setRowStyles($rowStyles = null)
163: {
164: $this->rowStyles = $rowStyles;
165:
166: }
167:
168: /**
169: * Set table cell styles
170: *
171: * @param array $cellStyles 2 d array of cell styles
172: *
173: * @return void
174: */
175: public function setCellStyles($cellStyles = null)
176: {
177: $this->cellStyles = $cellStyles;
178:
179: }
180:
181: /**
182: * Set flag to indicate content style
183: *
184: * @param array $isRows Flag to indicate content is html, not an array
185: *
186: * @return void
187: */
188: public function isRows($isRows = null)
189: {
190: $this->isRows = $isRows;
191: }
192:
193: /**
194: * Sort columns of one row
195: *
196: * @param array $row row to sort
197: *
198: * @return void
199: */
200: public function sortColumns($row)
201: {
202: foreach ($this->titles as $key => $title) {
203: if (isset($row[$key])) {
204: $row2[$key] = $row[$key];
205: }
206: }
207: return $row2;
208: }
209:
210: /**
211: * Sort all columns. Columns are sorted by title keys.
212: To use this feature, sort and add titles, then sortAllColumns
213: *
214: * @return void
215: */
216: public function sortAllColumns()
217: {
218: if ($this->rows) {
219: foreach ($this->rows as $key => $row) {
220: $this->rows[$key] = $this->sortColumns($row);
221: }
222:
223: }
224: }
225:
226: /**
227: * Set align of one column
228: *
229: * @param int $col Column number
230: * @param text $colalign align for each cell in column
231: *
232: * @return void
233: */
234: public function setColAlign($col = 0, $colalign = 'right')
235: {
236: $this->colAligns[$col] = $colalign;
237: }
238:
239: /**
240: * Set valign of one column
241: *
242: * @param int $col Column number
243: * @param text $colvalign valign for each cell in column
244: *
245: * @return void
246: */
247: public function setColValign($col = 0, $colvalign = 'top')
248: {
249: $this->colValigns[$col] = $colvalign;
250: foreach ($this->rows as $row) {
251: $colnum = 0;
252: foreach ($row as $cell) {
253: if ($colnum++ == $col) {
254: $cell->setAtt("valign", $colvalign);
255: }
256:
257: $row2[] = $cell;
258: }
259: $rows2[] = $row2;
260: $row2 = '';
261: }
262: $this->rows = $rows2;
263: }
264:
265: /**
266: * Set class of one column
267: *
268: * @param int $col Column number
269: * @param text $colclass class for each cell in column
270: *
271: * @return void
272: */
273: public function setColClass($col = 0, $colclass = 'colclass')
274: {
275: $this->colClasses[$col] = $colclass;
276: }
277:
278: /**
279: * Set align of one row
280: *
281: * @param int $row Row number
282: * @param text $rowalign align for row
283: *
284: * @return void
285: */
286: public function setRowAlign($row = 0, $rowalign = 'center')
287: {
288: $this->rowAligns[$row] = $rowalign;
289: }
290:
291: /**
292: * Set valign of one row
293: *
294: * @param int $row Row number
295: * @param text $rowvalign valign for each cell in row
296: *
297: * @return void
298: */
299: public function setRowValign($row = 0, $rowvalign = 'top')
300: {
301: $this->rowValigns[$row] = $rowvalign;
302: }
303:
304: /**
305: * Set class of one row
306: *
307: * @param int $row Row number
308: * @param text $rowclass class for row
309: *
310: * @return void
311: */
312: public function setRowClass($row = 0, $rowclass = 'rowclass')
313: {
314: $this->rowClasses[$row] = $rowclass;
315: }
316:
317: /**
318: * Set style of one row
319: *
320: * @param int $row Row number
321: * @param array $rowStyle style for each cell in row
322: *
323: * @return void
324: */
325: public function setRowStyle($row = 0, $rowStyle = 'rowStyle')
326: {
327: $this->rowStyles[$row] = $rowStyle;
328: }
329:
330: /**
331: * Print table tag
332: *
333: * @param array $rows Content for table
334: * @param array $altcontent Alernative content (Only used if table content is empty
335: *
336: * @return void
337: */
338: public function tag($rows = null, $altcontent = null)
339: {
340: $this->setText($rows);
341: if ($this->titles && $this->rows) {
342: array_unshift($this->rows, $this->titles);
343: } elseif ($this->titles) {
344: $this->rows = array(
345: $this->titles
346: );
347: }
348: if ($this->rows) {
349: $rowNum = 0;
350: foreach ($this->rows as $rowKey => $row) {
351: $row = Geo::arr($row);
352: $cellnum = 0;
353:
354: foreach ($row as $cellKey => $cell) {
355: if (!is_object($cell)) {
356: $cell = geoCell($cell);
357: }
358:
359: if (isset($this->colClasses[$cellnum]) && !($this->titles && !$rowNum)) {
360: $cell->setAtt("class", $this->colClasses[$cellnum]);
361: }
362:
363: if (isset($this->colAligns[$cellnum])) {
364: $cell->setAtt("align", $this->colAligns[$cellnum]);
365: }
366:
367: if (isset($this->cellClasses[$rowKey][$cellnum]) && is_array($this->cellClasses[$rowKey])) {
368: $cell->setAtt("class", $this->cellClasses[$rowKey][$cellnum]);
369: }
370:
371: if (isset($this->cellStyles[$rowKey][$cellnum])) {
372: if (is_array($this->cellStyles[$rowKey])) {
373: $cell->setAtt("style", $this->cellStyles[$rowKey][$cellnum]);
374: } else {
375: $cell->setAtt("style", $this->cellStyles[$rowKey]);
376: }
377: }
378:
379: $row2[$cellKey] = $cell;
380: $cellnum++;
381: }
382: $rows2[$rowKey] = $row2;
383: $row2 = '';
384: $rowNum++;
385: }
386: $this->rows = $rows2;
387: }
388:
389: $numRows = 0;
390: $tablecontent = '';
391: if ($this->rows) {
392: foreach ($this->rows as $key => $row) {
393: if (!$this->isRows) {
394: $tablecontent .= "<tr";
395:
396: if (isset($this->rowAligns[$numRows])) {
397: $tablecontent .= " align=\"" . $this->rowAligns[$numRows] . "\"";
398: }
399:
400: if (isset($this->rowValigns[$numRows])) {
401: $tablecontent .= " valign=\"" . $this->rowValigns[$numRows] . "\"";
402: }
403:
404: if (isset($this->rowClasses[$numRows])) {
405: if (!is_array($this->rowClasses[$numRows])) {
406: $tablecontent .= " class=\"" . $this->rowClasses[$numRows] . "\"";
407: }
408: }
409:
410: if (isset($this->rowStyles[$numRows])) {
411: if (!is_array($this->rowStyles[$numRows])) {
412: $tablecontent .= " style=\"" . $this->rowStyles[$numRows] . "\"";
413: }
414: }
415:
416: if (!is_int($key)) {
417: // use array key as row id
418: $key2 = str_replace(" ", "_", $key); // spaces are invalid in id
419:
420: if (is_numeric($key2[0])) {
421: // id which begin with integers are invalid
422: $key2 = "id_" . $key2;
423: }
424:
425: $tablecontent .= " id=\"" . $key2 . "\"";
426: }
427:
428: $numRows++;
429: $tablecontent .= ">";
430: }
431:
432: foreach ($row as $cell) {
433: $tablecontent .= $cell->tag();
434: }
435:
436: if (!$this->isRows) {
437: $tablecontent .= "</tr>";
438: }
439: }
440: }
441: if ($tablecontent) {
442: if ($this->isRows) {
443: return $tablecontent;
444: } else {
445: $this->text = Geo::arr($tablecontent);
446: }
447: return parent::baseTag($rows);
448: } else {
449: return $altcontent;
450: }
451: }
452: }
453: