Passed
Push — master ( 47de24...d4bd11 )
by Jean-Christophe
03:33
created

HtmlTableContent::getRowCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
namespace Ajax\semantic\html\content\table;
3
4
use Ajax\semantic\html\base\HtmlSemCollection;
5
use Ajax\service\JArray;
6
use Ajax\common\html\HtmlCollection;
7
use Ajax\common\html\HtmlDoubleElement;
8
use Ajax\semantic\html\base\traits\BaseTrait;
9
10
/**
11
 * a table content (thead, tbody or tfoot)
12
 *
13
 * @author jc
14
 *
15
 */
16
class HtmlTableContent extends HtmlSemCollection {
17
	protected $_focusable=false;
18
19
	protected $_tdTagNames = [
20
		"thead" => "th",
21
		"tbody" => "td",
22
		"tfoot" => "th"
23
	];
24
25
	protected $_merged = false;
26
27
	/**
28
	 *
29
	 * @param string $identifier
30
	 * @param string $tagName
31
	 * @param int $rowCount
32
	 * @param int $colCount
33
	 */
34
	public function __construct($identifier, $tagName = "tbody", $rowCount = NULL, $colCount = NULL) {
35
		parent::__construct($identifier, $tagName, "");
36
		if (isset($rowCount) && isset($colCount))
37
			$this->setRowCount($rowCount, $colCount);
38
	}
39
40
	/**
41
	 *
42
	 * @param int $rowCount
43
	 * @param int $colCount
44
	 * @return HtmlTableContent
45
	 */
46
	public function setRowCount($rowCount, $colCount) {
47
		$count = $this->count();
48
		for ($i = $count; $i < $rowCount; $i ++) {
49
			$this->addItem($colCount);
50
		}
51
		return $this;
52
	}
53
54
	public function getTdTagName($tagName) {
55
		return $this->_tdTagNames[$this->tagName];
56
	}
57
58
	public function refreshTR() {
59
		$this->_template = "%wrapContentBefore%%content%%wrapContentAfter%";
60
	}
61
62
	/**
63
	 *
64
	 * {@inheritdoc}
65
	 *
66
	 * @see \Ajax\common\html\HtmlCollection::createItem()
67
	 * @return HtmlTR
68
	 */
69
	protected function createItem($value) {
70
		$count = $this->count();
71
		$tr = new HtmlTR("");
72
		$tr->setContainer($this, $count);
73
		if($this->_focusable) {
74
			$tr->setProperty('tabindex', $count);
75
		}
76
		$tr->setTdTagName($this->_tdTagNames[$this->tagName]);
77
		if (isset($value) === true) {
78
			$tr->setColCount($value);
79
		}
80
		return $tr;
81
	}
82
83
	public function newRow($value) {
84
		return $this->createItem($value);
85
	}
86
87
	/**
88
	 *
89
	 * @param int $colCount
90
	 * @return HtmlTR
91
	 */
92
	public function addRow($colCount) {
93
		return $this->addItem($colCount);
94
	}
95
96
	/**
97
	 *
98
	 * @param mixed $row
99
	 * @return HtmlTR
100
	 */
101
	public function _addRow($row) {
102
		return $this->addItem($row);
103
	}
104
105
	public function addMergeRow($colCount, $value = null) {
106
		$row = $this->addRow($colCount);
107
		$row->mergeCol();
108
		if (isset($value)) {
109
			$row->setValues([
110
				$value
111
			]);
112
		}
113
		return $row;
114
	}
115
116
	/**
117
	 *
118
	 * @return HtmlTR
119
	 */
120
	public function getItem($index) {
121
		return parent::getItem($index);
122
	}
123
124
	/**
125
	 * Returns the cell (HtmlTD) at position $row,$col
126
	 *
127
	 * @param int $row
128
	 * @param int $col
129
	 * @return HtmlTD|HtmlDoubleElement
130
	 */
131
	public function getCell($row, $col) {
132
		$row = $this->getItem($row);
133
		if (isset($row) && $row instanceof HtmlCollection) {
134
			$col = $row->getItem($col);
135
		} else {
136
			$col = $row;
137
		}
138
		return $col;
139
	}
140
141
	/**
142
	 *
143
	 * @param int $index
144
	 * @return HtmlTR
145
	 */
146
	public function getRow($index) {
147
		return $this->getItem($index);
148
	}
149
150
	/**
151
	 *
152
	 * @param int $row
153
	 * @param int $col
154
	 * @param mixed $value
155
	 * @return HtmlTableContent
156
	 */
157
	public function setCellValue($row, $col, $value = "") {
158
		$cell = $this->getCell($row, $col);
159
		if (isset($cell) === true) {
160
			$cell->setValue($value);
161
		}
162
		return $this;
163
	}
164
165
	/**
166
	 * Sets the cells values
167
	 *
168
	 * @param mixed $values
169
	 */
170
	public function setValues($values = array()) {
171
		return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
172
			$row->setValues($_values);
173
		});
174
	}
175
176
	/**
177
	 * Adds the cells values
178
	 *
179
	 * @param mixed $values
180
	 */
