1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\content\table; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* a table content (thead, tbody or tfoot) |
9
|
|
|
* @author jc |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class HtmlTableContent extends HtmlSemCollection{ |
13
|
|
|
|
14
|
|
|
protected $_tdTagNames=["thead"=>"th","tbody"=>"td","tfoot"=>"th"]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $identifier |
18
|
|
|
* @param string $tagName |
19
|
|
|
* @param int $rowCount |
20
|
|
|
* @param int $colCount |
21
|
|
|
*/ |
22
|
|
|
public function __construct( $identifier,$tagName="tbody",$rowCount=NULL,$colCount=NULL){ |
23
|
|
|
parent::__construct( $identifier, $tagName, ""); |
24
|
|
|
if(isset($rowCount) && isset($colCount)) |
25
|
|
|
$this->setRowCount($rowCount, $colCount); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param int $rowCount |
30
|
|
|
* @param int $colCount |
31
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
32
|
|
|
*/ |
33
|
|
|
public function setRowCount($rowCount,$colCount){ |
34
|
|
|
$count=$this->count(); |
35
|
|
|
for($i=$count;$i<$rowCount;$i++){ |
36
|
|
|
$this->addItem($colCount); |
37
|
|
|
} |
38
|
|
|
for($i=0;$i<$rowCount;$i++){ |
39
|
|
|
$item=$this->content[$i]; |
40
|
|
|
$item->setTdTagName($this->_tdTagNames[$this->tagName]); |
41
|
|
|
$this->content[$i]->setColCount($colCount); |
42
|
|
|
} |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
* @see \Ajax\common\html\HtmlCollection::createItem() |
49
|
|
|
*/ |
50
|
|
|
protected function createItem($value){ |
51
|
|
|
$count=$this->count(); |
52
|
|
|
$tr= new HtmlTR("", $value); |
53
|
|
|
$tr->setContainer($this, $count); |
54
|
|
|
return $tr; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the cell (HtmlTD) at position $row,$col |
59
|
|
|
* @param int $row |
60
|
|
|
* @param int $col |
61
|
|
|
* @return \Ajax\semantic\html\content\HtmlTD |
62
|
|
|
*/ |
63
|
|
|
public function getCell($row,$col){ |
64
|
|
|
$row=$this->getItem($row); |
65
|
|
|
if(isset($row)){ |
66
|
|
|
$col=$row->getItem($col); |
67
|
|
|
} |
68
|
|
|
return $col; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $index |
73
|
|
|
* @return \Ajax\semantic\html\content\HtmlTR |
74
|
|
|
*/ |
75
|
|
|
public function getRow($index){ |
76
|
|
|
return $this->getItem($index); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $row |
81
|
|
|
* @param int $col |
82
|
|
|
* @param mixed $value |
83
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
84
|
|
|
*/ |
85
|
|
|
public function setCellValue($row,$col,$value=""){ |
86
|
|
|
$cell=$this->getCell($row, $col); |
87
|
|
|
if(isset($cell)===true){ |
88
|
|
|
$cell->setValue($value); |
89
|
|
|
} |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the cells values |
95
|
|
|
* @param mixed $values |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function setValues($values=array()){ |
|
|
|
|
98
|
|
|
$count=$this->count(); |
99
|
|
|
if(\is_array($values)===false){ |
100
|
|
|
$values=\array_fill(0, $count, $values); |
101
|
|
|
} |
102
|
|
|
$count=\min(\sizeof($values),$count); |
103
|
|
|
|
104
|
|
|
for ($i=0;$i<$count;$i++){ |
105
|
|
|
$row=$this->content[$i]; |
106
|
|
|
$row->setValues($values[$i]); |
107
|
|
|
} |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function setColValues($colIndex,$values=array()){ |
|
|
|
|
112
|
|
|
$count=$this->count(); |
113
|
|
|
if(\is_array($values)===false){ |
114
|
|
|
$values=\array_fill(0, $count, $values); |
115
|
|
|
} |
116
|
|
|
$count=\min(\sizeof($values),$count); |
117
|
|
|
for ($i=0;$i<$count;$i++){ |
118
|
|
|
$this->getCell($i, $colIndex)->setValue($values[$i]); |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function colCenter($colIndex){ |
124
|
|
|
$count=$this->count(); |
125
|
|
|
for ($i=0;$i<$count;$i++){ |
126
|
|
|
$this->getCell($i, $colIndex)->textCenterAligned(); |
127
|
|
|
} |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the number of rows (TR) |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function getRowCount(){ |
136
|
|
|
return $this->count(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the number of columns (TD) |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
public function getColCount(){ |
144
|
|
|
$result=0; |
145
|
|
|
if($this->count()>0) |
146
|
|
|
$result=$this->getItem(0)->getColCount(); |
|
|
|
|
147
|
|
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Removes the cell at position $rowIndex,$colIndex |
152
|
|
|
* @param int $rowIndex |
153
|
|
|
* @param int $colIndex |
154
|
|
|
* @return \Ajax\semantic\html\content\table\HtmlTableContent |
155
|
|
|
*/ |
156
|
|
|
public function delete($rowIndex,$colIndex=NULL){ |
157
|
|
|
if(isset($colIndex)){ |
158
|
|
|
$row=$this->getItem($rowIndex); |
159
|
|
|
if(isset($row)===true){ |
160
|
|
|
$row->delete($colIndex); |
161
|
|
|
} |
162
|
|
|
}else{ |
163
|
|
|
$this->removeItem($rowIndex); |
164
|
|
|
} |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function mergeCol($rowIndex=0,$colIndex=0){ |
169
|
|
|
return $this->getItem($rowIndex)->mergeCol($colIndex); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function mergeRow($rowIndex=0,$colIndex=0){ |
173
|
|
|
return $this->getItem($rowIndex)->mergeRow($colIndex); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
} |
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.