<?php
namespace App\Component\AdminMenu\EventSubscriber;
use App\Component\AdminMenu\Event\AbstractBuildMenuEvent;
use App\Component\AdminMenu\Event\BuildMainMenuEvent;
use App\Component\AdminMenu\Event\BuildVacancyMenuEvent;
use App\Component\AdminMenu\MenuBuilder;
use App\Component\UserRole\Permission\UserRolePermission;
use App\Entity\Company;
use App\Entity\CompanyAttribute;
use App\Entity\CompanyStatus;
use App\Entity\Employee;
use App\Entity\EmployeeAttribute;
use App\Entity\FeedbackCompany;
use App\EventListener\FeatureFlagListener;
class CompanyMenuSubscriber extends AbstractMenuBuildEventSubscriber
{
protected static int $priority = -100;
protected static string $eventClass = BuildVacancyMenuEvent::class;
public static function getSubscribedEvents(): array
{
$subscribers = parent::getSubscribedEvents();
$subscribers[BuildMainMenuEvent::class] = ['buildMenu', -100];
return $subscribers;
}
public function buildMenu(AbstractBuildMenuEvent $event)
{
if (
(
!$this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Company::class]])
&& !$this->hasAccess(['ROLE_ADMIN_HIRING_MANAGER', [UserRolePermission::MASK_VIEW, Company::class]])
&& !$this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyAttribute::class]])
&& !$this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyStatus::class]])
&& !$this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Employee::class]])
)
|| !$this->checkFeatures([FeatureFlagListener::FEATURE_COMPANY, FeatureFlagListener::FEATURE_EMPLOYEE])
) {
return;
}
$menuBuilder = null;
if (!empty($event->getMenu()['Content']['OVERLAY']['Manage content'])) {
$menuItem = $event->getMenu()['Content']['OVERLAY']['Manage content'];
$menuBuilder = $this->getMenuBuilder($menuItem);
}
if ($event instanceof BuildVacancyMenuEvent) {
$menuBuilder = $this->getMenuBuilder($event->getMenu(), 'Vacancies')
->addMainItem('Companies', 'fal fa-building')
->addOverlay()
->addSection('Companies');
}
if (!$menuBuilder) {
return;
}
if ($this->checkCompanyColumnPermissions()) {
$this->renderCompanyColumn($menuBuilder);
}
if ($this->checkEmployeeColumnPermissions()) {
$this->renderEmployeeColumn($menuBuilder);
}
if ($this->checkOtherColumnPermissions() && $event instanceof BuildVacancyMenuEvent) {
$this->renderOtherColumn($menuBuilder);
}
}
private function checkCompanyColumnPermissions(): bool
{
return (
$this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Company::class]])
|| $this->hasAccess(['ROLE_ADMIN_HIRING_MANAGER', [UserRolePermission::MASK_VIEW, Company::class]])
|| $this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyAttribute::class]])
|| $this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyStatus::class]])
)
&& $this->checkFeatures([FeatureFlagListener::FEATURE_COMPANY]);
}
private function checkEmployeeColumnPermissions(): bool
{
return $this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Employee::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_EMPLOYEE]);
}
private function checkOtherColumnPermissions(): bool
{
return $this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, FeedbackCompany::class]])
&& $this->checkFeatures([FeatureFlagListener::FEATURE_FEEDBACK_COMPANY]);
}
private function renderCompanyColumn(MenuBuilder $menuBuilder)
{
$menuBuilder->addColumn('Companies', 'fal fa-building');
if ($this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, Company::class]])
|| $this->hasAccess(['ROLE_ADMIN_HIRING_MANAGER', [UserRolePermission::MASK_VIEW, Company::class]])
) {
$menuBuilder->addItem('Create company', 'company_create')
->addItem('View companies', 'admin_companies');
}
if ($this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyAttribute::class]])) {
$menuBuilder->addItem('Company attributes', 'admin_company_attribute_index');
}
if ($this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, CompanyStatus::class]])) {
$menuBuilder->addItem('Company Statuses', 'admin_company_status_index');
}
$menuBuilder->end();
}
private function renderEmployeeColumn(MenuBuilder $menuBuilder)
{
$menuBuilder->addColumn('Employees', 'fal fa-user-friends');
$menuBuilder->addItem('Create employee', 'employee_create');
$menuBuilder->addItem('View employees', 'admin_employees');
if ($this->hasAccess(['ROLE_ADMIN_USER', [UserRolePermission::MASK_VIEW, EmployeeAttribute::class]])) {
$menuBuilder->addItem('Employee attributes', 'admin_employee_attribute_index');
}
$menuBuilder->end()
;
}
private function renderOtherColumn(MenuBuilder $menuBuilder)
{
$menuBuilder->addColumn('Other', 'fal fa-globe')
->addItem('Feedback Company', 'admin_feedback_company_index');
}
}