Completed
Push — master ( 3555a5...e61355 )
by Jean-Christophe
03:33
created

HtmlTable::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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