|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace App\Twig\Component\Grid; |
| 15 | + |
| 16 | +use Pagerfanta\PagerfantaInterface; |
| 17 | +use Sylius\Component\Grid\Parameters; |
| 18 | +use Sylius\Component\Grid\Provider\ChainProvider; |
| 19 | +use Sylius\Component\Grid\Provider\GridProviderInterface; |
| 20 | +use Sylius\Component\Grid\View\GridViewFactoryInterface; |
| 21 | +use Sylius\Component\Grid\View\GridViewInterface; |
| 22 | +use Sylius\TwigHooks\Twig\Component\HookableComponentTrait; |
| 23 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
| 26 | +use Symfony\UX\LiveComponent\Attribute\LiveAction; |
| 27 | +use Symfony\UX\LiveComponent\Attribute\LiveArg; |
| 28 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 29 | +use Symfony\UX\LiveComponent\DefaultActionTrait; |
| 30 | + |
| 31 | +#[AsLiveComponent( |
| 32 | + name: 'sylius_grid_data_table', |
| 33 | + template: '@SyliusBootstrapAdminUi/shared/crud/index/content/grid/data_table.html.twig', |
| 34 | +)] |
| 35 | +final class DataTableComponent |
| 36 | +{ |
| 37 | + use DefaultActionTrait; |
| 38 | + use HookableComponentTrait; |
| 39 | + |
| 40 | + #[LiveProp(writable: true)] |
| 41 | + public ?string $grid = null; |
| 42 | + |
| 43 | + #[LiveProp(writable: true)] |
| 44 | + public int $page; |
| 45 | + |
| 46 | + /** @var array<string, mixed>|null */ |
| 47 | + #[LiveProp(writable: true)] |
| 48 | + public ?array $criteria = null; |
| 49 | + |
| 50 | + /** @var array<string, string>|null */ |
| 51 | + #[LiveProp(writable: true)] |
| 52 | + public ?array $sorting = null; |
| 53 | + |
| 54 | + #[LiveProp(writable: true)] |
| 55 | + public ?int $limit = null; |
| 56 | + |
| 57 | + #[LiveProp(writable: true)] |
| 58 | + public bool $pushOnBrowserHistory = true; |
| 59 | + |
| 60 | + private ?Request $request; |
| 61 | + |
| 62 | + public function __construct( |
| 63 | + #[Autowire(service: ChainProvider::class)] |
| 64 | + private readonly GridProviderInterface $gridProvider, |
| 65 | + private readonly GridViewFactoryInterface $gridViewFactory, |
| 66 | + ) { |
| 67 | + } |
| 68 | + |
| 69 | + public function getResources(): GridViewInterface |
| 70 | + { |
| 71 | + if (null === $this->grid) { |
| 72 | + throw new \RuntimeException('No Grid has been passed to the component.'); |
| 73 | + } |
| 74 | + |
| 75 | + $gridDefinition = $this->gridProvider->get($this->grid); |
| 76 | + |
| 77 | + $config = ['page' => $this->page]; |
| 78 | + |
| 79 | + if (null !== $this->criteria) { |
| 80 | + $config['criteria'] = $this->criteria; |
| 81 | + } |
| 82 | + |
| 83 | + if (null !== $this->sorting) { |
| 84 | + $config['sorting'] = $this->sorting; |
| 85 | + } |
| 86 | + |
| 87 | + $gridView = $this->gridViewFactory->create( |
| 88 | + $gridDefinition, |
| 89 | + new Parameters($config), |
| 90 | + ); |
| 91 | + |
| 92 | + $data = $gridView->getData(); |
| 93 | + |
| 94 | + if ($data instanceof PagerfantaInterface) { |
| 95 | + $data->setCurrentPage($this->page); |
| 96 | + |
| 97 | + if (null !== $this->limit) { |
| 98 | + $data->setMaxPerPage($this->limit); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return $gridView; |
| 103 | + } |
| 104 | + |
| 105 | + #[LiveAction] |
| 106 | + public function sortBy(#[LiveArg] string $field, #[LiveArg] string $order): void |
| 107 | + { |
| 108 | + $this->sorting = [$field => $order]; |
| 109 | + } |
| 110 | + |
| 111 | + #[LiveAction] |
| 112 | + public function updateLimit(#[LiveArg] int $limit): void |
| 113 | + { |
| 114 | + $this->page = 1; |
| 115 | + $this->criteria = null; |
| 116 | + $this->sorting = null; |
| 117 | + $this->limit = $limit; |
| 118 | + } |
| 119 | + |
| 120 | + #[LiveAction] |
| 121 | + public function changePage(#[LiveArg] int $page): void |
| 122 | + { |
| 123 | + $this->page = $page; |
| 124 | + } |
| 125 | + |
| 126 | + #[LiveAction] |
| 127 | + public function previousPage(): void |
| 128 | + { |
| 129 | + --$this->page; |
| 130 | + } |
| 131 | + |
| 132 | + #[LiveAction] |
| 133 | + public function nextPage(): void |
| 134 | + { |
| 135 | + ++$this->page; |
| 136 | + } |
| 137 | +} |
0 commit comments