phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlTableContent.php
Go to the documentation of this file.
1 <?php
3 
9 
17 
18  protected $_focusable = false;
19 
20  protected $_tdTagNames = [
21  "thead" => "th",
22  "tbody" => "td",
23  "tfoot" => "th"
24  ];
25 
26  protected $_merged = false;
27 
35  public function __construct($identifier, $tagName = "tbody", $rowCount = NULL, $colCount = NULL) {
36  parent::__construct($identifier, $tagName, "");
37  if (isset($rowCount) && isset($colCount))
38  $this->setRowCount($rowCount, $colCount);
39  }
40 
47  public function setRowCount($rowCount, $colCount) {
48  $count = $this->count();
49  for ($i = $count; $i < $rowCount; $i ++) {
50  $this->addItem($colCount);
51  }
52  return $this;
53  }
54 
55  public function getTdTagName($tagName) {
56  return $this->_tdTagNames[$this->tagName];
57  }
58 
59  public function refreshTR() {
60  $this->_template = "%wrapContentBefore%%content%%wrapContentAfter%";
61  }
62 
70  protected function createItem($value) {
71  $count = $this->count();
72  $tr = new HtmlTR("");
73  $tr->setContainer($this, $count);
74  if ($this->_focusable) {
75  $tr->setProperty('tabindex', $count);
76  }
77  $tr->setTdTagName($this->_tdTagNames[$this->tagName]);
78  if (isset($value) === true) {
79  $tr->setColCount($value);
80  }
81  return $tr;
82  }
83 
84  public function newRow($value) {
85  return $this->createItem($value);
86  }
87 
93  public function addRow($colCount) {
94  return $this->addItem($colCount);
95  }
96 
102  public function _addRow($row) {
103  return $this->addItem($row);
104  }
105 
106  public function addMergeRow($colCount, $value = null) {
107  $row = $this->addRow($colCount);
108  $row->mergeCol();
109  if (isset($value)) {
110  $row->setValues([
111  $value
112  ]);
113  }
114  return $row;
115  }
116 
121  public function getItem($index) {
122  return parent::getItem($index);
123  }
124 
132  public function getCell($row, $col) {
133  $row = $this->getItem($row);
134  if (isset($row) && $row instanceof HtmlCollection) {
135  $col = $row->getItem($col);
136  } else {
137  $col = $row;
138  }
139  return $col;
140  }
141 
147  public function getRow($index) {
148  return $this->getItem($index);
149  }
150 
158  public function setCellValue($row, $col, $value = "") {
159  $cell = $this->getCell($row, $col);
160  if (isset($cell) === true) {
161  $cell->setValue($value);
162  }
163  return $this;
164  }
165 
171  public function setValues($values = array()) {
172  return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
173  $row->setValues($_values);
174  });
175  }
176 
182  public function addValues($values = array()) {
183  return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
184  $row->addValues($_values);
185  });
186  }
187 
194  protected function _addOrSetValues($values, $callback) {
195  $count = $this->count();
196  $isArray = true;
197  if (! \is_array($values)) {
198  $values = \array_fill(0, $count, $values);
199  $isArray = false;
200  }
201  if (JArray::dimension($values) == 1 && $isArray)
202  $values = [
203  $values
204  ];
205 
206  $count = \min(\sizeof($values), $count);
207 
208  for ($i = 0; $i < $count; $i ++) {
209  $row = $this->content[$i];
210  $callback($row, $values[$i]);
211  }
212  return $this;
213  }
214 
215  public function setColValues($colIndex, $values = array()) {
216  $count = $this->count();
217  if (! \is_array($values)) {
218  $values = \array_fill(0, $count, $values);
219  }
220  $count = \min(\sizeof($values), $count);
221  for ($i = 0; $i < $count; $i ++) {
222  $this->getCell($i, $colIndex)->setValue($values[$i]);
223  }
224  return $this;
225  }
226 
227  public function addColVariations($colIndex, $variations = array()) {
228  $count = $this->count();
229  for ($i = 0; $i < $count; $i ++) {
230  $cell = $this->getCell($i, $colIndex);
231  if ($cell instanceof BaseTrait)
232  $cell->addVariations($variations);
233  }
234  return $this;
235  }
236 
237  public function addPropertyCol($colIndex, $name, $value) {
238  $count = $this->count();
239  for ($i = 0; $i < $count; $i ++) {
240  $cell = $this->getCell($i, $colIndex);
241  if (isset($cell))
242  $cell->addToProperty($name, $value);
243  }
244  return $this;
245  }
246 
247  public function setRowValues($rowIndex, $values = array()) {
248  $count = $this->count();
249  if (! \is_array($values)) {
250  $values = \array_fill(0, $count, $values);
251  }
252  $this->getItem($rowIndex)->setValues($values);
253  return $this;
254  }
255 
256  private function colAlign($colIndex, $function) {
257  $count = $this->count();
258  for ($i = 0; $i < $count; $i ++) {
259  $index = $this->content[$i]->getColPosition($colIndex);
260  if ($index !== NULL) {
261  $cell = $this->getCell($i, $index);
262  if ($cell != NULL) {
263  $cell->$function();
264  }
265  }
266  }
267  return $this;
268  }
269 
270  private function colAlignFromRight($colIndex, $function) {
271  $count = $this->count();
272  for ($i = 0; $i < $count; $i ++) {
273  $maxRow = $this->content[$i]->count();
274  $index = $maxRow - $colIndex - 1;
275  if (($cell = $this->getCell($i, $index)) !== NULL) {
276  if ($cell->getColspan() == 1)
277  $cell->$function();
278  }
279  }
280  return $this;
281  }
282 
283  public function colCenter($colIndex) {
284  return $this->colAlign($colIndex, "textCenterAligned");
285  }
286 
287  public function colRight($colIndex) {
288  return $this->colAlign($colIndex, "textRightAligned");
289  }
290 
291  public function colLeft($colIndex) {
292  return $this->colAlign($colIndex, "textLeftAligned");
293  }
294 
295  public function colCenterFromRight($colIndex) {
296  return $this->colAlignFromRight($colIndex, "textCenterAligned");
297  }
298 
299  public function colRightFromRight($colIndex) {
300  return $this->colAlignFromRight($colIndex, "textRightAligned");
301  }
302 
303  public function colLeftFromRight($colIndex) {
304  return $this->colAlignFromRight($colIndex, "textLeftAligned");
305  }
306 
312  public function getRowCount() {
313  return $this->count();
314  }
315 
321  public function getColCount() {
322  $result = 0;
323  if ($this->count() > 0)
324  $result = $this->getItem(0)->count();
325  return $result;
326  }
327 
335  public function delete($rowIndex, $colIndex = NULL) {
336  if (isset($colIndex)) {
337  $row = $this->getItem($rowIndex);
338  if (isset($row) === true) {
339  $row->delete($colIndex);
340  }
341  } else {
342  $this->removeItem($rowIndex);
343  }
344  return $this;
345  }
346 
347  public function toDelete($rowIndex, $colIndex) {
348  $row = $this->getItem($rowIndex);
349  if (isset($row) === true)
350  $row->toDelete($colIndex);
351  return $this;
352  }
353 
354  public function toRowspanned($rowIndex, $colIndex) {
355  $row = $this->getItem($rowIndex);
356  if (isset($row) === true)
357  $row->toRowspanned($colIndex);
358  return $this;
359  }
360 
361  public function mergeCol($rowIndex = 0, $colIndex = 0) {
362  return $this->getItem($rowIndex)->mergeCol($colIndex);
363  }
364 
365  public function mergeRow($rowIndex = 0, $colIndex = 0) {
366  return $this->getItem($rowIndex)->mergeRow($colIndex);
367  }
368 
369  public function setFullWidth() {
370  return $this->addToProperty("class", "full-width");
371  }
372 
373  public function sort($colIndex) {
374  $this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
375  return $this;
376  }
377 
384  public function conditionalCellFormat($callback, $format) {
385  $rows = $this->content;
386  foreach ($rows as $row) {
387  $row->conditionalCellFormat($callback, $format);
388  }
389  return $this;
390  }
391 
392  public function conditionalColFormat($colIndex, $callback, $format) {
393  $rows = $this->content;
394  foreach ($rows as $row) {
395  $cell = $row->getItem($colIndex);
396  $cell->conditionnalCellFormat($callback, $format);
397  }
398  return $this;
399  }
400 
407  public function conditionalRowFormat($callback, $format) {
408  $rows = $this->content;
409  foreach ($rows as $row) {
410  $row->conditionalRowFormat($callback, $format);
411  }
412  return $this;
413  }
414 
415  public function hideColumn($colIndex) {
416  $rows = $this->content;
417  foreach ($rows as $row) {
418  $cell = $row->getItem($colIndex);
419  $cell->addToProperty("style", "display:none;");
420  }
421  return $this;
422  }
423 
429  public function applyCells($callback) {
430  $rows = $this->content;
431  foreach ($rows as $row) {
432  $row->applyCells($callback);
433  }
434  return $this;
435  }
436 
442  public function applyRows($callback) {
443  $rows = $this->content;
444  foreach ($rows as $row) {
445  $row->apply($callback);
446  }
447  return $this;
448  }
449 
458  public function mergeIdentiqualValues($colIndex, $function = "strip_tags") {
459  $rows = $this->content;
460  $identiqual = null;
461  $counter = 0;
462  $cellToMerge = null;
463  $functionExists = \function_exists($function);
464  foreach ($rows as $row) {
465  $cell = $row->getItem($colIndex);
466  $value = $cell->getContent();
467  if ($functionExists)
468  $value = \call_user_func($function, $value);
469  if ($value !== $identiqual) {
470  if ($counter > 0 && isset($cellToMerge)) {
471  $cellToMerge->setRowspanned($counter);
472  }
473  $counter = 0;
474  $cellToMerge = $cell;
475  $identiqual = $value;
476  }
477  $counter ++;
478  }
479  if ($counter > 0 && isset($cellToMerge)) {
480  $cellToMerge->setRowspanned($counter);
481  }
482  return $this;
483  }
484 
485  public function _isMerged() {
486  return $this->_merged;
487  }
488 
489  public function _setMerged($value) {
490  $this->_merged = $value;
491  return $this;
492  }
493 
498  public function setFocusable(bool $focusable): void {
499  $this->_focusable = $focusable;
500  }
501 }
addToProperty($name, $value, $separator=" ")
_addOrSetValues($values, $callback)
Adds or sets the cells values.
__construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL)
Base class for Html collections.
addItem($item)
adds and returns an item
a table content (thead, tbody or tfoot)
setValues($values=array())
Sets values to the row cols.
Definition: HtmlTR.php:89
getRowCount()
Returns the number of rows (TR)
addValues($values=array())
Adds values to the row cols.
Definition: HtmlTR.php:100
addValues($values=array())
Adds the cells values.
addColVariations($colIndex, $variations=array())
getColCount()
Returns the number of columns (TD)
conditionalColFormat($colIndex, $callback, $format)
Base class for Semantic Html collections.
mergeIdentiqualValues($colIndex, $function="strip_tags")
static dimension($array)
Definition: JArray.php:57
getCell($row, $col)
Returns the cell (HtmlTD) at position $row,$col.
setValues($values=array())
Sets the cells values.