<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\TicketsBundle\EventSubscriber\CrmBundle;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Bluue\CrmBundle\Entity\Opportunity;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OpportunityViewLinksSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var Security
*/
protected Security $security;
/**
* @var RouterInterface
*/
protected RouterInterface $router;
/**
* @var EntityManagerInterface
*/
protected EntityManagerInterface $em;
/**
* @param TranslatorInterface $tr
* @param Security $security
* @param RouterInterface $router
* @param EntityManagerInterface $em
*/
public function __construct(
TranslatorInterface $tr,
Security $security,
RouterInterface $router,
EntityManagerInterface $em
) {
$this->tr = $tr;
$this->security = $security;
$this->router = $router;
$this->em = $em;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
if (CheckBundleInstall::exist('crm-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\CrmBundle\Event\OpportunityViewLinksEvent');
return [
$reflectionClass->getConstant('LINKS') => 'links'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function links(object $event): void
{
if (!$this->security->isGranted('ROLE__TICKETS__READ_ONLY')) {
return;
}
$opportunity = $this->em->getRepository(Opportunity::class)->find($event->getId());
if ($opportunity) {
$options = $opportunity->getOptions();
if (isset($options['tickets-bundle'])) {
$ticketNumber = isset($options['tickets-bundle']['ticketNumber']) ?
$options['tickets-bundle']['ticketNumber'] : null;
if ($ticketNumber) {
$href = $this->router->generate('tickets_bundle__ticket_view', ['number' => $ticketNumber]);
$link = '<a href="' . $href . '" class="btn btn-sm btn-primary mb-2"><i class="fas fa-exclamation-';
$link .= 'circle"></i> ' . $this->tr->trans('Linked ticket', [], 'TicketsBundle') . '</a>';
$event->setLinks(array_merge($event->getLinks(), [$link]));
}
}
}
}
}