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