-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_table.php
executable file
·82 lines (77 loc) · 2.34 KB
/
html_table.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
<?
class HTMLTable extends MicroObject{
/**
* $columns contains a hash with column properties:
* column : the name of the column
* type [optional] default: string, supported (extensible): string, icon, link
* title [optional] default: column
* for type icon (is also a link by default):
* icon_name [optional] default: column
* link_field [optional] default: $row[id]
* link_as [optional] default: id
* url
* for type link:
* link_field [optional] default: $row[id]
* link_as [optional] default: id
* url
* rows is an array of arrays
*/
function __construct($columns, $rows=array()){
$this->columns = $columns;
$this->rows = $rows;
}
function render_all(){
echo '<table>';
$this->render_cols();
$this->render_head();
$this->render_body();
echo '</table>';
}
function done_by_callback($part, $props){
return $this->do_callback("render_{$props['column']}_{$part}", $props)
|| $this->do_callback("render_".array_try($props, 'type', 'string')."_{$part}", $props)
|| $this->do_callback("render_{$part}", $props);
}
function render_cols(){
foreach($this->columns as $props){
if(!$this->done_by_callback("col", $props))
echo '<col/>';
}
}
function render_head(){
echo '<thead><tr>';
foreach($this->columns as $props){
if(!$this->done_by_callback("header", $props))
echo "<th>".array_try($props, 'title', $props['column'])."</th>";
}
echo '</tr></thead>';
}
function render_body(){
echo "\n<tbody>\n";
foreach($this->rows as $row){
echo '<tr>';
foreach($this->columns as $props){
if(!($this->do_callback("render_{$props['column']}_cell", $props, $row)
|| $this->do_callback("render_".array_try($props, 'type', 'string')."_cell", $props, $row)
|| $this->do_callback("render_cell", $props, $row)))
echo "<td>".$row[$props['column']]."</td>";
}
echo "</tr>\n";
}
echo "</tbody>\n";
}
function render_icon_cell($props, $row){
echo "<td>".linkify(
iconify(array_try($props, 'icon_name', $props['column'])),
$props['url'],
array(array_try($props, 'link_as', 'id')
=> $row[array_try($props, 'link_field', 'id')]
))."</td>";
}
function render_link_cell($props, $row){
echo "<td>".linkify($row[$props['column']], $props['url'],
array(array_try($props, 'link_as', 'id')
=> $row[array_try($props, 'link_field', 'id')]
))."</td>";
}
}