<?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\CrmBundle\EventSubscriber;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Bluue\CrmBundle\Entity\OpportunityDocument;
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 ProjectViewLinksSubscriber 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('projects-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\ProjectsBundle\Event\ProjectViewLinksEvent');
return [
$reflectionClass->getConstant('LINKS') => 'links'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function links(object $event): void
{
if (!$this->security->isGranted('ROLE__CRM__READ_ONLY')) {
return;
}
$projectQuotation = $this->em->getRepository('ProjectsBundle:ProjectQuotation')
->findOneBy(['project' => $event->getId()]);
if (!$projectQuotation) {
return;
}
$opportunityDocument = $this->em->getRepository(OpportunityDocument::class)
->findOneBy(['quotation' => $projectQuotation->getQuotation()]);/* @phpstan-ignore-line */
if (!$opportunityDocument) {
return;
}
$opportunity = $opportunityDocument->getOpportunity();
$href = $this->router->generate('crm_bundle__opportunity_view', ['id' => $opportunity->getId()]);
$link = '<a href="' . $href . '" class="btn btn-sm btn-primary"><i class="fas fa-handshake"></i> ';
$link .= $this->tr->trans('Opportunity:', [], 'CrmBundle') . ' ' . $opportunity->getName() . '</a>';
$newLinks = [$link];
$event->setLinks(array_merge($event->getLinks(), $newLinks));
}
}