Completed
Push — master ( bda9b8...aa1d0c )
by Jean-Christophe
03:12
created

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

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
291
		}
292
		if(isset($this->_afterCompileEvents["onNewRow"])){
293
			if(\is_callable($this->_afterCompileEvents["onNewRow"]))
294
				$this->_afterCompileEvents["onNewRow"]($result,$object);
295
		}
296
		return $result;
297
	}
298
299
	/**
300
	 * @param array $parts
301
	 * @return \Ajax\semantic\html\collections\HtmlTable
302
	 */
303
	public function setCompileParts($parts=["tbody"]) {
304
		$this->_compileParts=$parts;
305
		return $this;
306
	}
307
308
	public function refresh(){
309
		$this->_footer=$this->getFooter();
310
		$this->addEvent("execute", '$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");');
311
	}
312
313
	public function run(JsUtils $js){
314
		$result= parent::run($js);
315
		if(isset($this->_footer))
316
			$this->_footer->run($js);
317
		return $result;
318
	}
319
320
	/**
321
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
322
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
323
	 * @param callable $callback
324
	 * @return \Ajax\semantic\html\collections\HtmlTable
325
	 */
326
	public function onNewRow($callback) {
327
		$this->_afterCompileEvents["onNewRow"]=$callback;
328
		return $this;
329
	}
330
}