1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\content\table; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
6
|
|
|
use Ajax\service\JArray; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* a table content (thead, tbody or tfoot) |
10
|
|
|
* @author jc |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class HtmlTableContent extends HtmlSemCollection { |
14
|
|
|
protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @param string $identifier |
19
|
|
|
* @param string $tagName |
20
|
|
|
* @param int $rowCount |
21
|
|
|
* @param int $colCount |
22
|
|
|
*/ |
23
|
|
|
public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
24
|
|
|
parent::__construct($identifier, $tagName, ""); |
25
|
|
|
if (isset($rowCount) && isset($colCount)) |
26
|
|
|
$this->setRowCount($rowCount, $colCount); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @param int $rowCount |
32
|
|
|
* @param int $colCount |
33
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
34
|
|
|
*/ |
35
|
|
|
public function setRowCount($rowCount, $colCount) { |
36
|
|
|
$count=$this->count(); |
37
|
|
|
for($i=$count; $i < $rowCount; $i++) { |
38
|
|
|
$this->addItem($colCount); |
39
|
|
|
} |
40
|
|
|
/* |
|
|
|
|
41
|
|
|
* for($i=0; $i < $rowCount; $i++) { |
42
|
|
|
* $item=$this->content[$i]; |
43
|
|
|
* $item->setTdTagName($this->_tdTagNames[$this->tagName]); |
44
|
|
|
* $this->content[$i]->setColCount($colCount); |
45
|
|
|
* } |
46
|
|
|
*/ |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getTdTagName($tagName) { |
|
|
|
|
51
|
|
|
return $this->_tdTagNames[$this->tagName]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
* |
58
|
|
|
* @see \Ajax\common\html\HtmlCollection::createItem() |
59
|
|
|
*/ |
60
|
|
|
protected function createItem($value) { |
61
|
|
|
$count=$this->count(); |
62
|
|
|
$tr=new HtmlTR("", $value); |
|
|
|
|
63
|
|
|
$tr->setContainer($this, $count); |
64
|
|
|
$tr->setTdTagName($this->_tdTagNames[$this->tagName]); |
65
|
|
|
if (isset($value) === true) { |
66
|
|
|
$tr->setColCount($value); |
67
|
|
|
} |
68
|
|
|
return $tr; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function newRow($value) { |
72
|
|
|
return $this->createItem($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addRow($colCount) { |
76
|
|
|
return $this->addItem($colCount); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function _addRow($row) { |
80
|
|
|
$this->addItem($row); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the cell (HtmlTD) at position $row,$col |
85
|
|
|
* @param int $row |
86
|
|
|
* @param int $col |
87
|
|
|
* @return \Ajax\semantic\html\content\HtmlTD |
88
|
|
|
*/ |
89
|
|
|
public function getCell($row, $col) { |
90
|
|
|
$row=$this->getItem($row); |
91
|
|
|
if (isset($row)) { |
92
|
|
|
$col=$row->getItem($col); |
93
|
|
|
} |
94
|
|
|
return $col; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param int $index |
100
|
|
|
* @return \Ajax\semantic\html\content\HtmlTR |
101
|
|
|
*/ |
102
|
|
|
public function getRow($index) { |
103
|
|
|
return $this->getItem($index); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* @param int $row |
109
|
|
|
* @param int $col |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
112
|
|
|
*/ |
113
|
|
|
public function setCellValue($row, $col, $value="") { |
114
|
|
|
$cell=$this->getCell($row, $col); |
115
|
|
|
if (isset($cell) === true) { |
116
|
|
|
$cell->setValue($value); |
117
|
|
|
} |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the cells values |
123
|
|
|
* @param mixed $values |
124
|
|
|
*/ |
125
|
|
|
public function setValues($values=array()) { |
126
|
|
|
$count=$this->count(); |
127
|
|
|
$isArray=true; |
128
|
|
|
if (\is_array($values) === false) { |
129
|
|
|
$values=\array_fill(0, $count, $values); |
130
|
|
|
$isArray=false; |
131
|
|
|
} |
132
|
|
|
if (JArray::dimension($values) == 1 && $isArray) |
133
|
|
|
$values=[ $values ]; |
134
|
|
|
|
135
|
|
|
$count=\min(\sizeof($values), $count); |
136
|
|
|
|
137
|
|
|
for($i=0; $i < $count; $i++) { |
138
|
|
|
$row=$this->content[$i]; |
139
|
|
|
$row->setValues($values[$i]); |
140
|
|
|
} |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setColValues($colIndex, $values=array()) { |
145
|
|
|
$count=$this->count(); |
146
|
|
|
if (\is_array($values) === false) { |
147
|
|
|
$values=\array_fill(0, $count, $values); |
148
|
|
|
} |
149
|
|
|
$count=\min(\sizeof($values), $count); |
150
|
|
|
for($i=0; $i < $count; $i++) { |
151
|
|
|
$this->getCell($i, $colIndex)->setValue($values[$i]); |
152
|
|
|
} |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function addColVariations($colIndex, $variations=array()) { |
157
|
|
|
$count=$this->count(); |
158
|
|
|
for($i=0; $i < $count; $i++) { |
159
|
|
|
$this->getCell($i, $colIndex)->addVariations($variations); |
160
|
|
|
} |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function setRowValues($rowIndex, $values=array()) { |
165
|
|
|
$count=$this->count(); |
166
|
|
|
if (\is_array($values) === false) { |
167
|
|
|
$values=\array_fill(0, $count, $values); |
168
|
|
|
} |
169
|
|
|
$this->getItem($rowIndex)->setValues($values); |
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function colAlign($colIndex, $function) { |
174
|
|
|
$count=$this->count(); |
175
|
|
|
for($i=0; $i < $count; $i++) { |
176
|
|
|
$index=$this->content[$i]->getColPosition($colIndex); |
177
|
|
|
if ($index !== NULL) |
178
|
|
|
$this->getCell($i, $index)->$function(); |
179
|
|
|
} |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function colCenter($colIndex) { |
184
|
|
|
return $this->colAlign($colIndex, "textCenterAligned"); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function colRight($colIndex) { |
188
|
|
|
return $this->colAlign($colIndex, "textRightAligned"); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function colLeft($colIndex) { |
192
|
|
|
return $this->colAlign($colIndex, "textLeftAligned"); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the number of rows (TR) |
197
|
|
|
* @return int |
198
|
|
|
*/ |
199
|
|
|
public function getRowCount() { |
200
|
|
|
return $this->count(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the number of columns (TD) |
205
|
|
|
* @return int |
206
|
|
|
*/ |
207
|
|
|
public function getColCount() { |
208
|
|
|
$result=0; |
209
|
|
|
if ($this->count() > 0) |
210
|
|
|
$result=$this->getItem(0)->getColCount(); |
|
|
|
|
211
|
|
|
return $result; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Removes the cell at position $rowIndex,$colIndex |
216
|
|
|
* @param int $rowIndex |
217
|
|
|
* @param int $colIndex |
218
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
219
|
|
|
*/ |
220
|
|
|
public function delete($rowIndex, $colIndex=NULL) { |
221
|
|
|
if (isset($colIndex)) { |
222
|
|
|
$row=$this->getItem($rowIndex); |
223
|
|
|
if (isset($row) === true) { |
224
|
|
|
$row->delete($colIndex); |
225
|
|
|
} |
226
|
|
|
} else { |
227
|
|
|
$this->removeItem($rowIndex); |
228
|
|
|
} |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function mergeCol($rowIndex=0, $colIndex=0) { |
233
|
|
|
return $this->getItem($rowIndex)->mergeCol($colIndex); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function mergeRow($rowIndex=0, $colIndex=0) { |
237
|
|
|
return $this->getItem($rowIndex)->mergeRow($colIndex); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function setFullWidth() { |
241
|
|
|
return $this->addToProperty("class", "full-width"); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function sort($colIndex) { |
245
|
|
|
$this->content[0]->getItem($colIndex)->addToProperty("class", "sorted ascending"); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param mixed $callback |
250
|
|
|
* @param string $format |
251
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
252
|
|
|
*/ |
253
|
|
|
public function conditionalCellFormat($callback, $format) { |
254
|
|
|
$rows=$this->content; |
255
|
|
|
foreach ( $rows as $row ) { |
256
|
|
|
$row->conditionalCellFormat($callback, $format); |
257
|
|
|
} |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param mixed $callback |
263
|
|
|
* @param string $format |
264
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
265
|
|
|
*/ |
266
|
|
|
public function conditionalRowFormat($callback, $format) { |
267
|
|
|
$rows=$this->content; |
268
|
|
|
foreach ( $rows as $row ) { |
269
|
|
|
$row->conditionalRowFormat($callback, $format); |
270
|
|
|
} |
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param mixed $callback |
276
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
277
|
|
|
*/ |
278
|
|
|
public function applyCells($callback) { |
279
|
|
|
$rows=$this->content; |
280
|
|
|
foreach ( $rows as $row ) { |
281
|
|
|
$row->applyCells($callback); |
282
|
|
|
} |
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param mixed $callback |
288
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
289
|
|
|
*/ |
290
|
|
|
public function applyRows($callback) { |
291
|
|
|
$rows=$this->content; |
292
|
|
|
foreach ( $rows as $row ) { |
293
|
|
|
$row->apply($callback); |
294
|
|
|
} |
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.