vendor/bluue/crm-bundle/src/EventSubscriber/ProjectViewLinksSubscriber.php line 80

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\CrmBundle\EventSubscriber;
  9. use ReflectionClass;
  10. use App\Services\CheckBundleInstall;
  11. use Bluue\CrmBundle\Entity\OpportunityDocument;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProjectViewLinksSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var TranslatorInterface
  21.      */
  22.     private TranslatorInterface $tr;
  23.     /**
  24.      * @var Security
  25.      */
  26.     protected Security $security;
  27.     /**
  28.      * @var RouterInterface
  29.      */
  30.     protected RouterInterface $router;
  31.     /**
  32.      * @var EntityManagerInterface
  33.      */
  34.     protected EntityManagerInterface $em;
  35.     /**
  36.      * @param TranslatorInterface $tr
  37.      * @param Security $security
  38.      * @param RouterInterface $router
  39.      * @param EntityManagerInterface $em
  40.      */
  41.     public function __construct(
  42.         TranslatorInterface $tr,
  43.         Security $security,
  44.         RouterInterface $router,
  45.         EntityManagerInterface $em
  46.     ) {
  47.         $this->tr $tr;
  48.         $this->security $security;
  49.         $this->router $router;
  50.         $this->em $em;
  51.     }
  52.     /**
  53.      * @return array
  54.      */
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         if (CheckBundleInstall::exist('projects-bundle')) {
  58.             $reflectionClass = new ReflectionClass('Bluue\ProjectsBundle\Event\ProjectViewLinksEvent');
  59.             return [
  60.                 $reflectionClass->getConstant('LINKS') => 'links'
  61.             ];
  62.         }
  63.         return [];
  64.     }
  65.     /**
  66.      * @param object $event
  67.      * @return void
  68.      */
  69.     public function links(object $event): void
  70.     {
  71.         if (!$this->security->isGranted('ROLE__CRM__READ_ONLY')) {
  72.             return;
  73.         }
  74.         $projectQuotation $this->em->getRepository('ProjectsBundle:ProjectQuotation')
  75.             ->findOneBy(['project' => $event->getId()]);
  76.         if (!$projectQuotation) {
  77.             return;
  78.         }
  79.         $opportunityDocument $this->em->getRepository(OpportunityDocument::class)
  80.             ->findOneBy(['quotation' => $projectQuotation->getQuotation()]);/* @phpstan-ignore-line */
  81.         if (!$opportunityDocument) {
  82.             return;
  83.         }
  84.         $opportunity $opportunityDocument->getOpportunity();
  85.         $href $this->router->generate('crm_bundle__opportunity_view', ['id' => $opportunity->getId()]);
  86.         $link '<a href="' $href '" class="btn btn-sm btn-primary"><i class="fas fa-handshake"></i> ';
  87.         $link .= $this->tr->trans('Opportunity:', [], 'CrmBundle') . ' ' $opportunity->getName() . '</a>';
  88.         $newLinks = [$link];
  89.         $event->setLinks(array_merge($event->getLinks(), $newLinks));
  90.     }
  91. }