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
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getTdTagName($tagName) { |
|
|
|
|
44
|
|
|
return $this->_tdTagNames[$this->tagName]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
* |
51
|
|
|
* @see \Ajax\common\html\HtmlCollection::createItem() |
52
|
|
|
* @return HtmlTR |
53
|
|
|
*/ |
54
|
|
|
protected function createItem($value) { |
55
|
|
|
$count=$this->count(); |
56
|
|
|
$tr=new HtmlTR("", $value); |
|
|
|
|
57
|
|
|
$tr->setContainer($this, $count); |
58
|
|
|
$tr->setTdTagName($this->_tdTagNames[$this->tagName]); |
59
|
|
|
if (isset($value) === true) { |
60
|
|
|
$tr->setColCount($value); |
61
|
|
|
} |
62
|
|
|
return $tr; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function newRow($value) { |
66
|
|
|
return $this->createItem($value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int $colCount |
71
|
|
|
* @return HtmlTR |
72
|
|
|
*/ |
73
|
|
|
public function addRow($colCount) { |
74
|
|
|
return $this->addItem($colCount); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $row |
79
|
|
|
* @return HtmlTR |
80
|
|
|
*/ |
81
|
|
|
public function _addRow($row) { |
82
|
|
|
return $this->addItem($row); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the cell (HtmlTD) at position $row,$col |
87
|
|
|
* @param int $row |
88
|
|
|
* @param int $col |
89
|
|
|
* @return \Ajax\semantic\html\content\HtmlTD |
90
|
|
|
*/ |
91
|
|
|
public function getCell($row, $col) { |
92
|
|
|
$row=$this->getItem($row); |
93
|
|
|
if (isset($row)) { |
94
|
|
|
$col=$row->getItem($col); |
95
|
|
|
} |
96
|
|
|
return $col; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* @param int $index |
102
|
|
|
* @return \Ajax\semantic\html\content\HtmlTR |
103
|
|
|
*/ |
104
|
|
|
public function getRow($index) { |
105
|
|
|
return $this->getItem($index); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* |
110
|
|
|
* @param int $row |
111
|
|
|
* @param int $col |
112
|
|
|
* @param mixed $value |
113
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
114
|
|
|
*/ |
115
|
|
|
public function setCellValue($row, $col, $value="") { |
116
|
|
|
$cell=$this->getCell($row, $col); |
117
|
|
|
if (isset($cell) === true) { |
118
|
|
|
$cell->setValue($value); |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets the cells values |
125
|
|
|
* @param mixed $values |
126
|
|
|
*/ |
127
|
|
|
public function setValues($values=array()) { |
128
|
|
|
return $this->_addOrSetValues($values, function($row,$_values){$row->setValues($_values);}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Adds the cells values |
133
|
|
|
* @param mixed $values |
134
|
|
|
*/ |
135
|
|
|
public function addValues($values=array()) { |
136
|
|
|
return $this->_addOrSetValues($values, function($row,$_values){$row->addValues($_values);}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds or sets the cells values |
141
|
|
|
* @param mixed $values |
142
|
|
|
* @param callable $callback |
143
|
|
|
*/ |
144
|
|
|
protected function _addOrSetValues($values,$callback) { |
145
|
|
|
$count=$this->count(); |
146
|
|
|
$isArray=true; |
147
|
|
|
if (!\is_array($values)) { |
148
|
|
|
$values=\array_fill(0, $count, $values); |
149
|
|
|
$isArray=false; |
150
|
|
|
} |
151
|
|
|
if (JArray::dimension($values) == 1 && $isArray) |
152
|
|
|
$values=[ $values ]; |
153
|
|
|
|
154
|
|
|
$count=\min(\sizeof($values), $count); |
155
|
|
|
|
156
|
|
View Code Duplication |
for($i=0; $i < $count; $i++) { |
|
|
|
|
157
|
|
|
$row=$this->content[$i]; |
158
|
|
|
$callback($row,$values[$i]); |
159
|
|
|
} |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setColValues($colIndex, $values=array()) { |
164
|
|
|
$count=$this->count(); |
165
|
|
|
if (!\is_array($values)) { |
166
|
|
|
$values=\array_fill(0, $count, $values); |
167
|
|
|
} |
168
|
|
|
$count=\min(\sizeof($values), $count); |
169
|
|
|
for($i=0; $i < $count; $i++) { |
170
|
|
|
$this->getCell($i, $colIndex)->setValue($values[$i]); |
171
|
|
|
} |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function addColVariations($colIndex, $variations=array()) { |
176
|
|
|
$count=$this->count(); |
177
|
|
|
for($i=0; $i < $count; $i++) { |
178
|
|
|
$this->getCell($i, $colIndex)->addVariations($variations); |
179
|
|
|
} |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function setRowValues($rowIndex, $values=array()) { |
184
|
|
|
$count=$this->count(); |
185
|
|
|
if (!\is_array($values)) { |
186
|
|
|
$values=\array_fill(0, $count, $values); |
187
|
|
|
} |
188
|
|
|
$this->getItem($rowIndex)->setValues($values); |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function colAlign($colIndex, $function) { |
193
|
|
|
$count=$this->count(); |
194
|
|
|
for($i=0; $i < $count; $i++) { |
195
|
|
|
$index=$this->content[$i]->getColPosition($colIndex); |
196
|
|
|
if ($index !== NULL) |
197
|
|
|
$this->getCell($i, $index)->$function(); |
198
|
|
|
} |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function colCenter($colIndex) { |
203
|
|
|
return $this->colAlign($colIndex, "textCenterAligned"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function colRight($colIndex) { |
207
|
|
|
return $this->colAlign($colIndex, "textRightAligned"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function colLeft($colIndex) { |
211
|
|
|
return $this->colAlign($colIndex, "textLeftAligned"); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns the number of rows (TR) |
216
|
|
|
* @return int |
217
|
|
|
*/ |
218
|
|
|
public function getRowCount() { |
219
|
|
|
return $this->count(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns the number of columns (TD) |
224
|
|
|
* @return int |
225
|
|
|
*/ |
226
|
|
|
public function getColCount() { |
227
|
|
|
$result=0; |
228
|
|
|
if ($this->count() > 0) |
229
|
|
|
$result=$this->getItem(0)->getColCount(); |
|
|
|
|
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Removes the cell at position $rowIndex,$colIndex |
235
|
|
|
* @param int $rowIndex |
236
|
|
|
* @param int $colIndex |
237
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
238
|
|
|
*/ |
239
|
|
|
public function delete($rowIndex, $colIndex=NULL) { |
240
|
|
|
if (isset($colIndex)) { |
241
|
|
|
$row=$this->getItem($rowIndex); |
242
|
|
|
if (isset($row) === true) { |
243
|
|
|
$row->delete($colIndex); |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
|
|
$this->removeItem($rowIndex); |
247
|
|
|
} |
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function mergeCol($rowIndex=0, $colIndex=0) { |
252
|
|
|
return $this->getItem($rowIndex)->mergeCol($colIndex); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function mergeRow($rowIndex=0, $colIndex=0) { |
256
|
|
|
return $this->getItem($rowIndex)->mergeRow($colIndex); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function setFullWidth() { |
260
|
|
|
return $this->addToProperty("class", "full-width"); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function sort($colIndex) { |
264
|
|
|
$this->content[0]->getItem($colIndex)->addToProperty("class", "sorted ascending"); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param mixed $callback |
269
|
|
|
* @param string $format |
270
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
271
|
|
|
*/ |
272
|
|
|
public function conditionalCellFormat($callback, $format) { |
273
|
|
|
$rows=$this->content; |
274
|
|
|
foreach ( $rows as $row ) { |
275
|
|
|
$row->conditionalCellFormat($callback, $format); |
276
|
|
|
} |
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param mixed $callback |
282
|
|
|
* @param string $format |
283
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
284
|
|
|
*/ |
285
|
|
|
public function conditionalRowFormat($callback, $format) { |
286
|
|
|
$rows=$this->content; |
287
|
|
|
foreach ( $rows as $row ) { |
288
|
|
|
$row->conditionalRowFormat($callback, $format); |
289
|
|
|
} |
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param mixed $callback |
295
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
296
|
|
|
*/ |
297
|
|
|
public function applyCells($callback) { |
298
|
|
|
$rows=$this->content; |
299
|
|
|
foreach ( $rows as $row ) { |
300
|
|
|
$row->applyCells($callback); |
301
|
|
|
} |
302
|
|
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param mixed $callback |
307
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
308
|
|
|
*/ |
309
|
|
|
public function applyRows($callback) { |
310
|
|
|
$rows=$this->content; |
311
|
|
|
foreach ( $rows as $row ) { |
312
|
|
|
$row->apply($callback); |
313
|
|
|
} |
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.