Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DataGrid 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DataGrid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class DataGrid extends Nette\Application\UI\Control |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var callable[] |
||
| 24 | */ |
||
| 25 | public $onRedraw; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public static $icon_prefix = 'fa fa-'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * When set to TRUE, datagrid throws an exception |
||
| 34 | * when tring to get related entity within join and entity does not exist |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | public $strict_entity_property = FALSE; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | * @persistent |
||
| 42 | */ |
||
| 43 | public $page = 1; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | * @persistent |
||
| 48 | */ |
||
| 49 | public $per_page; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | * @persistent |
||
| 54 | */ |
||
| 55 | public $sort = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public $default_sort = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | * @persistent |
||
| 65 | */ |
||
| 66 | public $filter = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Callable[] |
||
| 70 | */ |
||
| 71 | public $onRender = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $use_happy_components = TRUE; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Callable[] |
||
| 80 | */ |
||
| 81 | protected $rowCallback; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $items_per_page_list; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $template_file; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Column\IColumn[] |
||
| 95 | */ |
||
| 96 | protected $columns = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var Column\Action[] |
||
| 100 | */ |
||
| 101 | protected $actions = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var GroupAction\GroupActionCollection |
||
| 105 | */ |
||
| 106 | protected $group_action_collection; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var Filter\Filter[] |
||
| 110 | */ |
||
| 111 | protected $filters = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var Export\Export[] |
||
| 115 | */ |
||
| 116 | protected $exports = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var DataModel |
||
| 120 | */ |
||
| 121 | protected $dataModel; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var DataFilter |
||
| 125 | */ |
||
| 126 | protected $dataFilter; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $primary_key = 'id'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $do_paginate = TRUE; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $csv_export = TRUE; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | protected $csv_export_filtered = TRUE; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var bool |
||
| 150 | */ |
||
| 151 | protected $sortable = FALSE; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string |
||
| 155 | */ |
||
| 156 | protected $sortable_handler = 'sort!'; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | protected $original_template; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | protected $redraw_item; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var mixed |
||
| 170 | */ |
||
| 171 | protected $translator; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | protected $force_filter_active; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var callable |
||
| 180 | */ |
||
| 181 | protected $tree_view_children_callback; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var string |
||
| 185 | */ |
||
| 186 | protected $tree_view_has_children_column; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var bool |
||
| 190 | */ |
||
| 191 | protected $outer_filter_rendering = FALSE; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | protected $columns_export_order = []; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var bool |
||
| 200 | */ |
||
| 201 | private $remember_state = TRUE; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var bool |
||
| 205 | */ |
||
| 206 | private $refresh_url = TRUE; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var Nette\Http\SessionSection |
||
| 210 | */ |
||
| 211 | private $grid_session; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @var Column\ItemDetail |
||
| 215 | */ |
||
| 216 | private $items_detail; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @var array |
||
| 220 | */ |
||
| 221 | private $row_conditions = [ |
||
| 222 | 'group_action' => FALSE, |
||
| 223 | 'action' => [] |
||
| 224 | ]; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @var bool |
||
| 228 | */ |
||
| 229 | protected $can_hide_columns = FALSE; |
||
| 230 | |||
| 231 | /** @var callable[] */ |
||
| 232 | public $onColumnAdd; |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
| 237 | * @param string $name |
||
| 238 | */ |
||
| 239 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * {inheritDoc} |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function attached($presenter) |
||
| 268 | |||
| 269 | |||
| 270 | /******************************************************************************** |
||
| 271 | * RENDERING * |
||
| 272 | ********************************************************************************/ |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Render template |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | public function render() |
||
| 353 | |||
| 354 | |||
| 355 | /******************************************************************************** |
||
| 356 | * ROW CALLBACK * |
||
| 357 | ********************************************************************************/ |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Each row can be modified with user callback |
||
| 362 | * @param callable $callback |
||
| 363 | * @return static |
||
| 364 | */ |
||
| 365 | public function setRowCallback(callable $callback) |
||
| 371 | |||
| 372 | |||
| 373 | /******************************************************************************** |
||
| 374 | * DATA SOURCE * |
||
| 375 | ********************************************************************************/ |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * By default ID, you can change that |
||
| 380 | * @param string $primary_key |
||
| 381 | * @return static |
||
| 382 | */ |
||
| 383 | public function setPrimaryKey($primary_key) |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Set Grid data source |
||
| 397 | * @param DataSource\IDataSource|array|\DibiFluent|Nette\Database\Table\Selection|\Kdyby\Doctrine\QueryBuilder $source |
||
| 398 | * @return static |
||
| 399 | */ |
||
| 400 | public function setDataSource($source) |
||
| 406 | |||
| 407 | |||
| 408 | /******************************************************************************** |
||
| 409 | * TEMPLATING * |
||
| 410 | ********************************************************************************/ |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * Set custom template file to render |
||
| 415 | * @param string $template_file |
||
| 416 | * @return static |
||
| 417 | */ |
||
| 418 | public function setTemplateFile($template_file) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Get DataGrid template file |
||
| 428 | * @return string |
||
| 429 | * @return static |
||
| 430 | */ |
||
| 431 | public function getTemplateFile() |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * Get DataGrid original template file |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getOriginalTemplateFile() |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Tell datagrid wheteher to use or not happy components |
||
| 449 | * @param boolean|NULL $use If not given, return value of static::$use_happy_components |
||
| 450 | * @return void|bool |
||
| 451 | */ |
||
| 452 | public function useHappyComponents($use = NULL) |
||
| 460 | |||
| 461 | |||
| 462 | /******************************************************************************** |
||
| 463 | * SORTING * |
||
| 464 | ********************************************************************************/ |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Set default sorting |
||
| 469 | * @param array $sort |
||
| 470 | * @return static |
||
| 471 | */ |
||
| 472 | public function setDefaultSort($sort) |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * User may set default sorting, apply it |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | public function findDefaultSort() |
||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * Set grido to be sortable |
||
| 506 | * @param bool $sortable |
||
| 507 | * @return static |
||
| 508 | */ |
||
| 509 | public function setSortable($sortable = TRUE) |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Set sortable handle |
||
| 523 | * @param string $handler |
||
| 524 | * @return static |
||
| 525 | */ |
||
| 526 | public function setSortableHandler($handler = 'sort!') |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * Tell whether DataGrid is sortable |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | public function isSortable() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Return sortable handle name |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | public function getSortableHandler() |
||
| 551 | |||
| 552 | |||
| 553 | /******************************************************************************** |
||
| 554 | * TREE VIEW * |
||
| 555 | ********************************************************************************/ |
||
| 556 | |||
| 557 | |||
| 558 | /** |
||
| 559 | * Is tree view set? |
||
| 560 | * @return boolean |
||
| 561 | */ |
||
| 562 | public function isTreeView() |
||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * Setting tree view |
||
| 570 | * @param callable $get_children_callback |
||
| 571 | * @param string $tree_view_has_children_column |
||
| 572 | * @return static |
||
| 573 | */ |
||
| 574 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
| 599 | |||
| 600 | |||
| 601 | /******************************************************************************** |
||
| 602 | * COLUMNS * |
||
| 603 | ********************************************************************************/ |
||
| 604 | |||
| 605 | |||
| 606 | /** |
||
| 607 | * Add text column with no other formating |
||
| 608 | * @param string $key |
||
| 609 | * @param string $name |
||
| 610 | * @param string|null $column |
||
| 611 | * @return Column\ColumnText |
||
| 612 | */ |
||
| 613 | public function addColumnText($key, $name, $column = NULL) |
||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * Add column with link |
||
| 624 | * @param string $key |
||
| 625 | * @param string $name |
||
| 626 | * @param string|null $column |
||
| 627 | * @return Column\ColumnLink |
||
| 628 | */ |
||
| 629 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Add column with possible number formating |
||
| 645 | * @param string $key |
||
| 646 | * @param string $name |
||
| 647 | * @param string|null $column |
||
| 648 | * @return Column\ColumnNumber |
||
| 649 | */ |
||
| 650 | public function addColumnNumber($key, $name, $column = NULL) |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * Add column with date formating |
||
| 661 | * @param string $key |
||
| 662 | * @param string $name |
||
| 663 | * @param string|null $column |
||
| 664 | * @return Column\ColumnDateTime |
||
| 665 | */ |
||
| 666 | public function addColumnDateTime($key, $name, $column = NULL) |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * @param string $key |
||
| 677 | * @param Ublaboo\DataGrid\Column\Column $column |
||
| 678 | * @return Ublaboo\DataGrid\Column\Column |
||
| 679 | */ |
||
| 680 | protected function addColumn($key, Ublaboo\DataGrid\Column\Column $column) |
||
| 685 | |||
| 686 | |||
| 687 | /** |
||
| 688 | * Return existing column |
||
| 689 | * @param string $key |
||
| 690 | * @return Column\Column |
||
| 691 | * @throws DataGridException |
||
| 692 | */ |
||
| 693 | public function getColumn($key) |
||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * Remove column |
||
| 705 | * @param string $key |
||
| 706 | * @return void |
||
| 707 | */ |
||
| 708 | public function removeColumn($key) |
||
| 712 | |||
| 713 | |||
| 714 | /** |
||
| 715 | * Check whether given key already exists in $this->columns |
||
| 716 | * @param string $key |
||
| 717 | * @throws DataGridException |
||
| 718 | */ |
||
| 719 | protected function addColumnCheck($key) |
||
| 725 | |||
| 726 | |||
| 727 | /******************************************************************************** |
||
| 728 | * ACTIONS * |
||
| 729 | ********************************************************************************/ |
||
| 730 | |||
| 731 | |||
| 732 | /** |
||
| 733 | * Create action |
||
| 734 | * @param string $key |
||
| 735 | * @param string $name |
||
| 736 | * @param string $href |
||
| 737 | * @param array|null $params |
||
| 738 | * @return Column\Action |
||
| 739 | */ |
||
| 740 | public function addAction($key, $name, $href = NULL, array $params = NULL) |
||
| 751 | |||
| 752 | |||
| 753 | /** |
||
| 754 | * Create action callback |
||
| 755 | * @param string $key |
||
| 756 | * @param string $name |
||
| 757 | * @return Column\Action |
||
| 758 | */ |
||
| 759 | public function addActionCallback($key, $name, $callback = NULL) |
||
| 776 | |||
| 777 | |||
| 778 | /** |
||
| 779 | * Get existing action |
||
| 780 | * @param string $key |
||
| 781 | * @return Column\Action |
||
| 782 | * @throws DataGridException |
||
| 783 | */ |
||
| 784 | public function getAction($key) |
||
| 792 | |||
| 793 | |||
| 794 | /** |
||
| 795 | * Remove action |
||
| 796 | * @param string $key |
||
| 797 | * @return void |
||
| 798 | */ |
||
| 799 | public function removeAction($key) |
||
| 803 | |||
| 804 | |||
| 805 | /** |
||
| 806 | * Check whether given key already exists in $this->filters |
||
| 807 | * @param string $key |
||
| 808 | * @throws DataGridException |
||
| 809 | */ |
||
| 810 | protected function addActionCheck($key) |
||
| 816 | |||
| 817 | |||
| 818 | /******************************************************************************** |
||
| 819 | * FILTERS * |
||
| 820 | ********************************************************************************/ |
||
| 821 | |||
| 822 | |||
| 823 | /** |
||
| 824 | * Add filter fot text search |
||
| 825 | * @param string $key |
||
| 826 | * @param string $name |
||
| 827 | * @param array|string $columns |
||
| 828 | * @return Filter\FilterText |
||
| 829 | * @throws DataGridException |
||
| 830 | */ |
||
| 831 | public function addFilterText($key, $name, $columns = NULL) |
||
| 843 | |||
| 844 | |||
| 845 | /** |
||
| 846 | * Add select box filter |
||
| 847 | * @param string $key |
||
| 848 | * @param string $name |
||
| 849 | * @param array $options |
||
| 850 | * @param string $column |
||
| 851 | * @return Filter\FilterSelect |
||
| 852 | * @throws DataGridException |
||
| 853 | */ |
||
| 854 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
| 866 | |||
| 867 | |||
| 868 | /** |
||
| 869 | * Add datepicker filter |
||
| 870 | * @param string $key |
||
| 871 | * @param string $name |
||
| 872 | * @param string $column |
||
| 873 | * @return Filter\FilterDate |
||
| 874 | * @throws DataGridException |
||
| 875 | */ |
||
| 876 | public function addFilterDate($key, $name, $column = NULL) |
||
| 888 | |||
| 889 | |||
| 890 | /** |
||
| 891 | * Add range filter (from - to) |
||
| 892 | * @param string $key |
||
| 893 | * @param string $name |
||
| 894 | * @param string $column |
||
| 895 | * @return Filter\FilterRange |
||
| 896 | * @throws DataGridException |
||
| 897 | */ |
||
| 898 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
| 910 | |||
| 911 | |||
| 912 | /** |
||
| 913 | * Add datepicker filter (from - to) |
||
| 914 | * @param string $key |
||
| 915 | * @param string $name |
||
| 916 | * @param string $column |
||
| 917 | * @return Filter\FilterDateRange |
||
| 918 | * @throws DataGridException |
||
| 919 | */ |
||
| 920 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
| 932 | |||
| 933 | |||
| 934 | /** |
||
| 935 | * Check whether given key already exists in $this->filters |
||
| 936 | * @param string $key |
||
| 937 | * @throws DataGridException |
||
| 938 | */ |
||
| 939 | protected function addFilterCheck($key) |
||
| 945 | |||
| 946 | |||
| 947 | /** |
||
| 948 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
| 949 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
| 950 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
| 951 | */ |
||
| 952 | public function assableFilters() |
||
| 980 | |||
| 981 | |||
| 982 | /** |
||
| 983 | * Remove filter |
||
| 984 | * @param string $key |
||
| 985 | * @return void |
||
| 986 | */ |
||
| 987 | public function removeFilter($key) |
||
| 991 | |||
| 992 | |||
| 993 | /******************************************************************************** |
||
| 994 | * FILTERING * |
||
| 995 | ********************************************************************************/ |
||
| 996 | |||
| 997 | |||
| 998 | /** |
||
| 999 | * Is filter active? |
||
| 1000 | * @return boolean |
||
| 1001 | */ |
||
| 1002 | public function isFilterActive() |
||
| 1008 | |||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Tell that filter is active from whatever reasons |
||
| 1012 | * return static |
||
| 1013 | */ |
||
| 1014 | public function setFilterActive() |
||
| 1020 | |||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * If we want to sent some initial filter |
||
| 1024 | * @param array $filter |
||
| 1025 | * @return static |
||
| 1026 | */ |
||
| 1027 | public function setFilter(array $filter) |
||
| 1033 | |||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * FilterAndGroupAction form factory |
||
| 1037 | * @return Form |
||
| 1038 | */ |
||
| 1039 | public function createComponentFilter() |
||
| 1071 | |||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Set $this->filter values after filter form submitted |
||
| 1075 | * @param Form $form |
||
| 1076 | * @return void |
||
| 1077 | */ |
||
| 1078 | public function filterSucceeded(Form $form) |
||
| 1104 | |||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Should be datagrid filters rendered separately? |
||
| 1108 | * @param boolean $out |
||
| 1109 | * @return static |
||
| 1110 | */ |
||
| 1111 | public function setOuterFilterRendering($out = TRUE) |
||
| 1117 | |||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Are datagrid filters rendered separately? |
||
| 1121 | * @return boolean |
||
| 1122 | */ |
||
| 1123 | public function hasOuterFilterRendering() |
||
| 1127 | |||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Try to restore session stuff |
||
| 1131 | * @return void |
||
| 1132 | */ |
||
| 1133 | public function findSessionFilters() |
||
| 1161 | |||
| 1162 | |||
| 1163 | /******************************************************************************** |
||
| 1164 | * EXPORTS * |
||
| 1165 | ********************************************************************************/ |
||
| 1166 | |||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Add export of type callback |
||
| 1170 | * @param string $text |
||
| 1171 | * @param callable $callback |
||
| 1172 | * @param boolean $filtered |
||
| 1173 | * @return Export\Export |
||
| 1174 | */ |
||
| 1175 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
| 1183 | |||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Add already implemented csv export |
||
| 1187 | * @param string $text |
||
| 1188 | * @param string $csv_file_name |
||
| 1189 | * @return Export\Export |
||
| 1190 | */ |
||
| 1191 | public function addExportCsv($text, $csv_file_name) |
||
| 1195 | |||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Add already implemented csv export, but for filtered data |
||
| 1199 | * @param string $text |
||
| 1200 | * @param string $csv_file_name |
||
| 1201 | * @return Export\Export |
||
| 1202 | */ |
||
| 1203 | public function addExportCsvFiltered($text, $csv_file_name) |
||
| 1207 | |||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Add export to array |
||
| 1211 | * @param Export\Export $export |
||
| 1212 | * @return Export\Export |
||
| 1213 | */ |
||
| 1214 | protected function addToExports(Export\Export $export) |
||
| 1222 | |||
| 1223 | |||
| 1224 | public function resetExportsLinks() |
||
| 1230 | |||
| 1231 | |||
| 1232 | /******************************************************************************** |
||
| 1233 | * GROUP ACTIONS * |
||
| 1234 | ********************************************************************************/ |
||
| 1235 | |||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Add group actino |
||
| 1239 | * @param string $title |
||
| 1240 | * @param array $options |
||
| 1241 | * @return GroupAction\GroupAction |
||
| 1242 | */ |
||
| 1243 | public function addGroupAction($title, $options = []) |
||
| 1247 | |||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Get collection of all group actions |
||
| 1251 | * @return GroupAction\GroupActionCollection |
||
| 1252 | */ |
||
| 1253 | public function getGroupActionCollection() |
||
| 1261 | |||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Has datagrid some group actions? |
||
| 1265 | * @return boolean |
||
| 1266 | */ |
||
| 1267 | public function hasGroupActions() |
||
| 1271 | |||
| 1272 | |||
| 1273 | /******************************************************************************** |
||
| 1274 | * HANDLERS * |
||
| 1275 | ********************************************************************************/ |
||
| 1276 | |||
| 1277 | |||
| 1278 | /** |
||
| 1279 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
| 1280 | * @param int $page |
||
| 1281 | * @return void |
||
| 1282 | */ |
||
| 1283 | public function handlePage($page) |
||
| 1293 | |||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Handler for sorting |
||
| 1297 | * @param array $sort |
||
| 1298 | * @return void |
||
| 1299 | */ |
||
| 1300 | public function handleSort(array $sort) |
||
| 1310 | |||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * handler for reseting the filter |
||
| 1314 | * @return void |
||
| 1315 | */ |
||
| 1316 | public function handleResetFilter() |
||
| 1333 | |||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Handler for export |
||
| 1337 | * @param int $id Key for particular export class in array $this->exports |
||
| 1338 | * @return void |
||
| 1339 | */ |
||
| 1340 | public function handleExport($id) |
||
| 1384 | |||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Handler for getting children of parent item (e.g. category) |
||
| 1388 | * @param int $parent |
||
| 1389 | * @return void |
||
| 1390 | */ |
||
| 1391 | public function handleGetChildren($parent) |
||
| 1408 | |||
| 1409 | |||
| 1410 | /** |
||
| 1411 | * Handler for getting item detail |
||
| 1412 | * @param mixed $id |
||
| 1413 | * @return void |
||
| 1414 | */ |
||
| 1415 | public function handleGetItemDetail($id) |
||
| 1429 | |||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Handler for inline editing |
||
| 1433 | * @param mixed $id |
||
| 1434 | * @param mixed $key |
||
| 1435 | * @return void |
||
| 1436 | */ |
||
| 1437 | public function handleEdit($id, $key) |
||
| 1444 | |||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Redraw $this |
||
| 1448 | * @return void |
||
| 1449 | */ |
||
| 1450 | public function reload($snippets = []) |
||
| 1473 | |||
| 1474 | |||
| 1475 | /** |
||
| 1476 | * Redraw just one row via ajax |
||
| 1477 | * @param int $id |
||
| 1478 | * @param mixed $primary_where_column |
||
| 1479 | * @return void |
||
| 1480 | */ |
||
| 1481 | public function redrawItem($id, $primary_where_column = NULL) |
||
| 1490 | |||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * Tell datagrid to display all columns |
||
| 1494 | * @return void |
||
| 1495 | */ |
||
| 1496 | public function handleShowAllColumns() |
||
| 1504 | |||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Notice datagrid to not display particular columns |
||
| 1508 | * @param string $column |
||
| 1509 | * @return void |
||
| 1510 | */ |
||
| 1511 | public function handleHideColumn($column) |
||
| 1530 | |||
| 1531 | |||
| 1532 | public function handleActionCallback($__key, $__id) |
||
| 1542 | |||
| 1543 | |||
| 1544 | /******************************************************************************** |
||
| 1545 | * PAGINATION * |
||
| 1546 | ********************************************************************************/ |
||
| 1547 | |||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Set options of select "items_per_page" |
||
| 1551 | * @param array $items_per_page_list |
||
| 1552 | * @return static |
||
| 1553 | */ |
||
| 1554 | public function setItemsPerPageList(array $items_per_page_list, $include_all = TRUE) |
||
| 1564 | |||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Paginator factory |
||
| 1568 | * @return Components\DataGridPaginator\DataGridPaginator |
||
| 1569 | */ |
||
| 1570 | public function createComponentPaginator() |
||
| 1585 | |||
| 1586 | |||
| 1587 | /** |
||
| 1588 | * PerPage form factory |
||
| 1589 | * @return Form |
||
| 1590 | */ |
||
| 1591 | public function createComponentPerPage() |
||
| 1617 | |||
| 1618 | |||
| 1619 | /** |
||
| 1620 | * Get parameter per_page |
||
| 1621 | * @return int |
||
| 1622 | */ |
||
| 1623 | public function getPerPage() |
||
| 1635 | |||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Get associative array of items_per_page_list |
||
| 1639 | * @return array |
||
| 1640 | */ |
||
| 1641 | public function getItemsPerPageList() |
||
| 1659 | |||
| 1660 | |||
| 1661 | /** |
||
| 1662 | * Order Grid to "be paginated" |
||
| 1663 | * @param bool $do |
||
| 1664 | * @return static |
||
| 1665 | */ |
||
| 1666 | public function setPagination($do) |
||
| 1672 | |||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Tell whether Grid is paginated |
||
| 1676 | * @return bool |
||
| 1677 | */ |
||
| 1678 | public function isPaginated() |
||
| 1682 | |||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * Return current paginator class |
||
| 1686 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
| 1687 | */ |
||
| 1688 | public function getPaginator() |
||
| 1696 | |||
| 1697 | |||
| 1698 | /******************************************************************************** |
||
| 1699 | * I18N * |
||
| 1700 | ********************************************************************************/ |
||
| 1701 | |||
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Set datagrid translator |
||
| 1705 | * @param Nette\Localization\ITranslator $translator |
||
| 1706 | * @return static |
||
| 1707 | */ |
||
| 1708 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
| 1714 | |||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Get translator for datagrid |
||
| 1718 | * @return Nette\Localization\ITranslator |
||
| 1719 | */ |
||
| 1720 | public function getTranslator() |
||
| 1728 | |||
| 1729 | |||
| 1730 | /******************************************************************************** |
||
| 1731 | * COLUMNS ORDER * |
||
| 1732 | ********************************************************************************/ |
||
| 1733 | |||
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Set order of datagrid columns |
||
| 1737 | * @param array $order |
||
| 1738 | * @return static |
||
| 1739 | */ |
||
| 1740 | public function setColumnsOrder($order) |
||
| 1758 | |||
| 1759 | |||
| 1760 | /** |
||
| 1761 | * Columns order may be different for export and normal grid |
||
| 1762 | * @param array $order |
||
| 1763 | */ |
||
| 1764 | public function setColumnsExportOrder($order) |
||
| 1768 | |||
| 1769 | |||
| 1770 | /******************************************************************************** |
||
| 1771 | * SESSION & URL * |
||
| 1772 | ********************************************************************************/ |
||
| 1773 | |||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Find some unique session key name |
||
| 1777 | * @return string |
||
| 1778 | */ |
||
| 1779 | public function getSessionSectionName() |
||
| 1783 | |||
| 1784 | |||
| 1785 | /** |
||
| 1786 | * Should datagrid remember its filters/pagination/etc using session? |
||
| 1787 | * @param bool $remember |
||
| 1788 | * @return static |
||
| 1789 | */ |
||
| 1790 | public function setRememberState($remember = TRUE) |
||
| 1796 | |||
| 1797 | |||
| 1798 | /** |
||
| 1799 | * Should datagrid refresh url using history API? |
||
| 1800 | * @param bool $refresh |
||
| 1801 | * @return static |
||
| 1802 | */ |
||
| 1803 | public function setRefreshUrl($refresh = TRUE) |
||
| 1810 | |||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Get session data if functionality is enabled |
||
| 1814 | * @param string $key |
||
| 1815 | * @return mixed |
||
| 1816 | */ |
||
| 1817 | public function getSessionData($key = NULL, $default_value = NULL) |
||
| 1825 | |||
| 1826 | |||
| 1827 | /** |
||
| 1828 | * Save session data - just if it is enabled |
||
| 1829 | * @param string $key |
||
| 1830 | * @param mixed $value |
||
| 1831 | * @return void |
||
| 1832 | */ |
||
| 1833 | public function saveSessionData($key, $value) |
||
| 1839 | |||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Delete session data |
||
| 1843 | * @return void |
||
| 1844 | */ |
||
| 1845 | public function deleteSesssionData($key) |
||
| 1849 | |||
| 1850 | |||
| 1851 | /******************************************************************************** |
||
| 1852 | * ITEM DETAIL * |
||
| 1853 | ********************************************************************************/ |
||
| 1854 | |||
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Get items detail parameters |
||
| 1858 | * @return array |
||
| 1859 | */ |
||
| 1860 | public function getItemsDetail() |
||
| 1864 | |||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * Items can have thair detail - toggled |
||
| 1868 | * @param mixed $detail callable|string|bool |
||
| 1869 | * @param bool|NULL $primary_where_column |
||
| 1870 | * @return Column\ItemDetail |
||
| 1871 | */ |
||
| 1872 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
| 1911 | |||
| 1912 | |||
| 1913 | /******************************************************************************** |
||
| 1914 | * ROW PRIVILEGES * |
||
| 1915 | ********************************************************************************/ |
||
| 1916 | |||
| 1917 | |||
| 1918 | public function allowRowsGroupAction(callable $condition) |
||
| 1922 | |||
| 1923 | |||
| 1924 | public function allowRowsAction($key, callable $condition) |
||
| 1928 | |||
| 1929 | |||
| 1930 | public function getRowCondition($name, $key = NULL) |
||
| 1944 | |||
| 1945 | |||
| 1946 | /******************************************************************************** |
||
| 1947 | * HIDEABLE COLUMNS * |
||
| 1948 | ********************************************************************************/ |
||
| 1949 | |||
| 1950 | |||
| 1951 | /** |
||
| 1952 | * Can datagrid hide colums? |
||
| 1953 | * @return boolean |
||
| 1954 | */ |
||
| 1955 | public function canHideColumns() |
||
| 1959 | |||
| 1960 | |||
| 1961 | /** |
||
| 1962 | * Order Grid to set columns hideable. |
||
| 1963 | * @param bool $do |
||
| 1964 | * @return static |
||
| 1965 | */ |
||
| 1966 | public function setColumnsHideable() |
||
| 1972 | |||
| 1973 | |||
| 1974 | /******************************************************************************** |
||
| 1975 | * INTERNAL * |
||
| 1976 | ********************************************************************************/ |
||
| 1977 | |||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Get cont of columns |
||
| 1981 | * @return int |
||
| 1982 | */ |
||
| 1983 | public function getColumnsCount() |
||
| 1997 | |||
| 1998 | |||
| 1999 | /** |
||
| 2000 | * Get primary key of datagrid data source |
||
| 2001 | * @return string |
||
| 2002 | */ |
||
| 2003 | public function getPrimaryKey() |
||
| 2007 | |||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Get set of set columns |
||
| 2011 | * @return Column\IColumn[] |
||
| 2012 | */ |
||
| 2013 | public function getColumns() |
||
| 2021 | |||
| 2022 | |||
| 2023 | /** |
||
| 2024 | * @return PresenterComponent |
||
| 2025 | */ |
||
| 2026 | public function getParent() |
||
| 2038 | |||
| 2039 | } |
||
| 2040 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: