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