Skip to content

Commit

Permalink
[TASK] Readd and fix BE list and order actions (#463)
Browse files Browse the repository at this point in the history
* [TASK] Readd BE actions for orders

The way to add actions in the docheader changed
with https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.5/Deprecation-95164-ExtbackendBackendTemplateView.html
which needs adaptions.

Fixes also the CSV export.

This also fixes the issue that numbers could
not be generated because `!isset('')` returns
false.

* [TASK] Code improvements after review

Adaptions in `OrderController`:
* Add searchArguments to export,
* Follow CGL
* Remove switch codeblock.
  • Loading branch information
rintisch authored Apr 2, 2024
1 parent 5a341cb commit 513d973
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 91 deletions.
140 changes: 133 additions & 7 deletions Classes/Controller/Backend/Order/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Controller\Backend\ActionController;
use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Repository\Order\ItemRepository;
use Extcode\Cart\Event\Order\NumberGeneratorEvent;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Expand All @@ -29,6 +33,8 @@

class OrderController extends ActionController
{
private const LANG_FILE = 'LLL:EXT:cart/Resources/Private/Language/locallang.xlf:';

protected PersistenceManager $persistenceManager;

private ModuleTemplate $moduleTemplate;
Expand Down Expand Up @@ -65,6 +71,8 @@ public function listAction(int $currentPage = 1): ResponseInterface
{
$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);

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

$this->moduleTemplate->assign('settings', $this->settings);
$this->moduleTemplate->assign('searchArguments', $this->searchArguments);

Expand Down Expand Up @@ -98,7 +106,6 @@ public function listAction(int $currentPage = 1): ResponseInterface
public function exportAction(): ResponseInterface
{
$format = $this->request->getFormat();

$orderItems = $this->itemRepository->findAll($this->searchArguments);

$this->view->assign('searchArguments', $this->searchArguments);
Expand All @@ -110,12 +117,11 @@ public function exportAction(): ResponseInterface
$title = 'Order-Export-' . date('Y-m-d_H-i');
$filename = $title . '.' . $format;

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

return $this->htmlResponse(null);
->withAddedHeader('Content-Disposition', 'attachment; filename="' . $filename . '"')
->withBody($this->streamFactory->createStream($this->view->render()));
}

/**
Expand All @@ -124,6 +130,8 @@ 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->moduleTemplate->assign('settings', $this->settings);
$this->moduleTemplate->assign('orderItem', $orderItem);
Expand Down Expand Up @@ -154,7 +162,25 @@ public function showAction(Item $orderItem): ResponseInterface
return $this->moduleTemplate->renderResponse('Show');
}

public function generateNumberAction(Item $orderItem, string $numberType): void
private function setDocHeader(array $buttons): void
{
$buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();

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

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

}

public function generateNumberAction(Item $orderItem, string $numberType): ResponseInterface
{
$getNumber = 'get' . ucfirst($numberType) . 'Number';

Expand Down Expand Up @@ -182,7 +208,7 @@ public function generateNumberAction(Item $orderItem, string $numberType): void
$this->addFlashMessage($msg, '', ContextualFeedbackSeverity::ERROR);
}

$this->redirect('show', 'Backend\Order\Order', null, ['orderItem' => $orderItem]);
return $this->redirect('show', 'Backend\Order\Order', null, ['orderItem' => $orderItem]);
}

public function getPaymentStatus(): array
Expand Down Expand Up @@ -234,4 +260,104 @@ public function getShippingStatus(): array
}
return $shippingStatusArray;
}

protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}

private function getOrderButtons(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,
];
}

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,
];
}

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,
],
];
}
}
12 changes: 6 additions & 6 deletions Classes/Domain/Model/Order/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class Item extends AbstractEntity

protected ?FrontendUser $feUser = null;

protected string $orderNumber;
protected string $orderNumber = '';

protected ?\DateTime $orderDate = null;

protected string $invoiceNumber;
protected string $invoiceNumber = '';

protected ?\DateTime $invoiceDate = null;

protected string $deliveryNumber;
protected string $deliveryNumber = '';

protected ?\DateTime $deliveryDate = null;

Expand Down Expand Up @@ -166,7 +166,7 @@ public function getOrderNumber(): ?string
*/
public function setOrderNumber(string $orderNumber): string
{
if (!isset($this->orderNumber)) {
if (empty($this->orderNumber)) {
$this->orderNumber = $orderNumber;
} else {
if ($this->orderNumber !== $orderNumber) {
Expand Down Expand Up @@ -196,7 +196,7 @@ public function getInvoiceNumber(): ?string
*/
public function setInvoiceNumber(string $invoiceNumber): string
{
if (!isset($this->invoiceNumber)) {
if (empty($this->invoiceNumber)) {
$this->invoiceNumber = $invoiceNumber;
} else {
if ($this->invoiceNumber !== $invoiceNumber) {
Expand Down Expand Up @@ -226,7 +226,7 @@ public function getDeliveryNumber(): ?string
*/
public function setDeliveryNumber(string $deliveryNumber): string
{
if (!isset($this->deliveryNumber)) {
if (empty($this->deliveryNumber)) {
$this->deliveryNumber = $deliveryNumber;
} else {
if ($this->deliveryNumber !== $deliveryNumber) {
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@
<trans-unit id="tx_cart.controller.order.action.list">
<source>List</source>
</trans-unit>
<trans-unit id="tx_cart.controller.order.action.close">
<source>Close</source>
</trans-unit>
<trans-unit id="tx_cart.controller.order.action.export.csv">
<source>Export (CSV)</source>
</trans-unit>
Expand Down
78 changes: 0 additions & 78 deletions Resources/Private/Partials/Backend/Order/Show/Actions.html

This file was deleted.

0 comments on commit 513d973

Please sign in to comment.