Passed
Push — master ( 57c450...393036 )
by Jean-Christophe
02:15
created

HtmlTableContent::addPropertyCol()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

242
				if($cell->/** @scrutinizer ignore-call */ getColspan()==1)
Loading history...
243
					$cell->$function();
244
			}
245
		}
246
		return $this;
247
	}
248
249
	public function colCenter($colIndex) {
250
		return $this->colAlign($colIndex, "textCenterAligned");
251
	}
252
253
	public function colRight($colIndex) {
254
		return $this->colAlign($colIndex, "textRightAligned");
255
	}
256
257
	public function colLeft($colIndex) {
258
		return $this->colAlign($colIndex, "textLeftAligned");
259
	}
260
	
261
	public function colCenterFromRight($colIndex) {
262
		return $this->colAlignFromRight($colIndex, "textCenterAligned");
263
	}
264
	
265
	public function colRightFromRight($colIndex) {
266
		return $this->colAlignFromRight($colIndex, "textRightAligned");
267
	}
268
	
269
	public function colLeftFromRight($colIndex) {
270
		return $this->colAlignFromRight($colIndex, "textLeftAligned");
271
	}
272
273
	/**
274
	 * Returns the number of rows (TR)
275
	 * @return int
276
	 */
277
	public function getRowCount() {
278
		return $this->count();
279
	}
280
281
	/**
282
	 * Returns the number of columns (TD)
283
	 * @return int
284
	 */
285
	public function getColCount() {
286
		$result=0;
287
		if ($this->count() > 0)
288
			$result=$this->getItem(0)->count();
289
		return $result;
290
	}
291
292
	/**
293
	 * Removes the cell at position $rowIndex,$colIndex
294
	 * @param int $rowIndex
295
	 * @param int $colIndex
296
	 * @return HtmlTableContent
297
	 */
298
	public function delete($rowIndex, $colIndex=NULL) {
299
		if (isset($colIndex)) {
300
			$row=$this->getItem($rowIndex);
301
			if (isset($row) === true) {
302
				$row->delete($colIndex);
303
			}
304
		} else {
305
			$this->removeItem($rowIndex);
306
		}
307
		return $this;
308
	}
309
310
	public function toDelete($rowIndex, $colIndex){
311
		$row=$this->getItem($rowIndex);
312
		if (isset($row) === true)
313
			$row->toDelete($colIndex);
314
		return $this;
315
	}
316
317
	public function mergeCol($rowIndex=0, $colIndex=0) {
318
		return $this->getItem($rowIndex)->mergeCol($colIndex);
319
	}
320
321
	public function mergeRow($rowIndex=0, $colIndex=0) {
322
		return $this->getItem($rowIndex)->mergeRow($colIndex);
323
	}
324
325
	public function setFullWidth() {
326
		return $this->addToProperty("class", "full-width");
327
	}
328
329
	public function sort($colIndex) {
330
		$this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
331
		return $this;
332
	}
333
334
	/**
335
	 * @param mixed $callback
336
	 * @param string $format
337
	 * @return HtmlTableContent
338
	 */
339
	public function conditionalCellFormat($callback, $format) {
340
		$rows=$this->content;
341
		foreach ( $rows as $row ) {
342
			$row->conditionalCellFormat($callback, $format);
343
		}
344
		return $this;
345
	}
346
347
	public function conditionalColFormat($colIndex,$callback,$format){
348
		$rows=$this->content;
349
		foreach ( $rows as $row ) {
350
			$cell=$row->getItem($colIndex);
351
			$cell->conditionnalCellFormat($callback,$format);
352
		}
353
		return $this;
354
	}
355
356
	/**
357
	 * @param mixed $callback
358
	 * @param string $format
359
	 * @return HtmlTableContent
360
	 */
361
	public function conditionalRowFormat($callback, $format) {
362
		$rows=$this->content;
363
		foreach ( $rows as $row ) {
364
			$row->conditionalRowFormat($callback, $format);
365
		}
366
		return $this;
367
	}
368
369
	public function hideColumn($colIndex){
370
		$rows=$this->content;
371
		foreach ( $rows as $row ) {
372
			$cell=$row->getItem($colIndex);
373
			$cell->addToProperty("style","display:none;");
374
		}
375
		return $this;
376
	}
377
378
	/**
379
	 * @param mixed $callback
380
	 * @return HtmlTableContent
381
	 */
382
	public function applyCells($callback) {
383
		$rows=$this->content;
384
		foreach ( $rows as $row ) {
385
			$row->applyCells($callback);
386
		}
387
		return $this;
388
	}
389
390
	/**
391
	 * @param mixed $callback
392
	 * @return HtmlTableContent
393
	 */
394
	public function applyRows($callback) {
395
		$rows=$this->content;
396
		foreach ( $rows as $row ) {
397
			$row->apply($callback);
398
		}
399
		return $this;
400
	}
401
402
	public function mergeIdentiqualValues($colIndex,$function="strip_tags"){
403
		$rows=$this->content;
404
		$identiqual=null;
405
		$counter=0;
406
		$cellToMerge=null;
407
		$functionExists=\function_exists($function);
408
		foreach ( $rows as $row ) {
409
			$cell=$row->getItem($colIndex);
410
			$value=$cell->getContent();
411
			if($functionExists)
412
				$value=\call_user_func($function,$value);
413
			if($value!==$identiqual){
414
				if($counter>0 && isset($cellToMerge)){
415
					$cellToMerge->setRowspan($counter);
416
				}
417
				$counter=0;
418
				$cellToMerge=$cell;
419
				$identiqual=$value;
420
			}
421
			$counter++;
422
		}
423
		if($counter>0 && isset($cellToMerge)){
424
			$cellToMerge->setRowspan($counter);
425
		}
426
		return $this;
427
	}
428
429
	public function _isMerged(){
430
		return $this->_merged;
431
	}
432
433
	public function _setMerged($value){
434
		$this->_merged=$value;
435
		return $this;
436
	}
437
}
438