181
	public function addValues($values = array()) {
182
		return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) {
183
			$row->addValues($_values);
184
		});
185
	}
186
187
	/**
188
	 * Adds or sets the cells values
189
	 *
190
	 * @param mixed $values
191
	 * @param callable $callback
192
	 */
193
	protected function _addOrSetValues($values, $callback) {
194
		$count = $this->count();
195
		$isArray = true;
196
		if (! \is_array($values)) {
197
			$values = \array_fill(0, $count, $values);
198
			$isArray = false;
199
		}
200
		if (JArray::dimension($values) == 1 && $isArray)
201
			$values = [
202
				$values
203
			];
204
205
		$count = \min(\sizeof($values), $count);
206
207
		for ($i = 0; $i < $count; $i ++) {
208
			$row = $this->content[$i];
209
			$callback($row, $values[$i]);
210
		}
211
		return $this;
212
	}
213
214
	public function setColValues($colIndex, $values = array()) {
215
		$count = $this->count();
216
		if (! \is_array($values)) {
217
			$values = \array_fill(0, $count, $values);
218
		}
219
		$count = \min(\sizeof($values), $count);
220
		for ($i = 0; $i < $count; $i ++) {
221
			$this->getCell($i, $colIndex)->setValue($values[$i]);
222
		}
223
		return $this;
224
	}
225
226
	public function addColVariations($colIndex, $variations = array()) {
227
		$count = $this->count();
228
		for ($i = 0; $i < $count; $i ++) {
229
			$cell = $this->getCell($i, $colIndex);
230
			if ($cell instanceof BaseTrait)
231
				$cell->addVariations($variations);
232
		}
233
		return $this;
234
	}
235
236
	public function addPropertyCol($colIndex, $name, $value) {
237
		$count = $this->count();
238
		for ($i = 0; $i < $count; $i ++) {
239
			$cell = $this->getCell($i, $colIndex);
240
			if (isset($cell))
241
				$cell->addToProperty($name, $value);
242
		}
243
		return $this;
244
	}
245
246
	public function setRowValues($rowIndex, $values = array()) {
247
		$count = $this->count();
248
		if (! \is_array($values)) {
249
			$values = \array_fill(0, $count, $values);
250
		}
251
		$this->getItem($rowIndex)->setValues($values);
252
		return $this;
253
	}
254
255
	private function colAlign($colIndex, $function) {
256
		$count = $this->count();
257
		for ($i = 0; $i < $count; $i ++) {
258
			$index = $this->content[$i]->getColPosition($colIndex);
259
			if ($index !== NULL)
260
				$this->getCell($i, $index)->$function();
261
		}
262
		return $this;
263
	}
264
265
	private function colAlignFromRight($colIndex, $function) {
266
		$count = $this->count();
267
		for ($i = 0; $i < $count; $i ++) {
268
			$maxRow = $this->content[$i]->count();
269
			$index = $maxRow - $colIndex - 1;
270
			if (($cell = $this->getCell($i, $index)) !== NULL) {
271
				if ($cell->getColspan() == 1)
0 ignored issues
show
Bug introduced by
The method getColspan() does not exist on Ajax\common\html\HtmlDoubleElement. It seems like you code against a sub-type of Ajax\common\html\HtmlDoubleElement such as Ajax\semantic\html\content\table\HtmlTD or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\semantic\html\base\HtmlSemNavElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
				if ($cell->/** @scrutinizer ignore-call */ getColspan() == 1)
Loading history...
272
					$cell->$function();
273
			}
274
		}
275
		return $this;
276
	}
277
278
	public function colCenter($colIndex) {
279
		return $this->colAlign($colIndex, "textCenterAligned");
280
	}
281
282
	public function colRight($colIndex) {
283
		return $this->colAlign($colIndex, "textRightAligned");
284
	}
285
286
	public function colLeft($colIndex) {
287
		return $this->colAlign($colIndex, "textLeftAligned");
288
	}
289
290
	public function colCenterFromRight($colIndex) {
291
		return $this->colAlignFromRight($colIndex, "textCenterAligned");
292
	}
293
294
	public function colRightFromRight($colIndex) {
295
		return $this->colAlignFromRight($colIndex, "textRightAligned");
296
	}
297
298
	public function colLeftFromRight($colIndex) {
299
		return $this->colAlignFromRight($colIndex, "textLeftAligned");
300
	}
301
302
	/**
303
	 * Returns the number of rows (TR)
304
	 *
305
	 * @return int
306
	 */
307
	public function getRowCount() {
308
		return $this->count();
309
	}
310
311
	/**
312
	 * Returns the number of columns (TD)
313
	 *
314
	 * @return int
315
	 */
316
	public function getColCount() {
317
		$result = 0;
318
		if ($this->count() > 0)
319
			$result = $this->getItem(0)->count();
320
		return $result;
321
	}
322
323
	/**
324
	 * Removes the cell at position $rowIndex,$colIndex
325
	 *
326
	 * @param int $rowIndex
327
	 * @param int $colIndex
328
	 * @return HtmlTableContent
329
	 */
