1
|
|
|
<?php |
2
|
|
|
namespace Jupitern\Table; |
3
|
|
|
|
4
|
|
|
Class TableColumn |
5
|
|
|
{ |
6
|
|
|
public $value; |
7
|
|
|
|
8
|
|
|
private $tableInstance; |
9
|
|
|
private $title; |
10
|
|
|
private $attrs; |
11
|
|
|
private $css; |
12
|
|
|
private $filter = false; |
13
|
|
|
private $filterData = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* create a new TableColumn instance |
17
|
|
|
* |
18
|
|
|
* @param Table $tableInstance |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Table &$tableInstance) |
21
|
|
|
{ |
22
|
|
|
$this->tableInstance = $tableInstance; |
23
|
|
|
$this->attrs = ['th' => new Properties(), 'tr' => new Properties(), 'td' => new Properties()]; |
24
|
|
|
$this->css = ['th' => new Properties(), 'tr' => new Properties(), 'td' => new Properties()]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __get( $prop ) |
28
|
|
|
{ |
29
|
|
|
return $this->$prop; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* set column title |
34
|
|
|
* |
35
|
|
|
* @param $title |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function title($title) |
39
|
|
|
{ |
40
|
|
|
$this->title = $title; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* bind colunm value. $value can be: |
47
|
|
|
* integer index for none associative array or json |
48
|
|
|
* string index for associative array, json, PDO or ORM result |
49
|
|
|
* a closure that returns a string |
50
|
|
|
* |
51
|
|
|
* @param $value |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function value($value) |
55
|
|
|
{ |
56
|
|
|
$this->value = $value; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* add a attribute to table <td> or <th> |
63
|
|
|
* |
64
|
|
|
* @param $elem |
65
|
|
|
* @param $attr |
66
|
|
|
* @param $value |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function attr($elem, $attr, $value) |
70
|
|
|
{ |
71
|
|
|
if (!in_array($elem, array_keys($this->attrs))) { |
72
|
|
|
throw new \Exception("element {$elem} not available in column scope"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->attrs[$elem]->add($attr, $value); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* add css to table <td> or <th> |
82
|
|
|
* |
83
|
|
|
* @param $elem |
84
|
|
|
* @param $attr |
85
|
|
|
* @param $value |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function css($elem, $attr, $value) |
89
|
|
|
{ |
90
|
|
|
$this->css[$elem]->add($attr, $value); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* add a filter to this column. |
97
|
|
|
* $data can be array (associative or not), json, PDO or ORM result |
98
|
|
|
* |
99
|
|
|
* @param null $data |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function filter($data = null) |
103
|
|
|
{ |
104
|
|
|
$this->filterData = Table::isJson($data) ? json_decode($data) : $data; |
105
|
|
|
$this->filter = true; |
106
|
|
|
$this->tableInstance->hasFilters = true; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* add this column to the table |
113
|
|
|
* |
114
|
|
|
* @return Table |
115
|
|
|
*/ |
116
|
|
|
public function add() |
117
|
|
|
{ |
118
|
|
|
return $this->tableInstance; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* render column header cell <th> |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function renderHeader() |
127
|
|
|
{ |
128
|
|
|
if ($this->title === false) { |
129
|
|
|
return ""; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (empty($this->title) && !$this->isCallable($this->value)) { |
133
|
|
|
if ($this->tableInstance->titlesMode == 'underscore') $this->title = $this->underscoreToTitle($this->value); |
134
|
|
|
elseif ($this->tableInstance->titlesMode == 'camelcase') $this->title = $this->camelToTitle($this->value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$template = '<th {attrs} style="{css}">{title}</th>'; |
138
|
|
|
$attrs = $this->attrs['th']->render('{prop}="{val}" '); |
139
|
|
|
$css = $this->css['th']->render('{prop}:{val}; '); |
140
|
|
|
|
141
|
|
|
return str_replace(['{attrs}', '{css}', '{title}'], [$attrs, $css, $this->title], $template); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* render column filter |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function renderFilter() |
150
|
|
|
{ |
151
|
|
|
$html = ''; |
152
|
|
|
if ($this->filterData !== null) { |
153
|
|
|
$html .= '<select class="form-control input-sm" style="width: 99%"><option value=""></option>'; |
154
|
|
|
foreach ($this->filterData as $option) { |
155
|
|
|
if (is_string($option)) { |
156
|
|
|
$option = [$option, $option]; |
157
|
|
|
} |
158
|
|
|
elseif (is_object($option)) { |
159
|
|
|
$option = array_values(get_object_vars($option)); |
160
|
|
|
} |
161
|
|
|
$html .= '<option value="'.$option[0].'">'.$option[1].'</option>'; |
162
|
|
|
} |
163
|
|
|
$html .= '</select>'; |
164
|
|
|
} |
165
|
|
|
elseif ($this->filter) { |
166
|
|
|
$html = '<input type="text" class="form-control input-sm" style="width: 99%">'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return '<td>'.$html.'</td>'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* render column body cell <td> |
174
|
|
|
* |
175
|
|
|
* @param $row |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
public function renderBody( &$row ) |
179
|
|
|
{ |
180
|
|
|
$template = '<td {attrs} style="{css}">{val}</td>'; |
181
|
|
|
$attrs = $this->attrs['td']->render('{prop}="{val}" '); |
182
|
|
|
$css = $this->css['td']->render('{prop}:{val}; '); |
183
|
|
|
|
184
|
|
|
$val = ""; |
185
|
|
|
if ($this->isCallable($this->value)) { |
186
|
|
|
$val = $this->value; |
187
|
|
|
$val = $val($row); |
188
|
|
|
} |
189
|
|
|
elseif (is_object($row)) { |
190
|
|
|
$val = $row->{$this->value}; |
191
|
|
|
} |
192
|
|
|
elseif (is_array($row)) { |
193
|
|
|
$val = $this->value !== null ? $row[$this->value] : ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return str_replace(['{attrs}','{css}','{title}','{val}'], [$attrs, $css, $this->title, $val], $template); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $str |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
|
|
private function camelToTitle($str) |
205
|
|
|
{ |
206
|
|
|
$intermediate = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $str); |
207
|
|
|
$titleStr = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $intermediate); |
208
|
|
|
|
209
|
|
|
return $titleStr; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $str |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
private function underscoreToTitle($str) |
218
|
|
|
{ |
219
|
|
|
$str = ucwords(str_replace("_", " ", $str)); |
220
|
|
|
|
221
|
|
|
return $str; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $var |
227
|
|
|
* @return boolean |
228
|
|
|
*/ |
229
|
|
|
private function isCallable($var) |
230
|
|
|
{ |
231
|
|
|
return (!is_string($var) && is_callable($var)) || (is_object($var) && $var instanceof \Closure); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|