<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
use Aqarmap\Bundle\ListingBundle\Constant\LocationLevels;
use Aqarmap\Bundle\ListingBundle\Entity\Listing;
use Aqarmap\Bundle\ListingBundle\EventListener\MenuBuilder\RenderSideBar;
use Aqarmap\Bundle\ListingBundle\Twig\ListingExtension;
use Aqarmap\Bundle\MainBundle\Event\ConfigureMenuEvent;
use Aqarmap\Bundle\MainBundle\Service\Setting;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConfigureMenuEventSubscriber implements EventSubscriberInterface
{
/**
* Container instance.
*/
protected ContainerInterface $container;
/**
* List of roles.
*
* @var array
*/
protected $roles = [
'ROLE_FREE_LISTING',
'ROLE_PROJECTS',
'ROLE_INCREASING_LISTINGS',
'ROLE_PAID_LISTING',
'ROLE_ADMIN_ACTIVITIES',
'ROLE_WEBMASTER',
'ROLE_ACCESS_TO_ADMIN_LEADS',
'ROLE_MARKETING',
'ROLE_CRM',
'ROLE_CUSTOMER_PROFILING',
'ROLE_SUPER_ADMIN',
'ROLE_LEADS_STATISTICS',
'ROLE_PENDING_FEATURING_LISTING',
'ROLE_LOCATIONS',
'ROLE_PUBLISHED_LISTINGS_COUNT',
'ROLE_RELIST_LISTING',
'ROLE_SCRAPPED_LISTING',
'ROLE_ACTIVE_CUSTOMER',
];
/**
* Translator instance.
*/
protected $translator;
/**
* @var FeatureToggleManager
*/
private $featureToggleManager;
/**
* @var Setting
*/
private $setting;
private AuthorizationCheckerInterface $authorizationChecker;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var ListingExtension
*/
private $listingExtension;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
ContainerInterface $container,
TranslatorInterface $translator,
FeatureToggleManager $featureToggleManager,
Setting $setting,
AuthorizationCheckerInterface $authorizationChecker,
EntityManagerInterface $entityManager,
ListingExtension $listingExtension,
RequestStack $requestStack
) {
$this->container = $container;
$this->translator = $translator;
$this->featureToggleManager = $featureToggleManager;
$this->setting = $setting;
$this->authorizationChecker = $authorizationChecker;
$this->entityManager = $entityManager;
$this->listingExtension = $listingExtension;
$this->requestStack = $requestStack;
}
public function onAdminSidebarMenuConfigure(ConfigureMenuEvent $event): void
{
$userRole = $this->authorizationChecker;
$renderSideBar = (new RenderSideBar($this->featureToggleManager, $this->authorizationChecker))
->setMenu($event->getMenu())
->setTranslator($this->translator);
foreach ($this->roles as $role) {
if ($userRole->isGranted($role)) {
$fn = 'has'.$this->snakeToCamel($role);
$renderSideBar->$fn();
}
}
}
public function onMainNavbarMenuConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
// My Listings
$menu
->addChild('my_listings', ['route' => 'aqarmap_listing_default_mylistings'])
->setLabel($this->translator->trans('layout.my_listings.title'))
->setExtra('position', 50);
// List your property
$menu
->addChild('add_listing', ['route' => 'listing_initialize'])
->setLabel($this->translator->trans('layout.add_listing'))
->setExtra('position', 40);
}
}
public function onBreadcrumbConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$listing = $this->getRequest()->attributes->get('listing');
if ($listing instanceof Listing) {
$this->getRequest()->attributes->set('section', $listing->getSection());
$this->getRequest()->attributes->set('propertyType', $listing->getPropertyType());
$this->getRequest()->attributes->set('location', $listing->getLocation());
}
if ($section = $this->getRequest()->attributes->get('section')) {
// If not searchable section, than it's premium section (Projects)
$premium_section = !$section->getSearchable() || ($listing instanceof Listing && $listing->getParent() instanceof Listing);
// Search links
if (($propertyType = $this->getRequest()->attributes->get('propertyType')) && !$premium_section) {
$menu
->addChild($section->getSlug(), [
'route' => 'property_type_search',
'routeParameters' => [
'section_slug' => $section->getSlug(),
'property_type_slug' => 'property-type',
],
])
->setAttribute('title', $this->translator
->trans('listing.quickLinks_title_section', [
'%section%' => $section->getTitle(),
]))
->setLabel($this->translator
->trans('listing.quickLinks_title_section', [
'%section%' => $section->getTitle(),
]));
$menu
->addChild($propertyType->getSlug(), [
'route' => 'property_type_search',
'routeParameters' => [
'section_slug' => $section->getSlug(),
'property_type_slug' => $propertyType->getSlug(),
],
])
->setAttribute('title', $this->translator
->trans('listing.quickLinks_title_wo_location', [
'%property_type%' => $propertyType->getTitle(),
'%section%' => $section->getTitle(),
]))
->setLabel($propertyType->getTitle());
// Locations
if ($location = $this->getRequest()->attributes->get('location')) {
/** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
$locationRepo = $this->entityManager->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Location::class);
foreach ($locationRepo->getPath($location) as $treeLocation) {
// Skip non-searchable locations
if ($treeLocation->getLevel() > LocationLevels::MAX_LEVEL) {
continue;
}
$menu
->addChild($treeLocation->getSlug(), [
'route' => 'search',
'routeParameters' => [
'section_slug' => $section->getSlug(),
'property_type_slug' => $propertyType->getSlug(),
'location_slug' => $treeLocation->getSlug(),
],
])
->setAttribute('title', $this->translator
->trans('listing.quickLinks_title', [
'%property_type%' => $propertyType->getTitle(),
'%section%' => $section->getTitle(),
'%location%' => $treeLocation->getTitle(),
]))
->setLabel($treeLocation->getTitle());
}
}
} elseif ($listing = $this->getRequest()->attributes->get('listing')) {
$menu
->addChild('section', ['route' => 'compound_search'])
->setAttribute('title', $this->translator
->trans('listing.quickLinks_title_section', [
'%section%' => $section->getTitle(),
]))
->setLabel($section->getTitle());
/** @var $compoundLocation \Aqarmap\Bundle\ListingBundle\Entity\CompoundLocation */
if ($compoundLocation = $listing->getCompoundLocation()) {
$compoundsLabel = $this->translator
->trans(
'layout.location_compound',
['%location%' => $compoundLocation]
);
if ('SA' == $this->setting->getSetting('general', 'country')) {
$compoundsLabel = $this->translator
->trans(
'layout.location_project',
['%location%' => $compoundLocation]
);
}
$menu
->addChild('section_location', [
'route' => 'compound_search_with_location',
'routeParameters' => [
'location' => $compoundLocation->getSlug(),
],
])
->setLabel($compoundsLabel);
}
}
}
$listingExtension = $this->listingExtension;
if ($listing && $listing->getParent()) {
// With Slug
if ($listing->getParent()->getSlug()) {
$parentRoute = [
'route' => 'listing_slug',
'routeParameters' => [
'id' => $listing->getParent()->getId(),
'slug' => $listing->getParent()->getSlug(),
],
'extras' => [
'safe_label' => true,
],
];
} // Without Slug
else {
$parentRoute = ['route' => 'listing_view', 'routeParameters' => [
'id' => $listing->getParent()->getId(),
]];
}
$menu
->addChild($listing->getParent()->getId(), $parentRoute)
->setLabel($listingExtension->fixArabicNumbers($listing->getParent()->getTitle()));
}
if ($listing) {
// With Slug
if ($listing->getSlug()) {
$listingRoute = [
'route' => 'listing_slug',
'routeParameters' => [
'id' => $listing->getId(),
'slug' => $listing->getSlug(),
],
'extras' => [
'safe_label' => true,
],
];
} // Without Slug
else {
$listingRoute = ['route' => 'listing_view', 'routeParameters' => [
'id' => $listing->getId(),
]];
}
$menu
->addChild($listing->getId(), $listingRoute)
->setLabel($listingExtension->fixArabicNumbers($listing->getTitle()));
} elseif ($section = $this->getRequest()->attributes->get('section')) {
$translatedTitle = $this->translator
->trans('listing.quickLinks_title_section', [
'%section%' => $section->getTitle(),
]);
$menu
->addChild($section->getSlug(), [
'route' => 'property_type_search',
'routeParameters' => [
'section_slug' => $section->getSlug(),
'property_type_slug' => 'property-type',
],
])
->setAttribute('title', $translatedTitle)
->setLabel($translatedTitle);
}
}
public function getRequest()
{
return $this->requestStack->getCurrentRequest();
}
/**
* Convert underscore_strings to camelCase.
*
* @param string $str
*
* @return string
*/
private function snakeToCamel($str)
{
return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($str))));
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
'aqarmap.main.admin_sidebar_menu_configure' => 'onAdminSidebarMenuConfigure',
'aqarmap.main.breadcrumb_configure' => 'onBreadcrumbConfigure',
];
}
}