<?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\SalesBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Services\CheckBundleInstall;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
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
*/
protected 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__SALES__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$menuSalesChilds = [
[
'key' => 'quotations',
'label' => $this->tr->trans('Quotations', [], 'SalesBundle'),
'route' => 'sales_bundle__quotation_list',
'matchPath' => 'bundles/sales-bundle/quotation/'
],
[
'key' => 'orders',
'label' => $this->tr->trans('Orders', [], 'SalesBundle'),
'route' => 'sales_bundle__order_list',
'matchPath' => 'bundles/sales-bundle/order/'
],
[
'key' => 'delivery_notes',
'label' => $this->tr->trans('Delivery notes', [], 'SalesBundle'),
'route' => 'sales_bundle__delivery_note_list',
'matchPath' => 'bundles/sales-bundle/delivery-note/'
],
[
'key' => 'invoices',
'label' => $this->tr->trans('Invoices', [], 'SalesBundle'),
'route' => 'sales_bundle__invoice_list',
'matchPath' => 'bundles/sales-bundle/invoice/'
],
[
'key' => 'credit_notes',
'label' => $this->tr->trans('Credit notes', [], 'SalesBundle'),
'route' => 'sales_bundle__credit_note_list',
'matchPath' => 'bundles/sales-bundle/credit-note/'
],
[
'key' => 'payments',
'label' => $this->tr->trans('Payments', [], 'SalesBundle'),
'route' => 'sales_bundle__payment_list'
]
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$salesTranslate = $this->tr->trans('Sales', [], 'SalesBundle');
$menu->addChild('SalesMenu')
->setLabel($salesTranslate)
->setExtra('icon', 'fas fa-money-bill-alt');
foreach ($menuSalesChilds as $menuSalesChild) {
$current = $menuSalesChild['route'] == $currentRoute
|| (
!empty($menuSalesChild['matchPath'])
&& strpos($currentPath, $menuSalesChild['matchPath']) !== false
)
;
$menu['SalesMenu']->addChild(
$menuSalesChild['key'],
[
'route' => $menuSalesChild['route'],
'label' => $menuSalesChild['label'],
'current' => $current
]
);
}
$matchPath = [
'bundles/sales-bundle/customer'
];
foreach ($matchPath as $match) {
if (strpos($currentPath, $match) !== false) {
$menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
}
}
}
}