Skip to content

Commit

Permalink
[TASK] Reduce get*ButtonButtons to one method
Browse files Browse the repository at this point in the history
Related: #588
  • Loading branch information
extcode committed Oct 23, 2024
1 parent e5ebb63 commit bb7005a
Showing 1 changed file with 33 additions and 47 deletions.
80 changes: 33 additions & 47 deletions Classes/Controller/Backend/Order/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function listAction(int $currentPage = 1): ResponseInterface
{
$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);

$this->setDocHeader($this->getListActionButtons());
$this->setDocHeader($this->dispatchModifyButtonBarEvent());
$this->addBackendAssets();

$this->moduleTemplate->assign('settings', $this->settings);
Expand Down Expand Up @@ -95,32 +95,11 @@ public function listAction(int $currentPage = 1): ResponseInterface
return $this->moduleTemplate->renderResponse('List');
}

public function exportAction(): ResponseInterface
{
$format = $this->request->getFormat();
$orderItems = $this->itemRepository->findAll($this->searchArguments);

$this->view->assign('searchArguments', $this->searchArguments);
$this->view->assign('orderItems', $orderItems);

$pdfRendererInstalled = ExtensionManagementUtility::isLoaded('cart_pdf');
$this->view->assign('pdfRendererInstalled', $pdfRendererInstalled);

$title = 'Order-Export-' . date('Y-m-d_H-i');
$filename = $title . '.' . $format;

return $this->responseFactory->createResponse()
->withAddedHeader('Content-Type', 'text/' . $format)
->withAddedHeader('Content-Description', 'File transfer')
->withAddedHeader('Content-Disposition', 'attachment; filename="' . $filename . '"')
->withBody($this->streamFactory->createStream($this->view->render()));
}

#[IgnoreValidation(['value' => 'orderItem'])]
public function showAction(Item $orderItem): ResponseInterface
{
$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);
$this->setDocHeader($this->getShowActionButtons($orderItem));
$this->setDocHeader($this->dispatchModifyButtonBarEvent($orderItem));

$this->addBackendAssets();

Expand Down Expand Up @@ -153,22 +132,25 @@ public function showAction(Item $orderItem): ResponseInterface
return $this->moduleTemplate->renderResponse('Show');
}

private function setDocHeader(array $buttons): void
public function exportAction(): ResponseInterface
{
$buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
$format = $this->request->getFormat();
$orderItems = $this->itemRepository->findAll($this->searchArguments);

foreach ($buttons as $button) {
$title = $this->getLanguageService()->sL(self::LANG_FILE . $button['title']);
$icon = $this->iconFactory->getIcon($button['icon'], Icon::SIZE_SMALL);
$this->view->assign('searchArguments', $this->searchArguments);
$this->view->assign('orderItems', $orderItems);

$viewButton = $buttonBar->makeLinkButton()
->setHref($button['link'])
->setTitle($title)
->setShowLabelText($button['showLabel'])
->setIcon($icon);
$buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, $button['group']);
}
$pdfRendererInstalled = ExtensionManagementUtility::isLoaded('cart_pdf');
$this->view->assign('pdfRendererInstalled', $pdfRendererInstalled);

$title = 'Order-Export-' . date('Y-m-d_H-i');
$filename = $title . '.' . $format;

return $this->responseFactory->createResponse()
->withAddedHeader('Content-Type', 'text/' . $format)
->withAddedHeader('Content-Description', 'File transfer')
->withAddedHeader('Content-Disposition', 'attachment; filename="' . $filename . '"')
->withBody($this->streamFactory->createStream($this->view->render()));
}

public function generateNumberAction(Item $orderItem, string $numberType): ResponseInterface
Expand Down Expand Up @@ -257,31 +239,35 @@ protected function getLanguageService(): LanguageService
return $GLOBALS['LANG'];
}

private function getShowActionButtons(Item $orderItem): array
private function dispatchModifyButtonBarEvent(?Item $orderItem = null): array
{
$showButtonEvent = new ModifyButtonBarEvent(
$modifyButtonBarEvent = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments,
$orderItem
);

$this->eventDispatcher->dispatch($showButtonEvent);
$this->eventDispatcher->dispatch($modifyButtonBarEvent);

return $showButtonEvent->getButtons();
return $modifyButtonBarEvent->getButtons();
}

private function getListActionButtons(): array
private function setDocHeader(array $buttons): void
{
$listButtonEvent = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments
);
$buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();

$this->eventDispatcher->dispatch($listButtonEvent);
foreach ($buttons as $button) {
$title = $this->getLanguageService()->sL(self::LANG_FILE . $button['title']);
$icon = $this->iconFactory->getIcon($button['icon'], Icon::SIZE_SMALL);

return $listButtonEvent->getButtons();
$viewButton = $buttonBar->makeLinkButton()
->setHref($button['link'])
->setTitle($title)
->setShowLabelText($button['showLabel'])
->setIcon($icon);
$buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, $button['group']);
}
}

private function addBackendAssets(): void
Expand Down

0 comments on commit bb7005a

Please sign in to comment.