Skip to content

Commit

Permalink
[FEATURE] Add events to add buttons to backend module
Browse files Browse the repository at this point in the history
Relates: #588
  • Loading branch information
extcode committed Oct 22, 2024
1 parent e35ac66 commit d8bdfec
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 90 deletions.
112 changes: 22 additions & 90 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->getListActionButtons());
$this->addBackendAssets();

$this->moduleTemplate->assign('settings', $this->settings);
Expand Down Expand Up @@ -119,8 +120,7 @@ public function exportAction(): ResponseInterface
public function showAction(Item $orderItem): ResponseInterface
{
$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);
$buttons = $this->getOrderButtons($orderItem);
$this->setDocHeader($buttons);
$this->setDocHeader($this->getShowActionButtons($orderItem));

$this->addBackendAssets();

Expand Down Expand Up @@ -257,99 +257,31 @@ protected function getLanguageService(): LanguageService
return $GLOBALS['LANG'];
}

private function getOrderButtons(Item $orderItem): array
private function getShowActionButtons(Item $orderItem): 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;
}

private function getDocumentButtons(Item $orderItem, string $type, int $groupId): array
{
$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,
];
}

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,
];
}
$showButtonEvent = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments,
$orderItem
);

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,
];
}
$this->eventDispatcher->dispatch($showButtonEvent);

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

private function getListButtons(): array
private function getListActionButtons(): 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,
],
];
$listButtonEvent = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments
);

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

return $listButtonEvent->getButtons();
}

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

declare(strict_types=1);

namespace Extcode\Cart\Event\Template\Components;

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;
}
}
150 changes: 150 additions & 0 deletions Classes/EventListener/Template/Components/ModifyButtonBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Extcode\Cart\EventListener\Template\Components;

use Extcode\Cart\Event\Template\Components\ModifyButtonBarEvent;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;

class ModifyButtonBar
{
public function __construct(
private readonly UriBuilder $uriBuilder
) {}

public function __invoke(ModifyButtonBarEvent $event): void
{
$request = $event->getRequest();

if ($request->getControllerName() !== 'Backend\Order\Order') {
return;
}

if ($request->getControllerActionName() === 'list') {
$this->modifyListActionButtons($event);
return;
}

if ($request->getControllerActionName() === 'show') {
$this->modifyShowActionButtons($event);
return;
}
}

private function modifyListActionButtons(ModifyButtonBarEvent $event)
{
$event->setButtons(
array_merge(
$event->getButtons(),
[ 'action-export-csv' => [
'link' => $this->uriBuilder->reset()->setRequest($event->getRequest())
->setArguments(['searchArguments' => $event->getSearchArguments()])
->setFormat('csv')
->uriFor('export'),
'title' => 'tx_cart.controller.order.action.export.csv',
'icon' => 'actions-file-csv-download',
'group' => 1,
'showLabel' => true,
],
]
)
);
}

private function modifyShowActionButtons(ModifyButtonBarEvent $event)
{
$event->setButtons(
array_merge(
$event->getButtons(),
[ 'action-close' => [
'link' => $this->uriBuilder->reset()->setRequest($event->getRequest())
->uriFor(
'list'
),
'title' => 'tx_cart.controller.order.action.close',
'icon' => 'actions-close',
'group' => 1,
'showLabel' => true,
],
]
)
);

$event->setButtons(
array_merge(
$event->getButtons(),
$this->getDocumentButtons($event, 'order', 2)
)
);
$event->setButtons(
array_merge(
$event->getButtons(),
$this->getDocumentButtons($event, 'invoice', 3)
)
);
$event->setButtons(
array_merge(
$event->getButtons(),
$this->getDocumentButtons($event, 'delivery', 4)
)
);
}

private function getDocumentButtons(ModifyButtonBarEvent $event, string $type, int $groupId): array
{
$buttons = [];

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

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

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

// ToDo: this could be moved to an EventListener to extcode/cart-pdf
if ($numberExists && ExtensionManagementUtility::isLoaded('cart_pdf')) {
$buttons['action-create-' . $type . '-pdf'] = [
'link' => $this->uriBuilder->reset()->setRequest($event->getRequest())
->uriFor(
'create',
['orderItem' => $event->getOrderItem(), 'pdfType' => $type],
'Backend\Order\Document'
),
'title' => 'tx_cart.controller.order.action.generate' . ucfirst($type) . 'Document',
'icon' => 'actions-file-pdf',
'group' => $groupId,
'showLabel' => true,
];
}

if ($documentExists) {
$buttons['action-download-' . $type . '-pdf'] = [
'link' => $this->uriBuilder->reset()->setRequest($event->getRequest())
->uriFor(
'download',
['orderItem' => $event->getOrderItem(), 'pdfType' => $type],
'Backend\Order\Document'
),
'title' => 'tx_cart.controller.order.action.download' . ucfirst($type) . 'Document',
'icon' => 'actions-file-t3d-download',
'group' => $groupId,
'showLabel' => true,
];
}

return $buttons;
}
}
12 changes: 12 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ services:
resource: '../Classes/*'
exclude: '../Classes/Widgets/*'

Extcode\Cart\EventListener\Template\Components\ModifyButtonBar:
tags:
- name: event.listener
identifier: 'cart--cart--template--components--modify-button-bar'
event: Extcode\Cart\Event\Template\Components\ModifyButtonBarEvent

Extcode\Cart\EventListener\Backend\Order\ShowButton:
tags:
- name: event.listener
identifier: 'cart--cart--backend-order-show-button'
event: Extcode\Cart\Event\Backend\Order\ShowButtonEvent

Extcode\Cart\EventListener\Cart\UpdateCountry:
tags:
- name: event.listener
Expand Down

0 comments on commit d8bdfec

Please sign in to comment.