<?php
namespace App\Component\AdminMenu\EventSubscriber;
use App\Component\AdminMenu\Event\AbstractBuildMenuEvent;
use App\Component\AdminMenu\MenuBuilder;
use App\Component\UserRole\Permission\UserRolePermission;
use App\Dto\MenuItem;
use App\Entity\Appointment;
use App\Entity\CallMeBackRequest;
use App\Entity\Contact;
use App\Entity\ContactCompany;
use App\Entity\Feedback;
use App\Entity\LearningTrackContact;
use App\Entity\Mail;
use App\Entity\RegionContact;
use App\Entity\SharpSpringForm;
use App\Entity\Subscription;
use App\Event\NewsletterEvent;
use App\EventListener\FeatureFlagListener;
use Knp\Menu\ItemInterface;
class ContactMenuSubscriber extends AbstractMenuBuildEventSubscriber
{
protected static int $priority = -20;
public function buildMenu(AbstractBuildMenuEvent $event)
{
if (
!$this->checkMailPermissions()
&& !$this->checkContactColumnPermissions()
&& !$this->checkSharpSpringColumnPermissions()
&& !$this->checkNewsLetterColumnPermissions()
) {
return;
}
$item = $event->getMenu();
$menuBuilder = $this->getMenuBuilder($item);
$menuBuilder->addMainItem('Contact', 'fal fa-exchange-alt');
/** @var ItemInterface $contactItem */
$contactItem = $menuBuilder->getCurrentContext();
$menuBuilder->addOverlay()
->addSection('Contact');
$contactItemContainer = $menuBuilder->getCurrentContext();
$contactDto = new MenuItem($contactItem, $contactItemContainer);
if ($this->checkMailPermissions()) {
$menuBuilder
->addColumn('Mail', 'fal fa-envelope')
->addItem('Mails', 'ats_mails')
->addItem('Mail sets', 'admin_mail_sets')
->addItem('Mail importance', 'admin_settings', ['type' => 'Mail importance'])
->end();
}
if ($this->checkContactColumnPermissions()) {
$menuBuilder->addColumn('Inbox', 'fal fa-inbox');
/** @var ItemInterface $currentItem */
$currentItem = $menuBuilder->getCurrentContext();
$inboxDto = new MenuItem($currentItem);
$this->addContactMenu($menuBuilder);
$this->addLearningTrackMenu($menuBuilder);
$this->addContactCompanyMenu($menuBuilder);
$this->addRegionContactMenu($menuBuilder);
$this->addCallMeBackMenu($menuBuilder);
$this->addFeedbackMenu($menuBuilder);
$this->addAppointmentMenu($menuBuilder);
$menuBuilder->end();
$inboxDto->close();
}
if ($this->checkSharpSpringColumnPermissions()) {
$menuBuilder
->addColumn('Sharpspring', 'fal fa-network-wired')
->addItem('Forms', 'admin_sharp_spring_index')
->end();
}
if ($this->checkNewsLetterColumnPermissions()) {
$menuBuilder->addColumn('Newsletter', 'fal fa-newspaper');
/** @var ItemInterface $currentItem */
$currentItem = $menuBuilder->getCurrentContext();
$newsletterDto = new MenuItem($currentItem);
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Subscription::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_NEWSLETTER])
) {
$menuBuilder->addItem('Newsletters', 'admin_newsletters');
}
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, NewsletterEvent::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_NEWSLETTER_EVENTS])
) {
$menuBuilder->addItem('Newsletter events', 'admin_newsletters_events');
}
$menuBuilder->end();
$newsletterDto->close();
}
$contactDto->close();
}
private function addContactMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Contact::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_CONTACT])
) {
$menuBuilder->addItem('Messages', 'admin_contact_index');
}
}
private function addLearningTrackMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, LearningTrackContact::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_LEARNING_TRACK_CONTACT])
) {
$menuBuilder->addItem('Learning Track messages', 'admin_learning_track_contact_index');
}
}
private function addContactCompanyMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, ContactCompany::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_CONTACT_COMPANY])
) {
$menuBuilder->addItem('Company contact', 'admin_contact_company_index');
}
}
private function addRegionContactMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, RegionContact::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_REGION_CONTACT])
) {
$menuBuilder->addItem('Regional contact', 'admin_region_contact_index');
}
}
private function addCallMeBackMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, CallMeBackRequest::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_CALL_ME_BACK])
) {
$menuBuilder->addItem('Call me back requests', 'admin_call_me_back_index');
}
}
private function addFeedbackMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Feedback::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_FEEDBACK_FORM])
) {
$menuBuilder->addItem('Feedback', 'admin_feedback_index');
}
}
private function addAppointmentMenu(MenuBuilder $menuBuilder)
{
if (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Appointment::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_APPOINTMENT])
) {
$menuBuilder->addItem('Appointments', 'admin_appointment_index');
}
}
private function checkMailPermissions(): bool
{
return $this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Mail::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_MAILS]);
}
private function checkContactColumnPermissions(): bool
{
return (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Contact::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, LearningTrackContact::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, ContactCompany::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, RegionContact::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, CallMeBackRequest::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, Feedback::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, Appointment::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, Subscription::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, NewsletterEvent::class]])
)
&& $this->checkFeatures([
FeatureFlagListener::FEATURE_CONTACT,
FeatureFlagListener::FEATURE_LEARNING_TRACK_CONTACT,
FeatureFlagListener::FEATURE_CONTACT_COMPANY,
FeatureFlagListener::FEATURE_REGION_CONTACT,
FeatureFlagListener::FEATURE_CALL_ME_BACK,
FeatureFlagListener::FEATURE_FEEDBACK_FORM,
FeatureFlagListener::FEATURE_APPOINTMENT,
FeatureFlagListener::FEATURE_NEWSLETTER,
FeatureFlagListener::FEATURE_NEWSLETTER_EVENTS,
]);
}
private function checkSharpSpringColumnPermissions(): bool
{
return $this->hasAccess([[UserRolePermission::MASK_VIEW, SharpSpringForm::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_SHARP_SPRING]);
}
private function checkNewsLetterColumnPermissions(): bool
{
return (
$this->hasAccess([[UserRolePermission::MASK_VIEW, Subscription::class]])
|| $this->hasAccess([[UserRolePermission::MASK_VIEW, NewsletterEvent::class]])
)
&& $this->checkFeatures([
FeatureFlagListener::FEATURE_NEWSLETTER,
FeatureFlagListener::FEATURE_NEWSLETTER_EVENTS,
]);
}
}