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 |
||
| 16 | class DataGrid extends Nette\Application\UI\Control |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var callable[] |
||
| 21 | */ |
||
| 22 | public $onRedraw; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | * @todo Tell about this on github |
||
| 27 | */ |
||
| 28 | public static $icon_prefix = 'fa fa-'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * When set to TRUE, datagrid throws an exception |
||
| 32 | * when tring to get related entity within join and entity does not exist |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | public $strict_entity_property = FALSE; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | * @persistent |
||
| 40 | */ |
||
| 41 | public $page = 1; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | * @persistent |
||
| 46 | */ |
||
| 47 | public $per_page; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | * @persistent |
||
| 52 | */ |
||
| 53 | public $sort = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | * @persistent |
||
| 58 | */ |
||
| 59 | public $filter = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Callable[] |
||
| 63 | */ |
||
| 64 | public $onRender = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Callable[] |
||
| 68 | */ |
||
| 69 | protected $rowCallback; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $items_per_page_list = [10, 20, 50]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $template_file; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var Column\IColumn[] |
||
| 83 | */ |
||
| 84 | protected $columns = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Column\Action[] |
||
| 88 | */ |
||
| 89 | protected $actions = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var GroupAction\GroupActionCollection |
||
| 93 | */ |
||
| 94 | protected $group_action_collection; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Filter\Filter[] |
||
| 98 | */ |
||
| 99 | protected $filters = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var Export\Export[] |
||
| 103 | */ |
||
| 104 | protected $exports = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var DataModel |
||
| 108 | */ |
||
| 109 | protected $dataModel; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var DataFilter |
||
| 113 | */ |
||
| 114 | protected $dataFilter; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | protected $primary_key = 'id'; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected $do_paginate = TRUE; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | protected $csv_export = TRUE; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $csv_export_filtered = TRUE; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | protected $sortable = FALSE; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $sortable_handler = 'sort!'; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $original_template; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $redraw_item; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var mixed |
||
| 158 | */ |
||
| 159 | protected $translator; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | protected $force_filter_active; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var callable |
||
| 168 | */ |
||
| 169 | protected $tree_view_children_callback; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var string |
||
| 173 | */ |
||
| 174 | protected $tree_view_has_children_column; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var bool |
||
| 178 | */ |
||
| 179 | protected $outer_filter_rendering = FALSE; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | protected $columns_export_order = []; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | private $remember_state = TRUE; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @var bool |
||
| 193 | */ |
||
| 194 | private $refresh_url = TRUE; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var Nette\Http\SessionSection |
||
| 198 | */ |
||
| 199 | private $grid_session; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var array |
||
| 203 | */ |
||
| 204 | private $items_detail = []; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @var array |
||
| 208 | */ |
||
| 209 | private $row_conditions = [ |
||
| 210 | 'group_action' => FALSE, |
||
| 211 | 'action' => [] |
||
| 212 | ]; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var bool |
||
| 216 | */ |
||
| 217 | protected $can_hide_columns = FALSE; |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
| 222 | * @param string $name |
||
| 223 | */ |
||
| 224 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
| 225 | { |
||
| 226 | parent::__construct($parent, $name); |
||
| 227 | |||
| 228 | $this->monitor('Nette\Application\UI\Presenter'); |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * {inheritDoc} |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | public function attached($presenter) |
||
| 237 | { |
||
| 238 | parent::attached($presenter); |
||
| 239 | |||
| 240 | if ($presenter instanceof Nette\Application\UI\Presenter) { |
||
| 241 | /** |
||
| 242 | * Get session |
||
| 243 | */ |
||
| 244 | $this->grid_session = $this->getPresenter()->getSession($this->getSessionSectionName()); |
||
|
|
|||
| 245 | |||
| 246 | /** |
||
| 247 | * Try to find previous filters/pagination/sort in session |
||
| 248 | */ |
||
| 249 | $this->findSessionFilters(); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | /******************************************************************************** |
||
| 255 | * RENDERING * |
||
| 256 | ********************************************************************************/ |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Render template |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function render() |
||
| 264 | { |
||
| 265 | /** |
||
| 266 | * Check whether datagrid has set some columns, initiated data source, etc |
||
| 267 | */ |
||
| 268 | if (!($this->dataModel instanceof DataModel)) { |
||
| 269 | throw new DataGridException('You have to set a data source first.'); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (empty($this->columns)) { |
||
| 273 | throw new DataGridException('You have to add at least one column.'); |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->template->setTranslator($this->getTranslator()); |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Invoke some possible events |
||
| 280 | */ |
||
| 281 | $this->onRender($this); |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Prepare data for rendering (datagrid may render just one item) |
||
| 285 | */ |
||
| 286 | $rows = []; |
||
| 287 | |||
| 288 | if (!empty($this->redraw_item)) { |
||
| 289 | $items = $this->dataModel->filterRow($this->redraw_item); |
||
| 290 | } else { |
||
| 291 | $items = Nette\Utils\Callback::invokeArgs( |
||
| 292 | [$this->dataModel, 'filterData'], |
||
| 293 | [ |
||
| 294 | $this->getPaginator(), |
||
| 295 | $this->sort, |
||
| 296 | $this->assableFilters() |
||
| 297 | ] |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | $callback = $this->rowCallback ?: NULL; |
||
| 302 | |||
| 303 | foreach ($items as $item) { |
||
| 304 | $rows[] = $row = new Row($this, $item, $this->getPrimaryKey()); |
||
| 305 | |||
| 306 | if ($callback) { |
||
| 307 | $callback($item, $row->getControl()); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($this->isTreeView()) { |
||
| 312 | $this->template->tree_view_has_children_column = $this->tree_view_has_children_column; |
||
| 313 | } |
||
| 314 | |||
| 315 | $this->template->rows = $rows; |
||
| 316 | |||
| 317 | $this->template->columns = $this->getColumns(); |
||
| 318 | $this->template->actions = $this->actions; |
||
| 319 | $this->template->exports = $this->exports; |
||
| 320 | $this->template->filters = $this->filters; |
||
| 321 | |||
| 322 | $this->template->filter_active = $this->isFilterActive(); |
||
| 323 | $this->template->original_template = $this->getOriginalTemplateFile(); |
||
| 324 | $this->template->icon_prefix = static::$icon_prefix; |
||
| 325 | $this->template->items_detail = $this->items_detail; |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Walkaround for Latte (does not know $form in snippet in {form} etc) |
||
| 329 | */ |
||
| 330 | $this->template->filter = $this['filter']; |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set template file and render it |
||
| 334 | */ |
||
| 335 | $this->template->setFile($this->getTemplateFile())->render(); |
||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | /******************************************************************************** |
||
| 340 | * ROW CALLBACK * |
||
| 341 | ********************************************************************************/ |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Each row can be modified with user callback |
||
| 346 | * @param callable $callback |
||
| 347 | * @return static |
||
| 348 | */ |
||
| 349 | public function setRowCallback(callable $callback) |
||
| 350 | { |
||
| 351 | $this->rowCallback = $callback; |
||
| 352 | |||
| 353 | return $this; |
||
| 354 | } |
||
| 355 | |||
| 356 | |||
| 357 | /******************************************************************************** |
||
| 358 | * DATA SOURCE * |
||
| 359 | ********************************************************************************/ |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * By default ID, you can change that |
||
| 364 | * @param string $primary_key |
||
| 365 | */ |
||
| 366 | public function setPrimaryKey($primary_key) |
||
| 367 | { |
||
| 368 | $this->primary_key = $primary_key; |
||
| 369 | |||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Set Grid data source |
||
| 376 | * @param DataSource\IDataSource|array|\DibiFluent $source |
||
| 377 | * @return DataGrid |
||
| 378 | */ |
||
| 379 | public function setDataSource($source) |
||
| 380 | { |
||
| 381 | if ($source instanceof DataSource\IDataSource) { |
||
| 382 | /** |
||
| 383 | * Custom user datasource is ready for use |
||
| 384 | */ |
||
| 385 | $data_source = $source; |
||
| 386 | |||
| 387 | } else if (is_array($source)) { |
||
| 388 | $data_source = new DataSource\ArrayDataSource($source); |
||
| 389 | |||
| 390 | } else if ($source instanceof \DibiFluent) { |
||
| 391 | $driver = $source->getConnection()->getDriver(); |
||
| 392 | |||
| 393 | if ($driver instanceof \DibiOdbcDriver) { |
||
| 394 | $data_source = new DataSource\DibiFluentMssqlDataSource($source, $this->primary_key); |
||
| 395 | |||
| 396 | } else if ($driver instanceof \DibiMsSqlDriver) { |
||
| 397 | $data_source = new DataSource\DibiFluentMssqlDataSource($source, $this->primary_key); |
||
| 398 | |||
| 399 | } else { |
||
| 400 | $data_source = new DataSource\DibiFluentDataSource($source, $this->primary_key); |
||
| 401 | } |
||
| 402 | |||
| 403 | } else if ($source instanceof Nette\Database\Table\Selection) { |
||
| 404 | $data_source = new DataSource\NetteDatabaseTableDataSource($source, $this->primary_key); |
||
| 405 | |||
| 406 | } else if ($source instanceof \Kdyby\Doctrine\QueryBuilder) { |
||
| 407 | $data_source = new DataSource\DoctrineDataSource($source, $this->primary_key); |
||
| 408 | |||
| 409 | } else { |
||
| 410 | $data_source_class = $source ? get_class($source) : 'NULL'; |
||
| 411 | throw new DataGridException("DataGrid can not take [$data_source_class] as data source."); |
||
| 412 | } |
||
| 413 | |||
| 414 | $this->dataModel = new DataModel($data_source); |
||
| 415 | |||
| 416 | return $this; |
||
| 417 | } |
||
| 418 | |||
| 419 | |||
| 420 | /******************************************************************************** |
||
| 421 | * TEMPLATING * |
||
| 422 | ********************************************************************************/ |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Set custom template file to render |
||
| 427 | * @param string $template_file |
||
| 428 | */ |
||
| 429 | public function setTemplateFile($template_file) |
||
| 430 | { |
||
| 431 | $this->template_file = $template_file; |
||
| 432 | |||
| 433 | return $this; |
||
| 434 | } |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * Get DataGrid template file |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getTemplateFile() |
||
| 442 | { |
||
| 443 | return $this->template_file ?: $this->getOriginalTemplateFile(); |
||
| 444 | } |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Get DataGrid original template file |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function getOriginalTemplateFile() |
||
| 452 | { |
||
| 453 | return __DIR__.'/templates/datagrid.latte'; |
||
| 454 | } |
||
| 455 | |||
| 456 | |||
| 457 | /******************************************************************************** |
||
| 458 | * SORTING * |
||
| 459 | ********************************************************************************/ |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * Set default sorting |
||
| 464 | * @param aray $sort |
||
| 465 | */ |
||
| 466 | public function setDefaultSort($sort) |
||
| 467 | { |
||
| 468 | if (empty($this->sort)) { |
||
| 469 | $this->sort = (array) $sort; |
||
| 470 | |||
| 471 | $this->saveSessionData('_grid_sort', $this->sort); |
||
| 472 | } |
||
| 473 | |||
| 474 | return $this; |
||
| 475 | } |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * Set grido to be sortable |
||
| 480 | * @param bool $sortable |
||
| 481 | */ |
||
| 482 | public function setSortable($sortable = TRUE) |
||
| 483 | { |
||
| 484 | if ($this->getItemsDetail()) { |
||
| 485 | throw new DataGridException('You can not use both sortable datagrid and items detail.'); |
||
| 486 | } |
||
| 487 | |||
| 488 | $this->sortable = (bool) $sortable; |
||
| 489 | |||
| 490 | return $this; |
||
| 491 | } |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Set sortable handle |
||
| 496 | * @param string $handle |
||
| 497 | */ |
||
| 498 | public function setSortableHandler($handler = 'sort!') |
||
| 499 | { |
||
| 500 | $this->sortable_handler = (string) $handler; |
||
| 501 | |||
| 502 | return $this; |
||
| 503 | } |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Tell whether DataGrid is sortable |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function isSortable() |
||
| 511 | { |
||
| 512 | return $this->sortable; |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Return sortable handle name |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | public function getSortableHandler() |
||
| 520 | { |
||
| 521 | return $this->sortable_handler; |
||
| 522 | } |
||
| 523 | |||
| 524 | |||
| 525 | /******************************************************************************** |
||
| 526 | * TREE VIEW * |
||
| 527 | ********************************************************************************/ |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * Is tree view set? |
||
| 532 | * @return boolean |
||
| 533 | */ |
||
| 534 | public function isTreeView() |
||
| 535 | { |
||
| 536 | return (bool) $this->tree_view_children_callback; |
||
| 537 | } |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * Setting tree view |
||
| 542 | * @param callable $get_children_callback |
||
| 543 | * @param string $tree_view_has_children_column |
||
| 544 | * @return DataGrid |
||
| 545 | */ |
||
| 546 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
| 571 | |||
| 572 | |||
| 573 | /******************************************************************************** |
||
| 574 | * COLUMNS * |
||
| 575 | ********************************************************************************/ |
||
| 576 | |||
| 577 | |||
| 578 | /** |
||
| 579 | * Add text column with no other formating |
||
| 580 | * @param string $key |
||
| 581 | * @param string $name |
||
| 582 | * @param string|null $column |
||
| 583 | * @return Column\ColumnText |
||
| 584 | */ |
||
| 585 | public function addColumnText($key, $name, $column = NULL) |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * Add column with link |
||
| 596 | * @param string $key |
||
| 597 | * @param string $name |
||
| 598 | * @param string|null $column |
||
| 599 | * @return Column\ColumnLink |
||
| 600 | */ |
||
| 601 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * Add column with possible number formating |
||
| 617 | * @param string $key |
||
| 618 | * @param string $name |
||
| 619 | * @param string|null $column |
||
| 620 | * @return Column\ColumnNumber |
||
| 621 | */ |
||
| 622 | public function addColumnNumber($key, $name, $column = NULL) |
||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * Add column with date formating |
||
| 633 | * @param string $key |
||
| 634 | * @param string $name |
||
| 635 | * @param string|null $column |
||
| 636 | * @return Column\ColumnDateTime |
||
| 637 | */ |
||
| 638 | public function addColumnDateTime($key, $name, $column = NULL) |
||
| 645 | |||
| 646 | |||
| 647 | /** |
||
| 648 | * Return existing column |
||
| 649 | * @param string $key |
||
| 650 | * @return Column\Column |
||
| 651 | * @throws DataGridException |
||
| 652 | */ |
||
| 653 | public function getColumn($key) |
||
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * Remove column |
||
| 665 | * @param string $key |
||
| 666 | * @return void |
||
| 667 | */ |
||
| 668 | public function removeColumn($key) |
||
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * Check whether given key already exists in $this->columns |
||
| 676 | * @param string $key |
||
| 677 | * @throws DataGridException |
||
| 678 | */ |
||
| 679 | protected function addColumnCheck($key) |
||
| 685 | |||
| 686 | |||
| 687 | /******************************************************************************** |
||
| 688 | * ACTIONS * |
||
| 689 | ********************************************************************************/ |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Create action |
||
| 694 | * @param string $key |
||
| 695 | * @param string $name |
||
| 696 | * @param string $href |
||
| 697 | * @param array|null $params |
||
| 698 | */ |
||
| 699 | public function addAction($key, $name = '', $href = NULL, array $params = NULL) |
||
| 710 | |||
| 711 | |||
| 712 | /** |
||
| 713 | * Get existing action |
||
| 714 | * @param string $key |
||
| 715 | * @return Column\Action |
||
| 716 | * @throws DataGridException |
||
| 717 | */ |
||
| 718 | public function getAction($key) |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * Remove action |
||
| 730 | * @param string $key |
||
| 731 | * @return void |
||
| 732 | */ |
||
| 733 | public function removeAction($key) |
||
| 737 | |||
| 738 | |||
| 739 | /** |
||
| 740 | * Check whether given key already exists in $this->filters |
||
| 741 | * @param string $key |
||
| 742 | * @throws DataGridException |
||
| 743 | */ |
||
| 744 | protected function addActionCheck($key) |
||
| 750 | |||
| 751 | |||
| 752 | /******************************************************************************** |
||
| 753 | * FILTERS * |
||
| 754 | ********************************************************************************/ |
||
| 755 | |||
| 756 | |||
| 757 | /** |
||
| 758 | * Add filter fot text search |
||
| 759 | * @param string $key |
||
| 760 | * @param string $name |
||
| 761 | * @param array|string $columns |
||
| 762 | * @throws DataGridException |
||
| 763 | */ |
||
| 764 | public function addFilterText($key, $name, $columns = NULL) |
||
| 776 | |||
| 777 | |||
| 778 | /** |
||
| 779 | * Add select box filter |
||
| 780 | * @param string $key |
||
| 781 | * @param string $name |
||
| 782 | * @param array $options |
||
| 783 | * @param string $column |
||
| 784 | * @throws DataGridException |
||
| 785 | */ |
||
| 786 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
| 798 | |||
| 799 | |||
| 800 | /** |
||
| 801 | * Add datepicker filter |
||
| 802 | * @param string $key |
||
| 803 | * @param string $name |
||
| 804 | * @param string $column |
||
| 805 | * @throws DataGridException |
||
| 806 | */ |
||
| 807 | public function addFilterDate($key, $name, $column = NULL) |
||
| 819 | |||
| 820 | |||
| 821 | /** |
||
| 822 | * Add range filter (from - to) |
||
| 823 | * @param string $key |
||
| 824 | * @param string $name |
||
| 825 | * @param string $column |
||
| 826 | * @throws DataGridException |
||
| 827 | */ |
||
| 828 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
| 840 | |||
| 841 | |||
| 842 | /** |
||
| 843 | * Add datepicker filter (from - to) |
||
| 844 | * @param string $key |
||
| 845 | * @param string $name |
||
| 846 | * @param string $column |
||
| 847 | * @throws DataGridException |
||
| 848 | */ |
||
| 849 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
| 861 | |||
| 862 | |||
| 863 | /** |
||
| 864 | * Check whether given key already exists in $this->filters |
||
| 865 | * @param string $key |
||
| 866 | * @throws DataGridException |
||
| 867 | */ |
||
| 868 | protected function addFilterCheck($key) |
||
| 874 | |||
| 875 | |||
| 876 | /** |
||
| 877 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
| 878 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
| 879 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
| 880 | */ |
||
| 881 | public function assableFilters() |
||
| 909 | |||
| 910 | |||
| 911 | /** |
||
| 912 | * Remove filter |
||
| 913 | * @param string $key |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | public function removeFilter($key) |
||
| 920 | |||
| 921 | |||
| 922 | /******************************************************************************** |
||
| 923 | * FILTERING * |
||
| 924 | ********************************************************************************/ |
||
| 925 | |||
| 926 | |||
| 927 | /** |
||
| 928 | * Is filter active? |
||
| 929 | * @return boolean |
||
| 930 | */ |
||
| 931 | public function isFilterActive() |
||
| 937 | |||
| 938 | |||
| 939 | /** |
||
| 940 | * Tell that filter is active from whatever reasons |
||
| 941 | * return static |
||
| 942 | */ |
||
| 943 | public function setFilterActive() |
||
| 949 | |||
| 950 | |||
| 951 | /** |
||
| 952 | * If we want to sent some initial filter |
||
| 953 | * @param array $filter |
||
| 954 | * @return static |
||
| 955 | */ |
||
| 956 | public function setFilter(array $filter) |
||
| 957 | { |
||
| 958 | $this->filter = $filter; |
||
| 959 | |||
| 960 | return $this; |
||
| 961 | } |
||
| 962 | |||
| 963 | |||
| 964 | /** |
||
| 965 | * FilterAndGroupAction form factory |
||
| 966 | * @return Form |
||
| 967 | */ |
||
| 968 | public function createComponentFilter() |
||
| 998 | |||
| 999 | |||
| 1000 | /** |
||
| 1001 | * Set $this->filter values after filter form submitted |
||
| 1002 | * @param Form $form |
||
| 1003 | * @return void |
||
| 1004 | */ |
||
| 1005 | public function filterSucceeded(Form $form) |
||
| 1031 | |||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Should be datagrid filters rendered separately? |
||
| 1035 | * @param boolean $out |
||
| 1036 | * @return static |
||
| 1037 | */ |
||
| 1038 | public function setOuterFilterRendering($out = TRUE) |
||
| 1044 | |||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Are datagrid filters rendered separately? |
||
| 1048 | * @return boolean |
||
| 1049 | */ |
||
| 1050 | public function hasOuterFilterRendering() |
||
| 1054 | |||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Try to restore session stuff |
||
| 1058 | * @return void |
||
| 1059 | */ |
||
| 1060 | public function findSessionFilters() |
||
| 1088 | |||
| 1089 | |||
| 1090 | /******************************************************************************** |
||
| 1091 | * EXPORTS * |
||
| 1092 | ********************************************************************************/ |
||
| 1093 | |||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Add export of type callback |
||
| 1097 | * @param string $text |
||
| 1098 | * @param callable $callback |
||
| 1099 | * @param boolean $filtered |
||
| 1100 | * @return Export\Export |
||
| 1101 | */ |
||
| 1102 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
| 1110 | |||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Add already implemented csv export |
||
| 1114 | * @param string $text |
||
| 1115 | * @param string $csv_file_name |
||
| 1116 | * @return Export\Export |
||
| 1117 | */ |
||
| 1118 | public function addExportCsv($text, $csv_file_name) |
||
| 1122 | |||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Add already implemented csv export, but for filtered data |
||
| 1126 | * @param string $text |
||
| 1127 | * @param string $csv_file_name |
||
| 1128 | * @return Export\Export |
||
| 1129 | */ |
||
| 1130 | public function addExportCsvFiltered($text, $csv_file_name) |
||
| 1134 | |||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Add export to array |
||
| 1138 | * @param Export\Export $export |
||
| 1139 | * @return Export\Export |
||
| 1140 | */ |
||
| 1141 | protected function addToExports(Export\Export $export) |
||
| 1149 | |||
| 1150 | |||
| 1151 | public function resetExportsLinks() |
||
| 1157 | |||
| 1158 | |||
| 1159 | /******************************************************************************** |
||
| 1160 | * GROUP ACTIONS * |
||
| 1161 | ********************************************************************************/ |
||
| 1162 | |||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Add group actino |
||
| 1166 | * @param string $title |
||
| 1167 | * @param array $options |
||
| 1168 | */ |
||
| 1169 | public function addGroupAction($title, $options = []) |
||
| 1173 | |||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Get collection of all group actions |
||
| 1177 | * @return GroupAction\GroupActionCollection |
||
| 1178 | */ |
||
| 1179 | public function getGroupActionCollection() |
||
| 1187 | |||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Has datagrid some group actions? |
||
| 1191 | * @return boolean |
||
| 1192 | */ |
||
| 1193 | public function hasGroupActions() |
||
| 1197 | |||
| 1198 | |||
| 1199 | /******************************************************************************** |
||
| 1200 | * HANDLERS * |
||
| 1201 | ********************************************************************************/ |
||
| 1202 | |||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
| 1206 | * @param int $page |
||
| 1207 | * @return void |
||
| 1208 | */ |
||
| 1209 | public function handlePage($page) |
||
| 1219 | |||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Handler for sorting |
||
| 1223 | * @param array $sort |
||
| 1224 | * @return void |
||
| 1225 | */ |
||
| 1226 | public function handleSort(array $sort) |
||
| 1236 | |||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * handler for reseting the filter |
||
| 1240 | * @return void |
||
| 1241 | */ |
||
| 1242 | public function handleResetFilter() |
||
| 1259 | |||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * Handler for export |
||
| 1263 | * @param int $id Key for particular export class in array $this->exports |
||
| 1264 | * @return void |
||
| 1265 | */ |
||
| 1266 | public function handleExport($id) |
||
| 1310 | |||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Handler for getting children of parent item (e.g. category) |
||
| 1314 | * @param int $parent |
||
| 1315 | * @return void |
||
| 1316 | */ |
||
| 1317 | View Code Duplication | public function handleGetChildren($parent) |
|
| 1334 | |||
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Handler for getting item detail |
||
| 1338 | * @param mixed $id |
||
| 1339 | * @return void |
||
| 1340 | */ |
||
| 1341 | View Code Duplication | public function handleGetItemDetail($id) |
|
| 1355 | |||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Handler for inline editing |
||
| 1359 | * @param mixed $id |
||
| 1360 | * @param mixed $key |
||
| 1361 | * @return void |
||
| 1362 | */ |
||
| 1363 | public function handleEdit($id, $key) |
||
| 1370 | |||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Redraw $this |
||
| 1374 | * @return void |
||
| 1375 | */ |
||
| 1376 | public function reload($snippets = []) |
||
| 1399 | |||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Redraw just one row via ajax |
||
| 1403 | * @param int $id |
||
| 1404 | * @param mixed $primary_where_column |
||
| 1405 | * @return void |
||
| 1406 | */ |
||
| 1407 | public function redrawItem($id, $primary_where_column = NULL) |
||
| 1416 | |||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Tell datagrid to display all columns |
||
| 1420 | * @return void |
||
| 1421 | */ |
||
| 1422 | public function handleShowAllColumns() |
||
| 1430 | |||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * Notice datagrid to not display particular columns |
||
| 1434 | * @param string $column |
||
| 1435 | * @return void |
||
| 1436 | */ |
||
| 1437 | public function handleHideColumn($column) |
||
| 1456 | |||
| 1457 | |||
| 1458 | /******************************************************************************** |
||
| 1459 | * PAGINATION * |
||
| 1460 | ********************************************************************************/ |
||
| 1461 | |||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Set options of select "items_per_page" |
||
| 1465 | * @param array $items_per_page_list |
||
| 1466 | */ |
||
| 1467 | public function setItemsPerPageList(array $items_per_page_list) |
||
| 1473 | |||
| 1474 | |||
| 1475 | /** |
||
| 1476 | * Paginator factory |
||
| 1477 | * @return Components\DataGridPaginator\DataGridPaginator |
||
| 1478 | */ |
||
| 1479 | public function createComponentPaginator() |
||
| 1492 | |||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * PerPage form factory |
||
| 1496 | * @return Form |
||
| 1497 | */ |
||
| 1498 | public function createComponentPerPage() |
||
| 1524 | |||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Get parameter per_page |
||
| 1528 | * @return int |
||
| 1529 | */ |
||
| 1530 | public function getPerPage() |
||
| 1540 | |||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * Get associative array of items_per_page_list |
||
| 1544 | * @return array |
||
| 1545 | */ |
||
| 1546 | public function getItemsPerPageList() |
||
| 1558 | |||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Order Grid to "be paginated" |
||
| 1562 | * @param bool $do |
||
| 1563 | * @return static |
||
| 1564 | */ |
||
| 1565 | public function setPagination($do) |
||
| 1571 | |||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Tell whether Grid is paginated |
||
| 1575 | * @return bool |
||
| 1576 | */ |
||
| 1577 | public function isPaginated() |
||
| 1581 | |||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Return current paginator class |
||
| 1585 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
| 1586 | */ |
||
| 1587 | public function getPaginator() |
||
| 1595 | |||
| 1596 | |||
| 1597 | /******************************************************************************** |
||
| 1598 | * I18N * |
||
| 1599 | ********************************************************************************/ |
||
| 1600 | |||
| 1601 | |||
| 1602 | /** |
||
| 1603 | * Set datagrid translator |
||
| 1604 | * @param Nette\Localization\ITranslator $translator |
||
| 1605 | * @return static |
||
| 1606 | */ |
||
| 1607 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
| 1613 | |||
| 1614 | |||
| 1615 | /** |
||
| 1616 | * Get translator for datagrid |
||
| 1617 | * @return Nette\Localization\ITranslator |
||
| 1618 | */ |
||
| 1619 | public function getTranslator() |
||
| 1627 | |||
| 1628 | |||
| 1629 | /******************************************************************************** |
||
| 1630 | * COLUMNS ORDER * |
||
| 1631 | ********************************************************************************/ |
||
| 1632 | |||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Set order of datagrid columns |
||
| 1636 | * @param array $order |
||
| 1637 | * @return static |
||
| 1638 | */ |
||
| 1639 | public function setColumnsOrder($order) |
||
| 1657 | |||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * Columns order may be different for export and normal grid |
||
| 1661 | * @param array $order |
||
| 1662 | */ |
||
| 1663 | public function setColumnsExportOrder($order) |
||
| 1667 | |||
| 1668 | |||
| 1669 | /******************************************************************************** |
||
| 1670 | * SESSION & URL * |
||
| 1671 | ********************************************************************************/ |
||
| 1672 | |||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Find some unique session key name |
||
| 1676 | * @return string |
||
| 1677 | */ |
||
| 1678 | public function getSessionSectionName() |
||
| 1682 | |||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * Should datagrid remember its filters/pagination/etc using session? |
||
| 1686 | * @param bool $remember |
||
| 1687 | * @return static |
||
| 1688 | */ |
||
| 1689 | public function setRememberState($remember = TRUE) |
||
| 1695 | |||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * Should datagrid refresh url using history API? |
||
| 1699 | * @param bool $refresh |
||
| 1700 | * @return static |
||
| 1701 | */ |
||
| 1702 | public function setRefreshUrl($refresh = TRUE) |
||
| 1709 | |||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Get session data if functionality is enabled |
||
| 1713 | * @param string $key |
||
| 1714 | * @return mixed |
||
| 1715 | */ |
||
| 1716 | public function getSessionData($key = NULL, $default_value = NULL) |
||
| 1724 | |||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Save session data - just if it is enabled |
||
| 1728 | * @param string $key |
||
| 1729 | * @param mixed $value |
||
| 1730 | * @return void |
||
| 1731 | */ |
||
| 1732 | public function saveSessionData($key, $value) |
||
| 1738 | |||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Delete session data |
||
| 1742 | * @return void |
||
| 1743 | */ |
||
| 1744 | public function deleteSesssionData($key) |
||
| 1748 | |||
| 1749 | |||
| 1750 | /******************************************************************************** |
||
| 1751 | * ITEM DETAIL * |
||
| 1752 | ********************************************************************************/ |
||
| 1753 | |||
| 1754 | |||
| 1755 | /** |
||
| 1756 | * Get items detail parameters |
||
| 1757 | * @return array |
||
| 1758 | */ |
||
| 1759 | public function getItemsDetail() |
||
| 1763 | |||
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Items can have thair detail - toggled |
||
| 1767 | * @param mixed $detail callable|string|bool |
||
| 1768 | * @param bool|NULL $primary_where_column |
||
| 1769 | * @return Column\ItemDetail |
||
| 1770 | */ |
||
| 1771 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
| 1810 | |||
| 1811 | |||
| 1812 | /******************************************************************************** |
||
| 1813 | * ROW PRIVILEGES * |
||
| 1814 | ********************************************************************************/ |
||
| 1815 | |||
| 1816 | |||
| 1817 | public function allowRowsGroupAction(callable $condition) |
||
| 1821 | |||
| 1822 | |||
| 1823 | public function allowRowsAction($key, callable $condition) |
||
| 1827 | |||
| 1828 | |||
| 1829 | public function getRowCondition($name, $key = NULL) |
||
| 1843 | |||
| 1844 | |||
| 1845 | /******************************************************************************** |
||
| 1846 | * HIDEABLE COLUMNS * |
||
| 1847 | ********************************************************************************/ |
||
| 1848 | |||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Can datagrid hide colums? |
||
| 1852 | * @return boolean |
||
| 1853 | */ |
||
| 1854 | public function canHideColumns() |
||
| 1858 | |||
| 1859 | |||
| 1860 | /** |
||
| 1861 | * Order Grid to set columns hideable. |
||
| 1862 | * @param bool $do |
||
| 1863 | * @return static |
||
| 1864 | */ |
||
| 1865 | public function setColumnsHideable() |
||
| 1871 | |||
| 1872 | |||
| 1873 | /******************************************************************************** |
||
| 1874 | * INTERNAL * |
||
| 1875 | ********************************************************************************/ |
||
| 1876 | |||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Get cont of columns |
||
| 1880 | * @return int |
||
| 1881 | */ |
||
| 1882 | public function getColumnsCount() |
||
| 1896 | |||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Get primary key of datagrid data source |
||
| 1900 | * @return string |
||
| 1901 | */ |
||
| 1902 | public function getPrimaryKey() |
||
| 1906 | |||
| 1907 | |||
| 1908 | /** |
||
| 1909 | * Get set of set columns |
||
| 1910 | * @return Column\IColumn[] |
||
| 1911 | */ |
||
| 1912 | public function getColumns() |
||
| 1920 | |||
| 1921 | |||
| 1922 | |||
| 1923 | /** |
||
| 1924 | * @return PresenterComponent |
||
| 1925 | */ |
||
| 1926 | public function getParent() |
||
| 1937 | |||
| 1938 | } |
||
| 1939 | |||
| 1944 |
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: