1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\table; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
use Ajax\semantic\html\content\table\HtmlTableContent; |
7
|
|
|
use Ajax\semantic\html\base\constants\Variation; |
8
|
|
|
use Ajax\JsUtils; |
9
|
|
|
|
10
|
|
|
use Ajax\service\JArray; |
11
|
|
|
use Ajax\semantic\html\content\table\HtmlTR; |
12
|
|
|
use Ajax\semantic\html\collections\table\traits\TableTrait; |
13
|
|
|
use Ajax\semantic\html\content\table\HtmlTD; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Semantic HTML Table component |
17
|
|
|
* @author jc |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class HtmlTable extends HtmlSemDoubleElement { |
21
|
|
|
use TableTrait; |
22
|
|
|
private $_colCount; |
23
|
|
|
private $_compileParts; |
24
|
|
|
private $_footer; |
25
|
|
|
private $_afterCompileEvents; |
26
|
|
|
private $_activeRowSelector; |
27
|
|
|
|
28
|
|
|
public function __construct($identifier, $rowCount, $colCount) { |
29
|
|
|
parent::__construct($identifier, "table", "ui table"); |
30
|
|
|
$this->content=array (); |
31
|
|
|
$this->setRowCount($rowCount, $colCount); |
32
|
|
|
$this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ]; |
33
|
|
|
$this->_compileParts=["thead","tbody","tfoot"]; |
34
|
|
|
$this->_afterCompileEvents=[]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
* @see TableTrait::getTable() |
40
|
|
|
*/ |
41
|
|
|
protected function getTable() { |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
47
|
|
|
* @param string $key |
48
|
|
|
* @return HtmlTableContent |
49
|
|
|
*/ |
50
|
|
|
public function getPart($key) { |
51
|
|
|
if (\array_key_exists($key, $this->content) === false) { |
52
|
|
|
$this->content[$key]=new HtmlTableContent("", $key); |
53
|
|
|
if ($key !== "tbody") { |
54
|
|
|
$this->content[$key]->setRowCount(1, $this->_colCount); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return $this->content[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function _getFirstPart(){ |
61
|
|
|
if(isset($this->content["thead"])){ |
62
|
|
|
return $this->content["thead"]; |
63
|
|
|
} |
64
|
|
|
return $this->content["tbody"]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns/create eventually the body of the table |
70
|
|
|
* @return HtmlTableContent |
71
|
|
|
*/ |
72
|
|
|
public function getBody() { |
73
|
|
|
return $this->getPart("tbody"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the number of rows (TR) |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getRowCount() { |
81
|
|
|
return $this->getPart("tbody")->count(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns/create eventually the header of the table |
86
|
|
|
* @return HtmlTableContent |
87
|
|
|
*/ |
88
|
|
|
public function getHeader() { |
89
|
|
|
return $this->getPart("thead"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns/create eventually the footer of the table |
94
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
95
|
|
|
*/ |
96
|
|
|
public function getFooter() { |
97
|
|
|
return $this->getPart("tfoot"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Checks if the part corresponding to $key exists |
102
|
|
|
* @param string $key |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function hasPart($key) { |
106
|
|
|
return \array_key_exists($key, $this->content) === true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @param int $rowCount |
112
|
|
|
* @param int $colCount |
113
|
|
|
* @return HtmlTableContent |
114
|
|
|
*/ |
115
|
|
|
public function setRowCount($rowCount, $colCount) { |
116
|
|
|
$this->_colCount=$colCount; |
117
|
|
|
return $this->getBody()->setRowCount($rowCount, $colCount); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the cell (HtmlTD) at position $row,$col |
122
|
|
|
* @param int $row |
123
|
|
|
* @param int $col |
124
|
|
|
* @return HtmlTD |
125
|
|
|
*/ |
126
|
|
|
public function getCell($row, $col) { |
127
|
|
|
return $this->getBody()->getCell($row, $col); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Retuns the row at $rowIndex |
132
|
|
|
* @param int $rowIndex |
133
|
|
|
* @return HtmlTR |
134
|
|
|
*/ |
135
|
|
|
public function getRow($rowIndex) { |
136
|
|
|
return $this->getBody()->getRow($rowIndex); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds a new row and sets $values to his cols |
141
|
|
|
* @param array $values |
142
|
|
|
* @return HtmlTR |
143
|
|
|
*/ |
144
|
|
|
public function addRow($values=array()) { |
145
|
|
|
$row=$this->getBody()->addRow($this->_colCount); |
146
|
|
|
$row->setValues(\array_values($values)); |
147
|
|
|
return $row; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* adds and returns a new row |
152
|
|
|
* @return HtmlTR |
153
|
|
|
*/ |
154
|
|
|
public function newRow() { |
155
|
|
|
return $this->getBody()->newRow($this->_colCount); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Sets the tbody values |
160
|
|
|
* @param array $values values in an array of array |
161
|
|
|
* @return HtmlTable |
162
|
|
|
*/ |
163
|
|
|
public function setValues($values=array()) { |
164
|
|
|
$this->getBody()->setValues($values); |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets the header values |
170
|
|
|
* @param array $values |
171
|
|
|
* @return HtmlTableContent |
172
|
|
|
*/ |
173
|
|
|
public function setHeaderValues($values=array()) { |
174
|
|
|
return $this->getHeader()->setValues($values); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets the footer values |
179
|
|
|
* @param array $values |
180
|
|
|
* @return HtmlTableContent |
181
|
|
|
*/ |
182
|
|
|
public function setFooterValues($values=array()) { |
183
|
|
|
return $this->getFooter()->setValues($values); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets values to the col at index $colIndex |
188
|
|
|
* @param int $colIndex |
189
|
|
|
* @param array $values |
190
|
|
|
* @return HtmlTable |
191
|
|
|
*/ |
192
|
|
|
public function setColValues($colIndex, $values=array()) { |
193
|
|
|
$this->getBody()->setColValues($colIndex, $values); |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets values to the row at index $rowIndex |
199
|
|
|
* @param int $rowIndex |
200
|
|
|
* @param array $values |
201
|
|
|
* @return HtmlTable |
202
|
|
|
*/ |
203
|
|
|
public function setRowValues($rowIndex, $values=array()) { |
204
|
|
|
$this->getBody()->setRowValues($rowIndex, $values); |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function addColVariations($colIndex, $variations=array()) { |
209
|
|
|
return $this->getBody()->addColVariations($colIndex, $variations); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Sets the col alignment to center |
214
|
|
|
* @param int $colIndex |
215
|
|
|
* @return HtmlTable |
216
|
|
|
*/ |
217
|
|
|
public function colCenter($colIndex) { |
218
|
|
|
return $this->colAlign($colIndex, "colCenter"); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sets the col alignment to right |
223
|
|
|
* @param int $colIndex |
224
|
|
|
* @return HtmlTable |
225
|
|
|
*/ |
226
|
|
|
public function colRight($colIndex) { |
227
|
|
|
return $this->colAlign($colIndex, "colRight"); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Sets col alignment to left |
232
|
|
|
* @param int $colIndex |
233
|
|
|
* @return HtmlTable |
234
|
|
|
*/ |
235
|
|
|
public function colLeft($colIndex) { |
236
|
|
|
return $this->colAlign($colIndex, "colLeft"); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function colAlign($colIndex, $function) { |
240
|
|
|
if (\is_array($colIndex)) { |
241
|
|
|
foreach ( $colIndex as $cIndex ) { |
242
|
|
|
$this->colAlign($cIndex, $function); |
243
|
|
|
} |
244
|
|
|
} else { |
245
|
|
|
if ($this->hasPart("thead")) { |
246
|
|
|
$this->getHeader()->$function($colIndex); |
247
|
|
|
} |
248
|
|
|
$this->getBody()->$function($colIndex); |
249
|
|
|
} |
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Applies a format on each cell when $callback returns true |
255
|
|
|
* @param callable $callback function with the cell as parameter, must return a boolean |
256
|
|
|
* @param string $format css class to apply |
257
|
|
|
* @return HtmlTable |
258
|
|
|
*/ |
259
|
|
|
public function conditionalCellFormat($callback, $format) { |
260
|
|
|
$this->getBody()->conditionalCellFormat($callback, $format); |
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Applies a format on each row when $callback returns true |
266
|
|
|
* @param callable $callback function with the row as parameter, must return a boolean |
267
|
|
|
* @param string $format css class to apply |
268
|
|
|
* @return HtmlTable |
269
|
|
|
*/ |
270
|
|
|
public function conditionalRowFormat($callback, $format) { |
271
|
|
|
$this->getBody()->conditionalRowFormat($callback, $format); |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Applies a callback function on each cell |
277
|
|
|
* @param callable $callback |
278
|
|
|
* @return HtmlTable |
279
|
|
|
*/ |
280
|
|
|
public function applyCells($callback) { |
281
|
|
|
$this->getBody()->applyCells($callback); |
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Applies a callback function on each row |
287
|
|
|
* @param callable $callback |
288
|
|
|
* @return HtmlTable |
289
|
|
|
*/ |
290
|
|
|
public function applyRows($callback) { |
291
|
|
|
$this->getBody()->applyRows($callback); |
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* |
297
|
|
|
* {@inheritDoc} |
298
|
|
|
* |
299
|
|
|
* @see HtmlSemDoubleElement::compile() |
300
|
|
|
*/ |
301
|
|
|
public function compile(JsUtils $js=NULL, &$view=NULL) { |
302
|
|
|
if(\sizeof($this->_compileParts)<3){ |
303
|
|
|
$this->_template="%content%"; |
304
|
|
|
$this->refresh(); |
305
|
|
|
} |
306
|
|
|
$this->content=JArray::sortAssociative($this->content, $this->_compileParts); |
307
|
|
|
return parent::compile($js, $view); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
311
|
|
|
if ($this->propertyContains("class", "sortable")) { |
312
|
|
|
$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));"); |
313
|
|
|
} |
314
|
|
|
if(isset($this->_activeRowSelector)){ |
315
|
|
|
$this->_activeRowSelector->compile(); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* |
321
|
|
|
* {@inheritDoc} |
322
|
|
|
* |
323
|
|
|
* @see BaseHtml::fromDatabaseObject() |
324
|
|
|
*/ |
325
|
|
|
public function fromDatabaseObject($object, $function) { |
326
|
|
|
$result=$function($object); |
327
|
|
|
if (\is_array($result)) { |
328
|
|
|
$result= $this->addRow($function($object)); |
329
|
|
|
} else { |
330
|
|
|
$result= $this->getBody()->_addRow($result); |
331
|
|
|
} |
332
|
|
|
if(isset($this->_afterCompileEvents["onNewRow"])){ |
333
|
|
|
if(\is_callable($this->_afterCompileEvents["onNewRow"])) |
334
|
|
|
$this->_afterCompileEvents["onNewRow"]($result,$object); |
335
|
|
|
} |
336
|
|
|
return $result; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Sets the parts of the Table to compile |
341
|
|
|
* @param array $parts array of thead,tbody,tfoot |
342
|
|
|
* @return HtmlTable |
343
|
|
|
*/ |
344
|
|
|
public function setCompileParts($parts=["tbody"]) { |
345
|
|
|
$this->_compileParts=$parts; |
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function refresh(){ |
350
|
|
|
$this->_footer=$this->getFooter(); |
351
|
|
|
$this->addEvent("execute", '$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function run(JsUtils $js){ |
355
|
|
|
$result= parent::run($js); |
356
|
|
|
if(isset($this->_footer)) |
357
|
|
|
$this->_footer->run($js); |
358
|
|
|
return $result; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* The callback function called after the insertion of each row when fromDatabaseObjects is called |
363
|
|
|
* callback function takes the parameters $row : the row inserted and $object: the instance of model used |
364
|
|
|
* @param callable $callback |
365
|
|
|
* @return HtmlTable |
366
|
|
|
*/ |
367
|
|
|
public function onNewRow($callback) { |
368
|
|
|
$this->_afterCompileEvents["onNewRow"]=$callback; |
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Defines how a row is selectable |
374
|
|
|
* @param string $class |
375
|
|
|
* @param string $event |
376
|
|
|
* @param boolean $multiple |
377
|
|
|
* @return HtmlTable |
378
|
|
|
*/ |
379
|
|
|
public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
380
|
|
|
$this->_activeRowSelector=new ActiveRow($this,$class,$event,$multiple); |
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function hideColumn($colIndex){ |
385
|
|
|
if(isset($this->content["thead"])){ |
386
|
|
|
$this->content["thead"]->hideColumn($colIndex); |
387
|
|
|
} |
388
|
|
|
$this->content["tbody"]->hideColumn($colIndex); |
389
|
|
|
if(isset($this->content["tfoot"])){ |
390
|
|
|
$this->content["tfoot"]->hideColumn($colIndex); |
391
|
|
|
} |
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function setColWidth($colIndex,$width){ |
396
|
|
|
$part=$this->_getFirstPart(); |
397
|
|
|
if($part!==null && $part->count()>0) |
398
|
|
|
$part->getCell(0, $colIndex)->setWidth($width); |
399
|
|
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function setColWidths($widths){ |
403
|
|
|
$part=$this->_getFirstPart(); |
404
|
|
|
if($part!==null && $part->col()>0){ |
405
|
|
|
$count=$part->getColCount(); |
406
|
|
|
if(!\is_array($widths)){ |
407
|
|
|
$widths=\array_fill(0, $count, $widths); |
408
|
|
|
} |
409
|
|
|
$max=\min(\sizeof($widths),$count); |
410
|
|
|
for($i=0;$i<$max;$i++){ |
411
|
|
|
$part->getCell(0, $i)->setWidth($widths[$i]); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
return $this; |
415
|
|
|
} |
416
|
|
|
} |