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