-
Notifications
You must be signed in to change notification settings - Fork 8
/
tablebuilder.php
436 lines (380 loc) · 8.45 KB
/
tablebuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
/**
Table Builder plugin for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2010-2011 Killsaw
Steven Bredenberg <[email protected]>
@package TableBuilder
@version 1.0.0
**/
//! Plugin for building HTML tables.
class TableBuilder extends Core {
//! Minimum framework version required to run
const F3_Minimum='1.4.0';
//@{
//! Locale-specific error/exception messages
const
TEXT_NoRecords='No records.',
TEXT_TableNoProperty='Table does not have property {@CONTEXT}';
//@}
//@{
//! TableBuilder properties
protected $header=array();
protected $rows=array();
protected $pager=NULL;
protected $properties=array();
//@}
/**
Return an instance of TableBuilder with $rows set.
@return TableBuilder
@param $rows array
@public
**/
public static function fromRows(array $rows) {
$table=new TableBuilder;
$table->addRows($rows);
return $table;
}
/**
Set table column headers given an array of names.
@param $fields array
@public
**/
public function setHeader(array $fields) {
$this->header = array();
foreach($fields as $f) {
$this->header[$f] = new TableColumn($f);
}
}
/**
Add a row to the table.
@param $row array
@public
**/
public function addRow(array $row) {
$this->rows[] = $row;
}
/**
Convenience method for bulk adding rows.
@param $rows array
@public
**/
public function addRows(array $rows) {
if (count($rows) > 0) {
$this->setHeader(array_keys($rows[0]));
$this->rows += $rows;
}
}
/**
Retrieve a TableColumn instance by name.
@return TableColumn|null
@param $name string
@public
**/
public function column($name) {
if (array_key_exists($name, $this->header)) {
return $this->header[$name];
}
return null;
}
/**
Add a table pager for pagination display.
@param $pager TablePager
@public
**/
public function addPager(TablePager $pager) {
$this->pager = $pager;
}
/**
Generate complete HTML table.
@return string
@public
**/
public function toString() {
// Build table attribute string.
$attr = '';
foreach($this->properties as $k=>$v) {
$attr .= sprintf(' %s="%s"', $k, $v);
}
$col_count = count($this->header);
foreach($this->header as $h) {
if ($h->display === false) {
$col_count--;
}
}
$html = sprintf("<table%s>", $attr);
// Check for empty recordset.
$has_records = ($this->rows && $col_count);
if ($has_records) {
// Build header.
$html .= '<thead><tr>'.join('', $this->header).'</tr></thead>';
// Optionally build pager.
if ($this->pager instanceof TablePager) {
$html .= sprintf('<tfoot><tr><td align="center" colspan="%d">%s</td></tr></tfoot>',
$col_count, $this->pager);
}
}
// Build body.
$html .= '<tbody>';
if (!$has_records) {
// Handle empty recordset.
$html .= sprintf('<tr><td>%s</td></tr>', self::TEXT_NoRecords);
} else {
foreach($this->rows as $row) {
$html .= '<tr>';
foreach($row as $name=>$field) {
// Support column hiding.
if ($this->header[$name]->display === false) {
continue;
}
// Support automatic field linking.
if ($format = $this->header[$name]->getLinkFormat()) {
$field = sprintf('<a href="%s%s">%s</a>', $format, $field, $field);
}
// Bum attributes from column. Not ideal, but good enough.
$props = $this->header[$name]->getProperties($toString=true);
$html .= sprintf('<td%s>%s</td>', $props, $field);
}
$html .= '</tr>';
}
}
$html .= '</tbody></table>';
return $html;
}
/**
Set a table HTML attribute
@param $name string
@param $value mixed
@public
**/
public function __set($name, $value) {
$this->properties[$name] = $value;
}
/**
Get a table HTML attribute
@return mixed
@param $name string
@public
**/
public function __get($name) {
if (array_key_exists($name, $this->properties)) {
return $this->properties[$name];
}
self::$global['CONTEXT'] = $name;
trigger_error(self::TEXT_TableNoProperty);
}
/**
Automagic. Return generated HTML table.
@return string
@public
**/
public function __toString() {
return $this->toString();
}
/**
TableBuilder constructor
@public
**/
public function __construct($rows=array()) {
$this->addRows($rows);
}
}
//! TableBuilder column.
class TableColumn {
//@{
//! Locale-specific error/exception messages
const
TEXT_ColumnNoProperty='Column does not have property {@CONTEXT}.',
TEXT_NoMethod='Method {@CONTEXT} does not exist.';
//@}
//@{
//! TableColumn properties
protected $label=NULL;
protected $link_format=NULL;
protected $properties=array();
protected $display=true;
//@}
/**
Set displayed column text.
@return TableBuilder
@param $label string
@public
**/
public function setLabel($label) {
$this->label=$label;
return $this;
}
/**
Get displayed column text.
@return string
@public
**/
public function getLabel() {
return $this->label;
}
/**
Sets link format for column row cells. Prepends cell
value with format.
@param $format string
@public
**/
public function linkify($format) {
$this->link_format = $format;
}
/**
Get link format for column row cells.
@return string
@public
**/
public function getLinkFormat() {
return $this->link_format;
}
/**
Get HTML attributes for column.
@param $toString bool
@return string
@public
**/
public function getProperties($toString=false) {
if ($toString) {
$attr = '';
foreach($this->properties as $k=>$v) {
$attr .= " {$k}=\"{$v}\"";
}
return $attr;
} else {
return $this->properties;
}
}
/**
Generate HTML for column header.
@return string
@public
**/
public function toString() {
if ($this->display !== false) {
return sprintf("<th%s>%s</th>",
$this->getProperties($toString=true),
$this->label);
} else {
return '';
}
}
/**
Set column HTML attribute.
@param $name string
@param $value mixed
@public
**/
public function __set($name, $value) {
$this->properties[$name] = $value;
}
/**
Get column HTML attribute.
@return mixed
@param $name string
@public
**/
public function __get($name) {
if (array_key_exists($name, $this->properties)) {
return $this->properties[$name];
}
}
/**
Pickup on set() method calls for set() chaining.
@return TableColumn
@param $name string
@param $args array
@public
**/
public function __call($name, $args) {
if (preg_match('/^set(.+)/', $name, $match)) {
$this->properties[strtolower($match[1])] = $args[0];
return $this;
}
self::$global['CONTEXT'] = $name;
trigger_error(self::TEXT_NoMethod);
}
/**
Automagic. Returns column header HTML.
@return string
**/
public function __toString() {
return $this->toString();
}
/**
TableColumn constructor
@param $label string
@public
**/
public function __construct($label) {
$this->label = $label;
}
}
//! TableBuilder pager.
class TablePager {
//@{
//! Locale-specific error/exception messages
const
TEXT_ColumnNoProperty='Column does not have property {@CONTEXT}.';
//@}
//@{
//! TableColumn properties
protected $link=NULL;
protected $limit=0;
protected $offset=0;
protected $max=0;
//@}
/**
Sets a prefix for pager links.
@param $link string
@public
**/
public function setLink($link) {
$this->link = $link;
}
/**
Generate HTML for pager.
@return string
@public
**/
public function toString() {
$page_count = ceil($this->max / $this->limit);
if ($this->offset > 0) {
$curr_page = ceil($this->offset / $this->limit);
} else {
$curr_page = 1;
}
$html = '';
for($i=1; $i <= $page_count; $i++) {
$html .= sprintf('<a href="%s%d"%s>%d</a> ', $this->link, $i,
($i == $curr_page)?' class="active_page"':'', $i);
}
return $html;
}
/**
Automagic. Return pager HTML.
@return string
**/
public function __toString() {
return $this->toString();
}
/**
TablePager constructor
@param $offset integer
@param $limit integer
@param $max integer
@public
**/
public function __construct($offset, $limit, $max=-1) {
if ($max < 0) {
$max = $limit;
}
$this->limit = $limit;
$this->offset = $offset;
$this->max = $max;
}
}