<?php
namespace Suzuki\CoreBundle\EventListener;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
use Suzuki\CoreBundle\Helpers\CoreHelper;
use Suzuki\OrderComponent\Repository\ConfigurationRepositoryInterface;
use Suzuki\SettingBundle\Constants\Constants as RoleConstants;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class CustomMenuAdminListener
*
* @package Suzuki\CoreBundle\EventListener
*/
class CustomMenuAdminListener {
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ConfigurationRepositoryInterface
*/
protected $configuration;
/**
* @var CoreHelper
*/
protected $coreHelper;
/**
* @var object|\Symfony\Component\Translation\DataCollectorTranslator
*/
protected $translator;
/**
* CustomMenuAdminListener constructor.
*
* @param ContainerInterface $container
* @param ConfigurationRepositoryInterface $configurationRepository
*/
public function __construct(
ContainerInterface $container,
ConfigurationRepositoryInterface $configurationRepository
) {
$this->container = $container;
$this->configuration = $configurationRepository;
$this->coreHelper = new CoreHelper();
$this->translator = $container->get('translator');
}
/**
* @param ConfigureMenuEvent $event
*/
public function customMenu(ConfigureMenuEvent $event) {
$token = $this->container->get('security.token_storage');
$user = $token->getToken()->getUser();
$menu = $event->getMenu()->getChildren();
$this->addTargetBlankIntoConfiguration($menu);
if (!in_array(RoleConstants::ROLE_SUPER_ADMIN, $user->getRoles())) {
$this->checkFitlerSoftDeleteAble();
$countClientToRaise = 0;
$countClient = 0;
$countOfferCommercial = 0;
$countPurchaseOrder = 0;
if (in_array(RoleConstants::ROLE_SELLER, $user->getRoles())) {
$countClientToRaise = $this->configuration->getCountClientToRaise($user->getID());
$countClient = $this->configuration->getCountConfiguration($user->getID());
$countOfferCommercial = $this->configuration->getCountOfferCommercial($user->getID());
$countPurchaseOrder = $this->configuration->getCountPurchaseOrder($user->getID());
}
if (in_array(RoleConstants::ROLE_REGION_MANAGER,
$user->getRoles()) ||
in_array(RoleConstants::ROLE_DEALER, $user->getRoles())) {
$dealerShipId = $this->coreHelper->getAllIdByObject(
$user,
'getDealerShip'
);
$countClientToRaise = $this->configuration->getCountClientToRaise(
NULL,
$dealerShipId
);
$countClient = $this->configuration->getCountConfiguration(
NULL,
$dealerShipId
);
$countOfferCommercial = $this->configuration->getCountOfferCommercial(
NULL,
$dealerShipId
);
$countPurchaseOrder = $this->configuration->getCountPurchaseOrder(
NULL,
$dealerShipId
);
}
$this->customMenuClient($menu, $countClient);
$this->customMenuClientAll($menu, $countClientToRaise);
$this->customMenuConfiguration($menu, $countOfferCommercial);
$this->customMenuPurchaseOrder($menu, $countPurchaseOrder);
}
}
/**
* @param $count
*
* @return string
*/
private function getCountHTML($count) {
return '<span class="count-menu">' . $count . '</span>';
}
/**
* @param $menu
*/
private function addTargetBlankIntoConfiguration($menu) {
if (isset($menu['Create Congifuration']) && $menu['Create Congifuration'] !== NULL) {
$menu['Create Congifuration']->setLinkAttributes(['target' => '_blank']);
}
}
/**
* check filter soft deleted
*/
private function checkFitlerSoftDeleteAble() {
$em = $this->container->get('doctrine.orm.entity_manager');
if (!$em->getFilters()->isEnabled('softdeleteable')) {
$em->getFilters()->enable('softdeleteable');
}
}
/**
* @param $menu
* @param $countClient
*/
private function customMenuClient($menu, $countClient) {
if (isset($menu['menu.client.all.label']) && $menu['menu.client.all.label'] !== '') {
$menuClient = $menu['menu.client.all.label'];
$menuClient->setExtra('safe_label', FALSE);
$menuClient->setLabel(
$this->translator->trans('client.label', [],
'SonataAdminMenu') .
' ' . $this->getCountHTML($countClient)
);
}
}
/**
* @param $menu
* @param $countClientToRaise
*/
private function customMenuClientAll($menu, $countClientToRaise) {
if (isset($menu['menu.client.label']) && $menu['menu.client.label'] !== '') {
$menuClientToRaise = $menu['menu.client.label'];
$menuClientToRaise->setExtra('safe_label', FALSE);
$menuClientToRaise->setLabel(
$this->translator->trans(
'client.to.raise.label',
[],
'SonataAdminMenu'
) . ' ' . $this->getCountHTML($countClientToRaise)
);
}
}
/**
* @param $menu
* @param $countOfferCommercial
*/
private function customMenuConfiguration($menu, $countOfferCommercial) {
if (isset($menu['menu.configuration.label']) && $menu['menu.configuration.label'] !== '') {
$menuOffer = $menu['menu.configuration.label'];
$menuOffer->setExtra('safe_label', FALSE);
$menuOffer->setLabel(
$this->translator->trans('menu.order.offer.commercial', [],
'SonataAdminMenu') .
' ' . $this->getCountHTML($countOfferCommercial)
);
}
}
/**
* @param $menu
* @param $countPurchaseOrder
*/
private function customMenuPurchaseOrder($menu, $countPurchaseOrder) {
if (isset($menu['menu.purchase.order.label']) && $menu['menu.purchase.order.label'] !== '') {
$menuPurchase = $menu['menu.purchase.order.label'];
$menuPurchase->setExtra('safe_label', FALSE);
$menuPurchase->setLabel(
$this->translator->trans('menu.order.purchase.order', [],
'SonataAdminMenu') .
' ' . $this->getCountHTML($countPurchaseOrder)
);
}
}
}