1
|
|
|
<?php |
2
|
|
|
namespace Guer\HTMLTable; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class that generates a table in HTML with table data from an multidimensional |
6
|
|
|
* associative array. |
7
|
|
|
* |
8
|
|
|
* Through a table specification array it is possible to set an CSS id or |
9
|
|
|
* a class for the table and a caption. |
10
|
|
|
* |
11
|
|
|
* Through a column specification it is possible to set the column title, set |
12
|
|
|
* a colspan for the table cell and footer cell, through type determine if the |
13
|
|
|
* row of cells belongs to the table body or table foot and through a function |
14
|
|
|
* include advanced settings for a column. |
15
|
|
|
* |
16
|
|
|
* @author Gunnar Eriksson <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class CHTMLTable |
19
|
|
|
{ |
20
|
|
|
const FOOTER = 'footer'; |
21
|
|
|
|
22
|
|
|
private $tableSpec; |
23
|
|
|
private $tableHead; |
24
|
|
|
private $tableBody; |
25
|
|
|
private $tableFoot; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* Creates a table with table head, table body and if specified, a table |
31
|
|
|
* footer. It is possible to specify the table and the tabel cells settings |
32
|
|
|
* per column. |
33
|
|
|
* |
34
|
|
|
* @param [] $tableSpecs table settings. |
|
|
|
|
35
|
|
|
* @param [] $data table cell data. |
|
|
|
|
36
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
37
|
|
|
*/ |
38
|
10 |
|
public function __construct($tableSpecs = [], $data = [], $columnSpecs = []) |
39
|
|
|
{ |
40
|
10 |
|
$this->create($tableSpecs, $data, $columnSpecs); |
41
|
10 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a table with cell data. |
45
|
|
|
* |
46
|
|
|
* Creates a table with table head, table body with table data and if |
47
|
|
|
* specified, a table footer. It is possible to specify the table and the |
48
|
|
|
* tabel cells settings per column. |
49
|
|
|
* |
50
|
|
|
* @param [] $tableSpec table settings. |
|
|
|
|
51
|
|
|
* @param [] $data table cell data. |
|
|
|
|
52
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* @return object the html table object. |
55
|
|
|
*/ |
56
|
10 |
|
public function create($tableSpecs = [], $data = [], $columnSpecs = []) |
57
|
|
|
{ |
58
|
10 |
|
$this->resetTableTags(); |
59
|
10 |
|
$this->setTableSpecifications($tableSpecs); |
60
|
|
|
|
61
|
10 |
|
$this->createTableHead($data, $columnSpecs); |
62
|
10 |
|
$this->createTableBody($data, $columnSpecs); |
63
|
10 |
|
$this->createTableFooter($columnSpecs); |
64
|
|
|
|
65
|
10 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Helper method to reset main parts of table tags. |
70
|
|
|
* |
71
|
|
|
* Sets the table head, table body and table foot tag to null. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
10 |
|
private function resetTableTags() |
76
|
|
|
{ |
77
|
10 |
|
$this->tableHead = null; |
78
|
10 |
|
$this->tableBody = null; |
79
|
10 |
|
$this->tableFoot = null; |
80
|
10 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Helper method to set the table specifications. |
84
|
|
|
* |
85
|
|
|
* Merges the table specifications with the default specifications. |
86
|
|
|
* Default table CSS id is html-table. |
87
|
|
|
* |
88
|
|
|
* @param [] $table table settings. |
|
|
|
|
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
10 |
|
private function setTableSpecifications($tableSpec) |
93
|
|
|
{ |
94
|
|
|
$defaults = [ |
95
|
|
|
// Always have a id for the form |
96
|
10 |
|
'id' => 'html-table', |
97
|
10 |
|
]; |
98
|
|
|
|
99
|
10 |
|
if ($this->isClassPresent($tableSpec)) { |
100
|
1 |
|
$tableSpec = $this->removeId($tableSpec); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
10 |
|
$this->tableSpec = array_merge($defaults, $tableSpec); |
104
|
10 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Helper method to check if a CSS class tag is present |
108
|
|
|
* |
109
|
|
|
* Checks if a CSS class tag is present in the table specification. |
110
|
|
|
* |
111
|
|
|
* @param [] $tableSpec the table specification. |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return boolean true if class is present in the table specification, |
114
|
|
|
* false otherwise. |
115
|
|
|
*/ |
116
|
10 |
|
private function isClassPresent($tableSpec) |
117
|
|
|
{ |
118
|
10 |
|
return isset($tableSpec['class']) ? true : false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Helper method to reset the id tag. |
123
|
|
|
* |
124
|
|
|
* Sets the CSS id tag to null. |
125
|
|
|
* |
126
|
|
|
* @param [] $tableSpec the table specification. |
|
|
|
|
127
|
|
|
* |
128
|
|
|
* @return [] the table specification without the CSS id tag. |
|
|
|
|
129
|
|
|
*/ |
130
|
1 |
|
private function removeId($tableSpec) { |
131
|
1 |
|
$tableSpec['id'] = null; |
132
|
|
|
|
133
|
1 |
|
return $tableSpec; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Helper method to create the table head. |
138
|
|
|
* |
139
|
|
|
* Creates the table head. The title of the columns are set according to |
140
|
|
|
* the table tag in the column specifications. Otherwise, the title is set |
141
|
|
|
* to the keys name in the table cell data array. |
142
|
|
|
* |
143
|
|
|
* @param [] $data table cell data. |
|
|
|
|
144
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
10 |
|
private function createTableHead($data, $columnSpecs) |
149
|
|
|
{ |
150
|
10 |
|
$this->tableHead = "\n<thead>"; |
151
|
10 |
|
$this->tableHead .= "\n<tr>"; |
152
|
|
|
|
153
|
10 |
|
if (empty($columnSpecs)) |
154
|
10 |
|
{ |
155
|
10 |
|
$this->setColumnTitlesFromData($data); |
156
|
10 |
|
} else { |
157
|
6 |
|
$this->setColumnTitlesFromColumnSpecifications($columnSpecs); |
158
|
|
|
} |
159
|
|
|
|
160
|
10 |
|
$this->tableHead .= "\n</tr>"; |
161
|
10 |
|
$this->tableHead .= "\n</thead>"; |
162
|
10 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Helper method to set the column titles from the data array. |
166
|
|
|
* |
167
|
|
|
* Uses the first row in the table cell data array to set the titles of |
168
|
|
|
* the columns. The name of the columns are the key name in the associative |
169
|
|
|
* array containing data for the table. |
170
|
|
|
* |
171
|
|
|
* @param [] $data table cell data. |
|
|
|
|
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
10 |
|
private function setColumnTitlesFromData($data) |
176
|
|
|
{ |
177
|
10 |
|
$firstRow = isset($data[0]) ? $data[0] : []; |
178
|
10 |
|
foreach ($firstRow as $key => $value) { |
179
|
4 |
|
$this->tableHead .= "\n<th>"; |
180
|
4 |
|
$this->tableHead .= $key; |
181
|
4 |
|
$this->tableHead .= "</th>"; |
182
|
10 |
|
} |
183
|
10 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Helper method to set the column titles from column specifications. |
187
|
|
|
* |
188
|
|
|
* Uses column specifications to set the name of the columns in the table |
189
|
|
|
* head. |
190
|
|
|
* |
191
|
|
|
* @param [] $columnSpecs table columns cell settings |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
6 |
|
private function setColumnTitlesFromColumnSpecifications($columnSpecs) |
196
|
|
|
{ |
197
|
6 |
|
foreach ($columnSpecs as $key => $columnSpec) { |
198
|
6 |
|
if (!$this->isTableFooter($columnSpec)) { |
199
|
6 |
|
$this->tableHead .= "\n<th>"; |
200
|
6 |
|
$this->tableHead .= $this->getTitle($key, $columnSpec); |
201
|
6 |
|
$this->tableHead .= "</th>"; |
202
|
6 |
|
} |
203
|
6 |
|
} |
204
|
6 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Helper method to check if the column cell belongs to the footer. |
208
|
|
|
* |
209
|
|
|
* Checks the type tag, in the column specification for one column, if the |
210
|
|
|
* tag is present and set to footer. |
211
|
|
|
* |
212
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
213
|
|
|
* |
214
|
|
|
* @return boolean true if the cell type belongs to the footer, false otherwise. |
215
|
|
|
*/ |
216
|
6 |
|
private function isTableFooter($columnSpec) |
217
|
|
|
{ |
218
|
6 |
|
$isFooter = false; |
219
|
6 |
|
if (isset($columnSpec['type'])) { |
220
|
2 |
|
if (strcmp($columnSpec['type'], self::FOOTER) === 0) { |
221
|
2 |
|
$isFooter = true; |
222
|
2 |
|
} |
223
|
2 |
|
} |
224
|
|
|
|
225
|
6 |
|
return $isFooter; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Helper method to get title from a column specification, if specified. |
230
|
|
|
* |
231
|
|
|
* Uses the title tag in the column specification for one column to get |
232
|
|
|
* the title. If the title tag is not set, the title is the key name in |
233
|
|
|
* the associative array containing data for the table. |
234
|
|
|
* |
235
|
|
|
* @param [] $key the name of the key for the table cell data. |
|
|
|
|
236
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
237
|
|
|
* |
238
|
|
|
* @return [] the name from the title tag in the cell specification. |
|
|
|
|
239
|
|
|
* Otherwise, the table cell data key name. |
240
|
|
|
*/ |
241
|
6 |
|
private function getTitle($key, $columnSpec) |
242
|
|
|
{ |
243
|
6 |
|
return isset($columnSpec['title']) ? $columnSpec['title'] : $key; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Helper method to create the table body with table cell data. |
248
|
|
|
* |
249
|
|
|
* Sets the table cell data in the table body. |
250
|
|
|
* |
251
|
|
|
* @param [] $data table cell data. |
|
|
|
|
252
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
10 |
|
private function createTableBody($data, $columnSpecs) |
257
|
|
|
{ |
258
|
10 |
|
$this->setTableData($data, $columnSpecs); |
259
|
10 |
|
if (isset($this->tableBody)) { |
260
|
10 |
|
$this->tableBody = "\n<tbody>" . $this->tableBody . "\n</tbody>"; |
261
|
10 |
|
} |
262
|
10 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Helper method to set table data in table body. |
266
|
|
|
* |
267
|
|
|
* Sets table data according to the column specifications, if it is |
268
|
|
|
* specified. Otherwise it sets the data as it is stored in the data array. |
269
|
|
|
* |
270
|
|
|
* @param [] $data table cell data. |
|
|
|
|
271
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
10 |
|
private function setTableData($data, $columnSpecs) |
276
|
|
|
{ |
277
|
10 |
|
if (empty($columnSpecs)) { |
278
|
10 |
|
$this->setTableDataFromData($data); |
279
|
10 |
|
} else { |
280
|
6 |
|
$this->setTableDataAsSpecified($data, $columnSpecs); |
281
|
|
|
} |
282
|
10 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Helper method to set table data from the data array. |
286
|
|
|
* |
287
|
|
|
* Sets table data from the data array. |
288
|
|
|
* |
289
|
|
|
* @param [] $data table cell data. |
|
|
|
|
290
|
|
|
* |
291
|
|
|
* @return void |
292
|
|
|
*/ |
293
|
10 |
|
private function setTableDataFromData($data) |
294
|
|
|
{ |
295
|
10 |
|
foreach ($data as $row) { |
296
|
4 |
|
$this->tableBody .= "\n<tr>"; |
297
|
4 |
|
foreach ($row as $value) { |
298
|
4 |
|
$this->tableBody .= "\n<td>"; |
299
|
4 |
|
$this->tableBody .= $value; |
300
|
4 |
|
$this->tableBody .= "</td>"; |
301
|
4 |
|
} |
302
|
4 |
|
$this->tableBody .= "\n</tr>"; |
303
|
10 |
|
} |
304
|
10 |
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Helper method to set table data according to the column specifications. |
308
|
|
|
* |
309
|
|
|
* Sets the table data according to the column specifications, if the cell |
310
|
|
|
* does not belong to the footer. Adds a colspan tag, if it is specified |
311
|
|
|
* for the cell in the column. |
312
|
|
|
* |
313
|
|
|
* @param [] $data table cell data. |
|
|
|
|
314
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
315
|
|
|
* |
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
6 |
|
private function setTableDataAsSpecified($data, $columnSpecs) |
319
|
|
|
{ |
320
|
6 |
|
foreach ($data as $row) { |
321
|
6 |
|
$this->tableBody .= "\n<tr>"; |
322
|
6 |
|
foreach ($columnSpecs as $key => $columnSpec) { |
323
|
6 |
View Code Duplication |
if (!$this->isTableFooter($columnSpec)) { |
|
|
|
|
324
|
6 |
|
$colspan = $this->getColspan($columnSpec); |
325
|
6 |
|
$this->tableBody .= "\n<td{$colspan}>"; |
326
|
6 |
|
$this->tableBody .= $this->getValue($row, $key, $columnSpec); |
327
|
6 |
|
$this->tableBody .= "</td>"; |
328
|
6 |
|
} |
329
|
6 |
|
} |
330
|
6 |
|
$this->tableBody .= "\n</tr>"; |
331
|
6 |
|
} |
332
|
6 |
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Helper method to get the colspan value, if specified in the column |
336
|
|
|
* specification for the cell. |
337
|
|
|
* |
338
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
339
|
|
|
* |
340
|
|
|
* @return int the colspan value if specified. Otherwise null. |
341
|
|
|
*/ |
342
|
6 |
|
private function getColspan($columnSpec) |
343
|
|
|
{ |
344
|
6 |
|
return isset($columnSpec['colspan']) ? " colspan='{$columnSpec['colspan']}'" : null; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Helper method to get the value for a specific position in one row in |
349
|
|
|
* the data array. |
350
|
|
|
* |
351
|
|
|
* Gets the data from a specific position in one row in the data array. |
352
|
|
|
* If a function is specified for the cell in the column, the data is |
353
|
|
|
* runned through the function before it is returned. |
354
|
|
|
* |
355
|
|
|
* @param [] $row one row of in the array of table data. |
|
|
|
|
356
|
|
|
* @param string $key the name of the key in the associative data array. |
357
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
358
|
|
|
*/ |
359
|
6 |
|
private function getValue($row, $key, $columnSpec) |
360
|
|
|
{ |
361
|
6 |
|
if ($this->isFunctionSpecified($columnSpec)) { |
362
|
2 |
|
$dataValue = isset($row[$key]) ? $row[$key] : ""; |
363
|
2 |
|
return $this->getValueThroughFunction($columnSpec, $dataValue); |
364
|
|
|
} else { |
365
|
6 |
|
return isset($row[$key]) ? $row[$key] : ""; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Helper method t check if the function tag is specified for the cells in |
371
|
|
|
* one column. |
372
|
|
|
* |
373
|
|
|
* Checks if the function tag is set for the cell in one column. |
374
|
|
|
* |
375
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
376
|
|
|
* |
377
|
|
|
* @return boolean true if a function is connected to the cell, false otherwise. |
378
|
|
|
*/ |
379
|
6 |
|
private function isFunctionSpecified($columnSpec) |
380
|
|
|
{ |
381
|
6 |
|
return isset($columnSpec['function']) ? true : false; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Helper method to run the value through a function before it is returned. |
386
|
|
|
* |
387
|
|
|
* Runs the value through a function, if a function is connected to the cell |
388
|
|
|
* in the column. If not function is connected to the cell through the |
389
|
|
|
* column specification, the value is returned as it is. |
390
|
|
|
* |
391
|
|
|
* @param [] $columnSpec cell settings for one column |
|
|
|
|
392
|
|
|
* @param mixed $dataValue the value to run through function, if specified. |
393
|
|
|
* |
394
|
|
|
* @return the value. |
395
|
|
|
*/ |
396
|
2 |
|
private function getValueThroughFunction($columnSpec, $dataValue) |
397
|
|
|
{ |
398
|
2 |
|
if (!empty($columnSpec['function'])) { |
399
|
1 |
|
return call_user_func($columnSpec['function'], $dataValue); |
400
|
|
|
} else { |
401
|
1 |
|
return $dataValue; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Helper method to create table footer with data. |
407
|
|
|
* |
408
|
|
|
* Creates table footer if the cell settings for the column is set to |
409
|
|
|
* footer in the column specifications. |
410
|
|
|
* Adds a colspan tag, if it is specified for the cell in the column. |
411
|
|
|
* |
412
|
|
|
* @param [] $columnSpecs table columns cell settings. |
|
|
|
|
413
|
|
|
* |
414
|
|
|
* @return void |
415
|
|
|
*/ |
416
|
10 |
|
private function createTableFooter($columnSpecs) |
417
|
|
|
{ |
418
|
10 |
|
foreach ($columnSpecs as $columnSpec) { |
419
|
6 |
View Code Duplication |
if ($this->isTableFooter($columnSpec)) { |
|
|
|
|
420
|
2 |
|
$colspan = $this->getColspan($columnSpec); |
421
|
2 |
|
$this->tableFoot .= "\n<td{$colspan}>"; |
422
|
2 |
|
$this->tableFoot .= $this->getFooterData($columnSpec); |
423
|
2 |
|
$this->tableFoot .= "</td>"; |
424
|
2 |
|
} |
425
|
10 |
|
} |
426
|
|
|
|
427
|
10 |
|
if (isset($this->tableFoot)) { |
428
|
2 |
|
$this->tableFoot = "\n<tfoot>\n<tr>" . $this->tableFoot . "\n</tr>\n</tfoot>"; |
429
|
2 |
|
} |
430
|
10 |
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Helper method to get table footer data. |
434
|
|
|
* |
435
|
|
|
* Gets table footer data from the column specification. Checks if the |
436
|
|
|
* value should be fetched from a function or from the value tag. |
437
|
|
|
* If either the function or the value specified, an empty string is |
438
|
|
|
* returned. |
439
|
|
|
* |
440
|
|
|
* @param [] $columnSpec cell settings for one column. |
|
|
|
|
441
|
|
|
* |
442
|
|
|
* @return mixed the cell data value. |
443
|
|
|
*/ |
444
|
2 |
|
private function getFooterData($columnSpec) |
445
|
|
|
{ |
446
|
2 |
|
if ($this->isFunctionSpecified($columnSpec)) { |
447
|
1 |
|
return call_user_func($columnSpec['function']); |
448
|
|
|
} else { |
449
|
1 |
|
return isset($columnSpec['value']) ? $columnSpec['value'] : ""; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Gets the table. |
455
|
|
|
* |
456
|
|
|
* Gets the table with table data. |
457
|
|
|
* |
458
|
|
|
* @return html the table with table data. |
459
|
|
|
*/ |
460
|
10 |
|
public function getHTMLTable() |
461
|
|
|
{ |
462
|
10 |
|
$id = isset($this->tableSpec['id']) ? " id='{$this->tableSpec['id']}'" : null; |
463
|
10 |
|
$class = isset($this->tableSpec['class']) ? " class='{$this->tableSpec['class']}'" : null; |
464
|
10 |
|
$caption = isset($this->tableSpec['caption']) ? "<caption>{$this->tableSpec['caption']}</caption>" : null; |
465
|
|
|
|
466
|
10 |
|
$htmlTable = "<table{$id}{$class}>"; |
467
|
10 |
|
$htmlTable .= $caption; |
468
|
10 |
|
$htmlTable .= $this->tableHead; |
469
|
10 |
|
$htmlTable .= $this->tableBody; |
470
|
10 |
|
$htmlTable .= $this->tableFoot; |
471
|
10 |
|
$htmlTable .= "\n</table>"; |
472
|
|
|
|
473
|
10 |
|
return $htmlTable; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.