|
16 | 16 | use App\Repository\ArtworkRepository;
|
17 | 17 | use App\Repository\ItemRepository;
|
18 | 18 | use Doctrine\ORM\EntityManagerInterface;
|
| 19 | +use Doctrine\ORM\Query; |
19 | 20 | use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate;
|
20 | 21 | use Knp\Component\Pager\PaginatorInterface;
|
21 | 22 | use OpenSpout\Common\Entity\Row;
|
@@ -63,70 +64,9 @@ public function index(Request $request, ItemRepository $itemRepository, Paginato
|
63 | 64 | #[Route(path: '/list/{itemType}', name: 'item_list', methods: ['GET'], defaults: ['itemType' => Artwork::ITEM_TYPE])]
|
64 | 65 | public function list(string $itemType, Request $request, ItemRepository $itemRepository, ArtworkRepository $artworkRepository, PaginatorInterface $paginator): Response
|
65 | 66 | {
|
66 |
| - $parameters = []; |
67 | 67 | $parameters['display_advanced_filters'] = false;
|
68 | 68 |
|
69 |
| - $itemTypeClass = $this->getItemTypeClass($itemType); |
70 |
| - $form = $this->getSearchForm($itemTypeClass); |
71 |
| - |
72 |
| - $form->handleRequest($request); |
73 |
| - |
74 |
| - if ($form->isSubmitted() && $form->isValid()) { |
75 |
| - $data = $form->getData(); |
76 |
| - |
77 |
| - $width = null !== $data['width'] ? json_decode($data['width']) : null; |
78 |
| - $height = null !== $data['height'] ? json_decode($data['height']) : null; |
79 |
| - |
80 |
| - switch ($itemType) { |
81 |
| - case 'artwork': |
82 |
| - $query = $artworkRepository->getQuery( |
83 |
| - $data['search'], |
84 |
| - $data['type'], |
85 |
| - $data['status'] ?? null, |
86 |
| - null, |
87 |
| - $data['building'] ?? null, |
88 |
| - $data['yearFrom'] ?? null, |
89 |
| - $data['yearTo'] ?? null, |
90 |
| - $width->min ?? null, |
91 |
| - $width->max ?? null, |
92 |
| - $height->min ?? null, |
93 |
| - $height->max ?? null, |
94 |
| - $data['artistGender'] ?? null, |
95 |
| - $data['priceFrom'] ?? null, |
96 |
| - $data['priceTo'] ?? null |
97 |
| - ); |
98 |
| - break; |
99 |
| - case 'furniture': |
100 |
| - default: |
101 |
| - $query = $itemRepository->getQuery( |
102 |
| - $itemTypeClass, |
103 |
| - $data['search'], |
104 |
| - $data['type'], |
105 |
| - null, |
106 |
| - $data['building'] |
107 |
| - ); |
108 |
| - } |
109 |
| - |
110 |
| - if (null !== $data['width'] |
111 |
| - || null !== $data['height'] |
112 |
| - || null !== $data['status'] |
113 |
| - || null !== $data['yearFrom'] |
114 |
| - || null !== $data['yearTo'] |
115 |
| - || null !== $data['artistGender'] |
116 |
| - || null !== $data['priceFrom'] |
117 |
| - || null !== $data['priceTo']) { |
118 |
| - $parameters['display_advanced_filters'] = true; |
119 |
| - } |
120 |
| - } else { |
121 |
| - switch ($itemType) { |
122 |
| - case 'artwork': |
123 |
| - $query = $artworkRepository->getQuery(); |
124 |
| - break; |
125 |
| - case 'furniture': |
126 |
| - default: |
127 |
| - $query = $itemRepository->getQuery($itemTypeClass); |
128 |
| - } |
129 |
| - } |
| 69 | + [$query, $form] = $this->getFilteredQuery($itemType, $request, $itemRepository, $artworkRepository, $parameters); |
130 | 70 |
|
131 | 71 | $pagination = $paginator->paginate(
|
132 | 72 | $query,
|
@@ -160,20 +100,15 @@ public function list(string $itemType, Request $request, ItemRepository $itemRep
|
160 | 100 | * @return Response
|
161 | 101 | */
|
162 | 102 | #[Route(path: '/{itemType}/export', name: 'item_export', methods: ['GET'])]
|
163 |
| - public function export(string $itemType, EntityManagerInterface $entityManager): Response |
| 103 | + public function export(string $itemType, Request $request, ItemRepository $itemRepository, ArtworkRepository $artworkRepository): Response |
164 | 104 | {
|
| 105 | + [$query] = $this->getFilteredQuery($itemType, $request, $itemRepository, $artworkRepository); |
| 106 | + |
165 | 107 | // Avoid php timeout errors.
|
166 | 108 | set_time_limit(0);
|
167 | 109 | $response = new StreamedResponse();
|
168 | 110 |
|
169 |
| - $itemTypeClass = $this->getItemTypeClass($itemType); |
170 |
| - $itemRepository = $entityManager->getRepository($itemTypeClass); |
171 |
| - |
172 |
| - $response->setCallback(function () use ($itemRepository) { |
173 |
| - $query = $itemRepository |
174 |
| - ->createQueryBuilder('e') |
175 |
| - ->getQuery(); |
176 |
| - |
| 111 | + $callback = function () use ($query) { |
177 | 112 | $iterableItems = SimpleBatchIteratorAggregate::fromQuery(
|
178 | 113 | $query,
|
179 | 114 | 100
|
@@ -267,7 +202,9 @@ public function export(string $itemType, EntityManagerInterface $entityManager):
|
267 | 202 | }
|
268 | 203 |
|
269 | 204 | $writer->close();
|
270 |
| - }); |
| 205 | + }; |
| 206 | + |
| 207 | + $response->setCallback($callback); |
271 | 208 |
|
272 | 209 | $filename = sprintf('%s-eksport-%s', $itemType, (new \DateTimeImmutable())->format('d-m-Y'));
|
273 | 210 | $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
@@ -572,4 +509,79 @@ private function getItemTypeClass(string $itemType): string
|
572 | 509 | default => Item::class
|
573 | 510 | };
|
574 | 511 | }
|
| 512 | + |
| 513 | + /** |
| 514 | + * @return array |
| 515 | + * [Query, Form] |
| 516 | + */ |
| 517 | + private function getFilteredQuery(string $itemType, Request $request, ItemRepository $itemRepository, ArtworkRepository $artworkRepository, array &$parameters = []): array |
| 518 | + { |
| 519 | + $itemTypeClass = $this->getItemTypeClass($itemType); |
| 520 | + $form = $this->getSearchForm($itemTypeClass); |
| 521 | + |
| 522 | + $form->handleRequest($request); |
| 523 | + |
| 524 | + if ($form->isSubmitted() && $form->isValid()) { |
| 525 | + $data = $form->getData(); |
| 526 | + |
| 527 | + if (null !== $data['width'] |
| 528 | + || null !== $data['height'] |
| 529 | + || null !== $data['status'] |
| 530 | + || null !== $data['yearFrom'] |
| 531 | + || null !== $data['yearTo'] |
| 532 | + || null !== $data['artistGender'] |
| 533 | + || null !== $data['priceFrom'] |
| 534 | + || null !== $data['priceTo']) { |
| 535 | + $parameters['display_advanced_filters'] = true; |
| 536 | + } |
| 537 | + |
| 538 | + $width = null !== $data['width'] ? json_decode($data['width']) : null; |
| 539 | + $height = null !== $data['height'] ? json_decode($data['height']) : null; |
| 540 | + |
| 541 | + switch ($itemType) { |
| 542 | + case 'artwork': |
| 543 | + $query = $artworkRepository->getQuery( |
| 544 | + $data['search'], |
| 545 | + $data['type'], |
| 546 | + $data['status'] ?? null, |
| 547 | + null, |
| 548 | + $data['building'] ?? null, |
| 549 | + $data['yearFrom'] ?? null, |
| 550 | + $data['yearTo'] ?? null, |
| 551 | + $width->min ?? null, |
| 552 | + $width->max ?? null, |
| 553 | + $height->min ?? null, |
| 554 | + $height->max ?? null, |
| 555 | + $data['artistGender'] ?? null, |
| 556 | + $data['priceFrom'] ?? null, |
| 557 | + $data['priceTo'] ?? null |
| 558 | + ); |
| 559 | + break; |
| 560 | + |
| 561 | + case 'furniture': |
| 562 | + default: |
| 563 | + $query = $itemRepository->getQuery( |
| 564 | + $itemTypeClass, |
| 565 | + $data['search'], |
| 566 | + $data['type'], |
| 567 | + null, |
| 568 | + $data['building'] |
| 569 | + ); |
| 570 | + break; |
| 571 | + } |
| 572 | + } else { |
| 573 | + switch ($itemType) { |
| 574 | + case 'artwork': |
| 575 | + $query = $artworkRepository->getQuery(); |
| 576 | + break; |
| 577 | + |
| 578 | + case 'furniture': |
| 579 | + default: |
| 580 | + $query = $itemRepository->getQuery($itemTypeClass); |
| 581 | + break; |
| 582 | + } |
| 583 | + } |
| 584 | + |
| 585 | + return [$query, $form]; |
| 586 | + } |
575 | 587 | }
|
0 commit comments