| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Core\Factory; |
| 8: | use Atk4\Core\HookTrait; |
| 9: | use Atk4\Data\Field; |
| 10: | use Atk4\Data\Model; |
| 11: | use Atk4\Ui\Js\Jquery; |
| 12: | use Atk4\Ui\Js\JsExpressionable; |
| 13: | use Atk4\Ui\Js\JsReload; |
| 14: | use Atk4\Ui\UserAction\ConfirmationExecutor; |
| 15: | use Atk4\Ui\UserAction\ExecutorFactory; |
| 16: | use Atk4\Ui\UserAction\ExecutorInterface; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | class Grid extends View |
| 22: | { |
| 23: | use HookTrait; |
| 24: | |
| 25: | |
| 26: | public $menu; |
| 27: | |
| 28: | |
| 29: | public $quickSearch; |
| 30: | |
| 31: | |
| 32: | public $searchFieldNames = []; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public $paginator; |
| 42: | |
| 43: | |
| 44: | public $ipp = 50; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | public $actionButtons; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public $actionMenu; |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public $selection; |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public $sortable; |
| 77: | |
| 78: | |
| 79: | public $sortTrigger; |
| 80: | |
| 81: | |
| 82: | public $table; |
| 83: | |
| 84: | |
| 85: | public $container; |
| 86: | |
| 87: | public $defaultTemplate = 'grid.html'; |
| 88: | |
| 89: | |
| 90: | protected $actionButtonsDecorator = [Table\Column\ActionButtons::class]; |
| 91: | |
| 92: | |
| 93: | protected $actionMenuDecorator = [Table\Column\ActionMenu::class, 'label' => 'Actions...']; |
| 94: | |
| 95: | #[\Override] |
| 96: | protected function init(): void |
| 97: | { |
| 98: | parent::init(); |
| 99: | |
| 100: | $this->container = View::addTo($this, ['template' => $this->template->cloneRegion('Container')]); |
| 101: | $this->template->del('Container'); |
| 102: | |
| 103: | if (!$this->sortTrigger) { |
| 104: | $this->sortTrigger = $this->name . '_sort'; |
| 105: | } |
| 106: | |
| 107: | if ($this->menu !== false && !is_object($this->menu)) { |
| 108: | $this->menu = $this->add(Factory::factory([Menu::class, 'activateOnClick' => false], $this->menu), 'Menu'); |
| 109: | } |
| 110: | |
| 111: | $this->table = $this->initTable(); |
| 112: | |
| 113: | if ($this->paginator !== false) { |
| 114: | $seg = View::addTo($this->container, [], ['Paginator'])->setStyle('text-align', 'center'); |
| 115: | $this->paginator = $seg->add(Factory::factory([Paginator::class, 'reload' => $this->container], $this->paginator)); |
| 116: | $this->stickyGet($this->paginator->name); |
| 117: | } |
| 118: | |
| 119: | |
| 120: | if ($this->menu !== false) { |
| 121: | $appUniqueHashesBackup = $this->getApp()->uniqueNameHashes; |
| 122: | $menuElementNameCountsBackup = \Closure::bind(fn () => $this->_elementNameCounts, $this->menu, AbstractView::class)(); |
| 123: | try { |
| 124: | $menuRight = $this->menu->addMenuRight(); |
| 125: | $menuItemView = View::addTo($menuRight->addItem()->setElement('div')); |
| 126: | $quickSearch = JsSearch::addTo($menuItemView); |
| 127: | $this->stickyGet($quickSearch->name . '_q'); |
| 128: | $this->menu->removeElement($menuRight->shortName); |
| 129: | } finally { |
| 130: | $this->getApp()->uniqueNameHashes = $appUniqueHashesBackup; |
| 131: | \Closure::bind(fn () => $this->_elementNameCounts = $menuElementNameCountsBackup, $this->menu, AbstractView::class)(); |
| 132: | } |
| 133: | } |
| 134: | } |
| 135: | |
| 136: | protected function initTable(): Table |
| 137: | { |
| 138: | |
| 139: | $table = $this->container->add(Factory::factory([Table::class, 'class.very compact very basic striped single line' => true, 'reload' => $this->container], $this->table), 'Table'); |
| 140: | |
| 141: | return $table; |
| 142: | } |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function addColumn(?string $name, $columnDecorator = [], $field = []) |
| 155: | { |
| 156: | return $this->table->addColumn($name, $columnDecorator, $field); |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | public function addDecorator(string $name, $seed) |
| 167: | { |
| 168: | return $this->table->addDecorator($name, $seed); |
| 169: | } |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | public function addButton($label): Button |
| 177: | { |
| 178: | if (!$this->menu) { |
| 179: | throw new Exception('Unable to add Button without Menu'); |
| 180: | } |
| 181: | |
| 182: | return Button::addTo($this->menu->addItem(), [$label]); |
| 183: | } |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | public function setIpp($ipp, $label = 'Items per page:'): void |
| 194: | { |
| 195: | if (is_array($ipp)) { |
| 196: | $this->addItemsPerPageSelector($ipp, $label); |
| 197: | } else { |
| 198: | $this->ipp = $ipp; |
| 199: | } |
| 200: | } |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | public function addItemsPerPageSelector(array $items = [10, 100, 1000], $label = 'Items per page:') |
| 211: | { |
| 212: | $ipp = (int) $this->container->stickyGet('ipp'); |
| 213: | if ($ipp) { |
| 214: | $this->ipp = $ipp; |
| 215: | } else { |
| 216: | $this->ipp = $items[0]; |
| 217: | } |
| 218: | |
| 219: | $pageLength = ItemsPerPageSelector::addTo($this->paginator, ['pageLengthItems' => $items, 'label' => $label, 'currentIpp' => $this->ipp], ['afterPaginator']); |
| 220: | $this->paginator->template->trySet('PaginatorType', 'ui grid'); |
| 221: | |
| 222: | $sortBy = $this->getSortBy(); |
| 223: | if ($sortBy) { |
| 224: | $pageLength->stickyGet($this->sortTrigger, $sortBy); |
| 225: | } |
| 226: | |
| 227: | $pageLength->onPageLengthSelect(function (int $ipp) { |
| 228: | $this->ipp = $ipp; |
| 229: | $this->setModelLimitFromPaginator(); |
| 230: | |
| 231: | if ($this->quickSearch instanceof JsSearch) { |
| 232: | $this->container->js(true, $this->quickSearch->js()->atkJsSearch('setUrlArgs', ['ipp', $this->ipp])); |
| 233: | } |
| 234: | $this->applySort(); |
| 235: | |
| 236: | |
| 237: | return $this->container; |
| 238: | }); |
| 239: | |
| 240: | return $this; |
| 241: | } |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | public function addJsPaginator($ipp, $options = [], $container = null, $scrollRegion = 'Body') |
| 254: | { |
| 255: | if ($this->paginator) { |
| 256: | $this->paginator->destroy(); |
| 257: | |
| 258: | $this->paginator = null; |
| 259: | } |
| 260: | |
| 261: | $sortBy = $this->getSortBy(); |
| 262: | if ($sortBy) { |
| 263: | $this->stickyGet($this->sortTrigger, $sortBy); |
| 264: | } |
| 265: | $this->applySort(); |
| 266: | |
| 267: | $this->table->addJsPaginator($ipp, $options, $container, $scrollRegion); |
| 268: | |
| 269: | return $this; |
| 270: | } |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | public function addJsPaginatorInContainer($ipp, $containerHeight, $options = [], $container = null, $scrollRegion = 'Body') |
| 285: | { |
| 286: | $this->table->hasCollapsingCssActionColumn = false; |
| 287: | $options = array_merge($options, [ |
| 288: | 'hasFixTableHeader' => true, |
| 289: | 'tableContainerHeight' => $containerHeight, |
| 290: | ]); |
| 291: | |
| 292: | $options = array_merge(['stateContext' => $this->container], $options); |
| 293: | |
| 294: | return $this->addJsPaginator($ipp, $options, $container, $scrollRegion); |
| 295: | } |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | public function addQuickSearch($fields = [], $hasAutoQuery = false): void |
| 306: | { |
| 307: | if (!$this->model) { |
| 308: | throw new Exception('Call setModel() before addQuickSearch()'); |
| 309: | } |
| 310: | |
| 311: | if (!$fields) { |
| 312: | $fields = [$this->model->titleField]; |
| 313: | } |
| 314: | |
| 315: | if (!$this->menu) { |
| 316: | throw new Exception('Unable to add QuickSearch without Menu'); |
| 317: | } |
| 318: | |
| 319: | $view = View::addTo($this->menu->addMenuRight()->addItem()->setElement('div')); |
| 320: | |
| 321: | $this->quickSearch = JsSearch::addTo($view, ['reload' => $this->container, 'autoQuery' => $hasAutoQuery]); |
| 322: | $q = $this->stickyGet($this->quickSearch->name . '_q') ?? ''; |
| 323: | $qWords = preg_split('~\s+~', $q, -1, \PREG_SPLIT_NO_EMPTY); |
| 324: | if (count($qWords) > 0) { |
| 325: | $andScope = Model\Scope::createAnd(); |
| 326: | foreach ($qWords as $v) { |
| 327: | $orScope = Model\Scope::createOr(); |
| 328: | foreach ($fields as $field) { |
| 329: | $orScope->addCondition($field, 'like', '%' . $v . '%'); |
| 330: | } |
| 331: | $andScope->addCondition($orScope); |
| 332: | } |
| 333: | $this->model->addCondition($andScope); |
| 334: | } |
| 335: | $this->quickSearch->initValue = $q; |
| 336: | } |
| 337: | |
| 338: | #[\Override] |
| 339: | public function jsReload($args = [], $afterSuccess = null, $apiConfig = []): JsExpressionable |
| 340: | { |
| 341: | return new JsReload($this->container, $args, $afterSuccess, $apiConfig); |
| 342: | } |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | public function addActionButton($button, $action = null, string $confirmMsg = '', $isDisabled = false) |
| 355: | { |
| 356: | return $this->getActionButtons()->addButton($button, $action, $confirmMsg, $isDisabled); |
| 357: | } |
| 358: | |
| 359: | |
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: | public function addExecutorButton(UserAction\ExecutorInterface $executor, Button $button = null) |
| 365: | { |
| 366: | if ($button !== null) { |
| 367: | $this->add($button); |
| 368: | } else { |
| 369: | $button = $this->getExecutorFactory()->createTrigger($executor->getAction(), ExecutorFactory::TABLE_BUTTON); |
| 370: | } |
| 371: | |
| 372: | $confirmation = $executor->getAction()->getConfirmation(); |
| 373: | if (!$confirmation) { |
| 374: | $confirmation = ''; |
| 375: | } |
| 376: | $disabled = is_bool($executor->getAction()->enabled) ? !$executor->getAction()->enabled : $executor->getAction()->enabled; |
| 377: | |
| 378: | return $this->getActionButtons()->addButton($button, $executor, $confirmation, $disabled); |
| 379: | } |
| 380: | |
| 381: | private function getActionButtons(): Table\Column\ActionButtons |
| 382: | { |
| 383: | if ($this->actionButtons === null) { |
| 384: | $this->actionButtons = $this->table->addColumn(null, $this->actionButtonsDecorator); |
| 385: | } |
| 386: | |
| 387: | return $this->actionButtons; |
| 388: | } |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | public function addActionMenuItem($view, $action = null, string $confirmMsg = '', bool $isDisabled = false) |
| 400: | { |
| 401: | return $this->getActionMenu()->addActionMenuItem($view, $action, $confirmMsg, $isDisabled); |
| 402: | } |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | public function addExecutorMenuItem(ExecutorInterface $executor) |
| 408: | { |
| 409: | $item = $this->getExecutorFactory()->createTrigger($executor->getAction(), ExecutorFactory::TABLE_MENU_ITEM); |
| 410: | |
| 411: | $confirmation = !$executor instanceof ConfirmationExecutor ? $executor->getAction()->getConfirmation() : ''; |
| 412: | if (!$confirmation) { |
| 413: | $confirmation = ''; |
| 414: | } |
| 415: | $disabled = is_bool($executor->getAction()->enabled) ? !$executor->getAction()->enabled : $executor->getAction()->enabled; |
| 416: | |
| 417: | return $this->getActionMenu()->addActionMenuItem($item, $executor, $confirmation, $disabled); |
| 418: | } |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | private function getActionMenu() |
| 424: | { |
| 425: | if (!$this->actionMenu) { |
| 426: | $this->actionMenu = $this->table->addColumn(null, $this->actionMenuDecorator); |
| 427: | } |
| 428: | |
| 429: | return $this->actionMenu; |
| 430: | } |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | public function addActionMenuFromModel(string $appliesTo = null): void |
| 439: | { |
| 440: | foreach ($this->model->getUserActions($appliesTo) as $action) { |
| 441: | $this->addActionMenuItem($action); |
| 442: | } |
| 443: | } |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: | |
| 453: | public function addFilterColumn($names = null) |
| 454: | { |
| 455: | if (!$this->menu) { |
| 456: | throw new Exception('Unable to add Filter Column without Menu'); |
| 457: | } |
| 458: | $this->menu->addItem(['Clear Filters'], new JsReload($this->table->reload, ['atk_clear_filter' => 1])); |
| 459: | $this->table->setFilterColumn($names); |
| 460: | |
| 461: | return $this; |
| 462: | } |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | public function addDropdown(string $columnName, $items, \Closure $fx, $icon = 'caret square down', $menuId = null): void |
| 474: | { |
| 475: | $column = $this->table->columns[$columnName]; |
| 476: | |
| 477: | if (!$menuId) { |
| 478: | $menuId = $columnName; |
| 479: | } |
| 480: | |
| 481: | $column->addDropdown($items, static function (string $item) use ($fx) { |
| 482: | return $fx($item); |
| 483: | }, $icon, $menuId); |
| 484: | } |
| 485: | |
| 486: | |
| 487: | |
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | public function addPopup($columnName, $popup = null, $icon = 'caret square down') |
| 496: | { |
| 497: | $column = $this->table->columns[$columnName]; |
| 498: | |
| 499: | return $column->addPopup($popup, $icon); |
| 500: | } |
| 501: | |
| 502: | |
| 503: | |
| 504: | |
| 505: | |
| 506: | |
| 507: | |
| 508: | |
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | public function addModalAction($button, $title, \Closure $callback, $args = []) |
| 514: | { |
| 515: | return $this->getActionButtons()->addModal($button, $title, $callback, $this, $args); |
| 516: | } |
| 517: | |
| 518: | |
| 519: | |
| 520: | |
| 521: | private function explodeSelectionValue(string $value): array |
| 522: | { |
| 523: | return $value === '' ? [] : explode(',', $value); |
| 524: | } |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | |
| 530: | |
| 531: | |
| 532: | |
| 533: | |
| 534: | |
| 535: | |
| 536: | public function addBulkAction($item, \Closure $callback, $args = []) |
| 537: | { |
| 538: | $menuItem = $this->menu->addItem($item); |
| 539: | $menuItem->on('click', function (Js\Jquery $j, string $value) use ($callback) { |
| 540: | return $callback($j, $this->explodeSelectionValue($value)); |
| 541: | }, [$this->selection->jsChecked()]); |
| 542: | |
| 543: | return $menuItem; |
| 544: | } |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | |
| 550: | |
| 551: | |
| 552: | |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | public function addModalBulkAction($item, $title, \Closure $callback, $args = []) |
| 558: | { |
| 559: | $modalDefaults = is_string($title) ? ['title' => $title] : []; |
| 560: | |
| 561: | $modal = Modal::addTo($this->getOwner(), $modalDefaults); |
| 562: | $modal->set(function (View $t) use ($callback) { |
| 563: | $callback($t, $this->explodeSelectionValue($t->stickyGet($this->name) ?? '')); |
| 564: | }); |
| 565: | |
| 566: | $menuItem = $this->menu->addItem($item); |
| 567: | $menuItem->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); |
| 568: | |
| 569: | return $menuItem; |
| 570: | } |
| 571: | |
| 572: | |
| 573: | |
| 574: | |
| 575: | public function getSortBy(): ?string |
| 576: | { |
| 577: | return $this->getApp()->tryGetRequestQueryParam($this->sortTrigger); |
| 578: | } |
| 579: | |
| 580: | |
| 581: | |
| 582: | |
| 583: | public function applySort(): void |
| 584: | { |
| 585: | if ($this->sortable === false) { |
| 586: | return; |
| 587: | } |
| 588: | |
| 589: | $sortBy = $this->getSortBy(); |
| 590: | |
| 591: | if ($sortBy && $this->paginator) { |
| 592: | $this->paginator->addReloadArgs([$this->sortTrigger => $sortBy]); |
| 593: | } |
| 594: | |
| 595: | $isDesc = false; |
| 596: | if ($sortBy && substr($sortBy, 0, 1) === '-') { |
| 597: | $isDesc = true; |
| 598: | $sortBy = substr($sortBy, 1); |
| 599: | } |
| 600: | |
| 601: | $this->table->sortable = true; |
| 602: | |
| 603: | if ($sortBy && isset($this->table->columns[$sortBy]) && $this->model->hasField($sortBy)) { |
| 604: | $this->model->setOrder($sortBy, $isDesc ? 'desc' : 'asc'); |
| 605: | $this->table->sortBy = $sortBy; |
| 606: | $this->table->sortDirection = $isDesc ? 'desc' : 'asc'; |
| 607: | } |
| 608: | |
| 609: | $this->table->on( |
| 610: | 'click', |
| 611: | 'thead>tr>th.sortable', |
| 612: | new JsReload($this->container, [$this->sortTrigger => (new Jquery())->data('sort')]) |
| 613: | ); |
| 614: | } |
| 615: | |
| 616: | |
| 617: | |
| 618: | |
| 619: | #[\Override] |
| 620: | public function setModel(Model $model, array $fields = null): void |
| 621: | { |
| 622: | $this->table->setModel($model, $fields); |
| 623: | |
| 624: | parent::setModel($model); |
| 625: | |
| 626: | if ($this->searchFieldNames) { |
| 627: | $this->addQuickSearch($this->searchFieldNames, true); |
| 628: | } |
| 629: | } |
| 630: | |
| 631: | |
| 632: | |
| 633: | |
| 634: | |
| 635: | |
| 636: | |
| 637: | public function addSelection() |
| 638: | { |
| 639: | $this->selection = $this->table->addColumn(null, [Table\Column\Checkbox::class]); |
| 640: | |
| 641: | |
| 642: | array_unshift($this->table->columns, array_pop($this->table->columns)); |
| 643: | |
| 644: | return $this->selection; |
| 645: | } |
| 646: | |
| 647: | |
| 648: | |
| 649: | |
| 650: | |
| 651: | |
| 652: | |
| 653: | public function addDragHandler() |
| 654: | { |
| 655: | $handler = $this->table->addColumn(null, [Table\Column\DragHandler::class]); |
| 656: | |
| 657: | |
| 658: | array_unshift($this->table->columns, array_pop($this->table->columns)); |
| 659: | |
| 660: | return $handler; |
| 661: | } |
| 662: | |
| 663: | private function setModelLimitFromPaginator(): void |
| 664: | { |
| 665: | $this->paginator->setTotal((int) ceil($this->model->executeCountQuery() / $this->ipp)); |
| 666: | $this->model->setLimit($this->ipp, ($this->paginator->page - 1) * $this->ipp); |
| 667: | } |
| 668: | |
| 669: | #[\Override] |
| 670: | protected function renderView(): void |
| 671: | { |
| 672: | |
| 673: | if (!$this->table->jsPaginator) { |
| 674: | $this->applySort(); |
| 675: | } |
| 676: | |
| 677: | parent::renderView(); |
| 678: | } |
| 679: | |
| 680: | #[\Override] |
| 681: | protected function recursiveRender(): void |
| 682: | { |
| 683: | |
| 684: | if ($this->paginator) { |
| 685: | $this->setModelLimitFromPaginator(); |
| 686: | } |
| 687: | |
| 688: | if ($this->quickSearch instanceof JsSearch) { |
| 689: | $sortBy = $this->getSortBy(); |
| 690: | if ($sortBy) { |
| 691: | $this->container->js(true, $this->quickSearch->js()->atkJsSearch('setUrlArgs', [$this->sortTrigger, $sortBy])); |
| 692: | } |
| 693: | } |
| 694: | |
| 695: | parent::recursiveRender(); |
| 696: | } |
| 697: | |
| 698: | |
| 699: | |
| 700: | |
| 701: | |
| 702: | |
| 703: | public function jsRow(): JsExpressionable |
| 704: | { |
| 705: | return $this->table->jsRow(); |
| 706: | } |
| 707: | } |
| 708: | |