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