Completed
Push — master ( da7bd7...a4feba )
by Jean-Christophe
03:22
created

HtmlTable::setActiveClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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