Passed
Push — master ( 6da082...c653a4 )
by Jean-Christophe
03:08
created

HtmlTR::toRowspanned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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