<?php
namespace Aqarmap\Bundle\MainBundle\Listener;
use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
use Aqarmap\Bundle\MainBundle\Event\ConfigureMenuEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConfigureMenuEventSubscriber implements EventSubscriberInterface
{
/**
* Container instance.
*/
protected $container;
/**
* Translator instance.
*/
protected TranslatorInterface $translator;
private AuthorizationCheckerInterface $authorizationChecker;
private FeatureToggleManager $featureToggleManager;
public function __construct(
ContainerInterface $container,
TranslatorInterface $translator,
AuthorizationCheckerInterface $authorizationChecker,
FeatureToggleManager $featureToggleManager
) {
$this->container = $container;
$this->translator = $translator;
$this->authorizationChecker = $authorizationChecker;
$this->featureToggleManager = $featureToggleManager;
}
public function onAdminSidebarMenuConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$userRole = $this->authorizationChecker;
$menu
// Dashboard
->addChild('dashboard', ['route' => 'admin_homepage'])
->setLabel($this->translator->trans('layout.dashboard'))
->setExtra('position', 10)
->setExtra('icon', 'home');
if ($this->checkListingRoles($userRole)) {
// Listings
$menu
->addChild('listings_group', ['uri' => '#'])
->setLabel('Listings')
->setExtra('position', 20)
->setExtra('icon', 'th-list');
}
if ($this->checkKnowMoreRoles($userRole)) {
$menu
->addChild('know_more', ['uri' => '#'])
->setLabel('Know More')
->setExtra('position', 70)
->setExtra('icon', 'binoculars');
}
if ($this->checkAnalysisRoles($userRole)) {
$menu
->addChild('analysis', ['uri' => '#'])
->setLabel('Analysis')
->setExtra('position', 80)
->setExtra('icon', 'chart-bar');
}
if ($userRole->isGranted('ROLE_LOCATIONS')
|| $userRole->isGranted('ROLE_WEBMASTER')
) {
$menu
->addChild('structure_group', ['uri' => '#'])
->setLabel('Structure')
->setExtra('position', 60)
->setExtra('icon', 'sitemap');
}
if ($userRole->isGranted('ROLE_VE')) {
$menu
->addChild('value_engine', ['uri' => '#'])
->setLabel('Value Engine')
->setExtra('position', 81)
->setExtra('icon', 'chart-pie');
}
if ($userRole->isGranted('ROLE_VE')) {
$menu['value_engine']
->addChild('ve_listings', ['route' => 'aqarmap_admin_ve_listings_insights'])
->setLabel('Listings')
->setExtra('icon', 'list-ul');
}
if ($userRole->isGranted('ROLE_VE')) {
$menu['value_engine']
->addChild('ve_locations', ['route' => 'aqarmap_admin_ve_locations_insights'])
->setLabel('Locations')
->setExtra('icon', 'map-marker-alt');
}
if ($userRole->isGranted('ROLE_VE')) {
$menu['value_engine']
->addChild('ve_customers', ['route' => 'aqarmap_admin_ve_customers_insights'])
->setLabel('Customers')
->setExtra('icon', 'users');
}
if ($userRole->isGranted('ROLE_VE')) {
$menu['value_engine']
->addChild('ve_depth_products', ['route' => 'aqarmap_admin_ve_depth_product_insights'])
->setLabel('Depth Products')
->setExtra('icon', 'fa fa-chart-bar');
}
if ($this->checkExperimentsRoles($userRole)) {
$menu
->addChild('experiments', ['uri' => '#'])
->setLabel('Experiments')
->setExtra('position', 90)
->setExtra('icon', 'flask');
}
if ($userRole->isGranted('ROLE_API_DEVELOPER')) {
$menu
->addChild('featureToggles', ['route' => 'admin_feature_toggle'])
->setLabel('Feature Toggles')
->setExtra('position', 91)
->setExtra('icon', 'toggle-on');
}
if ($userRole->isGranted('ROLE_SUPPORT')) {
$menu
->addChild('joinUs', ['route' => 'admin_join_us'])
->setLabel('Join Us')
->setExtra('position', 94)
->setExtra('icon', 'fas fa-handshake');
}
if ($userRole->isGranted('ROLE_FINANCIAL_AID')) {
$menu
->addChild('FinancialAids', ['route' => 'admin_financial_aid'])
->setLabel('Financial Aids')
->setExtra('icon', 'fas fa-dollar-sign');
}
if ($this->checkSettingsRoles($userRole)) {
$menu
->addChild('settings', ['uri' => '#'])
->setLabel('Settings')
->setExtra('position', 100)
->setExtra('icon', 'cog');
$menu
->addChild('announcement', ['route' => 'aqarmap_admin_Announcement'])
->setLabel($this->translator->trans('layout.admin.announcement'))
->setExtra('position', 101)
->setExtra('icon', 'bullhorn');
}
if ($userRole->isGranted('ROLE_SUPPORT')) {
$menu['users_group']
->addChild('feedback', ['route' => 'admin_feedback'])
->setLabel($this->translator->trans('layout.admin.feedback'))
->setExtra('position', 55)
->setExtra('icon', 'weixin')
->setExtra('brand_icon', true);
}
if ($userRole->isGranted('ROLE_WEBMASTER')) {
$menu['settings']
->addChild('footermenu', ['route' => 'aqarmap_admin_menus'])
->setLabel($this->translator->trans('layout.admin.menu'))
->setExtra('position', 101)
->setExtra('icon', 'align-justify');
}
if ($this->featureToggleManager->isEnabled('web.seo.custom.pragraphs')
&& $userRole->isGranted('ROLE_CUSTOM_PARAGRAPH')) {
$menu
->addChild('customParagraphs', ['route' => 'admin_custom_paragraphs_index'])
->setLabel($this->translator->trans('layout.admin.custom_paragraph.title'))
->setExtra('position', 92)
->setExtra('icon', 'align-justify');
}
if ($userRole->isGranted('ROLE_SAVED_SEARCHES')) {
$menu
->addChild('searchResult', ['route' => 'aqarmap_admin_saved_searches'])
->setLabel('Searches Log')
->setExtra('position', 94)
->SetExtra('icon', 'search');
}
if ($userRole->isGranted('ROLE_TOP_CUSTOMER')) {
$menu
->addChild('sponsors', ['route' => 'admin_default_top_customers_index'])
->setLabel($this->translator->trans('layout.admin.default_top_customers.indexTitle'))
->setExtra('position', 93)
->setExtra('icon', 'certificate');
}
if ($userRole->isGranted('ROLE_MOBILE_SECTION_TOGGLE')) {
$menu
->addChild('Mobile Sections Toggles', ['route' => 'aqarmap_admin_mobile_sections_toggles'])
->setLabel('Mobile Sections Toggles')
->setExtra('icon', 'certificate');
}
}
public function onMainNavbarMenuConfigure(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$menu
// Homepage
->addChild('home', ['route' => 'homepage'])
->setLabel($this->translator->trans('layout.homepage'))
->setExtra('position', 10);
// Check if has admin role
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
$menu
// Dashboard
->addChild('dashboard', ['route' => 'admin_homepage'])
->setLabel($this->translator->trans('layout.dashboard'))
->setExtra('position', 20);
}
}
/**
* Check listing roles.
*/
private function checkListingRoles($userRole): bool
{
return $userRole->isGranted('ROLE_FREE_LISTING')
|| $userRole->isGranted('ROLE_PAID_LISTING')
|| $userRole->isGranted('ROLE_INCREASING_LISTINGS')
|| $userRole->isGranted('ROLE_ADMIN_ACTIVITIES')
|| $userRole->isGranted('ROLE_PUBLISHED_LISTINGS_COUNT')
|| $userRole->isGranted('ROLE_PENDING_FEATURING_LISTING')
|| $userRole->isGranted('ROLE_ACTIVE_CUSTOMER');
}
private function checkAnalysisRoles($userRole): bool
{
return $userRole->isGranted('ROLE_ANALYSIS')
|| $userRole->isGranted('ROLE_MARKETING')
|| $userRole->isGranted('ROLE_CHURN_EXPIRY')
|| $userRole->isGranted('ROLE_SALES_ANALYSIS');
}
private function checkKnowMoreRoles($userRole): bool
{
return $userRole->isGranted('ROLE_DISCUSSION')
|| $userRole->isGranted('ROLE_MARKETING')
|| $userRole->isGranted('ROLE_WEBMASTER')
|| $userRole->isGranted('ROLE_ACCESS_TO_ADMIN_LEADS');
}
private function checkExperimentsRoles($userRole): bool
{
return $userRole->isGranted('ROLE_WEBMASTER')
|| $userRole->isGranted('ROLE_NOTIFICATION_SETTING')
|| $userRole->isGranted('ROLE_NOTIFIER')
|| $userRole->isGranted('ROLE_CUSTOMER_PROFILING')
|| $userRole->isGranted('ROLE_SUPER_ADMIN');
}
private function checkSettingsRoles($userRole): bool
{
return $userRole->isGranted('ROLE_ANNOUNCEMENTS')
|| $userRole->isGranted('ROLE_MARKETING')
|| $userRole->isGranted('ROLE_WEBMASTER')
|| $userRole->isGranted('ROLE_CRM')
|| $userRole->isGranted('ROLE_SUPER_ADMIN');
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['aqarmap.main.admin_sidebar_menu_configure' => 'onAdminSidebarMenuConfigure'];
}
}