330
	public function delete($rowIndex, $colIndex = NULL) {
331
		if (isset($colIndex)) {
332
			$row = $this->getItem($rowIndex);
333
			if (isset($row) === true) {
334
				$row->delete($colIndex);
335
			}
336
		} else {
337
			$this->removeItem($rowIndex);
338
		}
339
		return $this;
340
	}
341
342
	public function toDelete($rowIndex, $colIndex) {
343
		$row = $this->getItem($rowIndex);
344
		if (isset($row) === true)
345
			$row->toDelete($colIndex);
346
		return $this;
347
	}
348
349
	public function toRowspanned($rowIndex, $colIndex) {
350
		$row = $this->getItem($rowIndex);
351
		if (isset($row) === true)
352
			$row->toRowspanned($colIndex);
353
		return $this;
354
	}
355
356
	public function mergeCol($rowIndex = 0, $colIndex = 0) {
357
		return $this->getItem($rowIndex)->mergeCol($colIndex);
358
	}
359
360
	public function mergeRow($rowIndex = 0, $colIndex = 0) {
361
		return $this->getItem($rowIndex)->mergeRow($colIndex);
362
	}
363
364
	public function setFullWidth() {
365
		return $this->addToProperty("class", "full-width");
366
	}
367
368
	public function sort($colIndex) {
369
		$this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
370
		return $this;
371
	}
372
373
	/**
374
	 *
375
	 * @param mixed $callback
376
	 * @param string $format
377
	 * @return HtmlTableContent
378
	 */
379
	public function conditionalCellFormat($callback, $format) {
380
		$rows = $this->content;
381
		foreach ($rows as $row) {
382
			$row->conditionalCellFormat($callback, $format);
383
		}
384
		return $this;
385
	}
386
387
	public function conditionalColFormat($colIndex, $callback, $format) {
388
		$rows = $this->content;
389
		foreach ($rows as $row) {
390
			$cell = $row->getItem($colIndex);
391
			$cell->conditionnalCellFormat($callback, $format);
392
		}
393
		return $this;
394
	}
395
396
	/**
397
	 *
398
	 * @param mixed $callback
399
	 * @param string $format
400
	 * @return HtmlTableContent
401
	 */
402
	public function conditionalRowFormat($callback, $format) {
403
		$rows = $this->content;
404
		foreach ($rows as $row) {
405
			$row->conditionalRowFormat($callback, $format);
406
		}
407
		return $this;
408
	}
409
410
	public function hideColumn($colIndex) {
411
		$rows = $this->content;
412
		foreach ($rows as $row) {
413
			$cell = $row->getItem($colIndex);
414
			$cell->addToProperty("style", "display:none;");
415
		}
416
		return $this;
417
	}
418
419
	/**
420
	 *
421
	 * @param mixed $callback
422
	 * @return HtmlTableContent
423
	 */
424
	public function applyCells($callback) {
425
		$rows = $this->content;
426
		foreach ($rows as $row) {
427
			$row->applyCells($callback);
428
		}
429
		return $this;
430
	}
431
432
	/**
433
	 *
434
	 * @param mixed $callback
435
	 * @return HtmlTableContent
436
	 */
437
	public function applyRows($callback) {
438
		$rows = $this->content;
439
		foreach ($rows as $row) {
440
			$row->apply($callback);
441
		}
442
		return $this;
443
	}
444
445
	/**
446
	 *
447
	 * @param int $colIndex
448
	 * @param string $function
449
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
450
	 * @see https://fomantic-ui.com/collections/table.html#definition needs rowspanned class
451
	 * @since fomantic-ui 2.4.8
452
	 */
453
	public function mergeIdentiqualValues($colIndex, $function = "strip_tags") {
454
		$rows = $this->content;
455
		$identiqual = null;
456
		$counter = 0;
457
		$cellToMerge = null;
458
		$functionExists = \function_exists($function);
459
		foreach ($rows as $row) {
460
			$cell = $row->getItem($colIndex);
461
			$value = $cell->getContent();
462
			if ($functionExists)
463
				$value = \call_user_func($function, $value);
464
			if ($value !== $identiqual) {
465
				if ($counter > 0 && isset($cellToMerge)) {
466
					$cellToMerge->setRowspanned($counter);
467
				}
468
				$counter = 0;
469
				$cellToMerge = $cell;
470
				$identiqual = $value;
471
			}
472
			$counter ++;
473
		}
474
		if ($counter > 0 && isset($cellToMerge)) {
475
			$cellToMerge->setRowspanned($counter);
476
		}
477
		return $this;
478
	}
479
480
	public function _isMerged() {
481
		return $this->_merged;
482
	}
483
484
	public function _setMerged($value) {
485
		$this->_merged = $value;
486
		return $this;
487
	}
488
489
	/**
490
	 * @param bool $focusable
491
	 */
492
	public function setFocusable(bool $focusable): void {
493
		$this->_focusable = $focusable;
494
	}
495
}
496