<?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\RecurringInvoicesBundle\EventSubscriber;
use Bluue\RecurringInvoicesBundle\Repository\RecurringInvoiceRepository;
use Symfony\Contracts\Translation\TranslatorInterface;
use Bluue\SalesBundle\Event\ConfigQuotationButtonsEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigQuotationButtonsSubscriber implements EventSubscriberInterface
{
/**
* @var ContainerInterface
*/
private ContainerInterface $container;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RecurringInvoiceRepository
*/
private RecurringInvoiceRepository $recurringInvoiceRepo;
/**
* @param ContainerInterface $container
* @param TranslatorInterface $tr
* @param RecurringInvoiceRepository $recurringInvoiceRepo
*/
public function __construct(
ContainerInterface $container,
TranslatorInterface $tr,
RecurringInvoiceRepository $recurringInvoiceRepo
) {
$this->container = $container;
$this->tr = $tr;
$this->recurringInvoiceRepo = $recurringInvoiceRepo;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigQuotationButtonsEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigQuotationButtonsEvent $event
* @return void
*/
public function onLoad(ConfigQuotationButtonsEvent $event): void
{
$id = $event->getQuotationId();
$recurringInvoiceExist = $this->recurringInvoiceRepo->findOneBy(['quotation' => $id]);
if (!$recurringInvoiceExist) {
$router = $this->container->get('router');
$buttons = $event->getButtons();
$path = $router->generate('recurring_invoices_bundle__quotation_to_recurring_invoice', ['id' => $id]);
$trView = $this->tr->trans('Convert to recurring invoice', [], 'RecurringInvoicesBundle');
$buttons[] = '<div class="btn-group mr-1 align-self-start">
<a href="' . $path . '" class="btn btn-primary">
<i class="fas fa-file-invoice"></i>
' . $trView . '
</a>
</div>';
$event->setButtons($buttons);
}
}
}