vendor/bluue/projects-bundle/src/EventSubscriber/ConfigurationBundleSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\ProjectsBundle\EventSubscriber;
  9. use App\Event\AdditionalJavascriptsEvent;
  10. use App\Event\ConfigureMenuEvent;
  11. use App\Event\MenuConfigurationPageEvent;
  12. use App\Event\BundleRolesEvent;
  13. use App\Services\CheckBundleInstall;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class ConfigurationBundleSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var TranslatorInterface
  22.      */
  23.     private TranslatorInterface $tr;
  24.     /**
  25.      * @var RequestStack
  26.      */
  27.     protected RequestStack $requestStack;
  28.     /**
  29.      * @var Security
  30.      */
  31.     private Security $security;
  32.     /**
  33.      * @param RequestStack $requestStack
  34.      * @param TranslatorInterface $tr
  35.      * @param Security $security
  36.      */
  37.     public function __construct(
  38.         RequestStack $requestStack,
  39.         TranslatorInterface $tr,
  40.         Security $security
  41.     ) {
  42.         $this->requestStack $requestStack;
  43.         $this->tr $tr;
  44.         $this->security $security;
  45.     }
  46.     /**
  47.      * @return string[]
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             ConfigureMenuEvent::NAME => 'mainMenu',
  53.             MenuConfigurationPageEvent::NAME => 'configurationMenu',
  54.             BundleRolesEvent::NAME => 'bundleRoles',
  55.             AdditionalJavascriptsEvent::NAME => 'additionalJs'
  56.         ];
  57.     }
  58.     /**
  59.      * @param ConfigureMenuEvent $event
  60.      * @return void
  61.      */
  62.     public function mainMenu(ConfigureMenuEvent $event): void
  63.     {
  64.         if (!$this->security->isGranted('ROLE__PROJECTS__READ_ONLY')) {
  65.             return;
  66.         }
  67.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  68.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  69.         $menu $event->getMenu();
  70.         $menu->addChild('ProjectsMenu')
  71.             ->setLabel($this->tr->trans('Projects / Tasks', [], 'ProjectsBundle'))
  72.             ->setExtra('icon''fas fa-project-diagram');
  73.         $projectsMenu = [
  74.             [
  75.                 'name' => $this->tr->trans('Projects', [], 'ProjectsBundle'),
  76.                 'route' => 'projects_bundle__project_list',
  77.                 'matchPath' => 'bundles/projects-bundle/project/'
  78.             ],
  79.             [
  80.                 'name' => $this->tr->trans('Tasks', [], 'ProjectsBundle'),
  81.                 'route' => 'projects_bundle__task_list',
  82.                 'matchPath' => 'bundles/projects-bundle/task/'
  83.             ]
  84.         ];
  85.         if ($this->security->isGranted('ROLE__PROJECTS__SMALL_ADMIN')) {
  86.             $projectsMenu[] = [
  87.                 'name' => $this->tr->trans('Mass tasks', [], 'ProjectsBundle'),
  88.                 'route' => 'projects_bundle__mass_task',
  89.                 'matchPath' => null
  90.             ];
  91.         }
  92.         foreach ($projectsMenu as $projectsMenuChild) {
  93.             $current $projectsMenuChild['route'] == $currentRoute
  94.                 || (
  95.                     $projectsMenuChild['matchPath']
  96.                     && strpos($currentPath$projectsMenuChild['matchPath']) !== false
  97.                 )
  98.             ;
  99.             $menu['ProjectsMenu']->addChild(
  100.                 $projectsMenuChild['name'],
  101.                 [
  102.                     'route' => $projectsMenuChild['route'],
  103.                     'current' => $current
  104.                 ]
  105.             );
  106.         }
  107.         if (CheckBundleInstall::exist('crm-bundle')) {
  108.             $matchPath 'bundles/projects-bundle/opportunity';
  109.             if (strpos($currentPath$matchPath) !== false) {
  110.                 $menu->getChild('CrmMenu')->getChild('opportunities')->setCurrent(true);
  111.             }
  112.         }
  113.         $matchPath = [
  114.             'bundles/projects-bundle/customer'
  115.         ];
  116.         foreach ($matchPath as $match) {
  117.             if (strpos($currentPath$match) !== false) {
  118.                 $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  119.             }
  120.         }
  121.     }
  122.     /**
  123.      * @param MenuConfigurationPageEvent $event
  124.      * @return void
  125.      */
  126.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  127.     {
  128.         if ($this->security->isGranted('ROLE__PROJECTS__ADMIN')) {
  129.             $pages $event->getPages();
  130.             $pages[] = [
  131.                 'id' => 'projects_bundle__configuration_settings',
  132.                 'name' => $this->tr->trans('Projects Module', [], 'ProjectsBundle'),
  133.                 'config' => [],
  134.                 'subpages' => [
  135.                     ['id' => 'projects_bundle__configuration_settings'],
  136.                     ['id' => 'projects_bundle__configuration_project_step_list'],
  137.                     ['id' => 'projects_bundle__configuration_project_state_list'],
  138.                     ['id' => 'projects_bundle__configuration_project_type_list'],
  139.                     ['id' => 'projects_bundle__configuration_task_state_list'],
  140.                     ['id' => 'projects_bundle__configuration_task_priority_list'],
  141.                     ['id' => 'projects_bundle__task_models']
  142.                 ]
  143.             ];
  144.             $event->setPages($pages);
  145.         }
  146.     }
  147.     /**
  148.      * @param BundleRolesEvent $event
  149.      * @return void
  150.      */
  151.     public function bundleRoles(BundleRolesEvent $event): void
  152.     {
  153.         $bundles $event->getBundles();
  154.         $bundles[] = [
  155.             'select_name' => 'projects',
  156.             'name' => $this->tr->trans('Projects', [], 'ProjectsBundle')
  157.         ];
  158.         $event->setBundles($bundles);
  159.     }
  160.     /**
  161.      * @param AdditionalJavascriptsEvent $event
  162.      * @return void
  163.      */
  164.     public function additionalJs(AdditionalJavascriptsEvent $event): void
  165.     {
  166.         $javascripts = [
  167.             [
  168.                 'entry' => 'projects_bundle',
  169.                 'controllers' => [
  170.                     [
  171.                         'name' => 'projects--task-model-form',
  172.                         'routes' => [
  173.                             'products_bundle__product_sheet'
  174.                         ]
  175.                     ]
  176.                 ]
  177.             ]
  178.         ];
  179.         $event->addJavascripts($javascripts);
  180.     }
  181. }