<?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\PlanningBundle\EventSubscriber\ProjectsBundle;
use ReflectionClass;
use App\Services\CheckBundleInstall;
use Bluue\ProjectsBundle\Entity\Task;
use Symfony\Component\HttpFoundation\RequestStack;
use Bluue\PlanningBundle\Repository\SlotRepository;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TaskViewSubscriber implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var SlotRepository
*/
private SlotRepository $slotRepo;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param SlotRepository $slotRepo
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
SlotRepository $slotRepo
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->slotRepo = $slotRepo;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
if (CheckBundleInstall::exist('projects-bundle')) {
$reflectionClass = new ReflectionClass('Bluue\ProjectsBundle\Event\TaskViewEvent');
return [
$reflectionClass->getConstant('TABS') => 'tabs',
$reflectionClass->getConstant('TABS_PARAMS') => 'tabsParams',
$reflectionClass->getConstant('PATHS') => 'paths'
];
}
return [];
}
/**
* @param object $event
* @return void
*/
public function tabs(object $event): void
{
$tabs = $event->getTabs();
$newTabs = [
[
'template' => '@Planning/ProjectsBundle/planning.html.twig',
'name' => $this->tr->trans('Planning', [], 'PlanningBundle'),
'icon' => 'fas fa-calendar-alt',
'key' => 'planning'
]
];
$event->setTabs(array_merge($tabs, $newTabs));
}
/**
* @param object $event
* @return void
*/
public function tabsParams(object $event): void
{
$params = $event->getTabsParams();
$task = $params['task'];
$event->setTabsParams(array_merge($params, [
'slots' => $this->slotRepo->findBy(['task' => $task])
]));
}
/**
* @param object $event
* @return void
*/
public function paths(object $event): void
{
/** @var Task $task */
$task = $event->getTabsParams()['task'];
$event->addPath([
'url' => 'planning_bundle__task_view_next_slot',
'id' => $task->getId()
]);
}
}