Passed
Push — master ( ff68ab...5f63ee )
by Jean-Christophe
03:29
created

HtmlTR::setTdTagName()   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\semantic\html\base\constants\State;
7
use Ajax\semantic\html\base\traits\TableElementTrait;
8
use Ajax\service\JArray;
9
10
/**
11
 *
12
 * @author jc
13
 *
14
 */
15
class HtmlTR extends HtmlSemCollection {
16
	use TableElementTrait;
17
	private $_tdTagName;
18
	private $_container;
19
	private $_row;
20
21
	public function __construct($identifier) {
22
		parent::__construct($identifier, "tr", "");
23
		$this->_states=[ State::ACTIVE,State::POSITIVE,State::NEGATIVE,State::WARNING,State::ERROR,State::DISABLED ];
24
	}
25
26
	public function setColCount($colCount) {
27
		$count=$this->count();
28
		for($i=$count; $i < $colCount; $i++) {
29
			$item=$this->addItem(NULL);
30
			$item->setTagName($this->_tdTagName);
31
		}
32
		return $this;
33
	}
34
35
	public function getColCount(){
36
		return $this->count();
37
	}
38
39
	/**
40
	 *
41
	 * {@inheritDoc}
42
	 *
43
	 * @see \Ajax\common\html\HtmlCollection::createItem()
44
	 */
45
	protected function createItem($value) {
46
		$count=$this->count();
47
		$td=new HtmlTD("", $value, $this->_tdTagName);
48
		$td->setContainer($this->_container, $this->_row, $count);
49
		return $td;
50
	}
51
	
52
	/**
53
	 * @return HtmlTD
54
	 */
55
	public function getItem($index){
56
		return parent::getItem($index);
57
	}
58
59
	public function setTdTagName($tagName="td") {
60
		$this->_tdTagName=$tagName;
61
	}
62
63
	/**
64
	 * Define the container (HtmlTableContent) and the num of the row
65
	 * @param HtmlTableContent $container
66
	 * @param int $row
67
	 */
68
	public function setContainer($container, $row) {
69
		$this->_container=$container;
70
		$this->_row=$row;
71
	}
72
73
	/**
74
	 * Sets values to the row cols
75
	 * @param mixed $values
76
	 */
77
	public function setValues($values=array()) {
78
		return $this->_addOrSetValues($values, function(HtmlTD &$cell,$value){$cell->setValue($value);});
79
	}
80
81
	/**
82
	 * Adds values to the row cols
83
	 * @param mixed $values
84
	 */
85
	public function addValues($values=array()) {
86
		return $this->_addOrSetValues($values, function(HtmlTD &$cell,$value){$cell->addValue($value);});
87
	}
88
89
	/**
90
	 * Sets or adds values to the row cols
91
	 * @param mixed $values
92
	 */
93
	protected function _addOrSetValues($values,$callback) {
94
		$count=$this->count();
95
		if (!\is_array($values)) {
96
			$values=\array_fill(0, $count, $values);
97
		} else {
98
			if (JArray::isAssociative($values) === true) {
99
				$values=\array_values($values);
100
			}
101
		}
102
		$count=\min(\sizeof($values), $count);
103
104
		for($i=0; $i < $count; $i++) {
105
			$cell=$this->content[$i];
106
			$callback($cell,$values[$i]);
107
		}
108
		return $this;
109
	}
110
111
	/**
112
	 * Removes the col at $index
113
	 * @param int $index the index of the col to remove
114
	 * @return \Ajax\semantic\html\content\table\HtmlTR
115
	 */
116
	public function delete($index) {
117
		$this->removeItem($index);
118
		return $this;
119
	}
120
121
	public function mergeCol($colIndex=0) {
122
		return $this->getItem($colIndex)->mergeCol();
123
	}
124
125
	public function mergeRow($colIndex=0) {
126
		return $this->getItem($colIndex)->mergeRow();
127
	}
128
129
	public function getColPosition($colIndex) {
130
		if($this->_container->_isMerged()!==true)
131
			return $colIndex;
132
		$pos=0;
133
		$rows=$this->_container->getContent();
134
		for($i=0; $i < $this->_row; $i++) {
135
			$max=\min($colIndex, $rows[$i]->count());
136
			for($j=0; $j < $max; $j++) {
137
				$rowspan=$rows[$i]->getItem($j)->getRowspan();
138
				if ($rowspan + $i > $this->_row)
139
					$pos++;
140
			}
141
		}
142
		if ($pos > $colIndex)
143
			return NULL;
144
		$count=$this->count();
145
		for($i=0; $i < $count; $i++) {
146
			$pos+=$this->content[$i]->getColspan();
147
			if ($pos >= $colIndex + 1)
148
				return $i;
149
		}
150
		return null;
151
	}
152
153
	public function conditionalCellFormat($callback, $format) {
154
		$cells=$this->content;
155
		foreach ( $cells as $cell ) {
156
			$cell->conditionalCellFormat($callback, $format);
157
		}
158
		return $this;
159
	}
160
161
	public function conditionalRowFormat($callback, $format) {
162
		if ($callback($this)) {
163
			$this->addToProperty("class", $format);
164
		}
165
		return $this;
166
	}
167
168
	public function containsStr($needle) {
169
		$cells=$this->content;
170
		foreach ( $cells as $cell ) {
171
			if (\strpos($cell->getContent(), $needle) !== false)
172
				return true;
173
		}
174
		return false;
175
	}
176
177
	public function apply($callback) {
178
		$callback($this);
179
		return $this;
180
	}
181
182
	public function applyCells($callback) {
183
		$cells=$this->content;
184
		foreach ( $cells as $cell ) {
185
			$cell->apply($callback);
186
		}
187
		return $this;
188
	}
189
190
	public function toDelete($colIndex){
191
		$this->getItem($colIndex)->toDelete();
192
		return $this;
193
	}
194
}
195