| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 269 |
| Code Lines | 160 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 499 | public function render() |
||
| 500 | { |
||
| 501 | if ($this->config[ 'ordering' ]) { |
||
| 502 | $this->attributes->addAttributeClass('table-ordering'); |
||
| 503 | } |
||
| 504 | |||
| 505 | // Render header |
||
| 506 | $tr = $this->header->createRow(); |
||
| 507 | |||
| 508 | foreach ($this->columns as $key => $column) { |
||
| 509 | |||
| 510 | $th = $tr->createColumn('th'); |
||
| 511 | |||
| 512 | $column[ 'attr' ][ 'data-column' ] = $key; |
||
| 513 | |||
| 514 | foreach ($column[ 'attr' ] as $name => $value) { |
||
| 515 | if ( ! empty($value)) { |
||
| 516 | $th->attributes->addAttribute($name, $value); |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | if ($column[ 'sorting' ] === true) { |
||
| 521 | $icon = new Ui\Contents\Icon('fa fa-sort'); |
||
| 522 | $icon->attributes->addAttributeClass(['text-muted', 'float-right', 'mt-1']); |
||
| 523 | |||
| 524 | $th->attributes->addAttributeClass('col-sortable'); |
||
| 525 | $th->attributes->addAttribute('data-toggle', 'sort'); |
||
| 526 | $th->childNodes->push($icon); |
||
| 527 | } |
||
| 528 | |||
| 529 | if ($column[ 'show' ] === false) { |
||
| 530 | $th->attributes->addAttributeClass('hidden'); |
||
| 531 | } |
||
| 532 | |||
| 533 | $th->textContent->push(language()->getLine($column[ 'label' ])); |
||
| 534 | } |
||
| 535 | |||
| 536 | // Render tbody |
||
| 537 | if ($this->rows->count() > 0) { |
||
| 538 | $totalEntries = $this->rows->count(); |
||
| 539 | $totalRows = $this->rows->countAll(); |
||
| 540 | $currentPage = input()->get('page') ? input()->get('page') : 1; |
||
| 541 | $startNumber = $currentPage == 1 ? 1 : $currentPage * $this->config[ 'entries' ][ 'minimum' ]; |
||
| 542 | $totalPages = round($totalRows / $this->config[ 'entries' ][ 'minimum' ]); |
||
| 543 | $totalPages = $totalPages == 0 && $totalRows > 0 ? 1 : $totalPages; |
||
| 544 | |||
| 545 | $i = $startNumber; |
||
| 546 | foreach ($this->rows as $row) { |
||
| 547 | $row->numbering = $i; |
||
| 548 | $tr = $this->body->createRow(); |
||
| 549 | $tr->attributes->addAttribute('data-id', $row->id); |
||
| 550 | |||
| 551 | foreach ($this->columns as $key => $column) { |
||
| 552 | |||
| 553 | $column[ 'key' ] = $key; |
||
| 554 | $td = $tr->createColumn(); |
||
| 555 | |||
| 556 | $column[ 'attr' ][ 'data-column' ] = $key; |
||
| 557 | |||
| 558 | foreach ($column[ 'attr' ] as $name => $value) { |
||
| 559 | if ( ! empty($value)) { |
||
| 560 | $td->attributes->addAttribute($name, $value); |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | if ($column[ 'show' ] === false) { |
||
| 565 | $td->attributes->addAttributeClass('hidden'); |
||
| 566 | } |
||
| 567 | |||
| 568 | $td->attributes->addAttributeClass('col-body-' . $key); |
||
| 569 | |||
| 570 | $this->renderBodyColumn($td, $column, $row); |
||
| 571 | } |
||
| 572 | |||
| 573 | $i++; |
||
| 574 | } |
||
| 575 | } else { |
||
| 576 | $totalEntries = 0; |
||
| 577 | $totalRows = 0; |
||
| 578 | $currentPage = 1; |
||
| 579 | $startNumber = 0; |
||
| 580 | $totalPages = 0; |
||
| 581 | } |
||
| 582 | |||
| 583 | $showEntries = input()->get('entries') ? input()->get('entries') : $this->config[ 'entries' ][ 'minimum' ]; |
||
| 584 | |||
| 585 | // Build table card |
||
| 586 | $card = new Ui\Components\Card(); |
||
| 587 | $card->attributes->addAttribute('data-role', 'table-crud'); |
||
| 588 | $card->attributes->addAttributeClass('card-table'); |
||
| 589 | |||
| 590 | // Build table header tools |
||
| 591 | $tools = new Element('div', 'cardHeaderTools'); |
||
| 592 | $tools->attributes->addAttributeClass(['card-tools', 'float-left']); |
||
| 593 | |||
| 594 | $buttons = new Ui\Components\Buttons\Group(); |
||
| 595 | foreach ($this->tools as $key => $tool) { |
||
| 596 | if ($tool[ 'show' ] === false) { |
||
| 597 | continue; |
||
| 598 | } |
||
| 599 | |||
| 600 | if ($tool[ 'label' ] === true) { |
||
| 601 | $button = $buttons->createButton(language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
||
| 602 | if ( ! empty($tool[ 'icon' ])) { |
||
| 603 | $button->setIcon($tool[ 'icon' ]); |
||
| 604 | } |
||
| 605 | } else { |
||
| 606 | $button = $buttons->createButton(new Ui\Contents\Icon($tool[ 'icon' ])); |
||
| 607 | $button->attributes->addAttribute('data-toggle', 'tooltip'); |
||
| 608 | $button->attributes->addAttribute('title', |
||
| 609 | language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
||
| 610 | } |
||
| 611 | |||
| 612 | if ( ! empty($tool[ 'href' ])) { |
||
| 613 | $tool[ 'data-url' ] = $tool[ 'href' ]; |
||
| 614 | } |
||
| 615 | |||
| 616 | if (isset($tool[ 'attr' ])) { |
||
| 617 | foreach ($tool[ 'attr' ] as $name => $value) { |
||
| 618 | $button->attributes->addAttribute($name, $value); |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | $button->setContextualClass($tool[ 'contextual' ]); |
||
| 623 | $button->smallSize(); |
||
| 624 | $button->attributes->addAttribute('data-action', $key); |
||
| 625 | } |
||
| 626 | |||
| 627 | $tools->childNodes->push($buttons); |
||
| 628 | |||
| 629 | $card->header->childNodes->push($tools); |
||
| 630 | |||
| 631 | // Build table header options |
||
| 632 | $options = new Element('div', 'cardHeaderOptions'); |
||
| 633 | $options->attributes->addAttributeClass(['card-options', 'float-right']); |
||
| 634 | |||
| 635 | $buttons = new Ui\Components\Buttons\Group(); |
||
| 636 | foreach ($this->options as $key => $option) { |
||
| 637 | if ($option[ 'show' ] === false) { |
||
| 638 | continue; |
||
| 639 | } |
||
| 640 | |||
| 641 | if ($option[ 'label' ] === true) { |
||
| 642 | $button = $buttons->createButton(language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
||
| 643 | if ( ! empty($option[ 'icon' ])) { |
||
| 644 | $button->setIcon($option[ 'icon' ]); |
||
| 645 | } |
||
| 646 | } else { |
||
| 647 | $button = $buttons->createButton(new Ui\Contents\Icon($option[ 'icon' ])); |
||
| 648 | $button->attributes->addAttribute('data-toggle', 'tooltip'); |
||
| 649 | $button->attributes->addAttribute('title', |
||
| 650 | language()->getLine('TABLE_BUTTON_' . strtoupper($key))); |
||
| 651 | } |
||
| 652 | |||
| 653 | if ( ! empty($option[ 'href' ])) { |
||
| 654 | $option[ 'data-url' ] = $option[ 'href' ]; |
||
| 655 | } |
||
| 656 | |||
| 657 | if (isset($option[ 'attr' ])) { |
||
| 658 | foreach ($option[ 'attr' ] as $name => $value) { |
||
| 659 | $button->attributes->addAttribute($name, $value); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | $button->setContextualClass($option[ 'contextual' ]); |
||
| 664 | $button->smallSize(); |
||
| 665 | $button->attributes->addAttribute('data-action', $key); |
||
| 666 | } |
||
| 667 | |||
| 668 | $options->childNodes->push($buttons); |
||
| 669 | |||
| 670 | $card->header->childNodes->push($options); |
||
| 671 | |||
| 672 | // Build table body |
||
| 673 | $cardBody = $card->createBody(); |
||
| 674 | $cardBodyRow = $cardBody->createRow(); |
||
| 675 | |||
| 676 | $columnSearch = $cardBodyRow->createColumn(['class' => 'col-md-4']); |
||
| 677 | $columnShow = $cardBodyRow->createColumn(['class' => 'col-md-8']); |
||
| 678 | |||
| 679 | // Search |
||
| 680 | $search = new Ui\Components\Form\Elements\Input([ |
||
| 681 | 'name' => 'query', |
||
| 682 | 'data-role' => 'table-crud-search', |
||
| 683 | 'placeholder' => language()->getLine('TABLE_LABEL_SEARCH'), |
||
| 684 | 'value' => input()->get('query'), |
||
| 685 | ]); |
||
| 686 | |||
| 687 | $columnSearch->childNodes->push($search); |
||
| 688 | |||
| 689 | // Show |
||
| 690 | $inputGroup = new Ui\Components\Form\Input\Group(); |
||
| 691 | |||
| 692 | $addOn = new Ui\Components\Form\Input\Group\AddOn(); |
||
| 693 | $addOn->textContent->push(language()->getLine('TABLE_LABEL_SHOW')); |
||
| 694 | |||
| 695 | $inputGroup->childNodes->push($addOn); |
||
| 696 | |||
| 697 | $options = []; |
||
| 698 | foreach (range((int)$this->config[ 'entries' ][ 'minimum' ], (int)$this->config[ 'entries' ][ 'maximum' ], |
||
| 699 | (int)$this->config[ 'entries' ][ 'step' ]) as $entries |
||
| 700 | ) { |
||
| 701 | $options[ $entries ] = $entries; |
||
| 702 | } |
||
| 703 | |||
| 704 | $columnsDropdown = new Ui\Components\Dropdown(language()->getLine('TABLE_LABEL_COLUMNS')); |
||
| 705 | $columnsDropdown->attributes->addAttribute('data-role', 'table-crud-columns'); |
||
| 706 | |||
| 707 | foreach ($this->columns as $key => $column) { |
||
| 708 | |||
| 709 | if ($column[ 'hiding' ] === false) { |
||
| 710 | continue; |
||
| 711 | } |
||
| 712 | |||
| 713 | $label = new Ui\Components\Form\Elements\Label(); |
||
| 714 | $label->attributes->addAttributeClass('form-check-label'); |
||
| 715 | $label->textContent->push(language()->getLine($column[ 'label' ])); |
||
| 716 | |||
| 717 | $checkbox = new Ui\Components\Form\Elements\Input([ |
||
| 718 | 'type' => 'checkbox', |
||
| 719 | 'class' => 'form-check-input', |
||
| 720 | 'data-toggle' => 'table-crud-columns', |
||
| 721 | 'data-column' => $key, |
||
| 722 | ]); |
||
| 723 | |||
| 724 | $checkbox->attributes->removeAttributeClass('form-control'); |
||
| 725 | |||
| 726 | if ($column[ 'show' ] === true) { |
||
| 727 | $checkbox->attributes->addAttribute('checked', 'checked'); |
||
| 728 | } |
||
| 729 | |||
| 730 | $label->childNodes->push($checkbox); |
||
| 731 | |||
| 732 | $columnsDropdown->menu->createItem($label); |
||
| 733 | } |
||
| 734 | |||
| 735 | $inputGroup->childNodes->push($columnsDropdown); |
||
| 736 | |||
| 737 | $select = new Ui\Components\Form\Elements\Select(); |
||
| 738 | $select->attributes->addAttribute('name', 'entries'); |
||
| 739 | $select->createOptions($options, $showEntries); |
||
| 740 | $inputGroup->childNodes->push($select); |
||
| 741 | |||
| 742 | $addOn = new Ui\Components\Form\Input\Group\AddOn(); |
||
| 743 | $addOn->textContent->push(language()->getLine('TABLE_LABEL_PAGE', [ |
||
| 744 | $startNumber, |
||
| 745 | $totalEntries, |
||
| 746 | $totalRows, |
||
| 747 | $currentPage, |
||
| 748 | $totalPages, |
||
| 749 | ])); |
||
| 750 | |||
| 751 | $inputGroup->childNodes->push($addOn); |
||
| 752 | |||
| 753 | $input = new Ui\Components\Form\Elements\Input([ |
||
| 754 | 'name' => 'page', |
||
| 755 | 'placeholder' => language()->getLine('TABLE_LABEL_GOTO'), |
||
| 756 | ]); |
||
| 757 | |||
| 758 | $inputGroup->childNodes->push($input); |
||
| 759 | |||
| 760 | $pagination = new Ui\Components\Pagination($totalRows, $showEntries); |
||
| 761 | $inputGroup->childNodes->push($pagination); |
||
| 762 | |||
| 763 | $columnShow->childNodes->push($inputGroup); |
||
| 764 | |||
| 765 | $card->textContent->push(parent::render()); |
||
| 766 | |||
| 767 | return $card->render(); |
||
| 768 | } |
||
| 895 | } |