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

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