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
|
|
|
public function setTdTagName($tagName="td") { |
53
|
|
|
$this->_tdTagName=$tagName; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Define the container (HtmlTableContent) and the num of the row |
58
|
|
|
* @param HtmlTableContent $container |
59
|
|
|
* @param int $row |
60
|
|
|
*/ |
61
|
|
|
public function setContainer($container, $row) { |
62
|
|
|
$this->_container=$container; |
63
|
|
|
$this->_row=$row; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets values to the row cols |
68
|
|
|
* @param mixed $values |
69
|
|
|
*/ |
70
|
|
|
public function setValues($values=array()) { |
71
|
|
|
return $this->_addOrSetValues($values, function(&$cell,$value){$cell->setValue($value);}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Adds values to the row cols |
76
|
|
|
* @param mixed $values |
77
|
|
|
*/ |
78
|
|
|
public function addValues($values=array()) { |
79
|
|
|
return $this->_addOrSetValues($values, function(&$cell,$value){$cell->addValue($value);}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets or adds values to the row cols |
84
|
|
|
* @param mixed $values |
85
|
|
|
*/ |
86
|
|
|
protected function _addOrSetValues($values,$callback) { |
87
|
|
|
$count=$this->count(); |
88
|
|
|
if (!\is_array($values)) { |
89
|
|
|
$values=\array_fill(0, $count, $values); |
90
|
|
|
} else { |
91
|
|
|
if (JArray::isAssociative($values) === true) { |
92
|
|
|
$values=\array_values($values); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
$count=\min(\sizeof($values), $count); |
96
|
|
|
|
97
|
|
View Code Duplication |
for($i=0; $i < $count; $i++) { |
|
|
|
|
98
|
|
|
$cell=$this->content[$i]; |
99
|
|
|
$callback($cell,$values[$i]); |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Removes the col at $index |
106
|
|
|
* @param int $index the index of the col to remove |
107
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTR |
108
|
|
|
*/ |
109
|
|
|
public function delete($index) { |
110
|
|
|
$this->removeItem($index); |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function mergeCol($colIndex=0) { |
115
|
|
|
return $this->getItem($colIndex)->mergeCol(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function mergeRow($colIndex=0) { |
119
|
|
|
return $this->getItem($colIndex)->mergeRow(); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getColPosition($colIndex) { |
123
|
|
|
$pos=0; |
124
|
|
|
$rows=$this->_container->getContent(); |
125
|
|
|
for($i=0; $i < $this->_row; $i++) { |
126
|
|
|
$max=\min($colIndex, $rows[$i]->count()); |
127
|
|
|
for($j=0; $j < $max; $j++) { |
128
|
|
|
$rowspan=$rows[$i]->getItem($j)->getRowspan(); |
129
|
|
|
if ($rowspan + $i > $this->_row) |
130
|
|
|
$pos++; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if ($pos > $colIndex) |
134
|
|
|
return NULL; |
135
|
|
|
$count=$this->count(); |
136
|
|
|
for($i=0; $i < $count; $i++) { |
137
|
|
|
$pos+=$this->content[$i]->getColspan(); |
138
|
|
|
if ($pos >= $colIndex + 1) |
139
|
|
|
return $i; |
140
|
|
|
} |
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function conditionalCellFormat($callback, $format) { |
145
|
|
|
$cells=$this->content; |
146
|
|
|
foreach ( $cells as $cell ) { |
147
|
|
|
$cell->conditionalCellFormat($callback, $format); |
148
|
|
|
} |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function conditionalRowFormat($callback, $format) { |
153
|
|
|
if ($callback($this)) { |
154
|
|
|
$this->addToProperty("class", $format); |
155
|
|
|
} |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function containsStr($needle) { |
160
|
|
|
$cells=$this->content; |
161
|
|
|
foreach ( $cells as $cell ) { |
162
|
|
|
if (\strpos($cell->getContent(), $needle) !== false) |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function apply($callback) { |
169
|
|
|
$callback($this); |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function applyCells($callback) { |
174
|
|
|
$cells=$this->content; |
175
|
|
|
foreach ( $cells as $cell ) { |
176
|
|
|
$cell->apply($callback); |
177
|
|
|
} |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.