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 |
||
15 | class DataGrid extends Nette\Application\UI\Control |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var callable[] |
||
20 | */ |
||
21 | public $onRedraw; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | * @todo Tell about this on github |
||
26 | */ |
||
27 | public static $icon_prefix = 'fa fa-'; |
||
28 | |||
29 | /** |
||
30 | * When set to TRUE, datagrid throws an exception |
||
31 | * when tring to get related entity within join and entity does not exist |
||
32 | * @var bool |
||
33 | */ |
||
34 | public $strict_entity_property = FALSE; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | * @persistent |
||
39 | */ |
||
40 | public $page = 1; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | * @persistent |
||
45 | */ |
||
46 | public $per_page; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | * @persistent |
||
51 | */ |
||
52 | public $sort = []; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | * @persistent |
||
57 | */ |
||
58 | public $filter = []; |
||
59 | |||
60 | /** |
||
61 | * @var Callable[] |
||
62 | */ |
||
63 | public $onRender = []; |
||
64 | |||
65 | /** |
||
66 | * @var Callable[] |
||
67 | */ |
||
68 | protected $rowCallback; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $items_per_page_list = [10, 20, 50]; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $template_file; |
||
79 | |||
80 | /** |
||
81 | * @var Column\IColumn[] |
||
82 | */ |
||
83 | protected $columns = []; |
||
84 | |||
85 | /** |
||
86 | * @var Column\Action[] |
||
87 | */ |
||
88 | protected $actions = []; |
||
89 | |||
90 | /** |
||
91 | * @var GroupAction\GroupActionCollection |
||
92 | */ |
||
93 | protected $group_action_collection; |
||
94 | |||
95 | /** |
||
96 | * @var Filter\Filter[] |
||
97 | */ |
||
98 | protected $filters = []; |
||
99 | |||
100 | /** |
||
101 | * @var Export\Export[] |
||
102 | */ |
||
103 | protected $exports = []; |
||
104 | |||
105 | /** |
||
106 | * @var DataModel |
||
107 | */ |
||
108 | protected $dataModel; |
||
109 | |||
110 | /** |
||
111 | * @var DataFilter |
||
112 | */ |
||
113 | protected $dataFilter; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $primary_key = 'id'; |
||
119 | |||
120 | /** |
||
121 | * @var bool |
||
122 | */ |
||
123 | protected $do_paginate = TRUE; |
||
124 | |||
125 | /** |
||
126 | * @var bool |
||
127 | */ |
||
128 | protected $csv_export = TRUE; |
||
129 | |||
130 | /** |
||
131 | * @var bool |
||
132 | */ |
||
133 | protected $csv_export_filtered = TRUE; |
||
134 | |||
135 | /** |
||
136 | * @var bool |
||
137 | */ |
||
138 | protected $sortable = FALSE; |
||
139 | |||
140 | /** |
||
141 | * @var string |
||
142 | */ |
||
143 | protected $sortable_handler = 'sort!'; |
||
144 | |||
145 | /** |
||
146 | * @var string |
||
147 | */ |
||
148 | protected $original_template; |
||
149 | |||
150 | /** |
||
151 | * @var array |
||
152 | */ |
||
153 | protected $redraw_item; |
||
154 | |||
155 | /** |
||
156 | * @var mixed |
||
157 | */ |
||
158 | protected $translator; |
||
159 | |||
160 | /** |
||
161 | * @var bool |
||
162 | */ |
||
163 | protected $force_filter_active; |
||
164 | |||
165 | /** |
||
166 | * @var callable |
||
167 | */ |
||
168 | protected $tree_view_children_callback; |
||
169 | |||
170 | /** |
||
171 | * @var string |
||
172 | */ |
||
173 | protected $tree_view_has_children_column; |
||
174 | |||
175 | /** |
||
176 | * @var bool |
||
177 | */ |
||
178 | protected $outer_filter_rendering = FALSE; |
||
179 | |||
180 | /** |
||
181 | * @var array |
||
182 | */ |
||
183 | protected $columns_export_order = []; |
||
184 | |||
185 | /** |
||
186 | * @var bool |
||
187 | */ |
||
188 | private $remember_state = TRUE; |
||
189 | |||
190 | /** |
||
191 | * @var bool |
||
192 | */ |
||
193 | private $refresh_url = TRUE; |
||
194 | |||
195 | /** |
||
196 | * @var Nette\Http\SessionSection |
||
197 | */ |
||
198 | private $grid_session; |
||
199 | |||
200 | /** |
||
201 | * @var array |
||
202 | */ |
||
203 | private $items_detail = []; |
||
204 | |||
205 | /** |
||
206 | * @var array |
||
207 | */ |
||
208 | private $row_conditions = [ |
||
209 | 'group_action' => FALSE, |
||
210 | 'action' => [] |
||
211 | ]; |
||
212 | |||
213 | /** |
||
214 | * @var bool |
||
215 | */ |
||
216 | protected $can_hide_columns = FALSE; |
||
217 | |||
218 | |||
219 | /** |
||
220 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
221 | * @param string $name |
||
222 | */ |
||
223 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
224 | { |
||
225 | parent::__construct($parent, $name); |
||
226 | |||
227 | $this->monitor('Nette\Application\UI\Presenter'); |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * {inheritDoc} |
||
233 | * @return void |
||
234 | */ |
||
235 | public function attached($presenter) |
||
236 | { |
||
237 | parent::attached($presenter); |
||
238 | |||
239 | if ($presenter instanceof Nette\Application\UI\Presenter) { |
||
240 | /** |
||
241 | * Get session |
||
242 | */ |
||
243 | $this->grid_session = $this->getPresenter()->getSession($this->getSessionSectionName()); |
||
|
|||
244 | |||
245 | /** |
||
246 | * Try to find previous filters/pagination/sort in session |
||
247 | */ |
||
248 | $this->findSessionFilters(); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | |||
253 | /******************************************************************************** |
||
254 | * RENDERING * |
||
255 | ********************************************************************************/ |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Render template |
||
260 | * @return void |
||
261 | */ |
||
262 | public function render() |
||
263 | { |
||
264 | /** |
||
265 | * Check whether datagrid has set some columns, initiated data source, etc |
||
266 | */ |
||
267 | if (!($this->dataModel instanceof DataModel)) { |
||
268 | throw new DataGridException('You have to set a data source first.'); |
||
269 | } |
||
270 | |||
271 | if (empty($this->columns)) { |
||
272 | throw new DataGridException('You have to add at least one column.'); |
||
273 | } |
||
274 | |||
275 | $this->template->setTranslator($this->getTranslator()); |
||
276 | |||
277 | /** |
||
278 | * Invoke some possible events |
||
279 | */ |
||
280 | $this->onRender($this); |
||
281 | |||
282 | /** |
||
283 | * Prepare data for rendering (datagrid may render just one item) |
||
284 | */ |
||
285 | $rows = []; |
||
286 | |||
287 | if (!empty($this->redraw_item)) { |
||
288 | $items = $this->dataModel->filterRow($this->redraw_item); |
||
289 | } else { |
||
290 | $items = Nette\Utils\Callback::invokeArgs( |
||
291 | [$this->dataModel, 'filterData'], |
||
292 | [ |
||
293 | $this->getPaginator(), |
||
294 | $this->sort, |
||
295 | $this->assableFilters() |
||
296 | ] |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | $callback = $this->rowCallback ?: NULL; |
||
301 | |||
302 | foreach ($items as $item) { |
||
303 | $rows[] = $row = new Row($this, $item, $this->getPrimaryKey()); |
||
304 | |||
305 | if ($callback) { |
||
306 | $callback($item, $row->getControl()); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | if ($this->isTreeView()) { |
||
311 | $this->template->tree_view_has_children_column = $this->tree_view_has_children_column; |
||
312 | } |
||
313 | |||
314 | $this->template->rows = $rows; |
||
315 | |||
316 | $this->template->columns = $this->getColumns(); |
||
317 | $this->template->actions = $this->actions; |
||
318 | $this->template->exports = $this->exports; |
||
319 | $this->template->filters = $this->filters; |
||
320 | |||
321 | $this->template->filter_active = $this->isFilterActive(); |
||
322 | $this->template->original_template = $this->getOriginalTemplateFile(); |
||
323 | $this->template->icon_prefix = static::$icon_prefix; |
||
324 | $this->template->items_detail = $this->items_detail; |
||
325 | |||
326 | /** |
||
327 | * Walkaround for Latte (does not know $form in snippet in {form} etc) |
||
328 | */ |
||
329 | $this->template->filter = $this['filter']; |
||
330 | |||
331 | /** |
||
332 | * Set template file and render it |
||
333 | */ |
||
334 | $this->template->setFile($this->getTemplateFile())->render(); |
||
335 | } |
||
336 | |||
337 | |||
338 | /******************************************************************************** |
||
339 | * ROW CALLBACK * |
||
340 | ********************************************************************************/ |
||
341 | |||
342 | |||
343 | /** |
||
344 | * Each row can be modified with user callback |
||
345 | * @param callable $callback |
||
346 | * @return static |
||
347 | */ |
||
348 | public function setRowCallback(callable $callback) |
||
349 | { |
||
350 | $this->rowCallback = $callback; |
||
351 | |||
352 | return $this; |
||
353 | } |
||
354 | |||
355 | |||
356 | /******************************************************************************** |
||
357 | * DATA SOURCE * |
||
358 | ********************************************************************************/ |
||
359 | |||
360 | |||
361 | /** |
||
362 | * By default ID, you can change that |
||
363 | * @param string $primary_key |
||
364 | */ |
||
365 | public function setPrimaryKey($primary_key) |
||
366 | { |
||
367 | $this->primary_key = $primary_key; |
||
368 | |||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | |||
373 | /** |
||
374 | * Set Grid data source |
||
375 | * @param DataSource\IDataSource|array|\DibiFluent $source |
||
376 | * @return DataGrid |
||
377 | */ |
||
378 | public function setDataSource($source) |
||
379 | { |
||
380 | if ($source instanceof DataSource\IDataSource) { |
||
381 | // $source is ready for interact |
||
382 | |||
383 | } else if (is_array($source)) { |
||
384 | $data_source = new DataSource\ArrayDataSource($source); |
||
385 | |||
386 | } else if ($source instanceof \DibiFluent) { |
||
387 | $driver = $source->getConnection()->getDriver(); |
||
388 | |||
389 | if ($driver instanceof \DibiOdbcDriver) { |
||
390 | $data_source = new DataSource\DibiFluentMssqlDataSource($source, $this->primary_key); |
||
391 | |||
392 | } else if ($driver instanceof \DibiMsSqlDriver) { |
||
393 | $data_source = new DataSource\DibiFluentMssqlDataSource($source, $this->primary_key); |
||
394 | |||
395 | } else { |
||
396 | $data_source = new DataSource\DibiFluentDataSource($source, $this->primary_key); |
||
397 | } |
||
398 | |||
399 | } else if ($source instanceof Nette\Database\Table\Selection) { |
||
400 | $data_source = new DataSource\NetteDatabaseTableDataSource($source, $this->primary_key); |
||
401 | |||
402 | } else if ($source instanceof \Kdyby\Doctrine\QueryBuilder) { |
||
403 | $data_source = new DataSource\DoctrineDataSource($source, $this->primary_key); |
||
404 | |||
405 | } else { |
||
406 | $data_source_class = $source ? get_class($source) : 'NULL'; |
||
407 | throw new DataGridException("DataGrid can not take [$data_source_class] as data source."); |
||
408 | } |
||
409 | |||
410 | $this->dataModel = new DataModel($data_source); |
||
411 | |||
412 | return $this; |
||
413 | } |
||
414 | |||
415 | |||
416 | /******************************************************************************** |
||
417 | * TEMPLATING * |
||
418 | ********************************************************************************/ |
||
419 | |||
420 | |||
421 | /** |
||
422 | * Set custom template file to render |
||
423 | * @param string $template_file |
||
424 | */ |
||
425 | public function setTemplateFile($template_file) |
||
431 | |||
432 | |||
433 | /** |
||
434 | * Get DataGrid template file |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getTemplateFile() |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Get DataGrid original template file |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getOriginalTemplateFile() |
||
451 | |||
452 | |||
453 | /******************************************************************************** |
||
454 | * SORTING * |
||
455 | ********************************************************************************/ |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Set default sorting |
||
460 | * @param aray $sort |
||
461 | */ |
||
462 | public function setDefaultSort($sort) |
||
470 | |||
471 | |||
472 | /** |
||
473 | * Set grido to be sortable |
||
474 | * @param bool $sortable |
||
475 | */ |
||
476 | public function setSortable($sortable = TRUE) |
||
486 | |||
487 | |||
488 | /** |
||
489 | * Set sortable handle |
||
490 | * @param string $handle |
||
491 | */ |
||
492 | public function setSortableHandler($handler = 'sort!') |
||
498 | |||
499 | |||
500 | /** |
||
501 | * Tell whether DataGrid is sortable |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function isSortable() |
||
508 | |||
509 | /** |
||
510 | * Return sortable handle name |
||
511 | * @return string |
||
512 | */ |
||
513 | public function getSortableHandler() |
||
517 | |||
518 | |||
519 | /******************************************************************************** |
||
520 | * TREE VIEW * |
||
521 | ********************************************************************************/ |
||
522 | |||
523 | |||
524 | /** |
||
525 | * Is tree view set? |
||
526 | * @return boolean |
||
527 | */ |
||
528 | public function isTreeView() |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Setting tree view |
||
536 | * @param callable $get_children_callback |
||
537 | * @param string $tree_view_has_children_column |
||
538 | */ |
||
539 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
564 | |||
565 | |||
566 | /******************************************************************************** |
||
567 | * COLUMNS * |
||
568 | ********************************************************************************/ |
||
569 | |||
570 | |||
571 | /** |
||
572 | * Add text column with no other formating |
||
573 | * @param string $key |
||
574 | * @param string $name |
||
575 | * @param string|null $column |
||
576 | * @return Column\Column |
||
577 | */ |
||
578 | public function addColumnText($key, $name, $column = NULL) |
||
585 | |||
586 | |||
587 | /** |
||
588 | * Add column with link |
||
589 | * @param string $key |
||
590 | * @param string $name |
||
591 | * @param string|null $column |
||
592 | * @return Column\Column |
||
593 | */ |
||
594 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
606 | |||
607 | |||
608 | /** |
||
609 | * Add column with possible number formating |
||
610 | * @param string $key |
||
611 | * @param string $name |
||
612 | * @param string|null $column |
||
613 | * @return Column\Column |
||
614 | */ |
||
615 | public function addColumnNumber($key, $name, $column = NULL) |
||
622 | |||
623 | |||
624 | /** |
||
625 | * Add column with date formating |
||
626 | * @param string $key |
||
627 | * @param string $name |
||
628 | * @param string|null $column |
||
629 | * @return Column\Column |
||
630 | */ |
||
631 | public function addColumnDateTime($key, $name, $column = NULL) |
||
638 | |||
639 | |||
640 | /** |
||
641 | * Return existing column |
||
642 | * @param string $key |
||
643 | * @return Column\Column |
||
644 | * @throws DataGridException |
||
645 | */ |
||
646 | public function getColumn($key) |
||
654 | |||
655 | |||
656 | /** |
||
657 | * Remove column |
||
658 | * @param string $key |
||
659 | * @return void |
||
660 | */ |
||
661 | public function removeColumn($key) |
||
665 | |||
666 | |||
667 | /** |
||
668 | * Check whether given key already exists in $this->columns |
||
669 | * @param string $key |
||
670 | * @throws DataGridException |
||
671 | */ |
||
672 | protected function addColumnCheck($key) |
||
678 | |||
679 | |||
680 | /******************************************************************************** |
||
681 | * ACTIONS * |
||
682 | ********************************************************************************/ |
||
683 | |||
684 | |||
685 | /** |
||
686 | * Create action |
||
687 | * @param string $key |
||
688 | * @param string $name |
||
689 | * @param string $href |
||
690 | * @param array|null $params |
||
691 | */ |
||
692 | public function addAction($key, $name = '', $href = NULL, array $params = NULL) |
||
703 | |||
704 | |||
705 | /** |
||
706 | * Get existing action |
||
707 | * @param string $key |
||
708 | * @return Column\Action |
||
709 | * @throws DataGridException |
||
710 | */ |
||
711 | public function getAction($key) |
||
719 | |||
720 | |||
721 | /** |
||
722 | * Remove action |
||
723 | * @param string $key |
||
724 | * @return void |
||
725 | */ |
||
726 | public function removeAction($key) |
||
730 | |||
731 | |||
732 | /** |
||
733 | * Check whether given key already exists in $this->filters |
||
734 | * @param string $key |
||
735 | * @throws DataGridException |
||
736 | */ |
||
737 | protected function addActionCheck($key) |
||
743 | |||
744 | |||
745 | /******************************************************************************** |
||
746 | * FILTERS * |
||
747 | ********************************************************************************/ |
||
748 | |||
749 | |||
750 | /** |
||
751 | * Add filter fot text search |
||
752 | * @param string $key |
||
753 | * @param string $name |
||
754 | * @param array|string $columns |
||
755 | * @throws DataGridException |
||
756 | */ |
||
757 | public function addFilterText($key, $name, $columns = NULL) |
||
769 | |||
770 | |||
771 | /** |
||
772 | * Add select box filter |
||
773 | * @param string $key |
||
774 | * @param string $name |
||
775 | * @param array $options |
||
776 | * @param string $column |
||
777 | * @throws DataGridException |
||
778 | */ |
||
779 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
791 | |||
792 | |||
793 | /** |
||
794 | * Add datepicker filter |
||
795 | * @param string $key |
||
796 | * @param string $name |
||
797 | * @param string $column |
||
798 | * @throws DataGridException |
||
799 | */ |
||
800 | public function addFilterDate($key, $name, $column = NULL) |
||
812 | |||
813 | |||
814 | /** |
||
815 | * Add range filter (from - to) |
||
816 | * @param string $key |
||
817 | * @param string $name |
||
818 | * @param string $column |
||
819 | * @throws DataGridException |
||
820 | */ |
||
821 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
833 | |||
834 | |||
835 | /** |
||
836 | * Add datepicker filter (from - to) |
||
837 | * @param string $key |
||
838 | * @param string $name |
||
839 | * @param string $column |
||
840 | * @throws DataGridException |
||
841 | */ |
||
842 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
854 | |||
855 | |||
856 | /** |
||
857 | * Check whether given key already exists in $this->filters |
||
858 | * @param string $key |
||
859 | * @throws DataGridException |
||
860 | */ |
||
861 | protected function addFilterCheck($key) |
||
867 | |||
868 | |||
869 | /** |
||
870 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
871 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
872 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
873 | */ |
||
874 | public function assableFilters() |
||
902 | |||
903 | |||
904 | /** |
||
905 | * Remove filter |
||
906 | * @param string $key |
||
907 | * @return void |
||
908 | */ |
||
909 | public function removeFilter($key) |
||
913 | |||
914 | |||
915 | /******************************************************************************** |
||
916 | * FILTERING * |
||
917 | ********************************************************************************/ |
||
918 | |||
919 | |||
920 | /** |
||
921 | * Is filter active? |
||
922 | * @return boolean |
||
923 | */ |
||
924 | public function isFilterActive() |
||
930 | |||
931 | |||
932 | /** |
||
933 | * Tell that filter is active from whatever reasons |
||
934 | * return self |
||
935 | */ |
||
936 | public function setFilterActive() |
||
942 | |||
943 | |||
944 | /** |
||
945 | * If we want to sent some initial filter |
||
946 | * @param array $filter |
||
947 | */ |
||
948 | public function setFilter(array $filter) |
||
954 | |||
955 | |||
956 | /** |
||
957 | * FilterAndGroupAction form factory |
||
958 | * @return Form |
||
959 | */ |
||
960 | public function createComponentFilter() |
||
990 | |||
991 | |||
992 | /** |
||
993 | * Set $this->filter values after filter form submitted |
||
994 | * @param Form $form |
||
995 | * @return void |
||
996 | */ |
||
997 | public function filterSucceeded(Form $form) |
||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * Should be datagrid filters rendered separately? |
||
1027 | * @param boolean $out |
||
1028 | */ |
||
1029 | public function setOuterFilterRendering($out = TRUE) |
||
1035 | |||
1036 | |||
1037 | /** |
||
1038 | * Are datagrid filters rendered separately? |
||
1039 | * @return boolean |
||
1040 | */ |
||
1041 | public function hasOuterFilterRendering() |
||
1045 | |||
1046 | |||
1047 | /** |
||
1048 | * Try to restore session stuff |
||
1049 | * @return void |
||
1050 | */ |
||
1051 | public function findSessionFilters() |
||
1052 | { |
||
1053 | if ($this->filter || ($this->page != 1) || $this->sort || $this->per_page) { |
||
1054 | return; |
||
1055 | } |
||
1056 | |||
1057 | if (!$this->remember_state) { |
||
1058 | return; |
||
1059 | } |
||
1060 | |||
1061 | if ($page = $this->getSessionData('_grid_page')) { |
||
1062 | $this->page = $page; |
||
1063 | } |
||
1064 | |||
1065 | if ($per_page = $this->getSessionData('_grid_per_page')) { |
||
1066 | $this->per_page = $per_page; |
||
1067 | } |
||
1068 | |||
1069 | if ($sort = $this->getSessionData('_grid_sort')) { |
||
1070 | $this->sort = $sort; |
||
1071 | } |
||
1072 | |||
1073 | foreach ($this->getSessionData() as $key => $value) { |
||
1074 | if (!in_array($key, ['_grid_per_page', '_grid_sort', '_grid_page', '_grid_hidden_columns'])) { |
||
1075 | $this->filter[$key] = $value; |
||
1076 | } |
||
1077 | } |
||
1078 | } |
||
1079 | |||
1080 | |||
1081 | /******************************************************************************** |
||
1082 | * EXPORTS * |
||
1083 | ********************************************************************************/ |
||
1084 | |||
1085 | |||
1086 | /** |
||
1087 | * Add export of type callback |
||
1088 | * @param string $text |
||
1089 | * @param callable $callback |
||
1090 | * @param boolean $filtered |
||
1091 | */ |
||
1092 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
1100 | |||
1101 | |||
1102 | /** |
||
1103 | * Add already implemented csv export |
||
1104 | * @param string $text |
||
1105 | * @param string $csv_file_name |
||
1106 | */ |
||
1107 | public function addExportCsv($text, $csv_file_name) |
||
1111 | |||
1112 | |||
1113 | /** |
||
1114 | * Add already implemented csv export, but for filtered data |
||
1115 | * @param string $text |
||
1116 | * @param string $csv_file_name |
||
1117 | */ |
||
1118 | public function addExportCsvFiltered($text, $csv_file_name) |
||
1122 | |||
1123 | |||
1124 | /** |
||
1125 | * Add export to array |
||
1126 | * @param Export\Export $export |
||
1127 | */ |
||
1128 | protected function addToExports(Export\Export $export) |
||
1136 | |||
1137 | |||
1138 | public function resetExportsLinks() |
||
1144 | |||
1145 | |||
1146 | /******************************************************************************** |
||
1147 | * GROUP ACTIONS * |
||
1148 | ********************************************************************************/ |
||
1149 | |||
1150 | |||
1151 | /** |
||
1152 | * Add group actino |
||
1153 | * @param string $title |
||
1154 | * @param array $options |
||
1155 | */ |
||
1156 | public function addGroupAction($title, $options = []) |
||
1160 | |||
1161 | |||
1162 | /** |
||
1163 | * Get collection of all group actions |
||
1164 | * @return GroupAction\GroupActionCollection |
||
1165 | */ |
||
1166 | public function getGroupActionCollection() |
||
1174 | |||
1175 | |||
1176 | /** |
||
1177 | * Has datagrid some group actions? |
||
1178 | * @return boolean |
||
1179 | */ |
||
1180 | public function hasGroupActions() |
||
1184 | |||
1185 | |||
1186 | /******************************************************************************** |
||
1187 | * HANDLERS * |
||
1188 | ********************************************************************************/ |
||
1189 | |||
1190 | |||
1191 | /** |
||
1192 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
1193 | * @param int $page |
||
1194 | * @return void |
||
1195 | */ |
||
1196 | public function handlePage($page) |
||
1206 | |||
1207 | |||
1208 | /** |
||
1209 | * Handler for sorting |
||
1210 | * @return void |
||
1211 | */ |
||
1212 | public function handleSort(array $sort) |
||
1222 | |||
1223 | |||
1224 | /** |
||
1225 | * handler for reseting the filter |
||
1226 | * @return void |
||
1227 | */ |
||
1228 | public function handleResetFilter() |
||
1229 | { |
||
1230 | /** |
||
1231 | * Session stuff |
||
1232 | */ |
||
1233 | $this->deleteSesssionData('_grid_page'); |
||
1234 | |||
1235 | foreach ($this->getSessionData() as $key => $value) { |
||
1236 | if (!in_array($key, ['_grid_per_page', '_grid_sort', '_grid_page'])) { |
||
1237 | $this->deleteSesssionData($key); |
||
1238 | } |
||
1239 | } |
||
1240 | |||
1241 | $this->filter = []; |
||
1242 | |||
1243 | $this->reload(['grid']); |
||
1244 | } |
||
1245 | |||
1246 | |||
1247 | /** |
||
1248 | * Handler for export |
||
1249 | * @param int $id Key for particular export class in array $this->exports |
||
1250 | * @return void |
||
1251 | */ |
||
1252 | public function handleExport($id) |
||
1296 | |||
1297 | |||
1298 | /** |
||
1299 | * Handler for getting children of parent item (e.g. category) |
||
1300 | * @param int $parent |
||
1301 | * @return void |
||
1302 | */ |
||
1303 | View Code Duplication | public function handleGetChildren($parent) |
|
1320 | |||
1321 | |||
1322 | /** |
||
1323 | * Handler for getting item detail |
||
1324 | * @param mixed $id |
||
1325 | * @return void |
||
1326 | */ |
||
1327 | View Code Duplication | public function handleGetItemDetail($id) |
|
1341 | |||
1342 | |||
1343 | /** |
||
1344 | * Handler for inline editing |
||
1345 | * @param mixed $id |
||
1346 | * @param mixed $key |
||
1347 | * @return void |
||
1348 | */ |
||
1349 | public function handleEdit($id, $key) |
||
1356 | |||
1357 | |||
1358 | /** |
||
1359 | * Redraw $this |
||
1360 | * @return void |
||
1361 | */ |
||
1362 | public function reload($snippets = []) |
||
1385 | |||
1386 | |||
1387 | /** |
||
1388 | * Redraw just one row via ajax |
||
1389 | * @param int $id |
||
1390 | * @param mixed $primary_where_column |
||
1391 | * @return void |
||
1392 | */ |
||
1393 | public function redrawItem($id, $primary_where_column = NULL) |
||
1402 | |||
1403 | |||
1404 | /** |
||
1405 | * Tell datagrid to display all columns |
||
1406 | * @return void |
||
1407 | */ |
||
1408 | public function handleShowAllColumns() |
||
1416 | |||
1417 | |||
1418 | /** |
||
1419 | * Notice datagrid to not display particular columns |
||
1420 | * @param string $column |
||
1421 | * @return void |
||
1422 | */ |
||
1423 | public function handleHideColumn($column) |
||
1442 | |||
1443 | |||
1444 | /******************************************************************************** |
||
1445 | * PAGINATION * |
||
1446 | ********************************************************************************/ |
||
1447 | |||
1448 | |||
1449 | /** |
||
1450 | * Set options of select "items_per_page" |
||
1451 | * @param array $items_per_page_list |
||
1452 | */ |
||
1453 | public function setItemsPerPageList(array $items_per_page_list) |
||
1459 | |||
1460 | |||
1461 | /** |
||
1462 | * Paginator factory |
||
1463 | * @return Components\DataGridPaginator\DataGridPaginator |
||
1464 | */ |
||
1465 | public function createComponentPaginator() |
||
1478 | |||
1479 | |||
1480 | /** |
||
1481 | * PerPage form factory |
||
1482 | * @return Form |
||
1483 | */ |
||
1484 | public function createComponentPerPage() |
||
1510 | |||
1511 | |||
1512 | /** |
||
1513 | * Get parameter per_page |
||
1514 | * @return int |
||
1515 | */ |
||
1516 | public function getPerPage() |
||
1526 | |||
1527 | |||
1528 | /** |
||
1529 | * Get associative array of items_per_page_list |
||
1530 | * @return array |
||
1531 | */ |
||
1532 | public function getItemsPerPageList() |
||
1544 | |||
1545 | |||
1546 | /** |
||
1547 | * Order Grid to "be paginated" |
||
1548 | * @param bool $do |
||
1549 | */ |
||
1550 | public function setPagination($do) |
||
1556 | |||
1557 | |||
1558 | /** |
||
1559 | * Tell whether Grid is paginated |
||
1560 | * @return bool |
||
1561 | */ |
||
1562 | public function isPaginated() |
||
1566 | |||
1567 | |||
1568 | /** |
||
1569 | * Return current paginator class |
||
1570 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
1571 | */ |
||
1572 | public function getPaginator() |
||
1580 | |||
1581 | |||
1582 | /******************************************************************************** |
||
1583 | * I18N * |
||
1584 | ********************************************************************************/ |
||
1585 | |||
1586 | |||
1587 | /** |
||
1588 | * Set datagrid translator |
||
1589 | * @param Nette\Localization\ITranslator $translator |
||
1590 | */ |
||
1591 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
1597 | |||
1598 | |||
1599 | /** |
||
1600 | * Get translator for datagrid |
||
1601 | * @return Nette\Localization\ITranslator |
||
1602 | */ |
||
1603 | public function getTranslator() |
||
1611 | |||
1612 | |||
1613 | /******************************************************************************** |
||
1614 | * COLUMNS ORDER * |
||
1615 | ********************************************************************************/ |
||
1616 | |||
1617 | |||
1618 | /** |
||
1619 | * Set order of datagrid columns |
||
1620 | * @param array $order |
||
1621 | */ |
||
1622 | public function setColumnsOrder($order) |
||
1640 | |||
1641 | |||
1642 | /** |
||
1643 | * Columns order may be different for export and normal grid |
||
1644 | * @param array $order |
||
1645 | */ |
||
1646 | public function setColumnsExportOrder($order) |
||
1650 | |||
1651 | |||
1652 | /******************************************************************************** |
||
1653 | * SESSION & URL * |
||
1654 | ********************************************************************************/ |
||
1655 | |||
1656 | |||
1657 | /** |
||
1658 | * Find some unique session key name |
||
1659 | * @return string |
||
1660 | */ |
||
1661 | public function getSessionSectionName() |
||
1665 | |||
1666 | |||
1667 | /** |
||
1668 | * Should datagrid remember its filters/pagination/etc using session? |
||
1669 | * @param bool $remember |
||
1670 | */ |
||
1671 | public function setRememberState($remember = TRUE) |
||
1677 | |||
1678 | |||
1679 | /** |
||
1680 | * Should datagrid refresh url using history API? |
||
1681 | * @param bool $refresh |
||
1682 | */ |
||
1683 | public function setRefreshUrl($refresh = TRUE) |
||
1690 | |||
1691 | |||
1692 | /** |
||
1693 | * Get session data if functionality is enabled |
||
1694 | * @param string $key |
||
1695 | * @return mixed |
||
1696 | */ |
||
1697 | public function getSessionData($key = NULL, $default_value = NULL) |
||
1705 | |||
1706 | |||
1707 | /** |
||
1708 | * Save session data - just if it is enabled |
||
1709 | * @param string $key |
||
1710 | * @param mixed $value |
||
1711 | * @return void |
||
1712 | */ |
||
1713 | public function saveSessionData($key, $value) |
||
1719 | |||
1720 | |||
1721 | /** |
||
1722 | * Delete session data |
||
1723 | * @return void |
||
1724 | */ |
||
1725 | public function deleteSesssionData($key) |
||
1729 | |||
1730 | |||
1731 | /******************************************************************************** |
||
1732 | * ITEM DETAIL * |
||
1733 | ********************************************************************************/ |
||
1734 | |||
1735 | |||
1736 | /** |
||
1737 | * Get items detail parameters |
||
1738 | * @return array |
||
1739 | */ |
||
1740 | public function getItemsDetail() |
||
1744 | |||
1745 | |||
1746 | /** |
||
1747 | * Items can have thair detail - toggled |
||
1748 | * @param mixed $detail callable|string|bool |
||
1749 | */ |
||
1750 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
1789 | |||
1790 | |||
1791 | /******************************************************************************** |
||
1792 | * ROW PRIVILEGES * |
||
1793 | ********************************************************************************/ |
||
1794 | |||
1795 | |||
1796 | public function allowRowsGroupAction(callable $condition) |
||
1800 | |||
1801 | |||
1802 | public function allowRowsAction($key, callable $condition) |
||
1806 | |||
1807 | |||
1808 | public function getRowCondition($name, $key = NULL) |
||
1822 | |||
1823 | |||
1824 | /******************************************************************************** |
||
1825 | * HIDEABLE COLUMNS * |
||
1826 | ********************************************************************************/ |
||
1827 | |||
1828 | |||
1829 | /** |
||
1830 | * Can datagrid hide colums? |
||
1831 | * @return boolean |
||
1832 | */ |
||
1833 | public function canHideColumns() |
||
1837 | |||
1838 | |||
1839 | /** |
||
1840 | * Order Grid to set columns hideable. |
||
1841 | * @param bool $do |
||
1842 | */ |
||
1843 | public function setColumnsHideable() |
||
1849 | |||
1850 | |||
1851 | /******************************************************************************** |
||
1852 | * INTERNAL * |
||
1853 | ********************************************************************************/ |
||
1854 | |||
1855 | |||
1856 | /** |
||
1857 | * Get cont of columns |
||
1858 | * @return int |
||
1859 | */ |
||
1860 | public function getColumnsCount() |
||
1874 | |||
1875 | |||
1876 | /** |
||
1877 | * Get primary key of datagrid data source |
||
1878 | * @return string |
||
1879 | */ |
||
1880 | public function getPrimaryKey() |
||
1884 | |||
1885 | |||
1886 | /** |
||
1887 | * Get set of set columns |
||
1888 | * @return Column\IColumn[] |
||
1889 | */ |
||
1890 | public function getColumns() |
||
1898 | |||
1899 | } |
||
1900 | |||
1905 |
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: