vendor/bluue/tickets-bundle/src/EventSubscriber/CrmBundle/OpportunityViewLinksSubscriber.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\TicketsBundle\EventSubscriber\CrmBundle;
  9. use ReflectionClass;
  10. use App\Services\CheckBundleInstall;
  11. use Bluue\CrmBundle\Entity\Opportunity;
  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 OpportunityViewLinksSubscriber 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('crm-bundle')) {
  58.             $reflectionClass = new ReflectionClass('Bluue\CrmBundle\Event\OpportunityViewLinksEvent');
  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__TICKETS__READ_ONLY')) {
  72.             return;
  73.         }
  74.         $opportunity $this->em->getRepository(Opportunity::class)->find($event->getId());
  75.         if ($opportunity) {
  76.             $options $opportunity->getOptions();
  77.             if (isset($options['tickets-bundle'])) {
  78.                 $ticketNumber = isset($options['tickets-bundle']['ticketNumber']) ?
  79.                     $options['tickets-bundle']['ticketNumber'] : null;
  80.                 if ($ticketNumber) {
  81.                     $href $this->router->generate('tickets_bundle__ticket_view', ['number' => $ticketNumber]);
  82.                     $link '<a href="' $href '" class="btn btn-sm btn-primary mb-2"><i class="fas fa-exclamation-';
  83.                     $link .= 'circle"></i> ' $this->tr->trans('Linked ticket', [], 'TicketsBundle') . '</a>';
  84.                     $event->setLinks(array_merge($event->getLinks(), [$link]));
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }