Total Complexity | 81 |
Total Lines | 517 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like HtmlTable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class HtmlTable extends HtmlSemDoubleElement { |
||
23 | use TableTrait; |
||
24 | private $_colCount; |
||
25 | private $_compileParts; |
||
26 | private $_footer; |
||
27 | private $_afterCompileEvents; |
||
28 | private $_activeRowSelector; |
||
29 | private $_focusable=false; |
||
|
|||
30 | |||
31 | /** |
||
32 | * @return ActiveRow |
||
33 | */ |
||
34 | public function getActiveRowSelector() { |
||
35 | return $this->_activeRowSelector; |
||
36 | } |
||
37 | |||
38 | protected $_innerScript; |
||
39 | |||
40 | |||
41 | public function __construct($identifier, $rowCount, $colCount) { |
||
42 | parent::__construct($identifier, "table", "ui table"); |
||
43 | $this->content=array (); |
||
44 | $this->setRowCount($rowCount, $colCount); |
||
45 | $this->_variations=[ Variation::CELLED,Variation::PADDED,Variation::COMPACT ]; |
||
46 | $this->_compileParts=["thead","tbody","tfoot"]; |
||
47 | $this->_afterCompileEvents=[]; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
52 | * @param string $key |
||
53 | * @return HtmlTableContent |
||
54 | */ |
||
55 | public function getPart($key) { |
||
56 | if (\array_key_exists($key, $this->content) === false) { |
||
57 | $this->content[$key]=new HtmlTableContent("", $key); |
||
58 | if ($key !== "tbody") { |
||
59 | $this->content[$key]->setRowCount(1, $this->_colCount); |
||
60 | } |
||
61 | } |
||
62 | return $this->content[$key]; |
||
63 | } |
||
64 | |||
65 | protected function _getFirstPart(){ |
||
66 | if(isset($this->content["thead"])){ |
||
67 | return $this->content["thead"]; |
||
68 | } |
||
69 | return $this->content["tbody"]; |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Returns/create eventually the body of the table |
||
75 | * @return HtmlTableContent |
||
76 | */ |
||
77 | public function getBody() { |
||
78 | return $this->getPart("tbody"); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns the number of rows (TR) |
||
83 | * @return int |
||
84 | */ |
||
85 | public function getRowCount() { |
||
86 | return $this->getPart("tbody")->count(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Returns/create eventually the header of the table |
||
91 | * @return HtmlTableContent |
||
92 | */ |
||
93 | public function getHeader() { |
||
94 | return $this->getPart("thead"); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Returns/create eventually the footer of the table |
||
99 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
100 | */ |
||
101 | public function getFooter() { |
||
102 | return $this->getPart("tfoot"); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Checks if the part corresponding to $key exists |
||
107 | * @param string $key |
||
108 | * @return boolean |
||
109 | */ |
||
110 | public function hasPart($key) { |
||
111 | return \array_key_exists($key, $this->content) === true; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * |
||
116 | * @param int $rowCount |
||
117 | * @param int $colCount |
||
118 | * @return HtmlTableContent |
||
119 | */ |
||
120 | public function setRowCount($rowCount, $colCount) { |
||
121 | $this->_colCount=$colCount; |
||
122 | return $this->getBody()->setRowCount($rowCount, $colCount); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns the cell (HtmlTD) at position $row,$col |
||
127 | * @param int $row |
||
128 | * @param int $col |
||
129 | * @return HtmlTD |
||
130 | */ |
||
131 | public function getCell($row, $col) { |
||
132 | return $this->getBody()->getCell($row, $col); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Retuns the row at $rowIndex |
||
137 | * @param int $rowIndex |
||
138 | * @return HtmlTR |
||
139 | */ |
||
140 | public function getRow($rowIndex) { |
||
141 | return $this->getBody()->getRow($rowIndex); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Adds a new row and sets $values to his cols |
||
146 | * @param array $values |
||
147 | * @return HtmlTR |
||
148 | */ |
||
149 | public function addRow($values=array()) { |
||
150 | $row=$this->getBody()->addRow($this->_colCount); |
||
151 | $row->setValues(\array_values($values)); |
||
152 | return $row; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * adds and returns a new row |
||
157 | * @return HtmlTR |
||
158 | */ |
||
159 | public function newRow() { |
||
160 | return $this->getBody()->newRow($this->_colCount); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Sets the tbody values |
||
165 | * @param array $values values in an array of array |
||
166 | * @return HtmlTable |
||
167 | */ |
||
168 | public function setValues($values=array()) { |
||
169 | $this->getBody()->setValues($values); |
||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Sets the header values |
||
175 | * @param array $values |
||
176 | * @return HtmlTableContent |
||
177 | */ |
||
178 | public function setHeaderValues($values=array()) { |
||
179 | return $this->getHeader()->setValues($values); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Sets the footer values |
||
184 | * @param array $values |
||
185 | * @return HtmlTableContent |
||
186 | */ |
||
187 | public function setFooterValues($values=array()) { |
||
188 | return $this->getFooter()->setValues($values); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Sets values to the col at index $colIndex |
||
193 | * @param int $colIndex |
||
194 | * @param array $values |
||
195 | * @return HtmlTable |
||
196 | */ |
||
197 | public function setColValues($colIndex, $values=array()) { |
||
198 | $this->getBody()->setColValues($colIndex, $values); |
||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Sets values to the row at index $rowIndex |
||
204 | * @param int $rowIndex |
||
205 | * @param array $values |
||
206 | * @return HtmlTable |
||
207 | */ |
||
208 | public function setRowValues($rowIndex, $values=array()) { |
||
209 | $this->getBody()->setRowValues($rowIndex, $values); |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function addColVariations($colIndex, $variations=array()) { |
||
214 | return $this->getBody()->addColVariations($colIndex, $variations); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Sets the col alignment to center |
||
219 | * @param int $colIndex |
||
220 | * @return HtmlTable |
||
221 | */ |
||
222 | public function colCenter($colIndex) { |
||
223 | return $this->colAlign($colIndex, "colCenter"); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Sets the col alignment to right |
||
228 | * @param int $colIndex |
||
229 | * @return HtmlTable |
||
230 | */ |
||
231 | public function colRight($colIndex) { |
||
232 | return $this->colAlign($colIndex, "colRight"); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Sets col alignment to left |
||
237 | * @param int $colIndex |
||
238 | * @return HtmlTable |
||
239 | */ |
||
240 | public function colLeft($colIndex) { |
||
241 | return $this->colAlign($colIndex, "colLeft"); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Sets the col alignment to center |
||
246 | * @param int $colIndex |
||
247 | * @return HtmlTable |
||
248 | */ |
||
249 | public function colCenterFromRight($colIndex) { |
||
250 | return $this->colAlign($colIndex, "colCenterFromRight"); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Sets the col alignment to right |
||
255 | * @param int $colIndex |
||
256 | * @return HtmlTable |
||
257 | */ |
||
258 | public function colRightFromRight($colIndex) { |
||
259 | return $this->colAlign($colIndex, "colRightFromRight"); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Sets col alignment to left |
||
264 | * @param int $colIndex |
||
265 | * @return HtmlTable |
||
266 | */ |
||
267 | public function colLeftFromRight($colIndex) { |
||
268 | return $this->colAlign($colIndex, "colLeftFromRight"); |
||
269 | } |
||
270 | |||
271 | public function setColAlignment($colIndex,$alignment){ |
||
272 | switch ($alignment){ |
||
273 | case TextAlignment::LEFT: |
||
274 | $function="colLeft"; |
||
275 | break; |
||
276 | |||
277 | case TextAlignment::RIGHT: |
||
278 | $function="colRight"; |
||
279 | break; |
||
280 | |||
281 | case TextAlignment::CENTER: |
||
282 | $function="colCenter"; |
||
283 | break; |
||
284 | |||
285 | default: |
||
286 | $function="colLeft"; |
||
287 | } |
||
288 | $this->colAlign($colIndex, $function); |
||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | public function setColAlignmentFromRight($colIndex,$alignment){ |
||
293 | switch ($alignment){ |
||
294 | case TextAlignment::LEFT: |
||
295 | $function="colLeftFromRight"; |
||
296 | break; |
||
297 | |||
298 | case TextAlignment::RIGHT: |
||
299 | $function="colRightFromRight"; |
||
300 | break; |
||
301 | |||
302 | case TextAlignment::CENTER: |
||
303 | $function="colCenterFromRight"; |
||
304 | break; |
||
305 | |||
306 | default: |
||
307 | $function="colLeftFromRight"; |
||
308 | } |
||
309 | $this->colAlign($colIndex, $function); |
||
310 | return $this; |
||
311 | } |
||
312 | |||
313 | private function colAlign($colIndex, $function) { |
||
314 | if (\is_array($colIndex)) { |
||
315 | foreach ( $colIndex as $cIndex ) { |
||
316 | $this->colAlign($cIndex, $function); |
||
317 | } |
||
318 | } else { |
||
319 | if ($this->hasPart("thead")) { |
||
320 | $this->getHeader()->$function($colIndex); |
||
321 | } |
||
322 | $this->getBody()->$function($colIndex); |
||
323 | } |
||
324 | return $this; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Applies a format on each cell when $callback returns true |
||
329 | * @param callable $callback function with the cell as parameter, must return a boolean |
||
330 | * @param string $format css class to apply |
||
331 | * @return HtmlTable |
||
332 | */ |
||
333 | public function conditionalCellFormat($callback, $format) { |
||
334 | $this->getBody()->conditionalCellFormat($callback, $format); |
||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Applies a format on each row when $callback returns true |
||
340 | * @param callable $callback function with the row as parameter, must return a boolean |
||
341 | * @param string $format css class to apply |
||
342 | * @return HtmlTable |
||
343 | */ |
||
344 | public function conditionalRowFormat($callback, $format) { |
||
345 | $this->getBody()->conditionalRowFormat($callback, $format); |
||
346 | return $this; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Applies a callback function on each cell |
||
351 | * @param callable $callback |
||
352 | * @return HtmlTable |
||
353 | */ |
||
354 | public function applyCells($callback) { |
||
355 | $this->getBody()->applyCells($callback); |
||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Applies a callback function on each row |
||
361 | * @param callable $callback |
||
362 | * @return HtmlTable |
||
363 | */ |
||
364 | public function applyRows($callback) { |
||
365 | $this->getBody()->applyRows($callback); |
||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * |
||
371 | * {@inheritDoc} |
||
372 | * |
||
373 | * @see HtmlSemDoubleElement::compile() |
||
374 | */ |
||
375 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
376 | if(\sizeof($this->_compileParts)<3){ |
||
377 | $this->_template="%content%"; |
||
378 | $this->refresh($js); |
||
379 | } |
||
380 | $this->content=JArray::sortAssociative($this->content, $this->_compileParts); |
||
381 | return parent::compile($js, $view); |
||
382 | } |
||
383 | |||
384 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
385 | parent::compile_once($js,$view); |
||
386 | if ($this->propertyContains("class", "sortable")) { |
||
387 | $this->addEvent("execute", "$('#" . $this->identifier . "').tablesort().data('tablesort').sort($('th.default-sort'));"); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * |
||
393 | * {@inheritDoc} |
||
394 | * |
||
395 | * @see BaseHtml::fromDatabaseObject() |
||
396 | */ |
||
397 | public function fromDatabaseObject($object, $function) { |
||
398 | $result=$function($object); |
||
399 | if (\is_array($result)) { |
||
400 | $result= $this->addRow($function($object)); |
||
401 | } else { |
||
402 | $result= $this->getBody()->_addRow($result); |
||
403 | } |
||
404 | if(isset($this->_afterCompileEvents["onNewRow"])){ |
||
405 | if(\is_callable($this->_afterCompileEvents["onNewRow"])) |
||
406 | $this->_afterCompileEvents["onNewRow"]($result,$object); |
||
407 | } |
||
408 | return $result; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Sets the parts of the Table to compile |
||
413 | * @param array $parts array of thead,tbody,tfoot |
||
414 | * @return HtmlTable |
||
415 | */ |
||
416 | public function setCompileParts($parts=["tbody"]) { |
||
417 | $this->_compileParts=$parts; |
||
418 | return $this; |
||
419 | } |
||
420 | |||
421 | public function refreshTR(){ |
||
422 | $this->setCompileParts(); |
||
423 | $this->getPart("tbody")->refreshTR(); |
||
424 | } |
||
425 | |||
426 | public function refresh($js){ |
||
427 | $this->_footer=$this->getFooter(); |
||
428 | if(isset($js)){ |
||
429 | $js->exec('$("#'.$this->identifier.' tfoot").replaceWith("'.\addslashes($this->_footer).'");',true); |
||
430 | } |
||
431 | } |
||
432 | |||
433 | public function run(JsUtils $js){ |
||
434 | if(!$this->_runned){ |
||
435 | if(isset($this->_activeRowSelector)){ |
||
436 | $this->_activeRowSelector->run(); |
||
437 | } |
||
438 | } |
||
439 | $result= parent::run($js); |
||
440 | if(isset($this->_footer)) |
||
441 | $this->_footer->run($js); |
||
442 | $this->_runned=true; |
||
443 | return $result; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
448 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
449 | * @param callable $callback |
||
450 | * @return HtmlTable |
||
451 | */ |
||
452 | public function onNewRow($callback) { |
||
453 | $this->_afterCompileEvents["onNewRow"]=$callback; |
||
454 | return $this; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Defines how a row is selectable |
||
459 | * @param string $class |
||
460 | * @param string $event |
||
461 | * @param boolean $multiple |
||
462 | * @return HtmlTable |
||
463 | */ |
||
464 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
465 | $this->_activeRowSelector=new ActiveRow($this,$class,$event,$multiple); |
||
466 | return $this; |
||
467 | } |
||
468 | |||
469 | public function hasActiveRowSelector(){ |
||
470 | return isset($this->_activeRowSelector); |
||
471 | } |
||
472 | |||
473 | public function hideColumn($colIndex){ |
||
474 | if(isset($this->content["thead"])){ |
||
475 | $this->content["thead"]->hideColumn($colIndex); |
||
476 | } |
||
477 | $this->content["tbody"]->hideColumn($colIndex); |
||
478 | if(isset($this->content["tfoot"])){ |
||
479 | $this->content["tfoot"]->hideColumn($colIndex); |
||
480 | } |
||
481 | return $this; |
||
482 | } |
||
483 | |||
484 | public function setColWidth($colIndex,$width){ |
||
485 | $part=$this->_getFirstPart(); |
||
486 | if($part!==null && $part->count()>0) |
||
487 | $part->getCell(0, $colIndex)->setWidth($width); |
||
488 | return $this; |
||
489 | } |
||
490 | |||
491 | public function setColWidths($widths){ |
||
492 | $part=$this->_getFirstPart(); |
||
493 | if($part!==null && $part->count()>0){ |
||
494 | $count=$part->getColCount(); |
||
495 | if(!\is_array($widths)){ |
||
496 | $widths=\array_fill(0, $count, $widths); |
||
497 | } |
||
498 | $max=\min(\sizeof($widths),$count); |
||
499 | for($i=0;$i<$max;$i++){ |
||
500 | $part->getCell(0, $i)->setWidth($widths[$i]); |
||
501 | } |
||
502 | } |
||
503 | return $this; |
||
504 | } |
||
505 | |||
506 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
507 | $body=$this->getBody(); |
||
508 | $body->mergeIdentiqualValues($colIndex,$function); |
||
509 | return $this; |
||
510 | } |
||
511 | /** |
||
512 | * @return mixed |
||
513 | */ |
||
514 | public function getInnerScript() { |
||
515 | return $this->_innerScript; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param mixed $_innerScript |
||
520 | */ |
||
521 | public function setInnerScript($_innerScript) { |
||
522 | $this->_innerScript = $_innerScript; |
||
523 | } |
||
524 | |||
525 | public function onActiveRowChange($jsCode){ |
||
526 | $this->on("activeRowChange",$jsCode); |
||
527 | return $this; |
||
528 | } |
||
529 | |||
530 | public function addMergeRow($colCount,$value=null){ |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * @param bool $focusable |
||
536 | */ |
||
537 | public function setFocusable(bool $focusable): void { |
||
538 | $this->getBody()->setFocusable($focusable); |
||
539 | } |
||
540 | |||
541 | } |
||
542 |