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) |
||
| 239 | { |
||
| 240 | parent::__construct($parent, $name); |
||
| 241 | |||
| 242 | $this->monitor('Nette\Application\UI\Presenter'); |
||
| 243 | } |
||
| 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() |
||
| 279 | { |
||
| 280 | /** |
||
| 281 | * Check whether datagrid has set some columns, initiated data source, etc |
||
| 282 | */ |
||
| 283 | if (!($this->dataModel instanceof DataModel)) { |
||
| 284 | throw new DataGridException('You have to set a data source first.'); |
||
| 285 | } |
||
| 286 | |||
| 287 | if (empty($this->columns)) { |
||
| 288 | throw new DataGridException('You have to add at least one column.'); |
||
| 289 | } |
||
| 290 | |||
| 291 | $this->template->setTranslator($this->getTranslator()); |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Invoke some possible events |
||
| 295 | */ |
||
| 296 | $this->onRender($this); |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Prepare data for rendering (datagrid may render just one item) |
||
| 300 | */ |
||
| 301 | $rows = []; |
||
| 302 | |||
| 303 | if (!empty($this->redraw_item)) { |
||
| 304 | $items = $this->dataModel->filterRow($this->redraw_item); |
||
| 305 | } else { |
||
| 306 | $items = Nette\Utils\Callback::invokeArgs( |
||
| 307 | [$this->dataModel, 'filterData'], |
||
| 308 | [ |
||
| 309 | $this->getPaginator(), |
||
| 310 | $this->sort, |
||
| 311 | $this->assableFilters() |
||
| 312 | ] |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | $callback = $this->rowCallback ?: NULL; |
||
| 317 | |||
| 318 | foreach ($items as $item) { |
||
| 319 | $rows[] = $row = new Row($this, $item, $this->getPrimaryKey()); |
||
| 320 | |||
| 321 | if ($callback) { |
||
| 322 | $callback($item, $row->getControl()); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | if ($this->isTreeView()) { |
||
| 327 | $this->template->add('tree_view_has_children_column', $this->tree_view_has_children_column); |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->template->add('rows', $rows); |
||
| 331 | |||
| 332 | $this->template->add('columns', $this->getColumns()); |
||
| 333 | $this->template->add('actions', $this->actions); |
||
| 334 | $this->template->add('exports', $this->exports); |
||
| 335 | $this->template->add('filters', $this->filters); |
||
| 336 | |||
| 337 | $this->template->add('filter_active', $this->isFilterActive()); |
||
| 338 | $this->template->add('original_template', $this->getOriginalTemplateFile()); |
||
| 339 | $this->template->add('icon_prefix', static::$icon_prefix); |
||
| 340 | $this->template->add('items_detail', $this->items_detail); |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Walkaround for Latte (does not know $form in snippet in {form} etc) |
||
| 344 | */ |
||
| 345 | $this->template->add('filter', $this['filter']); |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set template file and render it |
||
| 349 | */ |
||
| 350 | $this->template->setFile($this->getTemplateFile())->render(); |
||
| 351 | } |
||
| 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) |
||
| 383 | { |
||
| 384 | if ($this->dataModel instanceof DataModel) { |
||
| 385 | throw new DataGridException('Please set datagrid primary key before setting datasource.'); |
||
| 386 | } |
||
| 387 | |||
| 388 | $this->primary_key = $primary_key; |
||
| 389 | |||
| 390 | return $this; |
||
| 391 | } |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Set Grid data source |
||
| 396 | * @param DataSource\IDataSource|array|\DibiFluent|Nette\Database\Table\Selection|\Doctrine\ORM\QueryBuilder $source |
||
| 397 | * @return static |
||
| 398 | */ |
||
| 399 | public function setDataSource($source) |
||
| 400 | { |
||
| 401 | $this->dataModel = new DataModel($source, $this->primary_key); |
||
| 402 | |||
| 403 | return $this; |
||
| 404 | } |
||
| 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) |
||
| 418 | { |
||
| 419 | $this->template_file = $template_file; |
||
| 420 | |||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Get DataGrid template file |
||
| 427 | * @return string |
||
| 428 | * @return static |
||
| 429 | */ |
||
| 430 | public function getTemplateFile() |
||
| 431 | { |
||
| 432 | return $this->template_file ?: $this->getOriginalTemplateFile(); |
||
| 433 | } |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * Get DataGrid original template file |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getOriginalTemplateFile() |
||
| 441 | { |
||
| 442 | return __DIR__.'/templates/datagrid.latte'; |
||
| 443 | } |
||
| 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) |
||
| 509 | { |
||
| 510 | if ($this->getItemsDetail()) { |
||
| 511 | throw new DataGridException('You can not use both sortable datagrid and items detail.'); |
||
| 512 | } |
||
| 513 | |||
| 514 | $this->sortable = (bool) $sortable; |
||
| 515 | |||
| 516 | return $this; |
||
| 517 | } |
||
| 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() |
||
| 538 | { |
||
| 539 | return $this->sortable; |
||
| 540 | } |
||
| 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 | * Add column status |
||
| 676 | * @param string $key |
||
| 677 | * @param string $name |
||
| 678 | * @param string|null $column |
||
| 679 | * @return Column\ColumnStatus |
||
| 680 | */ |
||
| 681 | public function addColumnStatus($key, $name, $column = NULL) |
||
| 688 | |||
| 689 | |||
| 690 | /** |
||
| 691 | * @param string $key |
||
| 692 | * @param Column\Column $column |
||
| 693 | * @return Column\Column |
||
| 694 | */ |
||
| 695 | protected function addColumn($key, Column\Column $column) |
||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * Return existing column |
||
| 705 | * @param string $key |
||
| 706 | * @return Column\Column |
||
| 707 | * @throws DataGridException |
||
| 708 | */ |
||
| 709 | public function getColumn($key) |
||
| 717 | |||
| 718 | |||
| 719 | /** |
||
| 720 | * Remove column |
||
| 721 | * @param string $key |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | public function removeColumn($key) |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * Check whether given key already exists in $this->columns |
||
| 732 | * @param string $key |
||
| 733 | * @throws DataGridException |
||
| 734 | */ |
||
| 735 | protected function addColumnCheck($key) |
||
| 741 | |||
| 742 | |||
| 743 | /******************************************************************************** |
||
| 744 | * ACTIONS * |
||
| 745 | ********************************************************************************/ |
||
| 746 | |||
| 747 | |||
| 748 | /** |
||
| 749 | * Create action |
||
| 750 | * @param string $key |
||
| 751 | * @param string $name |
||
| 752 | * @param string $href |
||
| 753 | * @param array|null $params |
||
| 754 | * @return Column\Action |
||
| 755 | */ |
||
| 756 | public function addAction($key, $name, $href = NULL, array $params = NULL) |
||
| 767 | |||
| 768 | |||
| 769 | /** |
||
| 770 | * Create action callback |
||
| 771 | * @param string $key |
||
| 772 | * @param string $name |
||
| 773 | * @return Column\Action |
||
| 774 | */ |
||
| 775 | public function addActionCallback($key, $name, $callback = NULL) |
||
| 792 | |||
| 793 | |||
| 794 | /** |
||
| 795 | * Get existing action |
||
| 796 | * @param string $key |
||
| 797 | * @return Column\Action |
||
| 798 | * @throws DataGridException |
||
| 799 | */ |
||
| 800 | public function getAction($key) |
||
| 808 | |||
| 809 | |||
| 810 | /** |
||
| 811 | * Remove action |
||
| 812 | * @param string $key |
||
| 813 | * @return void |
||
| 814 | */ |
||
| 815 | public function removeAction($key) |
||
| 819 | |||
| 820 | |||
| 821 | /** |
||
| 822 | * Check whether given key already exists in $this->filters |
||
| 823 | * @param string $key |
||
| 824 | * @throws DataGridException |
||
| 825 | */ |
||
| 826 | protected function addActionCheck($key) |
||
| 832 | |||
| 833 | |||
| 834 | /******************************************************************************** |
||
| 835 | * FILTERS * |
||
| 836 | ********************************************************************************/ |
||
| 837 | |||
| 838 | |||
| 839 | /** |
||
| 840 | * Add filter fot text search |
||
| 841 | * @param string $key |
||
| 842 | * @param string $name |
||
| 843 | * @param array|string $columns |
||
| 844 | * @return Filter\FilterText |
||
| 845 | * @throws DataGridException |
||
| 846 | */ |
||
| 847 | public function addFilterText($key, $name, $columns = NULL) |
||
| 859 | |||
| 860 | |||
| 861 | /** |
||
| 862 | * Add select box filter |
||
| 863 | * @param string $key |
||
| 864 | * @param string $name |
||
| 865 | * @param array $options |
||
| 866 | * @param string $column |
||
| 867 | * @return Filter\FilterSelect |
||
| 868 | * @throws DataGridException |
||
| 869 | */ |
||
| 870 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
| 882 | |||
| 883 | |||
| 884 | /** |
||
| 885 | * Add datepicker filter |
||
| 886 | * @param string $key |
||
| 887 | * @param string $name |
||
| 888 | * @param string $column |
||
| 889 | * @return Filter\FilterDate |
||
| 890 | * @throws DataGridException |
||
| 891 | */ |
||
| 892 | public function addFilterDate($key, $name, $column = NULL) |
||
| 904 | |||
| 905 | |||
| 906 | /** |
||
| 907 | * Add range filter (from - to) |
||
| 908 | * @param string $key |
||
| 909 | * @param string $name |
||
| 910 | * @param string $column |
||
| 911 | * @return Filter\FilterRange |
||
| 912 | * @throws DataGridException |
||
| 913 | */ |
||
| 914 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
| 926 | |||
| 927 | |||
| 928 | /** |
||
| 929 | * Add datepicker filter (from - to) |
||
| 930 | * @param string $key |
||
| 931 | * @param string $name |
||
| 932 | * @param string $column |
||
| 933 | * @return Filter\FilterDateRange |
||
| 934 | * @throws DataGridException |
||
| 935 | */ |
||
| 936 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
| 948 | |||
| 949 | |||
| 950 | /** |
||
| 951 | * Check whether given key already exists in $this->filters |
||
| 952 | * @param string $key |
||
| 953 | * @throws DataGridException |
||
| 954 | */ |
||
| 955 | protected function addFilterCheck($key) |
||
| 961 | |||
| 962 | |||
| 963 | /** |
||
| 964 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
| 965 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
| 966 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
| 967 | */ |
||
| 968 | public function assableFilters() |
||
| 996 | |||
| 997 | |||
| 998 | /** |
||
| 999 | * Remove filter |
||
| 1000 | * @param string $key |
||
| 1001 | * @return void |
||
| 1002 | */ |
||
| 1003 | public function removeFilter($key) |
||
| 1007 | |||
| 1008 | |||
| 1009 | /******************************************************************************** |
||
| 1010 | * FILTERING * |
||
| 1011 | ********************************************************************************/ |
||
| 1012 | |||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Is filter active? |
||
| 1016 | * @return boolean |
||
| 1017 | */ |
||
| 1018 | public function isFilterActive() |
||
| 1024 | |||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * Tell that filter is active from whatever reasons |
||
| 1028 | * return static |
||
| 1029 | */ |
||
| 1030 | public function setFilterActive() |
||
| 1036 | |||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * If we want to sent some initial filter |
||
| 1040 | * @param array $filter |
||
| 1041 | * @return static |
||
| 1042 | */ |
||
| 1043 | public function setFilter(array $filter) |
||
| 1049 | |||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * FilterAndGroupAction form factory |
||
| 1053 | * @return Form |
||
| 1054 | */ |
||
| 1055 | public function createComponentFilter() |
||
| 1087 | |||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Set $this->filter values after filter form submitted |
||
| 1091 | * @param Form $form |
||
| 1092 | * @return void |
||
| 1093 | */ |
||
| 1094 | public function filterSucceeded(Form $form) |
||
| 1120 | |||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Should be datagrid filters rendered separately? |
||
| 1124 | * @param boolean $out |
||
| 1125 | * @return static |
||
| 1126 | */ |
||
| 1127 | public function setOuterFilterRendering($out = TRUE) |
||
| 1133 | |||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Are datagrid filters rendered separately? |
||
| 1137 | * @return boolean |
||
| 1138 | */ |
||
| 1139 | public function hasOuterFilterRendering() |
||
| 1143 | |||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Try to restore session stuff |
||
| 1147 | * @return void |
||
| 1148 | */ |
||
| 1149 | public function findSessionFilters() |
||
| 1177 | |||
| 1178 | |||
| 1179 | /******************************************************************************** |
||
| 1180 | * EXPORTS * |
||
| 1181 | ********************************************************************************/ |
||
| 1182 | |||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Add export of type callback |
||
| 1186 | * @param string $text |
||
| 1187 | * @param callable $callback |
||
| 1188 | * @param boolean $filtered |
||
| 1189 | * @return Export\Export |
||
| 1190 | */ |
||
| 1191 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
| 1199 | |||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Add already implemented csv export |
||
| 1203 | * @param string $text |
||
| 1204 | * @param string $csv_file_name |
||
| 1205 | * @return Export\Export |
||
| 1206 | */ |
||
| 1207 | public function addExportCsv($text, $csv_file_name) |
||
| 1211 | |||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Add already implemented csv export, but for filtered data |
||
| 1215 | * @param string $text |
||
| 1216 | * @param string $csv_file_name |
||
| 1217 | * @return Export\Export |
||
| 1218 | */ |
||
| 1219 | public function addExportCsvFiltered($text, $csv_file_name) |
||
| 1223 | |||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Add export to array |
||
| 1227 | * @param Export\Export $export |
||
| 1228 | * @return Export\Export |
||
| 1229 | */ |
||
| 1230 | protected function addToExports(Export\Export $export) |
||
| 1238 | |||
| 1239 | |||
| 1240 | public function resetExportsLinks() |
||
| 1246 | |||
| 1247 | |||
| 1248 | /******************************************************************************** |
||
| 1249 | * GROUP ACTIONS * |
||
| 1250 | ********************************************************************************/ |
||
| 1251 | |||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Add group actino |
||
| 1255 | * @param string $title |
||
| 1256 | * @param array $options |
||
| 1257 | * @return GroupAction\GroupAction |
||
| 1258 | */ |
||
| 1259 | public function addGroupAction($title, $options = []) |
||
| 1263 | |||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Get collection of all group actions |
||
| 1267 | * @return GroupAction\GroupActionCollection |
||
| 1268 | */ |
||
| 1269 | public function getGroupActionCollection() |
||
| 1277 | |||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Has datagrid some group actions? |
||
| 1281 | * @return boolean |
||
| 1282 | */ |
||
| 1283 | public function hasGroupActions() |
||
| 1287 | |||
| 1288 | |||
| 1289 | /******************************************************************************** |
||
| 1290 | * HANDLERS * |
||
| 1291 | ********************************************************************************/ |
||
| 1292 | |||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
| 1296 | * @param int $page |
||
| 1297 | * @return void |
||
| 1298 | */ |
||
| 1299 | public function handlePage($page) |
||
| 1309 | |||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * Handler for sorting |
||
| 1313 | * @param array $sort |
||
| 1314 | * @return void |
||
| 1315 | */ |
||
| 1316 | public function handleSort(array $sort) |
||
| 1326 | |||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * handler for reseting the filter |
||
| 1330 | * @return void |
||
| 1331 | */ |
||
| 1332 | public function handleResetFilter() |
||
| 1349 | |||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Handler for export |
||
| 1353 | * @param int $id Key for particular export class in array $this->exports |
||
| 1354 | * @return void |
||
| 1355 | */ |
||
| 1356 | public function handleExport($id) |
||
| 1400 | |||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * Handler for getting children of parent item (e.g. category) |
||
| 1404 | * @param int $parent |
||
| 1405 | * @return void |
||
| 1406 | */ |
||
| 1407 | public function handleGetChildren($parent) |
||
| 1424 | |||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * Handler for getting item detail |
||
| 1428 | * @param mixed $id |
||
| 1429 | * @return void |
||
| 1430 | */ |
||
| 1431 | public function handleGetItemDetail($id) |
||
| 1445 | |||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Handler for inline editing |
||
| 1449 | * @param mixed $id |
||
| 1450 | * @param mixed $key |
||
| 1451 | * @return void |
||
| 1452 | */ |
||
| 1453 | public function handleEdit($id, $key) |
||
| 1460 | |||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Redraw $this |
||
| 1464 | * @return void |
||
| 1465 | */ |
||
| 1466 | public function reload($snippets = []) |
||
| 1489 | |||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Handler for column status |
||
| 1493 | * @param string $id |
||
| 1494 | * @param string $key |
||
| 1495 | * @param string $value |
||
| 1496 | * @return void |
||
| 1497 | */ |
||
| 1498 | public function handleChangeStatus($id, $key, $value) |
||
| 1510 | |||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * Redraw just one row via ajax |
||
| 1514 | * @param int $id |
||
| 1515 | * @param mixed $primary_where_column |
||
| 1516 | * @return void |
||
| 1517 | */ |
||
| 1518 | public function redrawItem($id, $primary_where_column = NULL) |
||
| 1527 | |||
| 1528 | |||
| 1529 | /** |
||
| 1530 | * Tell datagrid to display all columns |
||
| 1531 | * @return void |
||
| 1532 | */ |
||
| 1533 | public function handleShowAllColumns() |
||
| 1541 | |||
| 1542 | |||
| 1543 | /** |
||
| 1544 | * Notice datagrid to not display particular columns |
||
| 1545 | * @param string $column |
||
| 1546 | * @return void |
||
| 1547 | */ |
||
| 1548 | public function handleHideColumn($column) |
||
| 1567 | |||
| 1568 | |||
| 1569 | public function handleActionCallback($__key, $__id) |
||
| 1579 | |||
| 1580 | |||
| 1581 | /******************************************************************************** |
||
| 1582 | * PAGINATION * |
||
| 1583 | ********************************************************************************/ |
||
| 1584 | |||
| 1585 | |||
| 1586 | /** |
||
| 1587 | * Set options of select "items_per_page" |
||
| 1588 | * @param array $items_per_page_list |
||
| 1589 | * @return static |
||
| 1590 | */ |
||
| 1591 | public function setItemsPerPageList(array $items_per_page_list, $include_all = TRUE) |
||
| 1601 | |||
| 1602 | |||
| 1603 | /** |
||
| 1604 | * Paginator factory |
||
| 1605 | * @return Components\DataGridPaginator\DataGridPaginator |
||
| 1606 | */ |
||
| 1607 | public function createComponentPaginator() |
||
| 1622 | |||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * PerPage form factory |
||
| 1626 | * @return Form |
||
| 1627 | */ |
||
| 1628 | public function createComponentPerPage() |
||
| 1654 | |||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Get parameter per_page |
||
| 1658 | * @return int |
||
| 1659 | */ |
||
| 1660 | public function getPerPage() |
||
| 1672 | |||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Get associative array of items_per_page_list |
||
| 1676 | * @return array |
||
| 1677 | */ |
||
| 1678 | public function getItemsPerPageList() |
||
| 1696 | |||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * Order Grid to "be paginated" |
||
| 1700 | * @param bool $do |
||
| 1701 | * @return static |
||
| 1702 | */ |
||
| 1703 | public function setPagination($do) |
||
| 1709 | |||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Tell whether Grid is paginated |
||
| 1713 | * @return bool |
||
| 1714 | */ |
||
| 1715 | public function isPaginated() |
||
| 1719 | |||
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Return current paginator class |
||
| 1723 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
| 1724 | */ |
||
| 1725 | public function getPaginator() |
||
| 1733 | |||
| 1734 | |||
| 1735 | /******************************************************************************** |
||
| 1736 | * I18N * |
||
| 1737 | ********************************************************************************/ |
||
| 1738 | |||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Set datagrid translator |
||
| 1742 | * @param Nette\Localization\ITranslator $translator |
||
| 1743 | * @return static |
||
| 1744 | */ |
||
| 1745 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
| 1751 | |||
| 1752 | |||
| 1753 | /** |
||
| 1754 | * Get translator for datagrid |
||
| 1755 | * @return Nette\Localization\ITranslator |
||
| 1756 | */ |
||
| 1757 | public function getTranslator() |
||
| 1765 | |||
| 1766 | |||
| 1767 | /******************************************************************************** |
||
| 1768 | * COLUMNS ORDER * |
||
| 1769 | ********************************************************************************/ |
||
| 1770 | |||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Set order of datagrid columns |
||
| 1774 | * @param array $order |
||
| 1775 | * @return static |
||
| 1776 | */ |
||
| 1777 | public function setColumnsOrder($order) |
||
| 1795 | |||
| 1796 | |||
| 1797 | /** |
||
| 1798 | * Columns order may be different for export and normal grid |
||
| 1799 | * @param array $order |
||
| 1800 | */ |
||
| 1801 | public function setColumnsExportOrder($order) |
||
| 1805 | |||
| 1806 | |||
| 1807 | /******************************************************************************** |
||
| 1808 | * SESSION & URL * |
||
| 1809 | ********************************************************************************/ |
||
| 1810 | |||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Find some unique session key name |
||
| 1814 | * @return string |
||
| 1815 | */ |
||
| 1816 | public function getSessionSectionName() |
||
| 1820 | |||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * Should datagrid remember its filters/pagination/etc using session? |
||
| 1824 | * @param bool $remember |
||
| 1825 | * @return static |
||
| 1826 | */ |
||
| 1827 | public function setRememberState($remember = TRUE) |
||
| 1833 | |||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Should datagrid refresh url using history API? |
||
| 1837 | * @param bool $refresh |
||
| 1838 | * @return static |
||
| 1839 | */ |
||
| 1840 | public function setRefreshUrl($refresh = TRUE) |
||
| 1847 | |||
| 1848 | |||
| 1849 | /** |
||
| 1850 | * Get session data if functionality is enabled |
||
| 1851 | * @param string $key |
||
| 1852 | * @return mixed |
||
| 1853 | */ |
||
| 1854 | public function getSessionData($key = NULL, $default_value = NULL) |
||
| 1862 | |||
| 1863 | |||
| 1864 | /** |
||
| 1865 | * Save session data - just if it is enabled |
||
| 1866 | * @param string $key |
||
| 1867 | * @param mixed $value |
||
| 1868 | * @return void |
||
| 1869 | */ |
||
| 1870 | public function saveSessionData($key, $value) |
||
| 1876 | |||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Delete session data |
||
| 1880 | * @return void |
||
| 1881 | */ |
||
| 1882 | public function deleteSesssionData($key) |
||
| 1886 | |||
| 1887 | |||
| 1888 | /******************************************************************************** |
||
| 1889 | * ITEM DETAIL * |
||
| 1890 | ********************************************************************************/ |
||
| 1891 | |||
| 1892 | |||
| 1893 | /** |
||
| 1894 | * Get items detail parameters |
||
| 1895 | * @return array |
||
| 1896 | */ |
||
| 1897 | public function getItemsDetail() |
||
| 1901 | |||
| 1902 | |||
| 1903 | /** |
||
| 1904 | * Items can have thair detail - toggled |
||
| 1905 | * @param mixed $detail callable|string|bool |
||
| 1906 | * @param bool|NULL $primary_where_column |
||
| 1907 | * @return Column\ItemDetail |
||
| 1908 | */ |
||
| 1909 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
| 1948 | |||
| 1949 | |||
| 1950 | /******************************************************************************** |
||
| 1951 | * ROW PRIVILEGES * |
||
| 1952 | ********************************************************************************/ |
||
| 1953 | |||
| 1954 | |||
| 1955 | public function allowRowsGroupAction(callable $condition) |
||
| 1959 | |||
| 1960 | |||
| 1961 | public function allowRowsAction($key, callable $condition) |
||
| 1965 | |||
| 1966 | |||
| 1967 | public function getRowCondition($name, $key = NULL) |
||
| 1981 | |||
| 1982 | |||
| 1983 | /******************************************************************************** |
||
| 1984 | * HIDEABLE COLUMNS * |
||
| 1985 | ********************************************************************************/ |
||
| 1986 | |||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * Can datagrid hide colums? |
||
| 1990 | * @return boolean |
||
| 1991 | */ |
||
| 1992 | public function canHideColumns() |
||
| 1996 | |||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * Order Grid to set columns hideable. |
||
| 2000 | * @param bool $do |
||
| 2001 | * @return static |
||
| 2002 | */ |
||
| 2003 | public function setColumnsHideable() |
||
| 2009 | |||
| 2010 | |||
| 2011 | /******************************************************************************** |
||
| 2012 | * INTERNAL * |
||
| 2013 | ********************************************************************************/ |
||
| 2014 | |||
| 2015 | |||
| 2016 | /** |
||
| 2017 | * Get cont of columns |
||
| 2018 | * @return int |
||
| 2019 | */ |
||
| 2020 | public function getColumnsCount() |
||
| 2034 | |||
| 2035 | |||
| 2036 | /** |
||
| 2037 | * Get primary key of datagrid data source |
||
| 2038 | * @return string |
||
| 2039 | */ |
||
| 2040 | public function getPrimaryKey() |
||
| 2044 | |||
| 2045 | |||
| 2046 | /** |
||
| 2047 | * Get set of set columns |
||
| 2048 | * @return Column\IColumn[] |
||
| 2049 | */ |
||
| 2050 | public function getColumns() |
||
| 2058 | |||
| 2059 | |||
| 2060 | /** |
||
| 2061 | * @return PresenterComponent |
||
| 2062 | */ |
||
| 2063 | public function getParent() |
||
| 2075 | |||
| 2076 | } |
||
| 2077 |
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: