Completed
Push — master ( f99fed...57dceb )
by Jean-Christophe
03:29
created

HtmlTable::getRowCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\collections\table;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\content\table\HtmlTableContent;
7
use Ajax\semantic\html\base\constants\Variation;
8
use Ajax\JsUtils;
9
10
use Ajax\service\JArray;
11
use Ajax\semantic\html\content\table\HtmlTR;
12
use Ajax\semantic\html\collections\table\traits\TableTrait;
13
use Ajax\semantic\html\content\table\HtmlTD;
14
15
/**
16
 * Semantic HTML Table component
17
 * @author jc
18
 *
19
 */
20
class HtmlTable extends HtmlSemDoubleElement {
21
	use TableTrait;
22
	private $_colCount;
23
	private $_compileParts;
24
	private $_footer;
25
	private $_afterCompileEvents;
26
27
	public function __construct($identifier, $rowCount, $colCount) {
28
		parent::__construct($identifier, "table", "ui table");
29
		$this->content=array ();
30
		$this->setRowCount($rowCount, $colCount);
31
		$this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ];
32
		$this->_compileParts=["thead","tbody","tfoot"];
33
		$this->_afterCompileEvents=[];
34
	}
35
36
	/**
37
	 * {@inheritDoc}
38
	 * @see \Ajax\semantic\html\collections\table\TableTrait::getTable()
39
	 */
40
	protected function getTable() {
41
		return $this;
42
	}
43
44
	/**
45
	 * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot
46
	 * @param string $key
47
	 * @return HtmlTableContent
48
	 */
49
	public function getPart($key) {
50
		if (\array_key_exists($key, $this->content) === false) {
51
			$this->content[$key]=new HtmlTableContent("", $key);
52
			if ($key !== "tbody") {
53
				$this->content[$key]->setRowCount(1, $this->_colCount);
54
			}
55
		}
56
		return $this->content[$key];
57
	}
58
59
	/**
60
	 * Returns/create eventually the body of the table
61
	 * @return HtmlTableContent
62
	 */
63
	public function getBody() {
64
		return $this->getPart("tbody");
65
	}
66
67
	/**
68
	 * Returns the number of rows (TR)
69
	 * @return int
70
	 */
71
	public function getRowCount() {
72
		return $this->getPart("tbody")->count();
73
	}
74
75
	/**
76
	 * Returns/create eventually the header of the table
77
	 * @return HtmlTableContent
78
	 */
79
	public function getHeader() {
80
		return $this->getPart("thead");
81
	}
82
83
	/**
84
	 * Returns/create eventually the footer of the table
85
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
86
	 */
87
	public function getFooter() {
88
		return $this->getPart("tfoot");
89
	}
90
91
	/**
92
	 * Checks if the part corresponding to $key exists
93
	 * @param string $key
94
	 * @return boolean
95
	 */
96
	public function hasPart($key) {
97
		return \array_key_exists($key, $this->content) === true;
98
	}
99
100
	/**
101
	 *
102
	 * @param int $rowCount
103
	 * @param int $colCount
104
	 * @return HtmlTableContent
105
	 */
106
	public function setRowCount($rowCount, $colCount) {
107
		$this->_colCount=$colCount;
108
		return $this->getBody()->setRowCount($rowCount, $colCount);
109
	}
110
111
	/**
112
	 * Returns the cell (HtmlTD) at position $row,$col
113
	 * @param int $row
114
	 * @param int $col
115
	 * @return HtmlTD
116
	 */
117
	public function getCell($row, $col) {
118
		return $this->getBody()->getCell($row, $col);
119
	}
120
121
	/**
122
	 * Retuns the row at $rowIndex
123
	 * @param int $rowIndex
124
	 * @return HtmlTR
125
	 */
126
	public function getRow($rowIndex) {
127
		return $this->getBody()->getRow($rowIndex);
128
	}
129
130
	/**
131
	 * Adds a new row and sets $values to his cols
132
	 * @param array $values
133
	 * @return HtmlTR
134
	 */
135
	public function addRow($values=array()) {
136
		$row=$this->getBody()->addRow($this->_colCount);
137
		$row->setValues(\array_values($values));
138
		return $row;
139
	}
140
141
	/**
142
	 * adds and returns a new row
143
	 * @return HtmlTR
144
	 */
145
	public function newRow() {
146
		return $this->getBody()->newRow($this->_colCount);
147
	}
148
149
	public function setValues($values=array()) {
150
		$this->getBody()->setValues($values);
151
		return $this;
152
	}
153
154
	public function setHeaderValues($values=array()) {
155
		return $this->getHeader()->setValues($values);
156
	}
157
158
	public function setFooterValues($values=array()) {
159
		return $this->getFooter()->setValues($values);
160
	}
161
162
	/**
163
	 * Sets values to the col at index $colIndex
164
	 * @param int $colIndex
165
	 * @param array $values
166
	 * @return HtmlTable
167
	 */
168
	public function setColValues($colIndex, $values=array()) {
169
		$this->getBody()->setColValues($colIndex, $values);
170
		return $this;
171
	}
172
173
	/**
174
	 * Sets values to the row at index $rowIndex
175
	 * @param int $rowIndex
176
	 * @param array $values
177
	 * @return HtmlTable
178
	 */
179
	public function setRowValues($rowIndex, $values=array()) {
180
		$this->getBody()->setRowValues($rowIndex, $values);
181
		return $this;
182
	}
183
184
	public function addColVariations($colIndex, $variations=array()) {
185
		return $this->getBody()->addColVariations($colIndex, $variations);
186
	}
187
188
	public function colCenter($colIndex) {
189
		return $this->colAlign($colIndex, "colCenter");
190
	}
191
192
	public function colRight($colIndex) {
193
		return $this->colAlign($colIndex, "colRight");
194
	}
195
196
	public function colLeft($colIndex) {
197
		return $this->colAlign($colIndex, "colLeft");
198
	}
199
200
	private function colAlign($colIndex, $function) {
201
		if (\is_array($colIndex)) {
202
			foreach ( $colIndex as $cIndex ) {
203
				$this->colAlign($cIndex, $function);
204
			}
205
		} else {
206
			if ($this->hasPart("thead")) {
207
				$this->getHeader()->$function($colIndex);
208
			}
209
			$this->getBody()->$function($colIndex);
210
		}
211
		return $this;
212
	}
213
214
	public function conditionalCellFormat($callback, $format) {
215
		$this->getBody()->conditionalCellFormat($callback, $format);
216
		return $this;
217
	}
218
219
	public function conditionalRowFormat($callback, $format) {
220
		$this->getBody()->conditionalRowFormat($callback, $format);
221
		return $this;
222
	}
223
224
	public function applyCells($callback) {
225
		$this->getBody()->applyCells($callback);
226
		return $this;
227
	}
228
229
	public function applyRows($callback) {
230
		$this->getBody()->applyRows($callback);
231
		return $this;
232
	}
233
234
	/**
235
	 *
236
	 * {@inheritDoc}
237
	 *
238
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
239
	 */
240
	public function compile(JsUtils $js=NULL, &$view=NULL) {
241
		if(\sizeof($this->_compileParts)<3){
242
			$this->_template="%content%";
243
			$this->refresh();
244
		}else{
245
			if ($this->propertyContains("class", "sortable")) {
246
				$this->addEvent("execute", "$('#" . $this->identifier . "').tablesort();");
247
			}
248
		}
249
		$this->content=JArray::sortAssociative($this->content, $this->_compileParts);
250
		return parent::compile($js, $view);
251
	}
252
253
	/**
254
	 *
255
	 * {@inheritDoc}
256
	 *
257
	 * @see \Ajax\common\html\BaseHtml::fromDatabaseObject()
258
	 */
259
	public function fromDatabaseObject($object, $function) {
260
		$result=$function($object);
261
		if (\is_array($result)) {
262
			$result= $this->addRow($function($object));
263
		} else {
264
			$result= $this->getBody()->_addRow($result);
265
		}
266
		if(isset($this->_afterCompileEvents["onNewRow"])){
267
			if(\is_callable($this->_afterCompileEvents["onNewRow"]))
268
				$this->_afterCompileEvents["onNewRow"]($result,$object);
269
		}
270
		return $result;
271
	}
272
273
	/**
274
	 * @param array $parts
275
	 * @return HtmlTable
276
	 */
277
	public function setCompileParts($parts=["tbody"]) {
278
		$this->_compileParts=$parts;
279
		return $this;
280
	}
281
282
	public function refresh(){
283
		$this->_footer=$this->getFooter();
284
		$this->addEvent("execute", '$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");');
285
	}
286
287
	public function run(JsUtils $js){
288
		$result= parent::run($js);
289
		if(isset($this->_footer))
290
			$this->_footer->run($js);
291
		return $result;
292
	}
293
294
	/**
295
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
296
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
297
	 * @param callable $callback
298
	 * @return HtmlTable
299
	 */
300
	public function onNewRow($callback) {
301
		$this->_afterCompileEvents["onNewRow"]=$callback;
302
		return $this;
303
	}
304
}