Passed
Push — master ( 489ec7...331007 )
by Jean-Christophe
03:41
created

HtmlTableContent::colRightFromRight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
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
	/**
90
	 * @return HtmlTR
91
	 */
92
	public function getItem($index){
93
		return parent::getItem($index);
94
	}
95
96
	/**
97
	 * Returns the cell (HtmlTD) at position $row,$col
98
	 * @param int $row
99
	 * @param int $col
100
	 * @return HtmlTD|HtmlDoubleElement
101
	 */
102
	public function getCell($row, $col) {
103
		$row=$this->getItem($row);
104
		if (isset($row) && $row instanceof HtmlCollection) {
105
			$col=$row->getItem($col);
106
		}else{
107
			$col=$row;
108
		}
109
		return $col;
110
	}
111
112
	/**
113
	 *
114
	 * @param int $index
115
	 * @return HtmlTR
116
	 */
117
	public function getRow($index) {
118
		return $this->getItem($index);
119
	}
120
121
	/**
122
	 *
123
	 * @param int $row
124
	 * @param int $col
125
	 * @param mixed $value
126
	 * @return HtmlTableContent
127
	 */
128
	public function setCellValue($row, $col, $value="") {
129
		$cell=$this->getCell($row, $col);
130
		if (isset($cell) === true) {
131
			$cell->setValue($value);
132
		}
133
		return $this;
134
	}
135
136
	/**
137
	 * Sets the cells values
138
	 * @param mixed $values
139
	 */
140
	public function setValues($values=array()) {
141
		return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->setValues($_values);});
142
	}
143
144
	/**
145
	 * Adds the cells values
146
	 * @param mixed $values
147
	 */
148
	public function addValues($values=array()) {
149
		return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->addValues($_values);});
150
	}
151
152
	/**
153
	 * Adds or sets the cells values
154
	 * @param mixed $values
155
	 * @param callable $callback
156
	 */
157
	protected function _addOrSetValues($values,$callback) {
158
		$count=$this->count();
159
		$isArray=true;
160
		if (!\is_array($values)) {
161
			$values=\array_fill(0, $count, $values);
162
			$isArray=false;
163
		}
164
		if (JArray::dimension($values) == 1 && $isArray)
165
			$values=[ $values ];
166
167
		$count=\min(\sizeof($values), $count);
168
169
		for($i=0; $i < $count; $i++) {
170
			$row=$this->content[$i];
171
			$callback($row,$values[$i]);
172
		}
173
		return $this;
174
	}
175
176
	public function setColValues($colIndex, $values=array()) {
177
		$count=$this->count();
178
		if (!\is_array($values)) {
179
			$values=\array_fill(0, $count, $values);
180
		}
181
		$count=\min(\sizeof($values), $count);
182
		for($i=0; $i < $count; $i++) {
183
			$this->getCell($i, $colIndex)->setValue($values[$i]);
184
		}
185
		return $this;
186
	}
187
188
	public function addColVariations($colIndex, $variations=array()) {
189
		$count=$this->count();
190
		for($i=0; $i < $count; $i++) {
191
			$cell=$this->getCell($i, $colIndex);
192
			if($cell instanceof BaseTrait)
193
				$cell->addVariations($variations);
194
		}
195
		return $this;
196
	}
197
198
	public function setRowValues($rowIndex, $values=array()) {
199
		$count=$this->count();
200
		if (!\is_array($values)) {
201
			$values=\array_fill(0, $count, $values);
202
		}
203
		$this->getItem($rowIndex)->setValues($values);
204
		return $this;
205
	}
206
207
	private function colAlign($colIndex, $function) {
208
		$count=$this->count();
209
		for($i=0; $i < $count; $i++) {
210
			$index=$this->content[$i]->getColPosition($colIndex);
211
			if ($index !== NULL)
212
				$this->getCell($i, $index)->$function();
213
		}
214
		return $this;
215
	}
216
	
217
	private function colAlignFromRight($colIndex, $function) {
218
		$count=$this->count();
219
		for($i=0; $i < $count; $i++) {
220
			$maxRow=$this->content[$i]->count();
221
			$index=$maxRow-$colIndex-1;
222
			if (($cell=$this->getCell($i, $index))!== NULL)
223
				$cell->$function();
224
		}
225
		return $this;
226
	}
227
228
	public function colCenter($colIndex) {
229
		return $this->colAlign($colIndex, "textCenterAligned");
230
	}
231
232
	public function colRight($colIndex) {
233
		return $this->colAlign($colIndex, "textRightAligned");
234
	}
235
236
	public function colLeft($colIndex) {
237
		return $this->colAlign($colIndex, "textLeftAligned");
238
	}
239
	
240
	public function colCenterFromRight($colIndex) {
241
		return $this->colAlignFromRight($colIndex, "textCenterAligned");
242
	}
243
	
244
	public function colRightFromRight($colIndex) {
245
		return $this->colAlignFromRight($colIndex, "textRightAligned");
246
	}
247
	
