<?php
namespace App\Controller\BackOffice;
use App\Entity\Assay\Protocol;
use App\Entity\Assay\SampleType;
use App\Form\Type\BackOffice\ProtocolType;
use App\Model\PaginatedSearchCriteria;
use App\Model\ProtocolSearchCriteria;
use App\Service\ProtocolService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ProtocolsController
* @package App\Controller\BackOffice
* @Route(path="protocols")
*/
class ProtocolsController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* @var ProtocolService
*/
private $protocolService;
/**
* ProtocolsController constructor.
* @param EntityManagerInterface $entityManager
* @param FormFactoryInterface $formFactory
* @param ProtocolService $protocolService
*/
public function __construct(
EntityManagerInterface $entityManager,
FormFactoryInterface $formFactory,
ProtocolService $protocolService
) {
$this->entityManager = $entityManager;
$this->formFactory = $formFactory;
$this->protocolService = $protocolService;
}
/**
* @param Request $request
* @param bool $customerProtocols
* @return Response
* @Route(
* methods={"GET"},
* defaults={"customerProtocols": false}
* )
* @Route(
* methods={"GET"},
* path="/customer",
* defaults={"customerProtocols": true},
* name="app_backoffice_protocols_customer"
* )
*/
public function indexAction(Request $request, bool $customerProtocols): Response
{
$searchCriteria = new ProtocolSearchCriteria($request->query);
$searchCriteria->setCustomerProtocols($customerProtocols);
$searchCriteria->setPageSize(PaginatedSearchCriteria::BACKOFFICE_PAGINATION_SIZE);
$protocols = $this->entityManager
->getRepository(Protocol::class)
->search($searchCriteria);
return $this->render(
'backOffice/protocols/index.html.twig',
[
'protocols' => $protocols,
'customerProtocols' => $customerProtocols,
]
);
}
/**
* @Route(path="/new", methods={"GET", "POST"})
* @param Request $request
* @return RedirectResponse|Response
* @noinspection DuplicatedCode
*/
public function newAction(Request $request)
{
$protocol = new Protocol();
$protocolForm = $this->formFactory
->create(ProtocolType::class, $protocol, ['disable_sku' => false])
->add('submit', SubmitType::class);
$protocolForm->handleRequest($request);
if($protocolForm->isSubmitted() && $protocolForm->isValid()) {
/** @var Protocol $protocol */
$protocol = $protocolForm->getData();
$protocol->setVersion(1);
$this->protocolService->setDefaultSampleType($protocol);
foreach($protocol->getProtocolSampleTypes() as $protocolSampleType) {
$protocolSampleType->setProtocol($protocol);
}
$this->entityManager->persist($protocol);
$this->entityManager->flush();
return $this->redirectToRoute(
'app_backoffice_protocols_edit',
[
'protocolId' => $protocol->getProtocolId(),
]
);
}
$sampleTypes = $this->entityManager
->getRepository(SampleType::class)
->findAll();
return $this->render(
'backOffice/protocols/new.html.twig',
[
'protocol' => $protocol,
'protocolForm' => $protocolForm->createView(),
'allSampleTypes' => $sampleTypes
]
);
}
/**
* @Route(path="/{protocolId}", methods={"GET", "POST"})
* @param $protocolId
* @param Request $request
* @return Response
* @noinspection DuplicatedCode
*/
public function editAction($protocolId, Request $request): Response
{
$protocol = $this->loadProtocol($protocolId);
$protocolForm = $this->formFactory
->create(ProtocolType::class, $protocol, [
'disable_sku' => true,
'require_biomarker' => $protocol->getCustomer() === null,
'require_analysis_methods' => $protocol->getCustomer() === null,
])
->add(
'submitAndIncrementVersion',
SubmitType::class,
[
'label' => 'Save New Version',
'attr' => ['class' => 'btn-primary'],
]
);
$protocolForm->handleRequest($request);
if($protocolForm->isSubmitted() && $protocolForm->isValid()) {
/** @var Protocol $protocolVersion */
$protocolVersion = $protocolForm->getData();
if($protocolForm instanceof Form) {
$clickedButton = $protocolForm->getClickedButton();
if($clickedButton && $clickedButton->getName() === 'submitAndIncrementVersion') {
$protocolVersion = $this->protocolService->createNewProtocolVersion($protocolVersion);
$this->entityManager->refresh($protocol);
}
}
$this->entityManager->persist($protocolVersion);
$this->entityManager->flush();
return $this->redirectToRoute(
'app_backoffice_protocols_edit',
[
'protocolId' => $protocolVersion->getProtocolId(),
]
);
}
$protocolVersions = $this->entityManager
->getRepository(Protocol::class)
->findAllProtocolVersions($protocol->getSku());
$sampleTypes = $this->entityManager
->getRepository(SampleType::class)
->findAll();
return $this->render(
'backOffice/protocols/edit.html.twig',
[
'protocol' => $protocol,
'protocolForm' => $protocolForm->createView(),
'protocolVersions' => $protocolVersions,
'allSampleTypes' => $sampleTypes,
]
);
}
/**
* @param $protocolId
* @return Protocol
*/
protected function loadProtocol($protocolId): object
{
$protocol = $this->entityManager
->getRepository(Protocol::class)
->find($protocolId);
if (!$protocol) {
throw $this->createNotFoundException();
}
return $protocol;
}
}