<?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\AccountingBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Services\CheckBundleInstall;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureMenuSubscriber 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 => 'onLoad'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__ACCOUNTING__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$menuAccountingChilds = [
[
'key' => 'accountingExport',
'label' => $this->tr->trans('Accounting export', [], 'AccountingBundle'),
'route' => 'accounting_bundle__export',
]
];
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$accountingTranslate = $this->tr->trans('Accounting', [], 'AccountingBundle');
$menu->addChild('AccountingMenu')
->setLabel($accountingTranslate)
->setExtra('icon', 'fas fa-calculator');
foreach ($menuAccountingChilds as $menuAccountingChild) {
$current = $menuAccountingChild['route'] == $currentRoute;
$menu['AccountingMenu']->addChild(
$menuAccountingChild['key'],
[
'route' => $menuAccountingChild['route'],
'label' => $menuAccountingChild['label'],
'current' => $current
]
);
}
if (CheckBundleInstall::exist('sales-bundle')) {
$menu['AccountingMenu']->addChild(
$this->tr->trans('Payment expectations', [], 'AccountingBundle'),
[
'route' => 'sales_bundle__payment_expectation_list',
'current' => 'sales_bundle__payment_expectation_list' == $currentRoute
]
);
$menu['AccountingMenu']->addChild(
$this->tr->trans('Intra-community sales', [], 'AccountingBundle'),
[
'route' => 'sales_bundle__intracommunity_sales_list',
'current' => 'sales_bundle__intracommunity_sales_list' == $currentRoute
]
);
if ($this->security->isGranted('ROLE__SALES__READ_ONLY')) {
$menu['AccountingMenu']->addChild(
$this->tr->trans('Payments export', [], 'AccountingBundle'),
[
'route' => 'sales_bundle__payment_export',
'current' => 'sales_bundle__payment_export' == $currentRoute
]
);
}
}
}
}