app/Customize/EventListener/CustomTwigInitializeListener.php line 47

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Customize\Entity\Master\VendorStatus;
  4. use Customize\Enum\SessionNames;
  5. use Customize\Repository\Master\AuthorityRepository;
  6. use Customize\Repository\VendorRepository;
  7. use Eccube\Request\Context;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Twig\Environment;
  13. class CustomTwigInitializeListener implements EventSubscriberInterface
  14. {
  15.     private Environment $twig;
  16.     private Context $requestContext;
  17.     private AuthorityRepository $authRepo;
  18.     private VendorRepository $vendorRepo;
  19.     private SessionInterface $session;
  20.     public function __construct(Environment $twigContext $contextAuthorityRepository $authRepoVendorRepository $vendorRepoSessionInterface $session)
  21.     {
  22.         $this->twig           $twig;
  23.         $this->requestContext $context;
  24.         $this->authRepo       $authRepo;
  25.         $this->vendorRepo     $vendorRepo;
  26.         $this->session        $session;
  27.     }
  28.     public function onKernelController(ControllerEvent $event)
  29.     {
  30.         if ($this->requestContext->isAdmin()) {
  31.             $this->setAdminGlobals($event);
  32.         }
  33.     }
  34.     private function setAdminGlobals($event)
  35.     {
  36.         $isSysAdmin  $this->authRepo->isSystemAdmin();
  37.         $curVendorId $this->session->get(SessionNames::CURRENT_VENDOR_ID);
  38.         if ($isSysAdmin) {
  39.             $vendors  $this->vendorRepo->findBy(['Status' => VendorStatus::PUBLISHED]);
  40.             $filtered array_filter($vendors, function ($item) use ($curVendorId) {
  41.                 return $item['id'] == $curVendorId;
  42.             });
  43.             // In case of changing the status of the selected vendor
  44.             if (empty($filtered)) {
  45.                 $this->session->set(SessionNames::CURRENT_VENDOR_IDnull);
  46.             }
  47.             $this->twig->addGlobal('vendors'$vendors);
  48.             $this->twig->addGlobal('current_vendor_id'$curVendorId);
  49.         } else {
  50.             $user $this->requestContext->getCurrentUser();
  51.             $userVendorId = !empty($user) ? $user->getVendorId() : null;
  52.             if ($curVendorId != $userVendorId) {
  53.                 $this->session->set(SessionNames::CURRENT_VENDOR_ID$user->getVendorId());
  54.             }
  55.         }
  56.         $this->twig->addGlobal('isSysAdmin'$isSysAdmin);
  57.     }
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             KernelEvents::CONTROLLER => 'onKernelController',
  62.         ];
  63.     }
  64. }