Skip to content

Commit

Permalink
[FEATURE] Add event to modify buttons in backend module (#592)
Browse files Browse the repository at this point in the history
* [TASK] Add event to modify buttons in backend module
* [TASK] Reduce get*ButtonButtons in OrderController to one method
* [TASK] Add documentation for the new event

Related: #588
  • Loading branch information
extcode authored Oct 23, 2024
1 parent e35ac66 commit 9bf8d3d
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 123 deletions.
162 changes: 40 additions & 122 deletions Classes/Controller/Backend/Order/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Repository\Order\ItemRepository;
use Extcode\Cart\Event\Order\NumberGeneratorEvent;
use Extcode\Cart\Event\Template\Components\ModifyButtonBarEvent;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function listAction(int $currentPage = 1): ResponseInterface
{
$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);

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

$this->moduleTemplate->assign('settings', $this->settings);
Expand Down Expand Up @@ -94,33 +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);
$buttons = $this->getOrderButtons($orderItem);
$this->setDocHeader($buttons);
$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,99 +239,35 @@ protected function getLanguageService(): LanguageService
return $GLOBALS['LANG'];
}

private function getOrderButtons(Item $orderItem): array
private function dispatchModifyButtonBarEvent(?Item $orderItem = null): array
{
$buttons = [
[
'link' => $this->uriBuilder->reset()->setRequest($this->request)
->uriFor(
'list'
),
'title' => 'tx_cart.controller.order.action.close',
'icon' => 'actions-close',
'group' => 1,
'showLabel' => true,
],
];

$buttons = array_merge($buttons, $this->getDocumentButtons($orderItem, 'order', 2));
$buttons = array_merge($buttons, $this->getDocumentButtons($orderItem, 'invoice', 3));
$buttons = array_merge($buttons, $this->getDocumentButtons($orderItem, 'delivery', 4));

return $buttons;
$modifyButtonBarEvent = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments,
$orderItem
);

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

return $modifyButtonBarEvent->getButtons();
}

private function getDocumentButtons(Item $orderItem, string $type, int $groupId): array
private function setDocHeader(array $buttons): void
{
$buttons = [];

$numberGetter = 'get' . ucfirst($type) . 'Number';
$documentGetter = 'get' . ucfirst($type) . 'Pdfs';

$numberExists = $orderItem->$numberGetter();
$documentExists = $orderItem->$documentGetter()->current();

if (!$numberExists) {
$buttons[] = [
'link' => $this->uriBuilder->reset()->setRequest($this->request)
->uriFor(
'generateNumber',
['orderItem' => $orderItem, 'numberType' => $type]
),
'title' => 'tx_cart.controller.order.action.generate' . ucfirst($type) . 'Number',
'icon' => 'actions-duplicates',
'group' => $groupId,
'showLabel' => true,
];
}
$buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();

if ($numberExists && ExtensionManagementUtility::isLoaded('cart_pdf')) {
$buttons[] = [
'link' => $this->uriBuilder->reset()->setRequest($this->request)
->uriFor(
'create',
['orderItem' => $orderItem, 'pdfType' => $type],
'Backend\Order\Document'
),
'title' => 'tx_cart.controller.order.action.generate' . ucfirst($type) . 'Document',
'icon' => 'actions-file-pdf',
'group' => $groupId,
'showLabel' => true,
];
}
foreach ($buttons as $button) {
$title = $this->getLanguageService()->sL(self::LANG_FILE . $button['title']);
$icon = $this->iconFactory->getIcon($button['icon'], Icon::SIZE_SMALL);

if ($documentExists) {
$buttons[] = [
'link' => $this->uriBuilder->reset()->setRequest($this->request)
->uriFor(
'download',
['orderItem' => $orderItem, 'pdfType' => $type],
'Backend\Order\Document'
),
'title' => 'tx_cart.controller.order.action.download' . ucfirst($type) . 'Document',
'icon' => 'actions-file-t3d-download',
'group' => $groupId,
'showLabel' => true,
];
$viewButton = $buttonBar->makeLinkButton()
->setHref($button['link'])
->setTitle($title)
->setShowLabelText($button['showLabel'])
->setIcon($icon);
$buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, $button['group']);
}

return $buttons;
}

private function getListButtons(): array
{
return [
[
'link' => $this->uriBuilder->reset()->setRequest($this->request)
->setArguments(['searchArguments' => $this->searchArguments])
->setFormat('csv')
->uriFor('export'),
'title' => 'tx_cart.controller.order.action.export.csv',
'icon' => 'actions-file-csv-download',
'group' => 1,
'showLabel' => true,
],
];
}

private function addBackendAssets(): void
Expand Down
57 changes: 57 additions & 0 deletions Classes/Event/Template/Components/ModifyButtonBarEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Event\Template\Components;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Domain\Model\Order\Item;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;

final class ModifyButtonBarEvent
{
private array $buttons = [];

public function __construct(
private readonly RequestInterface $request,
private readonly array $settings,
private readonly array $searchArguments,
private readonly ?Item $orderItem = null,
) {}

public function getRequest(): RequestInterface
{
return $this->request;
}

public function getSettings(): array
{
return $this->settings;
}

public function getSearchArguments(): array
{
return $this->searchArguments;
}

public function getOrderItem(): ?Item
{
return $this->orderItem;
}

public function getButtons(): array
{
return $this->buttons;
}

public function setButtons(array $buttons): void
{
$this->buttons = $buttons;
}
}
Loading

0 comments on commit 9bf8d3d

Please sign in to comment.