248
	public function colLeftFromRight($colIndex) {
249
		return $this->colAlignFromRight($colIndex, "textLeftAligned");
250
	}
251
252
	/**
253
	 * Returns the number of rows (TR)
254
	 * @return int
255
	 */
256
	public function getRowCount() {
257
		return $this->count();
258
	}
259
260
	/**
261
	 * Returns the number of columns (TD)
262
	 * @return int
263
	 */
264
	public function getColCount() {
265
		$result=0;
266
		if ($this->count() > 0)
267
			$result=$this->getItem(0)->count();
268
		return $result;
269
	}
270
271
	/**
272
	 * Removes the cell at position $rowIndex,$colIndex
273
	 * @param int $rowIndex
274
	 * @param int $colIndex
275
	 * @return HtmlTableContent
276
	 */
277
	public function delete($rowIndex, $colIndex=NULL) {
278
		if (isset($colIndex)) {
279
			$row=$this->getItem($rowIndex);
280
			if (isset($row) === true) {
281
				$row->delete($colIndex);
282
			}
283
		} else {
284
			$this->removeItem($rowIndex);
285
		}
286
		return $this;
287
	}
288
289
	public function toDelete($rowIndex, $colIndex){
290
		$row=$this->getItem($rowIndex);
291
		if (isset($row) === true)
292
			$row->toDelete($colIndex);
293
		return $this;
294
	}
295
296
	public function mergeCol($rowIndex=0, $colIndex=0) {
297
		return $this->getItem($rowIndex)->mergeCol($colIndex);
298
	}
299
300
	public function mergeRow($rowIndex=0, $colIndex=0) {
301
		return $this->getItem($rowIndex)->mergeRow($colIndex);
302
	}
303
304
	public function setFullWidth() {
305
		return $this->addToProperty("class", "full-width");
306
	}
307
308
	public function sort($colIndex) {
309
		$this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort");
310
		return $this;
311
	}
312
313
	/**
314
	 * @param mixed $callback
315
	 * @param string $format
316
	 * @return HtmlTableContent
317
	 */
318
	public function conditionalCellFormat($callback, $format) {
319
		$rows=$this->content;
320
		foreach ( $rows as $row ) {
321
			$row->conditionalCellFormat($callback, $format);
322
		}
323
		return $this;
324
	}
325
326
	public function conditionalColFormat($colIndex,$callback,$format){
327
		$rows=$this->content;
328
		foreach ( $rows as $row ) {
329
			$cell=$row->getItem($colIndex);
330
			$cell->conditionnalCellFormat($callback,$format);
331
		}
332
		return $this;
333
	}
334
335
	/**
336
	 * @param mixed $callback
337
	 * @param string $format
338
	 * @return HtmlTableContent
339
	 */
340
	public function conditionalRowFormat($callback, $format) {
341
		$rows=$this->content;
342
		foreach ( $rows as $row ) {
343
			$row->conditionalRowFormat($callback, $format);
344
		}
345
		return $this;
346
	}
347
348
	public function hideColumn($colIndex){
349
		$rows=$this->content;
350
		foreach ( $rows as $row ) {
351
			$cell=$row->getItem($colIndex);
352
			$cell->addToProperty("style","display:none;");
353
		}
354
		return $this;
355
	}
356
357
	/**
358
	 * @param mixed $callback
359
	 * @return HtmlTableContent
360
	 */
361
	public function applyCells($callback) {
362
		$rows=$this->content;
363
		foreach ( $rows as $row ) {
364
			$row->applyCells($callback);
365
		}
366
		return $this;
367
	}
368
369
	/**
370
	 * @param mixed $callback
371
	 * @return HtmlTableContent
372
	 */
373
	public function applyRows($callback) {
374
		$rows=$this->content;
375
		foreach ( $rows as $row ) {
376
			$row->apply($callback);
377
		}
378
		return $this;
379
	}
380
381
	public function mergeIdentiqualValues($colIndex,$function="strip_tags"){
382
		$rows=$this->content;
383
		$identiqual=null;
384
		$counter=0;
385
		$cellToMerge=null;
386
		$functionExists=\function_exists($function);
387
		foreach ( $rows as $row ) {
388
			$cell=$row->getItem($colIndex);
389
			$value=$cell->getContent();
390
			if($functionExists)
391
				$value=\call_user_func($function,$value);
392
			if($value!==$identiqual){
393
				if($counter>0 && isset($cellToMerge)){
394
					$cellToMerge->setRowspan($counter);
395
				}
396
				$counter=0;
397
				$cellToMerge=$cell;
398
				$identiqual=$value;
399
			}
400
			$counter++;
401
		}
402
		if($counter>0 && isset($cellToMerge)){
403
			$cellToMerge->setRowspan($counter);
404
		}
405
		return $this;
406
	}
407
408
	public function _isMerged(){
409
		return $this->_merged;
410
	}
411
412
	public function _setMerged($value){
413
		$this->_merged=$value;
414
		return $this;
415
	}
416
}
417