Completed
Push — master ( a4feba...3ad5ef )
by Jean-Christophe
03:50
created

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