<?php
declare(strict_types=1);
namespace Bluue\ProjectsBundle\EventSubscriber;
use App\Event\MassManagementListButtonsEvent;
use App\Services\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Core\Security;
class ConfigureListButtonsSubscriber implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private RouterInterface $router;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var CsrfTokenManagerInterface
*/
private CsrfTokenManagerInterface $csrfToken;
/**
* @var Security
*/
private Security $security;
/**
* @var Context
*/
private Context $contextService;
public function __construct(
RouterInterface $router,
TranslatorInterface $tr,
CsrfTokenManagerInterface $csrfToken,
Context $contextService,
Security $security
) {
$this->router = $router;
$this->tr = $tr;
$this->csrfToken = $csrfToken;
$this->contextService = $contextService;
$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();
$context = $this->contextService->getActualOrDefault();
if (
(
str_starts_with($event->getListName(), '_projects_bundle__task_list_') ||
str_starts_with($event->getListName(), '_projects_bundle__customer_task_list_')
)
&&
$this->security->isGranted('ROLE__PROJECTS__SUPER_ADMIN')
) {
$param = [];
if (str_starts_with($event->getListName(), '_projects_bundle__customer_task_list_')) {
$listNameExplode = explode('_', $event->getListName());
$customerId = $listNameExplode[array_key_last($listNameExplode)];
$param['customerId'] = $customerId;
}
$deleteBtn = '<form action="';
$deleteBtn .= $this->router->generate('projects_bundle__task_mass_delete', $param);
$deleteBtn .= '" method="post">';
$deleteBtn .= '<input type="hidden" name="_token" value="';
$deleteBtn .= $this->csrfToken->getToken('mass-delete' . $context->getId());
$deleteBtn .= '"><button type="submit" class="btn btn-sm btn-danger" data-submit-alert="';
$deleteBtn .= $this->tr->trans(
'Really want to remove tasks selected?',
[],
'ProjectsBundle'
) . '">';
$deleteBtn .= $this->tr->trans(
'Remove tasks selected',
[],
'ProjectsBundle'
) . '</button></form>';
$buttons[] = [$deleteBtn];
}
$event->setButtons($buttons);
}
}