bundles/Suzuki/CoreBundle/EventListener/CustomMenuAdminListener.php line 57

Open in your IDE?
  1. <?php
  2. namespace Suzuki\CoreBundle\EventListener;
  3. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  4. use Suzuki\CoreBundle\Helpers\CoreHelper;
  5. use Suzuki\OrderComponent\Repository\ConfigurationRepositoryInterface;
  6. use Suzuki\SettingBundle\Constants\Constants as RoleConstants;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9.  * Class CustomMenuAdminListener
  10.  *
  11.  * @package Suzuki\CoreBundle\EventListener
  12.  */
  13. class CustomMenuAdminListener {
  14.     /**
  15.      * @var ContainerInterface
  16.      */
  17.     protected $container;
  18.     /**
  19.      * @var ConfigurationRepositoryInterface
  20.      */
  21.     protected $configuration;
  22.     /**
  23.      * @var CoreHelper
  24.      */
  25.     protected $coreHelper;
  26.     /**
  27.      * @var object|\Symfony\Component\Translation\DataCollectorTranslator
  28.      */
  29.     protected $translator;
  30.     /**
  31.      * CustomMenuAdminListener constructor.
  32.      *
  33.      * @param ContainerInterface               $container
  34.      * @param ConfigurationRepositoryInterface $configurationRepository
  35.      */
  36.     public function __construct(
  37.         ContainerInterface $container,
  38.         ConfigurationRepositoryInterface $configurationRepository
  39.     ) {
  40.         $this->container     $container;
  41.         $this->configuration $configurationRepository;
  42.         $this->coreHelper    = new CoreHelper();
  43.         $this->translator    $container->get('translator');
  44.     }
  45.     /**
  46.      * @param ConfigureMenuEvent $event
  47.      */
  48.     public function customMenu(ConfigureMenuEvent $event) {
  49.         $token $this->container->get('security.token_storage');
  50.         $user  $token->getToken()->getUser();
  51.         $menu  $event->getMenu()->getChildren();
  52.         $this->addTargetBlankIntoConfiguration($menu);
  53.         if (!in_array(RoleConstants::ROLE_SUPER_ADMIN$user->getRoles())) {
  54.             $this->checkFitlerSoftDeleteAble();
  55.             $countClientToRaise   0;
  56.             $countClient          0;
  57.             $countOfferCommercial 0;
  58.             $countPurchaseOrder   0;
  59.             if (in_array(RoleConstants::ROLE_SELLER$user->getRoles())) {
  60.                 $countClientToRaise   $this->configuration->getCountClientToRaise($user->getID());
  61.                 $countClient          $this->configuration->getCountConfiguration($user->getID());
  62.                 $countOfferCommercial $this->configuration->getCountOfferCommercial($user->getID());
  63.                 $countPurchaseOrder   $this->configuration->getCountPurchaseOrder($user->getID());
  64.             }
  65.             if (in_array(RoleConstants::ROLE_REGION_MANAGER,
  66.                     $user->getRoles()) ||
  67.                 in_array(RoleConstants::ROLE_DEALER$user->getRoles())) {
  68.                 $dealerShipId         $this->coreHelper->getAllIdByObject(
  69.                     $user,
  70.                     'getDealerShip'
  71.                 );
  72.                 $countClientToRaise   $this->configuration->getCountClientToRaise(
  73.                     NULL,
  74.                     $dealerShipId
  75.                 );
  76.                 $countClient          $this->configuration->getCountConfiguration(
  77.                     NULL,
  78.                     $dealerShipId
  79.                 );
  80.                 $countOfferCommercial $this->configuration->getCountOfferCommercial(
  81.                     NULL,
  82.                     $dealerShipId
  83.                 );
  84.                 $countPurchaseOrder   $this->configuration->getCountPurchaseOrder(
  85.                     NULL,
  86.                     $dealerShipId
  87.                 );
  88.             }
  89.             $this->customMenuClient($menu$countClient);
  90.             $this->customMenuClientAll($menu$countClientToRaise);
  91.             $this->customMenuConfiguration($menu$countOfferCommercial);
  92.             $this->customMenuPurchaseOrder($menu$countPurchaseOrder);
  93.         }
  94.     }
  95.     /**
  96.      * @param $count
  97.      *
  98.      * @return string
  99.      */
  100.     private function getCountHTML($count) {
  101.         return '<span class="count-menu">' $count '</span>';
  102.     }
  103.     /**
  104.      * @param $menu
  105.      */
  106.     private function addTargetBlankIntoConfiguration($menu) {
  107.         if (isset($menu['Create Congifuration']) && $menu['Create Congifuration'] !== NULL) {
  108.             $menu['Create Congifuration']->setLinkAttributes(['target' => '_blank']);
  109.         }
  110.     }
  111.     /**
  112.      * check filter soft deleted
  113.      */
  114.     private function checkFitlerSoftDeleteAble() {
  115.         $em $this->container->get('doctrine.orm.entity_manager');
  116.         if (!$em->getFilters()->isEnabled('softdeleteable')) {
  117.             $em->getFilters()->enable('softdeleteable');
  118.         }
  119.     }
  120.     /**
  121.      * @param $menu
  122.      * @param $countClient
  123.      */
  124.     private function customMenuClient($menu$countClient) {
  125.         if (isset($menu['menu.client.all.label']) && $menu['menu.client.all.label'] !== '') {
  126.             $menuClient $menu['menu.client.all.label'];
  127.             $menuClient->setExtra('safe_label'FALSE);
  128.             $menuClient->setLabel(
  129.                 $this->translator->trans('client.label', [],
  130.                     'SonataAdminMenu') .
  131.                 ' ' $this->getCountHTML($countClient)
  132.             );
  133.         }
  134.     }
  135.     /**
  136.      * @param $menu
  137.      * @param $countClientToRaise
  138.      */
  139.     private function customMenuClientAll($menu$countClientToRaise) {
  140.         if (isset($menu['menu.client.label']) && $menu['menu.client.label'] !== '') {
  141.             $menuClientToRaise $menu['menu.client.label'];
  142.             $menuClientToRaise->setExtra('safe_label'FALSE);
  143.             $menuClientToRaise->setLabel(
  144.                 $this->translator->trans(
  145.                     'client.to.raise.label',
  146.                     [],
  147.                     'SonataAdminMenu'
  148.                 ) . ' ' $this->getCountHTML($countClientToRaise)
  149.             );
  150.         }
  151.     }
  152.     /**
  153.      * @param $menu
  154.      * @param $countOfferCommercial
  155.      */
  156.     private function customMenuConfiguration($menu$countOfferCommercial) {
  157.         if (isset($menu['menu.configuration.label']) && $menu['menu.configuration.label'] !== '') {
  158.             $menuOffer $menu['menu.configuration.label'];
  159.             $menuOffer->setExtra('safe_label'FALSE);
  160.             $menuOffer->setLabel(
  161.                 $this->translator->trans('menu.order.offer.commercial', [],
  162.                     'SonataAdminMenu') .
  163.                 ' ' $this->getCountHTML($countOfferCommercial)
  164.             );
  165.         }
  166.     }
  167.     /**
  168.      * @param $menu
  169.      * @param $countPurchaseOrder
  170.      */
  171.     private function customMenuPurchaseOrder($menu$countPurchaseOrder) {
  172.         if (isset($menu['menu.purchase.order.label']) && $menu['menu.purchase.order.label'] !== '') {
  173.             $menuPurchase $menu['menu.purchase.order.label'];
  174.             $menuPurchase->setExtra('safe_label'FALSE);
  175.             $menuPurchase->setLabel(
  176.                 $this->translator->trans('menu.order.purchase.order', [],
  177.                     'SonataAdminMenu') .
  178.                 ' ' $this->getCountHTML($countPurchaseOrder)
  179.             );
  180.         }
  181.     }
  182. }