1
|
|
|
<?php |
2
|
|
|
namespace Jupitern\Table; |
3
|
|
|
|
4
|
|
|
class Table |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
public $columns; |
8
|
|
|
public $hasFilters; |
9
|
|
|
|
10
|
|
|
protected $data; |
11
|
|
|
protected $css; |
12
|
|
|
protected $attrs; |
13
|
|
|
protected $rowAttrs; |
14
|
|
|
protected $tablePlugin; |
15
|
|
|
public $titlesMode = null; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
protected function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->css = new Properties(); |
21
|
|
|
$this->attrs = new Properties(); |
22
|
|
|
$this->rowAttrs = new Properties(); |
23
|
|
|
$this->hasFilters = false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializes the Table. |
28
|
|
|
* |
29
|
|
|
* @return static |
30
|
|
|
*/ |
31
|
|
|
public static function instance() |
32
|
|
|
{ |
33
|
|
|
return new static(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* set data using a array, json string, pdo or your framework orm object. |
38
|
|
|
* |
39
|
|
|
* @param $data |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function setData($data) |
43
|
|
|
{ |
44
|
|
|
$this->data = $this->isJson($data) ? json_decode($data) : $data; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* set titles auto resolution mode from column name. Options: underscore, camelcase |
50
|
|
|
* |
51
|
|
|
* @param $data |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setAutoTitles($titleMode) |
55
|
|
|
{ |
56
|
|
|
if (!in_array(strtolower($titleMode), ['camelcase', 'underscore'])) { |
57
|
|
|
throw new \Exception("selected titles mode options not found"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->titlesMode = strtolower($titleMode); |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* add html table attribute |
66
|
|
|
* |
67
|
|
|
* @param $attr |
68
|
|
|
* @param $value |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function attr($attr, $value) |
72
|
|
|
{ |
73
|
|
|
$this->attrs->add($attr, $value); |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* add html table attributes |
79
|
|
|
* |
80
|
|
|
* @param $attrs |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function attrs($attrs) |
84
|
|
|
{ |
85
|
|
|
$this->attrs->addAll($attrs); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* add html table row attribute |
91
|
|
|
* |
92
|
|
|
* @param $attr |
93
|
|
|
* @param $value |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function rowAttr($attr, $value) |
97
|
|
|
{ |
98
|
|
|
$this->rowAttrs->add($attr, $value); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* add html table row attributes |
104
|
|
|
* |
105
|
|
|
* @param $attrs |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function rowAttrs($attrs) |
109
|
|
|
{ |
110
|
|
|
$this->rowAttrs->addAll($attrs); |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* add html table style |
116
|
|
|
* |
117
|
|
|
* @param $attr |
118
|
|
|
* @param $value |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function css($attr, $value) |
122
|
|
|
{ |
123
|
|
|
$this->css->add($attr, $value); |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* start a new column |
129
|
|
|
* |
130
|
|
|
* @return TableColumn |
131
|
|
|
*/ |
132
|
|
|
public function column() |
133
|
|
|
{ |
134
|
|
|
$column = new TableColumn($this); |
135
|
|
|
$this->columns[] = $column; |
136
|
|
|
return $column; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* generate table html |
141
|
|
|
* |
142
|
|
|
* @param bool $returnOutput |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public function render($returnOutput = false) |
146
|
|
|
{ |
147
|
|
|
$html = '<table {attrs} {css}><thead><tr>{thead}</tr>{theadFilters}</thead>'; |
148
|
|
|
$html .= '<tbody>{tbody}</tbody></table>'; |
149
|
|
|
$html .= "\n\n{plugin}"; |
150
|
|
|
|
151
|
|
|
$attrs = $this->attrs->render('{prop}="{val}" '); |
152
|
|
|
$css = $this->css->render('{prop}:{val}; '); |
153
|
|
|
|
154
|
|
|
$thead = ''; |
155
|
|
|
$theadFilters = ''; |
156
|
|
|
foreach ((array)$this->columns as $column) { |
157
|
|
|
$thead .= $column->renderHeader(); |
158
|
|
|
$theadFilters .= $column->renderFilter(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$rowTemplate = "<tr {attrs}>{cells}</tr>\n"; |
162
|
|
|
|
163
|
|
|
$tbody = ''; |
164
|
|
|
if (count($this->data)) { |
165
|
|
|
foreach ($this->data as $row) { |
166
|
|
|
$cells = ''; |
167
|
|
|
foreach ((array)$this->columns as $column) { |
168
|
|
|
$cells .= $column->renderBody($row); |
169
|
|
|
} |
170
|
|
|
$rowAttrs = $this->rowAttrs->render("{prop}='{val}' ", $row); |
171
|
|
|
$tbody .= str_replace(['{attrs}','{cells}'], [$rowAttrs, $cells], $rowTemplate); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$plugin = $this->tablePlugin !== null ? $this->tablePlugin->render() : ''; |
176
|
|
|
|
177
|
|
|
$output = str_replace( |
178
|
|
|
['{attrs}','{css}','{thead}','{theadFilters}','{tbody}', '{plugin}'], |
179
|
|
|
[ |
180
|
|
|
$attrs, $css, $thead, |
181
|
|
|
$this->hasFilters ? "<tr>{$theadFilters}</tr>" : "", |
182
|
|
|
$tbody, $plugin |
183
|
|
|
], |
184
|
|
|
$html |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
if (!$returnOutput) echo $output; |
188
|
|
|
return $output; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
private function isJson($string) |
193
|
|
|
{ |
194
|
|
|
if (!is_string($string)) return false; |
195
|
|
|
json_decode($string); |
196
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|