src/Controller/BackOffice/ProtocolsController.php line 154

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BackOffice;
  3. use App\Entity\Assay\Protocol;
  4. use App\Entity\Assay\SampleType;
  5. use App\Form\Type\BackOffice\ProtocolType;
  6. use App\Model\PaginatedSearchCriteria;
  7. use App\Model\ProtocolSearchCriteria;
  8. use App\Service\ProtocolService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\Form;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * Class ProtocolsController
  21.  * @package App\Controller\BackOffice
  22.  * @Route(path="protocols")
  23.  */
  24. class ProtocolsController extends AbstractController
  25. {
  26.     /**
  27.      * @var EntityManagerInterface
  28.      */
  29.     private $entityManager;
  30.     /**
  31.      * @var FormFactoryInterface
  32.      */
  33.     private $formFactory;
  34.     /**
  35.      * @var ProtocolService
  36.      */
  37.     private $protocolService;
  38.     /**
  39.      * ProtocolsController constructor.
  40.      * @param EntityManagerInterface $entityManager
  41.      * @param FormFactoryInterface $formFactory
  42.      * @param ProtocolService $protocolService
  43.      */
  44.     public function __construct(
  45.         EntityManagerInterface $entityManager,
  46.         FormFactoryInterface $formFactory,
  47.         ProtocolService $protocolService
  48.     ) {
  49.         $this->entityManager $entityManager;
  50.         $this->formFactory $formFactory;
  51.         $this->protocolService $protocolService;
  52.     }
  53.     /**
  54.      * @param Request $request
  55.      * @param bool $customerProtocols
  56.      * @return Response
  57.      * @Route(
  58.      *     methods={"GET"},
  59.      *     defaults={"customerProtocols": false}
  60.      * )
  61.      * @Route(
  62.      *     methods={"GET"},
  63.      *     path="/customer",
  64.      *     defaults={"customerProtocols": true},
  65.      *     name="app_backoffice_protocols_customer"
  66.      * )
  67.      */
  68.     public function indexAction(Request $requestbool $customerProtocols): Response
  69.     {
  70.         $searchCriteria = new ProtocolSearchCriteria($request->query);
  71.         $searchCriteria->setCustomerProtocols($customerProtocols);
  72.         $searchCriteria->setPageSize(PaginatedSearchCriteria::BACKOFFICE_PAGINATION_SIZE);
  73.         $protocols $this->entityManager
  74.             ->getRepository(Protocol::class)
  75.             ->search($searchCriteria);
  76.         return $this->render(
  77.             'backOffice/protocols/index.html.twig',
  78.             [
  79.                 'protocols' => $protocols,
  80.                 'customerProtocols' => $customerProtocols,
  81.             ]
  82.         );
  83.     }
  84.     /**
  85.      * @Route(path="/new", methods={"GET", "POST"})
  86.      * @param Request $request
  87.      * @return RedirectResponse|Response
  88.      * @noinspection DuplicatedCode
  89.      */
  90.     public function newAction(Request $request)
  91.     {
  92.         $protocol = new Protocol();
  93.         $protocolForm $this->formFactory
  94.             ->create(ProtocolType::class, $protocol, ['disable_sku' => false])
  95.             ->add('submit'SubmitType::class);
  96.         $protocolForm->handleRequest($request);
  97.         
  98.         if($protocolForm->isSubmitted() && $protocolForm->isValid()) {
  99.             /** @var Protocol $protocol */
  100.             $protocol $protocolForm->getData();
  101.             $protocol->setVersion(1);
  102.             $this->protocolService->setDefaultSampleType($protocol);
  103.             foreach($protocol->getProtocolSampleTypes() as $protocolSampleType) {
  104.                 $protocolSampleType->setProtocol($protocol);
  105.             }
  106.             $this->entityManager->persist($protocol);
  107.             $this->entityManager->flush();
  108.             return $this->redirectToRoute(
  109.                 'app_backoffice_protocols_edit',
  110.                 [
  111.                     'protocolId' => $protocol->getProtocolId(),
  112.                 ]
  113.             );
  114.         }
  115.         $sampleTypes $this->entityManager
  116.             ->getRepository(SampleType::class)
  117.             ->findAll();
  118.         return $this->render(
  119.             'backOffice/protocols/new.html.twig',
  120.             [
  121.                 'protocol' => $protocol,
  122.                 'protocolForm' => $protocolForm->createView(),
  123.                 'allSampleTypes' => $sampleTypes
  124.             ]
  125.         );
  126.     }
  127.     /**
  128.      * @Route(path="/{protocolId}", methods={"GET", "POST"})
  129.      * @param $protocolId
  130.      * @param Request $request
  131.      * @return Response
  132.      * @noinspection DuplicatedCode
  133.      */
  134.     public function editAction($protocolIdRequest $request): Response
  135.     {
  136.         $protocol $this->loadProtocol($protocolId);
  137.         $protocolForm $this->formFactory
  138.             ->create(ProtocolType::class, $protocol, [
  139.                 'disable_sku' => true,
  140.                 'require_biomarker' => $protocol->getCustomer() === null,
  141.                 'require_analysis_methods' => $protocol->getCustomer() === null,
  142.             ])
  143.             ->add(
  144.                 'submitAndIncrementVersion',
  145.                 SubmitType::class,
  146.                 [
  147.                     'label' => 'Save New Version',
  148.                     'attr' => ['class' => 'btn-primary'],
  149.                 ]
  150.             );
  151.         $protocolForm->handleRequest($request);
  152.         if($protocolForm->isSubmitted() && $protocolForm->isValid()) {
  153.             /** @var Protocol $protocolVersion */
  154.             $protocolVersion $protocolForm->getData();
  155.             if($protocolForm instanceof Form) {
  156.                 $clickedButton $protocolForm->getClickedButton();
  157.                 
  158.                 if($clickedButton && $clickedButton->getName() === 'submitAndIncrementVersion') {
  159.                     $protocolVersion $this->protocolService->createNewProtocolVersion($protocolVersion);
  160.                     $this->entityManager->refresh($protocol);
  161.                 }
  162.             }
  163.             $this->entityManager->persist($protocolVersion);
  164.             $this->entityManager->flush();
  165.             return $this->redirectToRoute(
  166.                 'app_backoffice_protocols_edit',
  167.                 [
  168.                     'protocolId' => $protocolVersion->getProtocolId(),
  169.                 ]
  170.             );
  171.         }
  172.         $protocolVersions $this->entityManager
  173.             ->getRepository(Protocol::class)
  174.             ->findAllProtocolVersions($protocol->getSku());
  175.         $sampleTypes $this->entityManager
  176.             ->getRepository(SampleType::class)
  177.             ->findAll();
  178.         return $this->render(
  179.             'backOffice/protocols/edit.html.twig',
  180.             [
  181.                 'protocol' => $protocol,
  182.                 'protocolForm' => $protocolForm->createView(),
  183.                 'protocolVersions' => $protocolVersions,
  184.                 'allSampleTypes' => $sampleTypes,
  185.             ]
  186.         );
  187.     }
  188.     /**
  189.      * @param $protocolId
  190.      * @return Protocol
  191.      */
  192.     protected function loadProtocol($protocolId): object
  193.     {
  194.         $protocol $this->entityManager
  195.             ->getRepository(Protocol::class)
  196.             ->find($protocolId);
  197.         if (!$protocol) {
  198.             throw $this->createNotFoundException();
  199.         }
  200.         return $protocol;
  201.     }
  202. }