Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add events to add buttons to backend module #592

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading