phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlTable.php
Go to the documentation of this file.
1 <?php
3 
7 use Ajax\JsUtils;
14 
22  use TableTrait;
23 
24  private $_colCount;
25 
26  private $_compileParts;
27 
28  private $_footer;
29 
31 
33 
34  private $_focusable = false;
35 
40  public function getActiveRowSelector() {
42  }
43 
44  protected $_innerScript;
45 
46  public function __construct($identifier, $rowCount, $colCount) {
47  parent::__construct($identifier, "table", "ui table");
48  $this->content = array();
49  $this->setRowCount($rowCount, $colCount);
50  $this->_variations = [
54  ];
55  $this->_compileParts = [
56  "thead",
57  "tbody",
58  "tfoot"
59  ];
60  $this->_afterCompileEvents = [];
61  }
62 
69  public function getPart($key) {
70  if (\array_key_exists($key, $this->content) === false) {
71  $this->content[$key] = new HtmlTableContent("", $key);
72  if ($key !== "tbody") {
73  $this->content[$key]->setRowCount(1, $this->_colCount);
74  }
75  }
76  return $this->content[$key];
77  }
78 
79  protected function _getFirstPart() {
80  if (isset($this->content["thead"])) {
81  return $this->content["thead"];
82  }
83  return $this->content["tbody"];
84  }
85 
91  public function getBody() {
92  return $this->getPart("tbody");
93  }
94 
100  public function getRowCount() {
101  return $this->getPart("tbody")->count();
102  }
103 
109  public function getHeader() {
110  return $this->getPart("thead");
111  }
112 
118  public function getFooter() {
119  return $this->getPart("tfoot");
120  }
121 
128  public function hasPart($key) {
129  return \array_key_exists($key, $this->content) === true;
130  }
131 
138  public function setRowCount($rowCount, $colCount) {
139  $this->_colCount = $colCount;
140  return $this->getBody()->setRowCount($rowCount, $colCount);
141  }
142 
150  public function getCell($row, $col) {
151  return $this->getBody()->getCell($row, $col);
152  }
153 
160  public function getRow($rowIndex) {
161  return $this->getBody()->getRow($rowIndex);
162  }
163 
170  public function addRow($values = array()) {
171  $row = $this->getBody()->addRow($this->_colCount);
172  $row->setValues(\array_values($values));
173  return $row;
174  }
175 
181  public function newRow() {
182  return $this->getBody()->newRow($this->_colCount);
183  }
184 
192  public function setValues($values = array()) {
193  $this->getBody()->setValues($values);
194  return $this;
195  }
196 
203  public function setHeaderValues($values = array()) {
204  return $this->getHeader()->setValues($values);
205  }
206 
213  public function setFooterValues($values = array()) {
214  return $this->getFooter()->setValues($values);
215  }
216 
224  public function setColValues($colIndex, $values = array()) {
225  $this->getBody()->setColValues($colIndex, $values);
226  return $this;
227  }
228 
236  public function setRowValues($rowIndex, $values = array()) {
237  $this->getBody()->setRowValues($rowIndex, $values);
238  return $this;
239  }
240 
241  public function addColVariations($colIndex, $variations = array()) {
242  return $this->getBody()->addColVariations($colIndex, $variations);
243  }
244 
251  public function colCenter($colIndex) {
252  return $this->colAlign($colIndex, "colCenter");
253  }
254 
261  public function colRight($colIndex) {
262  return $this->colAlign($colIndex, "colRight");
263  }
264 
271  public function colLeft($colIndex) {
272  return $this->colAlign($colIndex, "colLeft");
273  }
274 
281  public function colCenterFromRight($colIndex) {
282  return $this->colAlign($colIndex, "colCenterFromRight");
283  }
284 
291  public function colRightFromRight($colIndex) {
292  return $this->colAlign($colIndex, "colRightFromRight");
293  }
294 
301  public function colLeftFromRight($colIndex) {
302  return $this->colAlign($colIndex, "colLeftFromRight");
303  }
304 
305  public function setColAlignment($colIndex, $alignment) {
306  switch ($alignment) {
307  case TextAlignment::LEFT:
308  $function = "colLeft";
309  break;
310 
312  $function = "colRight";
313  break;
314 
316  $function = "colCenter";
317  break;
318 
319  default:
320  $function = "colLeft";
321  }
322  $this->colAlign($colIndex, $function);
323  return $this;
324  }
325 
326  public function setColAlignmentFromRight($colIndex, $alignment) {
327  switch ($alignment) {
328  case TextAlignment::LEFT:
329  $function = "colLeftFromRight";
330  break;
331 
333  $function = "colRightFromRight";
334  break;
335 
337  $function = "colCenterFromRight";
338  break;
339 
340  default:
341  $function = "colLeftFromRight";
342  }
343  $this->colAlign($colIndex, $function);
344  return $this;
345  }
346 
347  private function colAlign($colIndex, $function) {
348  if (\is_array($colIndex)) {
349  foreach ($colIndex as $cIndex) {
350  $this->colAlign($cIndex, $function);
351  }
352  } else {
353  if ($this->hasPart("thead")) {
354  $this->getHeader()->$function($colIndex);
355  }
356  $this->getBody()->$function($colIndex);
357  }
358  return $this;
359  }
360 
370  public function conditionalCellFormat($callback, $format) {
371  $this->getBody()->conditionalCellFormat($callback, $format);
372  return $this;
373  }
374 
384  public function conditionalRowFormat($callback, $format) {
385  $this->getBody()->conditionalRowFormat($callback, $format);
386  return $this;
387  }
388 
395  public function applyCells($callback) {
396  $this->getBody()->applyCells($callback);
397  return $this;
398  }
399 
406  public function applyRows($callback) {
407  $this->getBody()->applyRows($callback);
408  return $this;
409  }
410 
417  public function compile(JsUtils $js = NULL, &$view = NULL) {
418  if (\sizeof($this->_compileParts) < 3) {
419  $this->_template = "%content%";
420  $this->refresh($js);
421  }
422  $this->content = JArray::sortAssociative($this->content, $this->_compileParts);
423  return parent::compile($js, $view);
424  }
425 
426  protected function compile_once(JsUtils $js = NULL, &$view = NULL) {
427  parent::compile_once($js, $view);
428  if ($this->propertyContains("class", "sortable")) {
429  $this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));");
430  }
431  }
432 
439  public function fromDatabaseObject($object, $function) {
440  $result = $function($object);
441  if (\is_array($result)) {
442  $result = $this->addRow($function($object));
443  } else {
444  $result = $this->getBody()->_addRow($result);
445  }
446  if (isset($this->_afterCompileEvents["onNewRow"])) {
447  if (\is_callable($this->_afterCompileEvents["onNewRow"]))
448  $this->_afterCompileEvents["onNewRow"]($result, $object);
449  }
450  return $result;
451  }
452 
460  public function setCompileParts($parts = [
461  "tbody"
462  ]) {
463  $this->_compileParts = $parts;
464  return $this;
465  }
466 
467  public function refreshTR() {
468  $this->setCompileParts();
469  $this->getPart("tbody")->refreshTR();
470  }
471 
472  public function refresh($js) {
473  $this->_footer = $this->getFooter();
474  if (isset($js)) {
475  $js->exec('$("#' . $this->identifier . ' tfoot").replaceWith("' . \addslashes($this->_footer) . '");', true);
476  }
477  }
478 
479  public function run(JsUtils $js) {
480  if (! $this->_runned) {
481  if (isset($this->_activeRowSelector)) {
482  $this->_activeRowSelector->run();
483  }
484  }
485  $result = parent::run($js);
486  if (isset($this->_footer))
487  $this->_footer->run($js);
488  $this->_runned = true;
489  return $result;
490  }
491 
499  public function onNewRow($callback) {
500  $this->_afterCompileEvents["onNewRow"] = $callback;
501  return $this;
502  }
503 
512  public function setActiveRowSelector($class = "active", $event = "click", $multiple = false) {
513  $this->_activeRowSelector = new ActiveRow($this, $class, $event, $multiple);
514  return $this;
515  }
516 
517  public function getActiveRowClass() {
518  if (isset($this->_activeRowSelector)) {
519  return $this->_activeRowSelector->getClass();
520  }
521  return 'active';
522  }
523 
524  public function hasActiveRowSelector() {
525  return isset($this->_activeRowSelector);
526  }
527 
528  public function hideColumn($colIndex) {
529  if (isset($this->content["thead"])) {
530  $this->content["thead"]->hideColumn($colIndex);
531  }
532  $this->content["tbody"]->hideColumn($colIndex);
533  if (isset($this->content["tfoot"])) {
534  $this->content["tfoot"]->hideColumn($colIndex);
535  }
536  return $this;
537  }
538 
539  public function setColWidth($colIndex, $width) {
540  $part = $this->_getFirstPart();
541  if ($part !== null && $part->count() > 0)
542  $part->getCell(0, $colIndex)->setWidth($width);
543  return $this;
544  }
545 
546  public function setColWidths($widths) {
547  $part = $this->_getFirstPart();
548  if ($part !== null && $part->count() > 0) {
549  $count = $part->getColCount();
550  if (! \is_array($widths)) {
551  $widths = \array_fill(0, $count, $widths);
552  }
553  $max = \min(\sizeof($widths), $count);
554  for ($i = 0; $i < $max; $i ++) {
555  $part->getCell(0, $i)->setWidth($widths[$i]);
556  }
557  }
558  return $this;
559  }
560 
561  public function mergeIdentiqualValues($colIndex, $function = "strip_tags") {
562  $body = $this->getBody();
563  $body->mergeIdentiqualValues($colIndex, $function);
564  return $this;
565  }
566 
571  public function getInnerScript() {
572  return $this->_innerScript;
573  }
574 
579  public function setInnerScript($_innerScript) {
580  $this->_innerScript = $_innerScript;
581  }
582 
583  public function onActiveRowChange($jsCode) {
584  $this->on("activeRowChange", $jsCode);
585  return $this;
586  }
587 
588  public function addMergeRow($colCount, $value = null) {
589  return $this->getBody()->addMergeRow($colCount, $value);
590  }
591 
596  public function setFocusable(bool $focusable): void {
597  $this->getBody()->setFocusable($focusable);
598  }
599 }
setActiveRowSelector($class="active", $event="click", $multiple=false)
Defines how a row is selectable.
Definition: HtmlTable.php:512
addColVariations($colIndex, $variations=array())
Definition: HtmlTable.php:241
conditionalCellFormat($callback, $format)
Applies a format on each cell when $callback returns true.
Definition: HtmlTable.php:370
addRow($values=array())
Adds a new row and sets $values to his cols.
Definition: HtmlTable.php:170
newRow()
adds and returns a new row
Definition: HtmlTable.php:181
colRightFromRight($colIndex)
Sets the col alignment to right.
Definition: HtmlTable.php:291
applyRows($callback)
Applies a callback function on each row.
Definition: HtmlTable.php:406
mergeIdentiqualValues($colIndex, $function="strip_tags")
Definition: HtmlTable.php:561
compile_once(JsUtils $js=NULL, &$view=NULL)
Definition: HtmlTable.php:426
on($event, $jsCode, $stopPropagation=false, $preventDefault=false)
getHeader()
Returns/create eventually the header of the table.
Definition: HtmlTable.php:109
propertyContains($propertyName, $value)
A class for row selection in Table or DataTable.
Definition: ActiveRow.php:10
setCompileParts($parts=["tbody"])
Sets the parts of the Table to compile.
Definition: HtmlTable.php:460
colRight($colIndex)
Sets the col alignment to right.
Definition: HtmlTable.php:261
getFooter()
Returns/create eventually the footer of the table.
Definition: HtmlTable.php:118
applyCells($callback)
Applies a callback function on each cell.
Definition: HtmlTable.php:395
setRowValues($rowIndex, $values=array())
Sets values to the row at index $rowIndex.
Definition: HtmlTable.php:236
getCell($row, $col)
Returns the cell (HtmlTD) at position $row,$col.
Definition: HtmlTable.php:150
a table content (thead, tbody or tfoot)
colCenter($colIndex)
Sets the col alignment to center.
Definition: HtmlTable.php:251
setColAlignmentFromRight($colIndex, $alignment)
Definition: HtmlTable.php:326
colLeftFromRight($colIndex)
Sets col alignment to left.
Definition: HtmlTable.php:301
conditionalRowFormat($callback, $format)
Applies a format on each row when $callback returns true.
Definition: HtmlTable.php:384
compile(JsUtils $js=NULL, &$view=NULL)
{}
Definition: HtmlTable.php:417
setHeaderValues($values=array())
Sets the header values.
Definition: HtmlTable.php:203
setColValues($colIndex, $values=array())
Sets values to the col at index $colIndex.
Definition: HtmlTable.php:224
setFooterValues($values=array())
Sets the footer values.
Definition: HtmlTable.php:213
Semantic HTML Table component.
Definition: HtmlTable.php:21
Base class for Semantic double elements.
getRow($rowIndex)
Retuns the row at $rowIndex.
Definition: HtmlTable.php:160
onNewRow($callback)
The callback function called after the insertion of each row when fromDatabaseObjects is called callb...
Definition: HtmlTable.php:499
JQuery PHP library.
Definition: JsUtils.php:23
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
colCenterFromRight($colIndex)
Sets the col alignment to center.
Definition: HtmlTable.php:281
__construct($identifier, $rowCount, $colCount)
Definition: HtmlTable.php:46
hasPart($key)
Checks if the part corresponding to $key exists.
Definition: HtmlTable.php:128
setValues($values=array())
Sets the tbody values.
Definition: HtmlTable.php:192
getBody()
Returns/create eventually the body of the table.
Definition: HtmlTable.php:91
getRowCount()
Returns the number of rows (TR)
Definition: HtmlTable.php:100
colLeft($colIndex)
Sets col alignment to left.
Definition: HtmlTable.php:271
static sortAssociative($array, $sortedKeys=array())
Definition: JArray.php:66
getPart($key)
Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot...
Definition: HtmlTable.php:69