<?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\RecurringInvoicesBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Event\MenuConfigurationPageEvent;
use Bluue\SalesBundle\Event\MenuSalesConfigurationEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigurationBundleSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::NAME => 'mainMenu',
MenuConfigurationPageEvent::NAME => 'configurationMenu',
MenuSalesConfigurationEvent::NAME => 'configurationSalesMenu'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function mainMenu(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__SALES__READ_ONLY')) {
return;
}
$path = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$menu = $event->getMenu();
$recurringInvoicesMenu = [
[
'key' => 'recurring_invoices',
'name' => $this->tr->trans('Recurring invoices', [], 'RecurringInvoicesBundle'),
'route' => 'recurring_invoices_bundle__recurring_invoice_list',
'matchPath' => 'bundles/recurring-invoices-bundle/recurring-invoice/'
]
];
foreach ($recurringInvoicesMenu as $recurringInvoicesMenuChild) {
$current = $recurringInvoicesMenuChild['route'] == $currentRoute
|| strpos($path, $recurringInvoicesMenuChild['matchPath']) !== false;
$menu['SalesMenu']->addChild(
$recurringInvoicesMenuChild['key'],
[
'label' => $recurringInvoicesMenuChild['name'],
'route' => $recurringInvoicesMenuChild['route'],
'current' => $current
]
);
}
$matchPath = [
'bundles/recurring-invoices-bundle/recurring-invoice/customer'
];
foreach ($matchPath as $match) {
if (strpos($path, $match) !== false) {
$menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
$menu->getChild('SalesMenu')->getChild('recurring_invoices')->setCurrent(false);
}
}
}
/**
* @param MenuSalesConfigurationEvent $event
* @return void
*/
public function configurationSalesMenu(MenuSalesConfigurationEvent $event)
{
$pages = $event->getPages();
$pages[] = [
'id' => 'recurring_invoices_bundle__configuration_settings',
'name' => $this->tr->trans('Recurring invoices', [], 'RecurringInvoicesBundle')
];
$event->setPages($pages);
}
/**
* @param MenuConfigurationPageEvent $event
* @return void
*/
public function configurationMenu(MenuConfigurationPageEvent $event): void
{
$pages = $event->getPages();
$salesMenu = array_search('sales_bundle__configuration_settings', array_column($pages, 'id'));
if (is_numeric($salesMenu)) {
$pages[$salesMenu]['subpages'][] = ['id' => 'recurring_invoices_bundle__configuration_settings'];
$event->setPages($pages);
}
}
}