<?php
declare(strict_types=1);
namespace Bluue\SalesBundle\EventSubscriber;
use App\Event\MassManagementListButtonsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConfigureListButtonsSubscriber implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private RouterInterface $router;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var Security
*/
private Security $security;
public function __construct(
RouterInterface $router,
TranslatorInterface $tr,
Security $security
) {
$this->router = $router;
$this->tr = $tr;
$this->security = $security;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
MassManagementListButtonsEvent::NAME => 'onLoad',
];
}
/**
* @param MassManagementListButtonsEvent $event
* @return void
*/
public function onLoad(MassManagementListButtonsEvent $event): void
{
$buttons = $event->getButtons();
$types = ['order', 'invoice', 'delivery_note', 'credit_note', 'quotation'];
foreach ($types as $type) {
if (str_starts_with($event->getListName(), '_sales_bundle__' . $type . '_list_')) {
$printBtn = '<a target="_blank" href="';
$printBtn .= $this->router->generate(
'sales_bundle__mass_generation_pdf',
['type' => $type]
) . '" ';
$printBtn .= 'class="btn btn-sm btn-primary mx-2">';
$printBtn .= $this->tr->trans('Print', [], 'SalesBundle') . '</a>';
$printBtn .= '<a href="' . $this->router->generate(
'sales_bundle__document_export_all',
['document' => $type]
) . '"';
$printBtn .= ' class="btn btn-sm btn-primary bluue-modal-button" bluue-modal-label="';
$printBtn .= $this->tr->trans('Send by mail', [], 'SalesBundle') . '">';
$printBtn .= $this->tr->trans('Send by mail', [], 'SalesBundle') . '</a>';
if ($this->security->isGranted('ROLE__SALES__ADMIN') && $type == 'delivery_note') {
$printBtn .= '<form action="' . $this->router->generate(
'sales_bundle__invoice_multiple_delivery_notes',
['document' => $type]
) . '" method="POST" id="invoiceDeliveryNotes" class="mx-2">';
$printBtn .= '<button type="submit" class="btn btn-primary btn-sm">';
$printBtn .= $this->tr->trans('Invoice delivery notes', [], 'SalesBundle');
$printBtn .= '</button></form>';
}
$buttons[] = [$printBtn];
}
}
$event->setButtons($buttons);
}
}