vendor/bluue/tickets-bundle/src/EventSubscriber/CrmBundle/OpportunityViewLinksSubscriber.php line 79

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 Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class OpportunityViewLinksSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var TranslatorInterface
  20.      */
  21.     private TranslatorInterface $tr;
  22.     /**
  23.      * @var Security
  24.      */
  25.     protected Security $security;
  26.     /**
  27.      * @var RouterInterface
  28.      */
  29.     protected RouterInterface $router;
  30.     /**
  31.      * @var EntityManagerInterface
  32.      */
  33.     protected EntityManagerInterface $em;
  34.     /**
  35.      * @param TranslatorInterface $tr
  36.      * @param Security $security
  37.      * @param RouterInterface $router
  38.      * @param EntityManagerInterface $em
  39.      */
  40.     public function __construct(
  41.         TranslatorInterface $tr,
  42.         Security $security,
  43.         RouterInterface $router,
  44.         EntityManagerInterface $em
  45.     ) {
  46.         $this->tr $tr;
  47.         $this->security $security;
  48.         $this->router $router;
  49.         $this->em $em;
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         if (CheckBundleInstall::exist('crm-bundle')) {
  57.             $reflectionClass = new ReflectionClass('Bluue\CrmBundle\Event\OpportunityViewLinksEvent');
  58.             return [
  59.                 $reflectionClass->getConstant('LINKS') => 'links'
  60.             ];
  61.         }
  62.         return [];
  63.     }
  64.     /**
  65.      * @param object $event
  66.      * @return void
  67.      */
  68.     public function links(object $event): void
  69.     {
  70.         if (!$this->security->isGranted('ROLE__TICKETS__READ_ONLY')) {
  71.             return;
  72.         }
  73.         $opportunity $this->em->getRepository('CrmBundle:Opportunity')->find($event->getId());
  74.         if ($opportunity) {
  75.             $options $opportunity->getOptions();
  76.             if (isset($options['tickets_bundle'])) {
  77.                 $ticketNumber = isset($options['tickets_bundle']['ticketNumber']) ?
  78.                     $options['tickets_bundle']['ticketNumber'] : null;
  79.                 if ($ticketNumber) {
  80.                     $href $this->router->generate('tickets_bundle__ticket_view', ['number' => $ticketNumber]);
  81.                     $link '<a href="' $href '" class="btn btn-sm btn-primary mb-2"><i class="fas fa-exclamation-';
  82.                     $link .= 'circle"></i> ' $this->tr->trans('Linked ticket', [], 'TicketsBundle') . '</a>';
  83.                     $event->setLinks(array_merge($event->getLinks(), [$link]));
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }