vendor/bluue/projects-bundle/src/EventSubscriber/CrmBundle/OpportunityViewLinksSubscriber.php line 81

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\CrmBundle;
  9. use ReflectionClass;
  10. use App\Services\CheckBundleInstall;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Bluue\CrmBundle\Entity\OpportunityDocument;
  15. use Bluue\ProjectsBundle\Entity\ProjectQuotation;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OpportunityViewLinksSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var TranslatorInterface
  22.      */
  23.     private TranslatorInterface $tr;
  24.     /**
  25.      * @var Security
  26.      */
  27.     protected Security $security;
  28.     /**
  29.      * @var RouterInterface
  30.      */
  31.     protected RouterInterface $router;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     protected EntityManagerInterface $em;
  36.     /**
  37.      * @param TranslatorInterface $tr
  38.      * @param Security $security
  39.      * @param RouterInterface $router
  40.      * @param EntityManagerInterface $em
  41.      */
  42.     public function __construct(
  43.         TranslatorInterface $tr,
  44.         Security $security,
  45.         RouterInterface $router,
  46.         EntityManagerInterface $em
  47.     ) {
  48.         $this->tr $tr;
  49.         $this->security $security;
  50.         $this->router $router;
  51.         $this->em $em;
  52.     }
  53.     /**
  54.      * @return array
  55.      */
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         if (CheckBundleInstall::exist('crm-bundle')) {
  59.             $reflectionClass = new ReflectionClass('Bluue\CrmBundle\Event\OpportunityViewLinksEvent');
  60.             return [
  61.                 $reflectionClass->getConstant('LINKS') => 'links'
  62.             ];
  63.         }
  64.         return [];
  65.     }
  66.     /**
  67.      * @param object $event
  68.      * @return void
  69.      */
  70.     public function links(object $event): void
  71.     {
  72.         if (!$this->security->isGranted('ROLE__PROJECTS__READ_ONLY')) {
  73.             return;
  74.         }
  75.         $opportunityDocuments $this->em->getRepository(OpportunityDocument::class)
  76.             ->findBy(['opportunity' => $event->getId()]);
  77.         if (empty($opportunityDocuments)) {
  78.             return;
  79.         }
  80.         $projects $newLinks = [];
  81.         foreach ($opportunityDocuments as $opportunityDocument) {
  82.             $quotation $opportunityDocument->getQuotation();
  83.             if (!$quotation) {
  84.                 continue;
  85.             }
  86.             $projectQuotation $this->em->getRepository(ProjectQuotation::class)
  87.                 ->findOneBy(['quotation' => $quotation->getId()]);
  88.             if (!$projectQuotation) {
  89.                 continue;
  90.             }
  91.             $project $projectQuotation->getProject();
  92.             $projectId = (string) $project->getId();
  93.             if (!array_key_exists($projectId$projects)) {
  94.                 $projects[$projectId] = $project;
  95.             }
  96.         }
  97.         foreach ($projects as $project) {
  98.             $href $this->router->generate(
  99.                 'projects_bundle__project_view',
  100.                 ['id' => $project->getId(), 'step_id' => $project->getCurrentStep()->getId()]
  101.             );
  102.             $link '<a href="' $href '" class="btn btn-sm btn-primary mb-2"><i class="fas fa-project-diagram">';
  103.             $link .= '</i> ' $this->tr->trans('Project:', [], 'ProjectsBundle') . ' ' $project->getName() . '</a>';
  104.             $newLinks[] = $link;
  105.         }
  106.         $event->setLinks(array_merge($event->getLinks(), $newLinks));
  107.     }
  108. }