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 | * @todo Tell about this on github |
||
29 | */ |
||
30 | public static $icon_prefix = 'fa fa-'; |
||
31 | |||
32 | /** |
||
33 | * When set to TRUE, datagrid throws an exception |
||
34 | * when tring to get related entity within join and entity does not exist |
||
35 | * @var bool |
||
36 | */ |
||
37 | public $strict_entity_property = FALSE; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | * @persistent |
||
42 | */ |
||
43 | public $page = 1; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | * @persistent |
||
48 | */ |
||
49 | public $per_page; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | * @persistent |
||
54 | */ |
||
55 | public $sort = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | * @persistent |
||
60 | */ |
||
61 | public $filter = []; |
||
62 | |||
63 | /** |
||
64 | * @var Callable[] |
||
65 | */ |
||
66 | public $onRender = []; |
||
67 | |||
68 | /** |
||
69 | * @var Callable[] |
||
70 | */ |
||
71 | protected $rowCallback; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $items_per_page_list = [10, 20, 50]; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $template_file; |
||
82 | |||
83 | /** |
||
84 | * @var Column\IColumn[] |
||
85 | */ |
||
86 | protected $columns = []; |
||
87 | |||
88 | /** |
||
89 | * @var Column\Action[] |
||
90 | */ |
||
91 | protected $actions = []; |
||
92 | |||
93 | /** |
||
94 | * @var GroupAction\GroupActionCollection |
||
95 | */ |
||
96 | protected $group_action_collection; |
||
97 | |||
98 | /** |
||
99 | * @var Filter\Filter[] |
||
100 | */ |
||
101 | protected $filters = []; |
||
102 | |||
103 | /** |
||
104 | * @var Export\Export[] |
||
105 | */ |
||
106 | protected $exports = []; |
||
107 | |||
108 | /** |
||
109 | * @var DataModel |
||
110 | */ |
||
111 | protected $dataModel; |
||
112 | |||
113 | /** |
||
114 | * @var DataFilter |
||
115 | */ |
||
116 | protected $dataFilter; |
||
117 | |||
118 | /** |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $primary_key = 'id'; |
||
122 | |||
123 | /** |
||
124 | * @var bool |
||
125 | */ |
||
126 | protected $do_paginate = TRUE; |
||
127 | |||
128 | /** |
||
129 | * @var bool |
||
130 | */ |
||
131 | protected $csv_export = TRUE; |
||
132 | |||
133 | /** |
||
134 | * @var bool |
||
135 | */ |
||
136 | protected $csv_export_filtered = TRUE; |
||
137 | |||
138 | /** |
||
139 | * @var bool |
||
140 | */ |
||
141 | protected $sortable = FALSE; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $sortable_handler = 'sort!'; |
||
147 | |||
148 | /** |
||
149 | * @var string |
||
150 | */ |
||
151 | protected $original_template; |
||
152 | |||
153 | /** |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $redraw_item; |
||
157 | |||
158 | /** |
||
159 | * @var mixed |
||
160 | */ |
||
161 | protected $translator; |
||
162 | |||
163 | /** |
||
164 | * @var bool |
||
165 | */ |
||
166 | protected $force_filter_active; |
||
167 | |||
168 | /** |
||
169 | * @var callable |
||
170 | */ |
||
171 | protected $tree_view_children_callback; |
||
172 | |||
173 | /** |
||
174 | * @var string |
||
175 | */ |
||
176 | protected $tree_view_has_children_column; |
||
177 | |||
178 | /** |
||
179 | * @var bool |
||
180 | */ |
||
181 | protected $outer_filter_rendering = FALSE; |
||
182 | |||
183 | /** |
||
184 | * @var array |
||
185 | */ |
||
186 | protected $columns_export_order = []; |
||
187 | |||
188 | /** |
||
189 | * @var bool |
||
190 | */ |
||
191 | private $remember_state = TRUE; |
||
192 | |||
193 | /** |
||
194 | * @var bool |
||
195 | */ |
||
196 | private $refresh_url = TRUE; |
||
197 | |||
198 | /** |
||
199 | * @var Nette\Http\SessionSection |
||
200 | */ |
||
201 | private $grid_session; |
||
202 | |||
203 | /** |
||
204 | * @var array |
||
205 | */ |
||
206 | private $items_detail = []; |
||
207 | |||
208 | /** |
||
209 | * @var array |
||
210 | */ |
||
211 | private $row_conditions = [ |
||
212 | 'group_action' => FALSE, |
||
213 | 'action' => [] |
||
214 | ]; |
||
215 | |||
216 | /** |
||
217 | * @var bool |
||
218 | */ |
||
219 | protected $can_hide_columns = FALSE; |
||
220 | |||
221 | |||
222 | /** |
||
223 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
224 | * @param string $name |
||
225 | */ |
||
226 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
227 | { |
||
228 | parent::__construct($parent, $name); |
||
229 | |||
230 | $this->monitor('Nette\Application\UI\Presenter'); |
||
231 | } |
||
232 | |||
233 | |||
234 | /** |
||
235 | * {inheritDoc} |
||
236 | * @return void |
||
237 | */ |
||
238 | public function attached($presenter) |
||
239 | { |
||
240 | parent::attached($presenter); |
||
241 | |||
242 | if ($presenter instanceof Nette\Application\UI\Presenter) { |
||
243 | /** |
||
244 | * Get session |
||
245 | */ |
||
246 | $this->grid_session = $this->getPresenter()->getSession($this->getSessionSectionName()); |
||
|
|||
247 | |||
248 | /** |
||
249 | * Try to find previous filters/pagination/sort in session |
||
250 | */ |
||
251 | $this->findSessionFilters(); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | |||
256 | /******************************************************************************** |
||
257 | * RENDERING * |
||
258 | ********************************************************************************/ |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Render template |
||
263 | * @return void |
||
264 | */ |
||
265 | public function render() |
||
266 | { |
||
267 | /** |
||
268 | * Check whether datagrid has set some columns, initiated data source, etc |
||
269 | */ |
||
270 | if (!($this->dataModel instanceof DataModel)) { |
||
271 | throw new DataGridException('You have to set a data source first.'); |
||
272 | } |
||
273 | |||
274 | if (empty($this->columns)) { |
||
275 | throw new DataGridException('You have to add at least one column.'); |
||
276 | } |
||
277 | |||
278 | $this->template->setTranslator($this->getTranslator()); |
||
279 | |||
280 | /** |
||
281 | * Invoke some possible events |
||
282 | */ |
||
283 | $this->onRender($this); |
||
284 | |||
285 | /** |
||
286 | * Prepare data for rendering (datagrid may render just one item) |
||
287 | */ |
||
288 | $rows = []; |
||
289 | |||
290 | if (!empty($this->redraw_item)) { |
||
291 | $items = $this->dataModel->filterRow($this->redraw_item); |
||
292 | } else { |
||
293 | $items = Nette\Utils\Callback::invokeArgs( |
||
294 | [$this->dataModel, 'filterData'], |
||
295 | [ |
||
296 | $this->getPaginator(), |
||
297 | $this->sort, |
||
298 | $this->assableFilters() |
||
299 | ] |
||
300 | ); |
||
301 | } |
||
302 | |||
303 | $callback = $this->rowCallback ?: NULL; |
||
304 | |||
305 | foreach ($items as $item) { |
||
306 | $rows[] = $row = new Row($this, $item, $this->getPrimaryKey()); |
||
307 | |||
308 | if ($callback) { |
||
309 | $callback($item, $row->getControl()); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | if ($this->isTreeView()) { |
||
314 | $this->template->tree_view_has_children_column = $this->tree_view_has_children_column; |
||
315 | } |
||
316 | |||
317 | $this->template->rows = $rows; |
||
318 | |||
319 | $this->template->columns = $this->getColumns(); |
||
320 | $this->template->actions = $this->actions; |
||
321 | $this->template->exports = $this->exports; |
||
322 | $this->template->filters = $this->filters; |
||
323 | |||
324 | $this->template->filter_active = $this->isFilterActive(); |
||
325 | $this->template->original_template = $this->getOriginalTemplateFile(); |
||
326 | $this->template->icon_prefix = static::$icon_prefix; |
||
327 | $this->template->items_detail = $this->items_detail; |
||
328 | |||
329 | /** |
||
330 | * Walkaround for Latte (does not know $form in snippet in {form} etc) |
||
331 | */ |
||
332 | $this->template->filter = $this['filter']; |
||
333 | |||
334 | /** |
||
335 | * Set template file and render it |
||
336 | */ |
||
337 | $this->template->setFile($this->getTemplateFile())->render(); |
||
338 | } |
||
339 | |||
340 | |||
341 | /******************************************************************************** |
||
342 | * ROW CALLBACK * |
||
343 | ********************************************************************************/ |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Each row can be modified with user callback |
||
348 | * @param callable $callback |
||
349 | * @return static |
||
350 | */ |
||
351 | public function setRowCallback(callable $callback) |
||
352 | { |
||
353 | $this->rowCallback = $callback; |
||
354 | |||
355 | return $this; |
||
356 | } |
||
357 | |||
358 | |||
359 | /******************************************************************************** |
||
360 | * DATA SOURCE * |
||
361 | ********************************************************************************/ |
||
362 | |||
363 | |||
364 | /** |
||
365 | * By default ID, you can change that |
||
366 | * @param string $primary_key |
||
367 | */ |
||
368 | public function setPrimaryKey($primary_key) |
||
369 | { |
||
370 | $this->primary_key = $primary_key; |
||
371 | |||
372 | return $this; |
||
373 | } |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Set Grid data source |
||
378 | * @param DataSource\IDataSource|array|\DibiFluent|Nette\Database\Table\Selection|\Kdyby\Doctrine\QueryBuilder $source |
||
379 | * @return DataGrid |
||
380 | */ |
||
381 | public function setDataSource($source) |
||
382 | { |
||
383 | $this->dataModel = new DataModel($source, $this->primary_key); |
||
384 | |||
385 | return $this; |
||
386 | } |
||
387 | |||
388 | |||
389 | /******************************************************************************** |
||
390 | * TEMPLATING * |
||
391 | ********************************************************************************/ |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Set custom template file to render |
||
396 | * @param string $template_file |
||
397 | */ |
||
398 | public function setTemplateFile($template_file) |
||
399 | { |
||
400 | $this->template_file = $template_file; |
||
401 | |||
402 | return $this; |
||
403 | } |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Get DataGrid template file |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getTemplateFile() |
||
411 | { |
||
412 | return $this->template_file ?: $this->getOriginalTemplateFile(); |
||
413 | } |
||
414 | |||
415 | |||
416 | /** |
||
417 | * Get DataGrid original template file |
||
418 | * @return string |
||
419 | */ |
||
420 | public function getOriginalTemplateFile() |
||
421 | { |
||
422 | return __DIR__.'/templates/datagrid.latte'; |
||
423 | } |
||
424 | |||
425 | |||
426 | /******************************************************************************** |
||
427 | * SORTING * |
||
428 | ********************************************************************************/ |
||
429 | |||
430 | |||
431 | /** |
||
432 | * Set default sorting |
||
433 | * @param aray $sort |
||
434 | */ |
||
435 | public function setDefaultSort($sort) |
||
436 | { |
||
437 | if (empty($this->sort)) { |
||
438 | $this->sort = (array) $sort; |
||
439 | |||
440 | $this->saveSessionData('_grid_sort', $this->sort); |
||
441 | } |
||
442 | |||
443 | return $this; |
||
444 | } |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Set grido to be sortable |
||
449 | * @param bool $sortable |
||
450 | */ |
||
451 | public function setSortable($sortable = TRUE) |
||
452 | { |
||
453 | if ($this->getItemsDetail()) { |
||
454 | throw new DataGridException('You can not use both sortable datagrid and items detail.'); |
||
455 | } |
||
456 | |||
457 | $this->sortable = (bool) $sortable; |
||
458 | |||
459 | return $this; |
||
460 | } |
||
461 | |||
462 | |||
463 | /** |
||
464 | * Set sortable handle |
||
465 | * @param string $handle |
||
466 | */ |
||
467 | public function setSortableHandler($handler = 'sort!') |
||
468 | { |
||
469 | $this->sortable_handler = (string) $handler; |
||
470 | |||
471 | return $this; |
||
472 | } |
||
473 | |||
474 | |||
475 | /** |
||
476 | * Tell whether DataGrid is sortable |
||
477 | * @return bool |
||
478 | */ |
||
479 | public function isSortable() |
||
483 | |||
484 | /** |
||
485 | * Return sortable handle name |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getSortableHandler() |
||
489 | { |
||
490 | return $this->sortable_handler; |
||
491 | } |
||
492 | |||
493 | |||
494 | /******************************************************************************** |
||
495 | * TREE VIEW * |
||
496 | ********************************************************************************/ |
||
497 | |||
498 | |||
499 | /** |
||
500 | * Is tree view set? |
||
501 | * @return boolean |
||
502 | */ |
||
503 | public function isTreeView() |
||
504 | { |
||
505 | return (bool) $this->tree_view_children_callback; |
||
506 | } |
||
507 | |||
508 | |||
509 | /** |
||
510 | * Setting tree view |
||
511 | * @param callable $get_children_callback |
||
512 | * @param string $tree_view_has_children_column |
||
513 | * @return DataGrid |
||
514 | */ |
||
515 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
516 | { |
||
517 | if (!is_callable($get_children_callback)) { |
||
518 | throw new DataGridException( |
||
519 | 'Parameters to method DataGrid::setTreeView must be of type callable' |
||
520 | ); |
||
521 | } |
||
522 | |||
523 | $this->tree_view_children_callback = $get_children_callback; |
||
524 | $this->tree_view_has_children_column = $tree_view_has_children_column; |
||
525 | |||
526 | /** |
||
527 | * TUrn off pagination |
||
528 | */ |
||
529 | $this->setPagination(NULL); |
||
530 | |||
531 | /** |
||
532 | * Set tree view template file |
||
533 | */ |
||
534 | if (!$this->template_file) { |
||
535 | $this->setTemplateFile(__DIR__.'/templates/datagrid_tree.latte'); |
||
536 | } |
||
537 | |||
538 | return $this; |
||
539 | } |
||
540 | |||
541 | |||
542 | /******************************************************************************** |
||
543 | * COLUMNS * |
||
544 | ********************************************************************************/ |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Add text column with no other formating |
||
549 | * @param string $key |
||
550 | * @param string $name |
||
551 | * @param string|null $column |
||
552 | * @return Column\ColumnText |
||
553 | */ |
||
554 | public function addColumnText($key, $name, $column = NULL) |
||
555 | { |
||
556 | $this->addColumnCheck($key); |
||
557 | $column = $column ?: $key; |
||
558 | |||
559 | return $this->columns[$key] = new Column\ColumnText($column, $name); |
||
560 | } |
||
561 | |||
562 | |||
563 | /** |
||
564 | * Add column with link |
||
565 | * @param string $key |
||
566 | * @param string $name |
||
567 | * @param string|null $column |
||
568 | * @return Column\ColumnLink |
||
569 | */ |
||
570 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
571 | { |
||
572 | $this->addColumnCheck($key); |
||
573 | $column = $column ?: $key; |
||
574 | $href = $href ?: $key; |
||
575 | |||
576 | if (NULL === $params) { |
||
577 | $params = [$this->primary_key]; |
||
578 | } |
||
579 | |||
580 | return $this->columns[$key] = new Column\ColumnLink($this, $column, $name, $href, $params); |
||
581 | } |
||
582 | |||
583 | |||
584 | /** |
||
585 | * Add column with possible number formating |
||
586 | * @param string $key |
||
587 | * @param string $name |
||
588 | * @param string|null $column |
||
589 | * @return Column\ColumnNumber |
||
590 | */ |
||
591 | public function addColumnNumber($key, $name, $column = NULL) |
||
592 | { |
||
593 | $this->addColumnCheck($key); |
||
594 | $column = $column ?: $key; |
||
595 | |||
596 | return $this->columns[$key] = new Column\ColumnNumber($column, $name); |
||
597 | } |
||
598 | |||
599 | |||
600 | /** |
||
601 | * Add column with date formating |
||
602 | * @param string $key |
||
603 | * @param string $name |
||
604 | * @param string|null $column |
||
605 | * @return Column\ColumnDateTime |
||
606 | */ |
||
607 | public function addColumnDateTime($key, $name, $column = NULL) |
||
608 | { |
||
609 | $this->addColumnCheck($key); |
||
610 | $column = $column ?: $key; |
||
611 | |||
612 | return $this->columns[$key] = new Column\ColumnDateTime($column, $name); |
||
613 | } |
||
614 | |||
615 | |||
616 | /** |
||
617 | * Return existing column |
||
618 | * @param string $key |
||
619 | * @return Column\Column |
||
620 | * @throws DataGridException |
||
621 | */ |
||
622 | public function getColumn($key) |
||
623 | { |
||
624 | if (!isset($this->columns[$key])) { |
||
625 | throw new DataGridException("There is no column at key [$key] defined."); |
||
626 | } |
||
627 | |||
628 | return $this->columns[$key]; |
||
629 | } |
||
630 | |||
631 | |||
632 | /** |
||
633 | * Remove column |
||
634 | * @param string $key |
||
635 | * @return void |
||
636 | */ |
||
637 | public function removeColumn($key) |
||
638 | { |
||
639 | unset($this->columns[$key]); |
||
640 | } |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Check whether given key already exists in $this->columns |
||
645 | * @param string $key |
||
646 | * @throws DataGridException |
||
647 | */ |
||
648 | protected function addColumnCheck($key) |
||
649 | { |
||
650 | if (isset($this->columns[$key])) { |
||
651 | throw new DataGridException("There is already column at key [$key] defined."); |
||
652 | } |
||
653 | } |
||
654 | |||
655 | |||
656 | /******************************************************************************** |
||
657 | * ACTIONS * |
||
658 | ********************************************************************************/ |
||
659 | |||
660 | |||
661 | /** |
||
662 | * Create action |
||
663 | * @param string $key |
||
664 | * @param string $name |
||
665 | * @param string $href |
||
666 | * @param array|null $params |
||
667 | * @return Column\Action |
||
668 | */ |
||
669 | public function addAction($key, $name = '', $href = NULL, array $params = NULL) |
||
680 | |||
681 | |||
682 | /** |
||
683 | * Get existing action |
||
684 | * @param string $key |
||
685 | * @return Column\Action |
||
686 | * @throws DataGridException |
||
687 | */ |
||
688 | public function getAction($key) |
||
696 | |||
697 | |||
698 | /** |
||
699 | * Remove action |
||
700 | * @param string $key |
||
701 | * @return void |
||
702 | */ |
||
703 | public function removeAction($key) |
||
707 | |||
708 | |||
709 | /** |
||
710 | * Check whether given key already exists in $this->filters |
||
711 | * @param string $key |
||
712 | * @throws DataGridException |
||
713 | */ |
||
714 | protected function addActionCheck($key) |
||
715 | { |
||
716 | if (isset($this->actions[$key])) { |
||
717 | throw new DataGridException("There is already action at key [$key] defined."); |
||
718 | } |
||
719 | } |
||
720 | |||
721 | |||
722 | /******************************************************************************** |
||
723 | * FILTERS * |
||
724 | ********************************************************************************/ |
||
725 | |||
726 | |||
727 | /** |
||
728 | * Add filter fot text search |
||
729 | * @param string $key |
||
730 | * @param string $name |
||
731 | * @param array|string $columns |
||
732 | * @return Filter\FilterText |
||
733 | * @throws DataGridException |
||
734 | */ |
||
735 | public function addFilterText($key, $name, $columns = NULL) |
||
747 | |||
748 | |||
749 | /** |
||
750 | * Add select box filter |
||
751 | * @param string $key |
||
752 | * @param string $name |
||
753 | * @param array $options |
||
754 | * @param string $column |
||
755 | * @return Filter\FilterSelect |
||
756 | * @throws DataGridException |
||
757 | */ |
||
758 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
770 | |||
771 | |||
772 | /** |
||
773 | * Add datepicker filter |
||
774 | * @param string $key |
||
775 | * @param string $name |
||
776 | * @param string $column |
||
777 | * @return Filter\FilterDate |
||
778 | * @throws DataGridException |
||
779 | */ |
||
780 | public function addFilterDate($key, $name, $column = NULL) |
||
792 | |||
793 | |||
794 | /** |
||
795 | * Add range filter (from - to) |
||
796 | * @param string $key |
||
797 | * @param string $name |
||
798 | * @param string $column |
||
799 | * @return Filter\FilterRange |
||
800 | * @throws DataGridException |
||
801 | */ |
||
802 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
814 | |||
815 | |||
816 | /** |
||
817 | * Add datepicker filter (from - to) |
||
818 | * @param string $key |
||
819 | * @param string $name |
||
820 | * @param string $column |
||
821 | * @return Filter\FilterDateRange |
||
822 | * @throws DataGridException |
||
823 | */ |
||
824 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
836 | |||
837 | |||
838 | /** |
||
839 | * Check whether given key already exists in $this->filters |
||
840 | * @param string $key |
||
841 | * @throws DataGridException |
||
842 | */ |
||
843 | protected function addFilterCheck($key) |
||
849 | |||
850 | |||
851 | /** |
||
852 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
853 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
854 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
855 | */ |
||
856 | public function assableFilters() |
||
884 | |||
885 | |||
886 | /** |
||
887 | * Remove filter |
||
888 | * @param string $key |
||
889 | * @return void |
||
890 | */ |
||
891 | public function removeFilter($key) |
||
895 | |||
896 | |||
897 | /******************************************************************************** |
||
898 | * FILTERING * |
||
899 | ********************************************************************************/ |
||
900 | |||
901 | |||
902 | /** |
||
903 | * Is filter active? |
||
904 | * @return boolean |
||
905 | */ |
||
906 | public function isFilterActive() |
||
912 | |||
913 | |||
914 | /** |
||
915 | * Tell that filter is active from whatever reasons |
||
916 | * return static |
||
917 | */ |
||
918 | public function setFilterActive() |
||
924 | |||
925 | |||
926 | /** |
||
927 | * If we want to sent some initial filter |
||
928 | * @param array $filter |
||
929 | * @return static |
||
930 | */ |
||
931 | public function setFilter(array $filter) |
||
937 | |||
938 | |||
939 | /** |
||
940 | * FilterAndGroupAction form factory |
||
941 | * @return Form |
||
942 | */ |
||
943 | public function createComponentFilter() |
||
973 | |||
974 | |||
975 | /** |
||
976 | * Set $this->filter values after filter form submitted |
||
977 | * @param Form $form |
||
978 | * @return void |
||
979 | */ |
||
980 | public function filterSucceeded(Form $form) |
||
1006 | |||
1007 | |||
1008 | /** |
||
1009 | * Should be datagrid filters rendered separately? |
||
1010 | * @param boolean $out |
||
1011 | * @return static |
||
1012 | */ |
||
1013 | public function setOuterFilterRendering($out = TRUE) |
||
1019 | |||
1020 | |||
1021 | /** |
||
1022 | * Are datagrid filters rendered separately? |
||
1023 | * @return boolean |
||
1024 | */ |
||
1025 | public function hasOuterFilterRendering() |
||
1029 | |||
1030 | |||
1031 | /** |
||
1032 | * Try to restore session stuff |
||
1033 | * @return void |
||
1034 | */ |
||
1035 | public function findSessionFilters() |
||
1063 | |||
1064 | |||
1065 | /******************************************************************************** |
||
1066 | * EXPORTS * |
||
1067 | ********************************************************************************/ |
||
1068 | |||
1069 | |||
1070 | /** |
||
1071 | * Add export of type callback |
||
1072 | * @param string $text |
||
1073 | * @param callable $callback |
||
1074 | * @param boolean $filtered |
||
1075 | * @return Export\Export |
||
1076 | */ |
||
1077 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
1085 | |||
1086 | |||
1087 | /** |
||
1088 | * Add already implemented csv export |
||
1089 | * @param string $text |
||
1090 | * @param string $csv_file_name |
||
1091 | * @return Export\Export |
||
1092 | */ |
||
1093 | public function addExportCsv($text, $csv_file_name) |
||
1097 | |||
1098 | |||
1099 | /** |
||
1100 | * Add already implemented csv export, but for filtered data |
||
1101 | * @param string $text |
||
1102 | * @param string $csv_file_name |
||
1103 | * @return Export\Export |
||
1104 | */ |
||
1105 | public function addExportCsvFiltered($text, $csv_file_name) |
||
1109 | |||
1110 | |||
1111 | /** |
||
1112 | * Add export to array |
||
1113 | * @param Export\Export $export |
||
1114 | * @return Export\Export |
||
1115 | */ |
||
1116 | protected function addToExports(Export\Export $export) |
||
1124 | |||
1125 | |||
1126 | public function resetExportsLinks() |
||
1132 | |||
1133 | |||
1134 | /******************************************************************************** |
||
1135 | * GROUP ACTIONS * |
||
1136 | ********************************************************************************/ |
||
1137 | |||
1138 | |||
1139 | /** |
||
1140 | * Add group actino |
||
1141 | * @param string $title |
||
1142 | * @param array $options |
||
1143 | * @return GroupAction\GroupAction |
||
1144 | */ |
||
1145 | public function addGroupAction($title, $options = []) |
||
1149 | |||
1150 | |||
1151 | /** |
||
1152 | * Get collection of all group actions |
||
1153 | * @return GroupAction\GroupActionCollection |
||
1154 | */ |
||
1155 | public function getGroupActionCollection() |
||
1163 | |||
1164 | |||
1165 | /** |
||
1166 | * Has datagrid some group actions? |
||
1167 | * @return boolean |
||
1168 | */ |
||
1169 | public function hasGroupActions() |
||
1173 | |||
1174 | |||
1175 | /******************************************************************************** |
||
1176 | * HANDLERS * |
||
1177 | ********************************************************************************/ |
||
1178 | |||
1179 | |||
1180 | /** |
||
1181 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
1182 | * @param int $page |
||
1183 | * @return void |
||
1184 | */ |
||
1185 | public function handlePage($page) |
||
1195 | |||
1196 | |||
1197 | /** |
||
1198 | * Handler for sorting |
||
1199 | * @param array $sort |
||
1200 | * @return void |
||
1201 | */ |
||
1202 | public function handleSort(array $sort) |
||
1212 | |||
1213 | |||
1214 | /** |
||
1215 | * handler for reseting the filter |
||
1216 | * @return void |
||
1217 | */ |
||
1218 | public function handleResetFilter() |
||
1235 | |||
1236 | |||
1237 | /** |
||
1238 | * Handler for export |
||
1239 | * @param int $id Key for particular export class in array $this->exports |
||
1240 | * @return void |
||
1241 | */ |
||
1242 | public function handleExport($id) |
||
1286 | |||
1287 | |||
1288 | /** |
||
1289 | * Handler for getting children of parent item (e.g. category) |
||
1290 | * @param int $parent |
||
1291 | * @return void |
||
1292 | */ |
||
1293 | View Code Duplication | public function handleGetChildren($parent) |
|
1310 | |||
1311 | |||
1312 | /** |
||
1313 | * Handler for getting item detail |
||
1314 | * @param mixed $id |
||
1315 | * @return void |
||
1316 | */ |
||
1317 | View Code Duplication | public function handleGetItemDetail($id) |
|
1331 | |||
1332 | |||
1333 | /** |
||
1334 | * Handler for inline editing |
||
1335 | * @param mixed $id |
||
1336 | * @param mixed $key |
||
1337 | * @return void |
||
1338 | */ |
||
1339 | public function handleEdit($id, $key) |
||
1346 | |||
1347 | |||
1348 | /** |
||
1349 | * Redraw $this |
||
1350 | * @return void |
||
1351 | */ |
||
1352 | public function reload($snippets = []) |
||
1375 | |||
1376 | |||
1377 | /** |
||
1378 | * Redraw just one row via ajax |
||
1379 | * @param int $id |
||
1380 | * @param mixed $primary_where_column |
||
1381 | * @return void |
||
1382 | */ |
||
1383 | public function redrawItem($id, $primary_where_column = NULL) |
||
1392 | |||
1393 | |||
1394 | /** |
||
1395 | * Tell datagrid to display all columns |
||
1396 | * @return void |
||
1397 | */ |
||
1398 | public function handleShowAllColumns() |
||
1406 | |||
1407 | |||
1408 | /** |
||
1409 | * Notice datagrid to not display particular columns |
||
1410 | * @param string $column |
||
1411 | * @return void |
||
1412 | */ |
||
1413 | public function handleHideColumn($column) |
||
1432 | |||
1433 | |||
1434 | /******************************************************************************** |
||
1435 | * PAGINATION * |
||
1436 | ********************************************************************************/ |
||
1437 | |||
1438 | |||
1439 | /** |
||
1440 | * Set options of select "items_per_page" |
||
1441 | * @param array $items_per_page_list |
||
1442 | */ |
||
1443 | public function setItemsPerPageList(array $items_per_page_list) |
||
1449 | |||
1450 | |||
1451 | /** |
||
1452 | * Paginator factory |
||
1453 | * @return Components\DataGridPaginator\DataGridPaginator |
||
1454 | */ |
||
1455 | public function createComponentPaginator() |
||
1468 | |||
1469 | |||
1470 | /** |
||
1471 | * PerPage form factory |
||
1472 | * @return Form |
||
1473 | */ |
||
1474 | public function createComponentPerPage() |
||
1500 | |||
1501 | |||
1502 | /** |
||
1503 | * Get parameter per_page |
||
1504 | * @return int |
||
1505 | */ |
||
1506 | public function getPerPage() |
||
1516 | |||
1517 | |||
1518 | /** |
||
1519 | * Get associative array of items_per_page_list |
||
1520 | * @return array |
||
1521 | */ |
||
1522 | public function getItemsPerPageList() |
||
1534 | |||
1535 | |||
1536 | /** |
||
1537 | * Order Grid to "be paginated" |
||
1538 | * @param bool $do |
||
1539 | * @return static |
||
1540 | */ |
||
1541 | public function setPagination($do) |
||
1547 | |||
1548 | |||
1549 | /** |
||
1550 | * Tell whether Grid is paginated |
||
1551 | * @return bool |
||
1552 | */ |
||
1553 | public function isPaginated() |
||
1557 | |||
1558 | |||
1559 | /** |
||
1560 | * Return current paginator class |
||
1561 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
1562 | */ |
||
1563 | public function getPaginator() |
||
1571 | |||
1572 | |||
1573 | /******************************************************************************** |
||
1574 | * I18N * |
||
1575 | ********************************************************************************/ |
||
1576 | |||
1577 | |||
1578 | /** |
||
1579 | * Set datagrid translator |
||
1580 | * @param Nette\Localization\ITranslator $translator |
||
1581 | * @return static |
||
1582 | */ |
||
1583 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
1589 | |||
1590 | |||
1591 | /** |
||
1592 | * Get translator for datagrid |
||
1593 | * @return Nette\Localization\ITranslator |
||
1594 | */ |
||
1595 | public function getTranslator() |
||
1603 | |||
1604 | |||
1605 | /******************************************************************************** |
||
1606 | * COLUMNS ORDER * |
||
1607 | ********************************************************************************/ |
||
1608 | |||
1609 | |||
1610 | /** |
||
1611 | * Set order of datagrid columns |
||
1612 | * @param array $order |
||
1613 | * @return static |
||
1614 | */ |
||
1615 | public function setColumnsOrder($order) |
||
1633 | |||
1634 | |||
1635 | /** |
||
1636 | * Columns order may be different for export and normal grid |
||
1637 | * @param array $order |
||
1638 | */ |
||
1639 | public function setColumnsExportOrder($order) |
||
1643 | |||
1644 | |||
1645 | /******************************************************************************** |
||
1646 | * SESSION & URL * |
||
1647 | ********************************************************************************/ |
||
1648 | |||
1649 | |||
1650 | /** |
||
1651 | * Find some unique session key name |
||
1652 | * @return string |
||
1653 | */ |
||
1654 | public function getSessionSectionName() |
||
1658 | |||
1659 | |||
1660 | /** |
||
1661 | * Should datagrid remember its filters/pagination/etc using session? |
||
1662 | * @param bool $remember |
||
1663 | * @return static |
||
1664 | */ |
||
1665 | public function setRememberState($remember = TRUE) |
||
1671 | |||
1672 | |||
1673 | /** |
||
1674 | * Should datagrid refresh url using history API? |
||
1675 | * @param bool $refresh |
||
1676 | * @return static |
||
1677 | */ |
||
1678 | public function setRefreshUrl($refresh = TRUE) |
||
1685 | |||
1686 | |||
1687 | /** |
||
1688 | * Get session data if functionality is enabled |
||
1689 | * @param string $key |
||
1690 | * @return mixed |
||
1691 | */ |
||
1692 | public function getSessionData($key = NULL, $default_value = NULL) |
||
1700 | |||
1701 | |||
1702 | /** |
||
1703 | * Save session data - just if it is enabled |
||
1704 | * @param string $key |
||
1705 | * @param mixed $value |
||
1706 | * @return void |
||
1707 | */ |
||
1708 | public function saveSessionData($key, $value) |
||
1714 | |||
1715 | |||
1716 | /** |
||
1717 | * Delete session data |
||
1718 | * @return void |
||
1719 | */ |
||
1720 | public function deleteSesssionData($key) |
||
1724 | |||
1725 | |||
1726 | /******************************************************************************** |
||
1727 | * ITEM DETAIL * |
||
1728 | ********************************************************************************/ |
||
1729 | |||
1730 | |||
1731 | /** |
||
1732 | * Get items detail parameters |
||
1733 | * @return array |
||
1734 | */ |
||
1735 | public function getItemsDetail() |
||
1739 | |||
1740 | |||
1741 | /** |
||
1742 | * Items can have thair detail - toggled |
||
1743 | * @param mixed $detail callable|string|bool |
||
1744 | * @param bool|NULL $primary_where_column |
||
1745 | * @return Column\ItemDetail |
||
1746 | */ |
||
1747 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
1786 | |||
1787 | |||
1788 | /******************************************************************************** |
||
1789 | * ROW PRIVILEGES * |
||
1790 | ********************************************************************************/ |
||
1791 | |||
1792 | |||
1793 | public function allowRowsGroupAction(callable $condition) |
||
1797 | |||
1798 | |||
1799 | public function allowRowsAction($key, callable $condition) |
||
1803 | |||
1804 | |||
1805 | public function getRowCondition($name, $key = NULL) |
||
1819 | |||
1820 | |||
1821 | /******************************************************************************** |
||
1822 | * HIDEABLE COLUMNS * |
||
1823 | ********************************************************************************/ |
||
1824 | |||
1825 | |||
1826 | /** |
||
1827 | * Can datagrid hide colums? |
||
1828 | * @return boolean |
||
1829 | */ |
||
1830 | public function canHideColumns() |
||
1834 | |||
1835 | |||
1836 | /** |
||
1837 | * Order Grid to set columns hideable. |
||
1838 | * @param bool $do |
||
1839 | * @return static |
||
1840 | */ |
||
1841 | public function setColumnsHideable() |
||
1847 | |||
1848 | |||
1849 | /******************************************************************************** |
||
1850 | * INTERNAL * |
||
1851 | ********************************************************************************/ |
||
1852 | |||
1853 | |||
1854 | /** |
||
1855 | * Get cont of columns |
||
1856 | * @return int |
||
1857 | */ |
||
1858 | public function getColumnsCount() |
||
1872 | |||
1873 | |||
1874 | /** |
||
1875 | * Get primary key of datagrid data source |
||
1876 | * @return string |
||
1877 | */ |
||
1878 | public function getPrimaryKey() |
||
1882 | |||
1883 | |||
1884 | /** |
||
1885 | * Get set of set columns |
||
1886 | * @return Column\IColumn[] |
||
1887 | */ |
||
1888 | public function getColumns() |
||
1896 | |||
1897 | |||
1898 | |||
1899 | /** |
||
1900 | * @return PresenterComponent |
||
1901 | */ |
||
1902 | public function getParent() |
||
1913 | |||
1914 | } |
||
1915 |
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: