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 |
||
| 18 | class DataGrid extends Nette\Application\UI\Control |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var callable[] |
||
| 23 | */ |
||
| 24 | public $onRedraw; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public static $icon_prefix = 'fa fa-'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * When set to TRUE, datagrid throws an exception |
||
| 33 | * when tring to get related entity within join and entity does not exist |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | public $strict_entity_property = FALSE; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | * @persistent |
||
| 41 | */ |
||
| 42 | public $page = 1; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | * @persistent |
||
| 47 | */ |
||
| 48 | public $per_page; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | * @persistent |
||
| 53 | */ |
||
| 54 | public $sort = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | * @persistent |
||
| 59 | */ |
||
| 60 | public $filter = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Callable[] |
||
| 64 | */ |
||
| 65 | public $onRender = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $use_happy_components = TRUE; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Callable[] |
||
| 74 | */ |
||
| 75 | protected $rowCallback; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $items_per_page_list; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $template_file; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var Column\IColumn[] |
||
| 89 | */ |
||
| 90 | protected $columns = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Column\Action[] |
||
| 94 | */ |
||
| 95 | protected $actions = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var GroupAction\GroupActionCollection |
||
| 99 | */ |
||
| 100 | protected $group_action_collection; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var Filter\Filter[] |
||
| 104 | */ |
||
| 105 | protected $filters = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Export\Export[] |
||
| 109 | */ |
||
| 110 | protected $exports = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var DataModel |
||
| 114 | */ |
||
| 115 | protected $dataModel; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var DataFilter |
||
| 119 | */ |
||
| 120 | protected $dataFilter; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | protected $primary_key = 'id'; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | protected $do_paginate = TRUE; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var bool |
||
| 134 | */ |
||
| 135 | protected $csv_export = TRUE; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | protected $csv_export_filtered = TRUE; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var bool |
||
| 144 | */ |
||
| 145 | protected $sortable = FALSE; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var string |
||
| 149 | */ |
||
| 150 | protected $sortable_handler = 'sort!'; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var string |
||
| 154 | */ |
||
| 155 | protected $original_template; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected $redraw_item; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var mixed |
||
| 164 | */ |
||
| 165 | protected $translator; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var bool |
||
| 169 | */ |
||
| 170 | protected $force_filter_active; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var callable |
||
| 174 | */ |
||
| 175 | protected $tree_view_children_callback; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var string |
||
| 179 | */ |
||
| 180 | protected $tree_view_has_children_column; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var bool |
||
| 184 | */ |
||
| 185 | protected $outer_filter_rendering = FALSE; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var array |
||
| 189 | */ |
||
| 190 | protected $columns_export_order = []; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @var bool |
||
| 194 | */ |
||
| 195 | private $remember_state = TRUE; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @var bool |
||
| 199 | */ |
||
| 200 | private $refresh_url = TRUE; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var Nette\Http\SessionSection |
||
| 204 | */ |
||
| 205 | private $grid_session; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @var array |
||
| 209 | */ |
||
| 210 | private $items_detail = []; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @var array |
||
| 214 | */ |
||
| 215 | private $row_conditions = [ |
||
| 216 | 'group_action' => FALSE, |
||
| 217 | 'action' => [] |
||
| 218 | ]; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var bool |
||
| 222 | */ |
||
| 223 | protected $can_hide_columns = FALSE; |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
| 228 | * @param string $name |
||
| 229 | */ |
||
| 230 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * {inheritDoc} |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function attached($presenter) |
||
| 258 | |||
| 259 | |||
| 260 | /******************************************************************************** |
||
| 261 | * RENDERING * |
||
| 262 | ********************************************************************************/ |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Render template |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | public function render() |
||
| 343 | |||
| 344 | |||
| 345 | /******************************************************************************** |
||
| 346 | * ROW CALLBACK * |
||
| 347 | ********************************************************************************/ |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Each row can be modified with user callback |
||
| 352 | * @param callable $callback |
||
| 353 | * @return static |
||
| 354 | */ |
||
| 355 | public function setRowCallback(callable $callback) |
||
| 361 | |||
| 362 | |||
| 363 | /******************************************************************************** |
||
| 364 | * DATA SOURCE * |
||
| 365 | ********************************************************************************/ |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * By default ID, you can change that |
||
| 370 | * @param string $primary_key |
||
| 371 | */ |
||
| 372 | public function setPrimaryKey($primary_key) |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Set Grid data source |
||
| 386 | * @param DataSource\IDataSource|array|\DibiFluent|Nette\Database\Table\Selection|\Kdyby\Doctrine\QueryBuilder $source |
||
| 387 | * @return DataGrid |
||
| 388 | */ |
||
| 389 | public function setDataSource($source) |
||
| 395 | |||
| 396 | |||
| 397 | /******************************************************************************** |
||
| 398 | * TEMPLATING * |
||
| 399 | ********************************************************************************/ |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Set custom template file to render |
||
| 404 | * @param string $template_file |
||
| 405 | */ |
||
| 406 | public function setTemplateFile($template_file) |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Get DataGrid template file |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getTemplateFile() |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Get DataGrid original template file |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getOriginalTemplateFile() |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Tell datagrid wheteher to use or not happy components |
||
| 436 | * @param boolean|NULL $use If not given, return value of static::$use_happy_components |
||
| 437 | * @return void|bool |
||
| 438 | */ |
||
| 439 | public function useHappyComponents($use = NULL) |
||
| 447 | |||
| 448 | |||
| 449 | /******************************************************************************** |
||
| 450 | * SORTING * |
||
| 451 | ********************************************************************************/ |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Set default sorting |
||
| 456 | * @param aray $sort |
||
| 457 | */ |
||
| 458 | public function setDefaultSort($sort) |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Set grido to be sortable |
||
| 472 | * @param bool $sortable |
||
| 473 | */ |
||
| 474 | public function setSortable($sortable = TRUE) |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * Set sortable handle |
||
| 488 | * @param string $handle |
||
| 489 | */ |
||
| 490 | public function setSortableHandler($handler = 'sort!') |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Tell whether DataGrid is sortable |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function isSortable() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return sortable handle name |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getSortableHandler() |
||
| 515 | |||
| 516 | |||
| 517 | /******************************************************************************** |
||
| 518 | * TREE VIEW * |
||
| 519 | ********************************************************************************/ |
||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * Is tree view set? |
||
| 524 | * @return boolean |
||
| 525 | */ |
||
| 526 | public function isTreeView() |
||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * Setting tree view |
||
| 534 | * @param callable $get_children_callback |
||
| 535 | * @param string $tree_view_has_children_column |
||
| 536 | * @return DataGrid |
||
| 537 | */ |
||
| 538 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
| 563 | |||
| 564 | |||
| 565 | /******************************************************************************** |
||
| 566 | * COLUMNS * |
||
| 567 | ********************************************************************************/ |
||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * Add text column with no other formating |
||
| 572 | * @param string $key |
||
| 573 | * @param string $name |
||
| 574 | * @param string|null $column |
||
| 575 | * @return Column\ColumnText |
||
| 576 | */ |
||
| 577 | public function addColumnText($key, $name, $column = NULL) |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Add column with link |
||
| 588 | * @param string $key |
||
| 589 | * @param string $name |
||
| 590 | * @param string|null $column |
||
| 591 | * @return Column\ColumnLink |
||
| 592 | */ |
||
| 593 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Add column with possible number formating |
||
| 609 | * @param string $key |
||
| 610 | * @param string $name |
||
| 611 | * @param string|null $column |
||
| 612 | * @return Column\ColumnNumber |
||
| 613 | */ |
||
| 614 | public function addColumnNumber($key, $name, $column = NULL) |
||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * Add column with date formating |
||
| 625 | * @param string $key |
||
| 626 | * @param string $name |
||
| 627 | * @param string|null $column |
||
| 628 | * @return Column\ColumnDateTime |
||
| 629 | */ |
||
| 630 | public function addColumnDateTime($key, $name, $column = NULL) |
||
| 637 | |||
| 638 | |||
| 639 | /** |
||
| 640 | * Return existing column |
||
| 641 | * @param string $key |
||
| 642 | * @return Column\Column |
||
| 643 | * @throws DataGridException |
||
| 644 | */ |
||
| 645 | public function getColumn($key) |
||
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * Remove column |
||
| 657 | * @param string $key |
||
| 658 | * @return void |
||
| 659 | */ |
||
| 660 | public function removeColumn($key) |
||
| 664 | |||
| 665 | |||
| 666 | /** |
||
| 667 | * Check whether given key already exists in $this->columns |
||
| 668 | * @param string $key |
||
| 669 | * @throws DataGridException |
||
| 670 | */ |
||
| 671 | protected function addColumnCheck($key) |
||
| 677 | |||
| 678 | |||
| 679 | /******************************************************************************** |
||
| 680 | * ACTIONS * |
||
| 681 | ********************************************************************************/ |
||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * Create action |
||
| 686 | * @param string $key |
||
| 687 | * @param string $name |
||
| 688 | * @param string $href |
||
| 689 | * @param array|null $params |
||
| 690 | * @return Column\Action |
||
| 691 | */ |
||
| 692 | public function addAction($key, $name, $href = NULL, array $params = NULL) |
||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * Create action callback |
||
| 707 | * @param string $key |
||
| 708 | * @param string $name |
||
| 709 | * @return Column\Action |
||
| 710 | */ |
||
| 711 | public function addActionCallback($key, $name, $callback = NULL) |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * Get existing action |
||
| 732 | * @param string $key |
||
| 733 | * @return Column\Action |
||
| 734 | * @throws DataGridException |
||
| 735 | */ |
||
| 736 | public function getAction($key) |
||
| 744 | |||
| 745 | |||
| 746 | /** |
||
| 747 | * Remove action |
||
| 748 | * @param string $key |
||
| 749 | * @return void |
||
| 750 | */ |
||
| 751 | public function removeAction($key) |
||
| 755 | |||
| 756 | |||
| 757 | /** |
||
| 758 | * Check whether given key already exists in $this->filters |
||
| 759 | * @param string $key |
||
| 760 | * @throws DataGridException |
||
| 761 | */ |
||
| 762 | protected function addActionCheck($key) |
||
| 768 | |||
| 769 | |||
| 770 | /******************************************************************************** |
||
| 771 | * FILTERS * |
||
| 772 | ********************************************************************************/ |
||
| 773 | |||
| 774 | |||
| 775 | /** |
||
| 776 | * Add filter fot text search |
||
| 777 | * @param string $key |
||
| 778 | * @param string $name |
||
| 779 | * @param array|string $columns |
||
| 780 | * @return Filter\FilterText |
||
| 781 | * @throws DataGridException |
||
| 782 | */ |
||
| 783 | public function addFilterText($key, $name, $columns = NULL) |
||
| 795 | |||
| 796 | |||
| 797 | /** |
||
| 798 | * Add select box filter |
||
| 799 | * @param string $key |
||
| 800 | * @param string $name |
||
| 801 | * @param array $options |
||
| 802 | * @param string $column |
||
| 803 | * @return Filter\FilterSelect |
||
| 804 | * @throws DataGridException |
||
| 805 | */ |
||
| 806 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * Add datepicker filter |
||
| 822 | * @param string $key |
||
| 823 | * @param string $name |
||
| 824 | * @param string $column |
||
| 825 | * @return Filter\FilterDate |
||
| 826 | * @throws DataGridException |
||
| 827 | */ |
||
| 828 | public function addFilterDate($key, $name, $column = NULL) |
||
| 840 | |||
| 841 | |||
| 842 | /** |
||
| 843 | * Add range filter (from - to) |
||
| 844 | * @param string $key |
||
| 845 | * @param string $name |
||
| 846 | * @param string $column |
||
| 847 | * @return Filter\FilterRange |
||
| 848 | * @throws DataGridException |
||
| 849 | */ |
||
| 850 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
| 862 | |||
| 863 | |||
| 864 | /** |
||
| 865 | * Add datepicker filter (from - to) |
||
| 866 | * @param string $key |
||
| 867 | * @param string $name |
||
| 868 | * @param string $column |
||
| 869 | * @return Filter\FilterDateRange |
||
| 870 | * @throws DataGridException |
||
| 871 | */ |
||
| 872 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
| 884 | |||
| 885 | |||
| 886 | /** |
||
| 887 | * Check whether given key already exists in $this->filters |
||
| 888 | * @param string $key |
||
| 889 | * @throws DataGridException |
||
| 890 | */ |
||
| 891 | protected function addFilterCheck($key) |
||
| 897 | |||
| 898 | |||
| 899 | /** |
||
| 900 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
| 901 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
| 902 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
| 903 | */ |
||
| 904 | public function assableFilters() |
||
| 932 | |||
| 933 | |||
| 934 | /** |
||
| 935 | * Remove filter |
||
| 936 | * @param string $key |
||
| 937 | * @return void |
||
| 938 | */ |
||
| 939 | public function removeFilter($key) |
||
| 943 | |||
| 944 | |||
| 945 | /******************************************************************************** |
||
| 946 | * FILTERING * |
||
| 947 | ********************************************************************************/ |
||
| 948 | |||
| 949 | |||
| 950 | /** |
||
| 951 | * Is filter active? |
||
| 952 | * @return boolean |
||
| 953 | */ |
||
| 954 | public function isFilterActive() |
||
| 960 | |||
| 961 | |||
| 962 | /** |
||
| 963 | * Tell that filter is active from whatever reasons |
||
| 964 | * return static |
||
| 965 | */ |
||
| 966 | public function setFilterActive() |
||
| 972 | |||
| 973 | |||
| 974 | /** |
||
| 975 | * If we want to sent some initial filter |
||
| 976 | * @param array $filter |
||
| 977 | * @return static |
||
| 978 | */ |
||
| 979 | public function setFilter(array $filter) |
||
| 985 | |||
| 986 | |||
| 987 | /** |
||
| 988 | * FilterAndGroupAction form factory |
||
| 989 | * @return Form |
||
| 990 | */ |
||
| 991 | public function createComponentFilter() |
||
| 1021 | |||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Set $this->filter values after filter form submitted |
||
| 1025 | * @param Form $form |
||
| 1026 | * @return void |
||
| 1027 | */ |
||
| 1028 | public function filterSucceeded(Form $form) |
||
| 1054 | |||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Should be datagrid filters rendered separately? |
||
| 1058 | * @param boolean $out |
||
| 1059 | * @return static |
||
| 1060 | */ |
||
| 1061 | public function setOuterFilterRendering($out = TRUE) |
||
| 1067 | |||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Are datagrid filters rendered separately? |
||
| 1071 | * @return boolean |
||
| 1072 | */ |
||
| 1073 | public function hasOuterFilterRendering() |
||
| 1077 | |||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Try to restore session stuff |
||
| 1081 | * @return void |
||
| 1082 | */ |
||
| 1083 | public function findSessionFilters() |
||
| 1111 | |||
| 1112 | |||
| 1113 | /******************************************************************************** |
||
| 1114 | * EXPORTS * |
||
| 1115 | ********************************************************************************/ |
||
| 1116 | |||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Add export of type callback |
||
| 1120 | * @param string $text |
||
| 1121 | * @param callable $callback |
||
| 1122 | * @param boolean $filtered |
||
| 1123 | * @return Export\Export |
||
| 1124 | */ |
||
| 1125 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
| 1133 | |||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Add already implemented csv export |
||
| 1137 | * @param string $text |
||
| 1138 | * @param string $csv_file_name |
||
| 1139 | * @return Export\Export |
||
| 1140 | */ |
||
| 1141 | public function addExportCsv($text, $csv_file_name) |
||
| 1145 | |||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Add already implemented csv export, but for filtered data |
||
| 1149 | * @param string $text |
||
| 1150 | * @param string $csv_file_name |
||
| 1151 | * @return Export\Export |
||
| 1152 | */ |
||
| 1153 | public function addExportCsvFiltered($text, $csv_file_name) |
||
| 1157 | |||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Add export to array |
||
| 1161 | * @param Export\Export $export |
||
| 1162 | * @return Export\Export |
||
| 1163 | */ |
||
| 1164 | protected function addToExports(Export\Export $export) |
||
| 1172 | |||
| 1173 | |||
| 1174 | public function resetExportsLinks() |
||
| 1180 | |||
| 1181 | |||
| 1182 | /******************************************************************************** |
||
| 1183 | * GROUP ACTIONS * |
||
| 1184 | ********************************************************************************/ |
||
| 1185 | |||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Add group actino |
||
| 1189 | * @param string $title |
||
| 1190 | * @param array $options |
||
| 1191 | * @return GroupAction\GroupAction |
||
| 1192 | */ |
||
| 1193 | public function addGroupAction($title, $options = []) |
||
| 1197 | |||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Get collection of all group actions |
||
| 1201 | * @return GroupAction\GroupActionCollection |
||
| 1202 | */ |
||
| 1203 | public function getGroupActionCollection() |
||
| 1211 | |||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Has datagrid some group actions? |
||
| 1215 | * @return boolean |
||
| 1216 | */ |
||
| 1217 | public function hasGroupActions() |
||
| 1221 | |||
| 1222 | |||
| 1223 | /******************************************************************************** |
||
| 1224 | * HANDLERS * |
||
| 1225 | ********************************************************************************/ |
||
| 1226 | |||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
| 1230 | * @param int $page |
||
| 1231 | * @return void |
||
| 1232 | */ |
||
| 1233 | public function handlePage($page) |
||
| 1243 | |||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Handler for sorting |
||
| 1247 | * @param array $sort |
||
| 1248 | * @return void |
||
| 1249 | */ |
||
| 1250 | public function handleSort(array $sort) |
||
| 1260 | |||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * handler for reseting the filter |
||
| 1264 | * @return void |
||
| 1265 | */ |
||
| 1266 | public function handleResetFilter() |
||
| 1283 | |||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Handler for export |
||
| 1287 | * @param int $id Key for particular export class in array $this->exports |
||
| 1288 | * @return void |
||
| 1289 | */ |
||
| 1290 | public function handleExport($id) |
||
| 1334 | |||
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Handler for getting children of parent item (e.g. category) |
||
| 1338 | * @param int $parent |
||
| 1339 | * @return void |
||
| 1340 | */ |
||
| 1341 | View Code Duplication | public function handleGetChildren($parent) |
|
| 1358 | |||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Handler for getting item detail |
||
| 1362 | * @param mixed $id |
||
| 1363 | * @return void |
||
| 1364 | */ |
||
| 1365 | View Code Duplication | public function handleGetItemDetail($id) |
|
| 1379 | |||
| 1380 | |||
| 1381 | /** |
||
| 1382 | * Handler for inline editing |
||
| 1383 | * @param mixed $id |
||
| 1384 | * @param mixed $key |
||
| 1385 | * @return void |
||
| 1386 | */ |
||
| 1387 | public function handleEdit($id, $key) |
||
| 1394 | |||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Redraw $this |
||
| 1398 | * @return void |
||
| 1399 | */ |
||
| 1400 | public function reload($snippets = []) |
||
| 1423 | |||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Redraw just one row via ajax |
||
| 1427 | * @param int $id |
||
| 1428 | * @param mixed $primary_where_column |
||
| 1429 | * @return void |
||
| 1430 | */ |
||
| 1431 | public function redrawItem($id, $primary_where_column = NULL) |
||
| 1440 | |||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Tell datagrid to display all columns |
||
| 1444 | * @return void |
||
| 1445 | */ |
||
| 1446 | public function handleShowAllColumns() |
||
| 1454 | |||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Notice datagrid to not display particular columns |
||
| 1458 | * @param string $column |
||
| 1459 | * @return void |
||
| 1460 | */ |
||
| 1461 | public function handleHideColumn($column) |
||
| 1480 | |||
| 1481 | |||
| 1482 | public function handleActionCallback($__key, $__id) |
||
| 1492 | |||
| 1493 | |||
| 1494 | /******************************************************************************** |
||
| 1495 | * PAGINATION * |
||
| 1496 | ********************************************************************************/ |
||
| 1497 | |||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Set options of select "items_per_page" |
||
| 1501 | * @param array $items_per_page_list |
||
| 1502 | */ |
||
| 1503 | public function setItemsPerPageList(array $items_per_page_list, $include_all = TRUE) |
||
| 1513 | |||
| 1514 | |||
| 1515 | /** |
||
| 1516 | * Paginator factory |
||
| 1517 | * @return Components\DataGridPaginator\DataGridPaginator |
||
| 1518 | */ |
||
| 1519 | public function createComponentPaginator() |
||
| 1532 | |||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * PerPage form factory |
||
| 1536 | * @return Form |
||
| 1537 | */ |
||
| 1538 | public function createComponentPerPage() |
||
| 1564 | |||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Get parameter per_page |
||
| 1568 | * @return int |
||
| 1569 | */ |
||
| 1570 | public function getPerPage() |
||
| 1582 | |||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * Get associative array of items_per_page_list |
||
| 1586 | * @return array |
||
| 1587 | */ |
||
| 1588 | public function getItemsPerPageList() |
||
| 1606 | |||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Order Grid to "be paginated" |
||
| 1610 | * @param bool $do |
||
| 1611 | * @return static |
||
| 1612 | */ |
||
| 1613 | public function setPagination($do) |
||
| 1619 | |||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Tell whether Grid is paginated |
||
| 1623 | * @return bool |
||
| 1624 | */ |
||
| 1625 | public function isPaginated() |
||
| 1629 | |||
| 1630 | |||
| 1631 | /** |
||
| 1632 | * Return current paginator class |
||
| 1633 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
| 1634 | */ |
||
| 1635 | public function getPaginator() |
||
| 1643 | |||
| 1644 | |||
| 1645 | /******************************************************************************** |
||
| 1646 | * I18N * |
||
| 1647 | ********************************************************************************/ |
||
| 1648 | |||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Set datagrid translator |
||
| 1652 | * @param Nette\Localization\ITranslator $translator |
||
| 1653 | * @return static |
||
| 1654 | */ |
||
| 1655 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
| 1661 | |||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Get translator for datagrid |
||
| 1665 | * @return Nette\Localization\ITranslator |
||
| 1666 | */ |
||
| 1667 | public function getTranslator() |
||
| 1675 | |||
| 1676 | |||
| 1677 | /******************************************************************************** |
||
| 1678 | * COLUMNS ORDER * |
||
| 1679 | ********************************************************************************/ |
||
| 1680 | |||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Set order of datagrid columns |
||
| 1684 | * @param array $order |
||
| 1685 | * @return static |
||
| 1686 | */ |
||
| 1687 | public function setColumnsOrder($order) |
||
| 1705 | |||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Columns order may be different for export and normal grid |
||
| 1709 | * @param array $order |
||
| 1710 | */ |
||
| 1711 | public function setColumnsExportOrder($order) |
||
| 1715 | |||
| 1716 | |||
| 1717 | /******************************************************************************** |
||
| 1718 | * SESSION & URL * |
||
| 1719 | ********************************************************************************/ |
||
| 1720 | |||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Find some unique session key name |
||
| 1724 | * @return string |
||
| 1725 | */ |
||
| 1726 | public function getSessionSectionName() |
||
| 1730 | |||
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Should datagrid remember its filters/pagination/etc using session? |
||
| 1734 | * @param bool $remember |
||
| 1735 | * @return static |
||
| 1736 | */ |
||
| 1737 | public function setRememberState($remember = TRUE) |
||
| 1743 | |||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * Should datagrid refresh url using history API? |
||
| 1747 | * @param bool $refresh |
||
| 1748 | * @return static |
||
| 1749 | */ |
||
| 1750 | public function setRefreshUrl($refresh = TRUE) |
||
| 1757 | |||
| 1758 | |||
| 1759 | /** |
||
| 1760 | * Get session data if functionality is enabled |
||
| 1761 | * @param string $key |
||
| 1762 | * @return mixed |
||
| 1763 | */ |
||
| 1764 | public function getSessionData($key = NULL, $default_value = NULL) |
||
| 1772 | |||
| 1773 | |||
| 1774 | /** |
||
| 1775 | * Save session data - just if it is enabled |
||
| 1776 | * @param string $key |
||
| 1777 | * @param mixed $value |
||
| 1778 | * @return void |
||
| 1779 | */ |
||
| 1780 | public function saveSessionData($key, $value) |
||
| 1786 | |||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Delete session data |
||
| 1790 | * @return void |
||
| 1791 | */ |
||
| 1792 | public function deleteSesssionData($key) |
||
| 1796 | |||
| 1797 | |||
| 1798 | /******************************************************************************** |
||
| 1799 | * ITEM DETAIL * |
||
| 1800 | ********************************************************************************/ |
||
| 1801 | |||
| 1802 | |||
| 1803 | /** |
||
| 1804 | * Get items detail parameters |
||
| 1805 | * @return array |
||
| 1806 | */ |
||
| 1807 | public function getItemsDetail() |
||
| 1811 | |||
| 1812 | |||
| 1813 | /** |
||
| 1814 | * Items can have thair detail - toggled |
||
| 1815 | * @param mixed $detail callable|string|bool |
||
| 1816 | * @param bool|NULL $primary_where_column |
||
| 1817 | * @return Column\ItemDetail |
||
| 1818 | */ |
||
| 1819 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
| 1858 | |||
| 1859 | |||
| 1860 | /******************************************************************************** |
||
| 1861 | * ROW PRIVILEGES * |
||
| 1862 | ********************************************************************************/ |
||
| 1863 | |||
| 1864 | |||
| 1865 | public function allowRowsGroupAction(callable $condition) |
||
| 1869 | |||
| 1870 | |||
| 1871 | public function allowRowsAction($key, callable $condition) |
||
| 1875 | |||
| 1876 | |||
| 1877 | public function getRowCondition($name, $key = NULL) |
||
| 1891 | |||
| 1892 | |||
| 1893 | /******************************************************************************** |
||
| 1894 | * HIDEABLE COLUMNS * |
||
| 1895 | ********************************************************************************/ |
||
| 1896 | |||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Can datagrid hide colums? |
||
| 1900 | * @return boolean |
||
| 1901 | */ |
||
| 1902 | public function canHideColumns() |
||
| 1906 | |||
| 1907 | |||
| 1908 | /** |
||
| 1909 | * Order Grid to set columns hideable. |
||
| 1910 | * @param bool $do |
||
| 1911 | * @return static |
||
| 1912 | */ |
||
| 1913 | public function setColumnsHideable() |
||
| 1919 | |||
| 1920 | |||
| 1921 | /******************************************************************************** |
||
| 1922 | * INTERNAL * |
||
| 1923 | ********************************************************************************/ |
||
| 1924 | |||
| 1925 | |||
| 1926 | /** |
||
| 1927 | * Get cont of columns |
||
| 1928 | * @return int |
||
| 1929 | */ |
||
| 1930 | public function getColumnsCount() |
||
| 1944 | |||
| 1945 | |||
| 1946 | /** |
||
| 1947 | * Get primary key of datagrid data source |
||
| 1948 | * @return string |
||
| 1949 | */ |
||
| 1950 | public function getPrimaryKey() |
||
| 1954 | |||
| 1955 | |||
| 1956 | /** |
||
| 1957 | * Get set of set columns |
||
| 1958 | * @return Column\IColumn[] |
||
| 1959 | */ |
||
| 1960 | public function getColumns() |
||
| 1968 | |||
| 1969 | |||
| 1970 | |||
| 1971 | /** |
||
| 1972 | * @return PresenterComponent |
||
| 1973 | */ |
||
| 1974 | public function getParent() |
||
| 1985 | |||
| 1986 | } |
||
| 1987 |
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: