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