src/Aqarmap/Bundle/ListingBundle/EventListener/ConfigureMenuEventSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
  4. use Aqarmap\Bundle\ListingBundle\Constant\LocationLevels;
  5. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  6. use Aqarmap\Bundle\ListingBundle\EventListener\MenuBuilder\RenderSideBar;
  7. use Aqarmap\Bundle\ListingBundle\Twig\ListingExtension;
  8. use Aqarmap\Bundle\MainBundle\Event\ConfigureMenuEvent;
  9. use Aqarmap\Bundle\MainBundle\Service\Setting;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class ConfigureMenuEventSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * Container instance.
  20.      */
  21.     protected ContainerInterface $container;
  22.     /**
  23.      * List of roles.
  24.      *
  25.      * @var array
  26.      */
  27.     protected $roles = [
  28.         'ROLE_FREE_LISTING',
  29.         'ROLE_PROJECTS',
  30.         'ROLE_INCREASING_LISTINGS',
  31.         'ROLE_PAID_LISTING',
  32.         'ROLE_ADMIN_ACTIVITIES',
  33.         'ROLE_WEBMASTER',
  34.         'ROLE_ACCESS_TO_ADMIN_LEADS',
  35.         'ROLE_MARKETING',
  36.         'ROLE_CRM',
  37.         'ROLE_CUSTOMER_PROFILING',
  38.         'ROLE_SUPER_ADMIN',
  39.         'ROLE_LEADS_STATISTICS',
  40.         'ROLE_PENDING_FEATURING_LISTING',
  41.         'ROLE_LOCATIONS',
  42.         'ROLE_PUBLISHED_LISTINGS_COUNT',
  43.         'ROLE_RELIST_LISTING',
  44.         'ROLE_SCRAPPED_LISTING',
  45.         'ROLE_ACTIVE_CUSTOMER',
  46.     ];
  47.     /**
  48.      * Translator instance.
  49.      */
  50.     protected $translator;
  51.     /**
  52.      * @var FeatureToggleManager
  53.      */
  54.     private $featureToggleManager;
  55.     /**
  56.      * @var Setting
  57.      */
  58.     private $setting;
  59.     private AuthorizationCheckerInterface $authorizationChecker;
  60.     /**
  61.      * @var EntityManagerInterface
  62.      */
  63.     private $entityManager;
  64.     /**
  65.      * @var ListingExtension
  66.      */
  67.     private $listingExtension;
  68.     /**
  69.      * @var RequestStack
  70.      */
  71.     private $requestStack;
  72.     public function __construct(
  73.         ContainerInterface $container,
  74.         TranslatorInterface $translator,
  75.         FeatureToggleManager $featureToggleManager,
  76.         Setting $setting,
  77.         AuthorizationCheckerInterface $authorizationChecker,
  78.         EntityManagerInterface $entityManager,
  79.         ListingExtension $listingExtension,
  80.         RequestStack $requestStack
  81.     ) {
  82.         $this->container $container;
  83.         $this->translator $translator;
  84.         $this->featureToggleManager $featureToggleManager;
  85.         $this->setting $setting;
  86.         $this->authorizationChecker $authorizationChecker;
  87.         $this->entityManager $entityManager;
  88.         $this->listingExtension $listingExtension;
  89.         $this->requestStack $requestStack;
  90.     }
  91.     public function onAdminSidebarMenuConfigure(ConfigureMenuEvent $event): void
  92.     {
  93.         $userRole $this->authorizationChecker;
  94.         $renderSideBar = (new RenderSideBar($this->featureToggleManager$this->authorizationChecker))
  95.             ->setMenu($event->getMenu())
  96.             ->setTranslator($this->translator);
  97.         foreach ($this->roles as $role) {
  98.             if ($userRole->isGranted($role)) {
  99.                 $fn 'has'.$this->snakeToCamel($role);
  100.                 $renderSideBar->$fn();
  101.             }
  102.         }
  103.     }
  104.     public function onMainNavbarMenuConfigure(ConfigureMenuEvent $event): void
  105.     {
  106.         $menu $event->getMenu();
  107.         if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  108.             // My Listings
  109.             $menu
  110.                 ->addChild('my_listings', ['route' => 'aqarmap_listing_default_mylistings'])
  111.                 ->setLabel($this->translator->trans('layout.my_listings.title'))
  112.                 ->setExtra('position'50);
  113.             // List your property
  114.             $menu
  115.                 ->addChild('add_listing', ['route' => 'listing_initialize'])
  116.                 ->setLabel($this->translator->trans('layout.add_listing'))
  117.                 ->setExtra('position'40);
  118.         }
  119.     }
  120.     public function onBreadcrumbConfigure(ConfigureMenuEvent $event): void
  121.     {
  122.         $menu $event->getMenu();
  123.         $listing $this->getRequest()->attributes->get('listing');
  124.         if ($listing instanceof Listing) {
  125.             $this->getRequest()->attributes->set('section'$listing->getSection());
  126.             $this->getRequest()->attributes->set('propertyType'$listing->getPropertyType());
  127.             $this->getRequest()->attributes->set('location'$listing->getLocation());
  128.         }
  129.         if ($section $this->getRequest()->attributes->get('section')) {
  130.             // If not searchable section, than it's premium section (Projects)
  131.             $premium_section = !$section->getSearchable() || ($listing instanceof Listing && $listing->getParent() instanceof Listing);
  132.             // Search links
  133.             if (($propertyType $this->getRequest()->attributes->get('propertyType')) && !$premium_section) {
  134.                 $menu
  135.                     ->addChild($section->getSlug(), [
  136.                         'route' => 'property_type_search',
  137.                         'routeParameters' => [
  138.                             'section_slug' => $section->getSlug(),
  139.                             'property_type_slug' => 'property-type',
  140.                         ],
  141.                     ])
  142.                     ->setAttribute('title'$this->translator
  143.                         ->trans('listing.quickLinks_title_section', [
  144.                             '%section%' => $section->getTitle(),
  145.                         ]))
  146.                     ->setLabel($this->translator
  147.                         ->trans('listing.quickLinks_title_section', [
  148.                             '%section%' => $section->getTitle(),
  149.                         ]));
  150.                 $menu
  151.                     ->addChild($propertyType->getSlug(), [
  152.                         'route' => 'property_type_search',
  153.                         'routeParameters' => [
  154.                             'section_slug' => $section->getSlug(),
  155.                             'property_type_slug' => $propertyType->getSlug(),
  156.                         ],
  157.                     ])
  158.                     ->setAttribute('title'$this->translator
  159.                         ->trans('listing.quickLinks_title_wo_location', [
  160.                             '%property_type%' => $propertyType->getTitle(),
  161.                             '%section%' => $section->getTitle(),
  162.                         ]))
  163.                     ->setLabel($propertyType->getTitle());
  164.                 // Locations
  165.                 if ($location $this->getRequest()->attributes->get('location')) {
  166.                     /** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
  167.                     $locationRepo $this->entityManager->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Location::class);
  168.                     foreach ($locationRepo->getPath($location) as $treeLocation) {
  169.                         // Skip non-searchable locations
  170.                         if ($treeLocation->getLevel() > LocationLevels::MAX_LEVEL) {
  171.                             continue;
  172.                         }
  173.                         $menu
  174.                             ->addChild($treeLocation->getSlug(), [
  175.                                 'route' => 'search',
  176.                                 'routeParameters' => [
  177.                                     'section_slug' => $section->getSlug(),
  178.                                     'property_type_slug' => $propertyType->getSlug(),
  179.                                     'location_slug' => $treeLocation->getSlug(),
  180.                                 ],
  181.                             ])
  182.                             ->setAttribute('title'$this->translator
  183.                                 ->trans('listing.quickLinks_title', [
  184.                                     '%property_type%' => $propertyType->getTitle(),
  185.                                     '%section%' => $section->getTitle(),
  186.                                     '%location%' => $treeLocation->getTitle(),
  187.                                 ]))
  188.                             ->setLabel($treeLocation->getTitle());
  189.                     }
  190.                 }
  191.             } elseif ($listing $this->getRequest()->attributes->get('listing')) {
  192.                 $menu
  193.                     ->addChild('section', ['route' => 'compound_search'])
  194.                     ->setAttribute('title'$this->translator
  195.                         ->trans('listing.quickLinks_title_section', [
  196.                             '%section%' => $section->getTitle(),
  197.                         ]))
  198.                     ->setLabel($section->getTitle());
  199.                 /** @var $compoundLocation \Aqarmap\Bundle\ListingBundle\Entity\CompoundLocation */
  200.                 if ($compoundLocation $listing->getCompoundLocation()) {
  201.                     $compoundsLabel $this->translator
  202.                         ->trans(
  203.                             'layout.location_compound',
  204.                             ['%location%' => $compoundLocation]
  205.                         );
  206.                     if ('SA' == $this->setting->getSetting('general''country')) {
  207.                         $compoundsLabel $this->translator
  208.                             ->trans(
  209.                                 'layout.location_project',
  210.                                 ['%location%' => $compoundLocation]
  211.                             );
  212.                     }
  213.                     $menu
  214.                         ->addChild('section_location', [
  215.                             'route' => 'compound_search_with_location',
  216.                             'routeParameters' => [
  217.                                 'location' => $compoundLocation->getSlug(),
  218.                             ],
  219.                         ])
  220.                         ->setLabel($compoundsLabel);
  221.                 }
  222.             }
  223.         }
  224.         $listingExtension $this->listingExtension;
  225.         if ($listing && $listing->getParent()) {
  226.             // With Slug
  227.             if ($listing->getParent()->getSlug()) {
  228.                 $parentRoute = [
  229.                     'route' => 'listing_slug',
  230.                     'routeParameters' => [
  231.                         'id' => $listing->getParent()->getId(),
  232.                         'slug' => $listing->getParent()->getSlug(),
  233.                     ],
  234.                     'extras' => [
  235.                         'safe_label' => true,
  236.                     ],
  237.                 ];
  238.             } // Without Slug
  239.             else {
  240.                 $parentRoute = ['route' => 'listing_view''routeParameters' => [
  241.                     'id' => $listing->getParent()->getId(),
  242.                 ]];
  243.             }
  244.             $menu
  245.                 ->addChild($listing->getParent()->getId(), $parentRoute)
  246.                 ->setLabel($listingExtension->fixArabicNumbers($listing->getParent()->getTitle()));
  247.         }
  248.         if ($listing) {
  249.             // With Slug
  250.             if ($listing->getSlug()) {
  251.                 $listingRoute = [
  252.                     'route' => 'listing_slug',
  253.                     'routeParameters' => [
  254.                         'id' => $listing->getId(),
  255.                         'slug' => $listing->getSlug(),
  256.                     ],
  257.                     'extras' => [
  258.                         'safe_label' => true,
  259.                     ],
  260.                 ];
  261.             } // Without Slug
  262.             else {
  263.                 $listingRoute = ['route' => 'listing_view''routeParameters' => [
  264.                     'id' => $listing->getId(),
  265.                 ]];
  266.             }
  267.             $menu
  268.                 ->addChild($listing->getId(), $listingRoute)
  269.                 ->setLabel($listingExtension->fixArabicNumbers($listing->getTitle()));
  270.         } elseif ($section $this->getRequest()->attributes->get('section')) {
  271.             $translatedTitle $this->translator
  272.                 ->trans('listing.quickLinks_title_section', [
  273.                     '%section%' => $section->getTitle(),
  274.                 ]);
  275.             $menu
  276.                 ->addChild($section->getSlug(), [
  277.                     'route' => 'property_type_search',
  278.                     'routeParameters' => [
  279.                         'section_slug' => $section->getSlug(),
  280.                         'property_type_slug' => 'property-type',
  281.                     ],
  282.                 ])
  283.                 ->setAttribute('title'$translatedTitle)
  284.                 ->setLabel($translatedTitle);
  285.         }
  286.     }
  287.     public function getRequest()
  288.     {
  289.         return $this->requestStack->getCurrentRequest();
  290.     }
  291.     /**
  292.      * Convert underscore_strings to camelCase.
  293.      *
  294.      * @param string $str
  295.      *
  296.      * @return string
  297.      */
  298.     private function snakeToCamel($str)
  299.     {
  300.         return str_replace(' '''ucwords(str_replace('_'' 'strtolower($str))));
  301.     }
  302.     /**
  303.      * @return array<string, mixed>
  304.      */
  305.     public static function getSubscribedEvents(): array
  306.     {
  307.         return [
  308.             'aqarmap.main.admin_sidebar_menu_configure' => 'onAdminSidebarMenuConfigure',
  309.             'aqarmap.main.breadcrumb_configure' => 'onBreadcrumbConfigure',
  310.         ];
  311.     }
  312. }