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

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 false;
  94.             if ($currentRoute !== 'projects_bundle__task_time_spent') {
  95.                 $current $projectsMenuChild['route'] == $currentRoute
  96.                     || (
  97.                         $projectsMenuChild['matchPath']
  98.                         && strpos($currentPath$projectsMenuChild['matchPath']) !== false
  99.                     );
  100.             }
  101.             $menu['ProjectsMenu']->addChild(
  102.                 $projectsMenuChild['name'],
  103.                 [
  104.                     'route' => $projectsMenuChild['route'],
  105.                     'current' => $current
  106.                 ]
  107.             );
  108.         }
  109.         if (CheckBundleInstall::exist('crm-bundle')) {
  110.             $matchPath 'bundles/projects-bundle/opportunity';
  111.             if (strpos($currentPath$matchPath) !== false) {
  112.                 $menu->getChild('CrmMenu')->getChild('opportunities')->setCurrent(true);
  113.             }
  114.         }
  115.         $matchPath = [
  116.             'bundles/projects-bundle/customer'
  117.         ];
  118.         foreach ($matchPath as $match) {
  119.             if (strpos($currentPath$match) !== false) {
  120.                 $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  121.             }
  122.         }
  123.         if (
  124.             strpos($currentPath'bundles/projects-bundle/task/time-spent/') !== false
  125.             && strpos($currentPath'customer') !== false
  126.         ) {
  127.             $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  128.         }
  129.     }
  130.     /**
  131.      * @param MenuConfigurationPageEvent $event
  132.      * @return void
  133.      */
  134.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  135.     {
  136.         if ($this->security->isGranted('ROLE__PROJECTS__ADMIN')) {
  137.             $pages $event->getPages();
  138.             $pages[] = [
  139.                 'id' => 'projects_bundle__configuration_settings',
  140.                 'name' => $this->tr->trans('Projects Module', [], 'ProjectsBundle'),
  141.                 'config' => [],
  142.                 'subpages' => [
  143.                     ['id' => 'projects_bundle__configuration_settings'],
  144.                     ['id' => 'projects_bundle__configuration_project_step_list'],
  145.                     ['id' => 'projects_bundle__configuration_project_state_list'],
  146.                     ['id' => 'projects_bundle__configuration_project_type_list'],
  147.                     ['id' => 'projects_bundle__configuration_task_state_list'],
  148.                     ['id' => 'projects_bundle__configuration_task_priority_list'],
  149.                     ['id' => 'projects_bundle__task_models']
  150.                 ]
  151.             ];
  152.             $event->setPages($pages);
  153.         }
  154.     }
  155.     /**
  156.      * @param BundleRolesEvent $event
  157.      * @return void
  158.      */
  159.     public function bundleRoles(BundleRolesEvent $event): void
  160.     {
  161.         $bundles $event->getBundles();
  162.         $bundles[] = [
  163.             'select_name' => 'projects',
  164.             'name' => $this->tr->trans('Projects', [], 'ProjectsBundle')
  165.         ];
  166.         $event->setBundles($bundles);
  167.     }
  168.     /**
  169.      * @param AdditionalJavascriptsEvent $event
  170.      * @return void
  171.      */
  172.     public function additionalJs(AdditionalJavascriptsEvent $event): void
  173.     {
  174.         $javascripts = [
  175.             [
  176.                 'entry' => 'projects_bundle',
  177.                 'controllers' => [
  178.                     [
  179.                         'name' => 'projects--task-model-form',
  180.                         'routes' => [
  181.                             'products_bundle__product_sheet'
  182.                         ]
  183.                     ]
  184.                 ]
  185.             ]
  186.         ];
  187.         $event->addJavascripts($javascripts);
  188.     }
  189. }