<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\PimBundle\EventSubscriber;
use App\Event\MassManagementListButtonsEvent;
use Bluue\ProductsBundle\Event\ConfigurationProductsListButtonsEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureProductsListButtonsSubscriber implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private RouterInterface $router;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var Security
*/
private Security $security;
/**
* @param RouterInterface $router
* @param TranslatorInterface $tr
* @param Security $security
*/
public function __construct(
RouterInterface $router,
TranslatorInterface $tr,
Security $security
) {
$this->router = $router;
$this->tr = $tr;
$this->security = $security;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
MassManagementListButtonsEvent::NAME => 'onLoad',
ConfigurationProductsListButtonsEvent::NAME => 'productListButtons'
];
}
/**
* @param MassManagementListButtonsEvent $event
* @return void
*/
public function onLoad(MassManagementListButtonsEvent $event): void
{
if (
!$this->security->isGranted('ROLE__PIM__ADMIN')
|| !str_starts_with($event->getListName(), '_products_bundle__list')
) {
return;
}
$buttons = $event->getButtons();
$str = '<a href="' . $this->router->generate('pim_bundle__mass_management') . '" ';
$str .= 'class="btn btn-sm btn-primary mx-2">';
$str .= $this->tr->trans('Mass management', [], 'PimBundle') . '</a>';
$buttons[] = [$str];
$str = '<a href="' . $this->router->generate('pim_bundle__sequential_modification') . '" ';
$str .= 'class="btn btn-sm btn-primary mx-2">';
$str .= $this->tr->trans('Sequential modification', [], 'PimBundle') . '</a>';
$buttons[] = [$str];
$event->setButtons($buttons);
}
/**
* @param ConfigurationProductsListButtonsEvent $event
* @return void
*/
public function productListButtons(ConfigurationProductsListButtonsEvent $event): void
{
if (!$this->security->isGranted('ROLE__PIM__ADMIN')) {
return;
}
$buttons = $event->getButtons();
$str = '<a href="' . $this->router->generate('pim_bundle__mass_management') . '" ';
$str .= 'class="btn btn-sm btn-primary ml-2">';
$str .= $this->tr->trans('Mass management', [], 'PimBundle') . '</a>';
$buttons[] = [$str];
$event->setButtons($buttons);
}
}