1
|
|
|
<?php |
2
|
|
|
namespace Jupitern\Table; |
3
|
|
|
|
4
|
|
|
class Table |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
public $columns; |
8
|
|
|
public $hasFilters; |
9
|
|
|
|
10
|
|
|
private $data; |
11
|
|
|
private $css; |
12
|
|
|
private $attrs; |
13
|
|
|
private $tablePlugin; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
protected function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->css = new Properties(); |
19
|
|
|
$this->attrs = new Properties(); |
20
|
|
|
$this->hasFilters = false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initializes the Table. |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
public static function instance() |
29
|
|
|
{ |
30
|
|
|
return new static(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* set data using a array, json string, pdo or your framework orm object. |
35
|
|
|
* |
36
|
|
|
* @param $data |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setData($data) |
40
|
|
|
{ |
41
|
|
|
$this->data = $this->isJson($data) ? json_decode($data) : $data; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* add html table attribute |
47
|
|
|
* |
48
|
|
|
* @param $attr |
49
|
|
|
* @param $value |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function attr($attr, $value) |
53
|
|
|
{ |
54
|
|
|
$this->attrs->add($attr, $value); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* add html table attributes |
60
|
|
|
* |
61
|
|
|
* @param $attrs |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function attrs($attrs) |
65
|
|
|
{ |
66
|
|
|
$this->attrs->addAll($attrs); |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* add html table style |
72
|
|
|
* |
73
|
|
|
* @param $attr |
74
|
|
|
* @param $value |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function css($attr, $value) |
78
|
|
|
{ |
79
|
|
|
$this->css->add($attr, $value); |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* start a new column |
85
|
|
|
* |
86
|
|
|
* @return TableColumn |
87
|
|
|
*/ |
88
|
|
|
public function column() |
89
|
|
|
{ |
90
|
|
|
$column = new TableColumn($this); |
91
|
|
|
$this->columns[] = $column; |
92
|
|
|
return $column; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* generate table html |
97
|
|
|
* |
98
|
|
|
* @param bool $returnOutput |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function render($returnOutput = false) |
102
|
|
|
{ |
103
|
|
|
$html = '<table {attrs} {css}><thead><tr>{thead}</tr>{theadFilters}</thead>'; |
104
|
|
|
$html .= '<tbody>{tbody}</tbody></table>'; |
105
|
|
|
$html .= "\n\n{plugin}"; |
106
|
|
|
|
107
|
|
|
$attrs = $this->attrs->render('{prop}="{val}" '); |
108
|
|
|
$css = $this->css->render('{prop}:{val}; '); |
109
|
|
|
|
110
|
|
|
$thead = ''; |
111
|
|
|
$theadFilters = ''; |
112
|
|
|
foreach ((array)$this->columns as $column) { |
113
|
|
|
$thead .= $column->renderHeader(); |
114
|
|
|
$theadFilters .= $column->renderFilter(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$tbody = ''; |
118
|
|
|
if (count($this->data)) { |
119
|
|
|
foreach ($this->data as $row) { |
120
|
|
|
$tbody .= '<tr>'; |
121
|
|
|
foreach ((array)$this->columns as $column) { |
122
|
|
|
$tbody .= $column->renderBody($row); |
123
|
|
|
} |
124
|
|
|
$tbody .= '</tr>'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$plugin = $this->tablePlugin !== null ? $this->tablePlugin->render() : ''; |
129
|
|
|
|
130
|
|
|
$output = str_replace( |
131
|
|
|
['{attrs}','{css}','{thead}','{theadFilters}','{tbody}', '{plugin}'], |
132
|
|
|
[ |
133
|
|
|
$attrs, $css, $thead, |
134
|
|
|
$this->hasFilters ? "<tr>{$theadFilters}</tr>" : "", |
135
|
|
|
$tbody, $plugin |
136
|
|
|
], |
137
|
|
|
$html |
138
|
|
|
); |
139
|
|
|
if ($returnOutput) return $output; |
140
|
|
|
echo $output; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
private function isJson($string) |
145
|
|
|
{ |
146
|
|
|
if (!is_string($string)) return false; |
147
|
|
|
json_decode($string); |
148
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |