src/Controller/MachineController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Machine;
  4. use App\Entity\MachinePaperSizeMatrix;
  5. use App\Entity\PaperSize;
  6. use App\Form\MachinePaperSizeMatrixType;
  7. use App\Form\MachineType;
  8. use App\Repository\MachinePaperSizeMatrixRepository;
  9. use App\Repository\MachineRepository;
  10. use App\Repository\PaperSizeRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  18. /**
  19.  * @Route("/admin/machine")
  20.  */
  21. class MachineController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/", name="machine_index", methods={"GET"})
  25.      */
  26.     public function index(MachineRepository $machineRepository,
  27.                           PaginatorInterface $paginator,Request $request,
  28.                           EntityManagerInterface $em): Response
  29.     {
  30.         $queryString trim($request->query->get('query'));
  31.         $query $machineRepository->findByNameOrCodeQuery($queryString);
  32.         $pagination $paginator->paginate(
  33.             $query/* query NOT result */
  34.             $request->query->getInt('page'1), /*page number*/
  35.             20/*limit per page*/
  36.             [
  37.                 'defaultSortFieldName'      => 'm.createdAt',
  38.                 'defaultSortDirection' => 'desc'
  39.             ]
  40.         );
  41.         return $this->render('machine/index.html.twig', [
  42.             'pagination' => $pagination
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/new", name="machine_new", methods={"GET", "POST"})
  47.      */
  48.     public function new(Request $requestMachineRepository $machineRepository): Response
  49.     {
  50.         $machine = new Machine();
  51.         $form $this->createForm(MachineType::class, $machine);
  52.         $form->handleRequest($request);
  53.         if ($form->isSubmitted() && $form->isValid()) {
  54.             $machineRepository->add($machinetrue);
  55.             return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
  56.         }
  57.         return $this->renderForm('machine/new.html.twig', [
  58.             'machine' => $machine,
  59.             'form' => $form,
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/{id}", name="machine_show", methods={"GET"})
  64.      */
  65.     public function show(Machine $machine): Response
  66.     {
  67.         return $this->render('machine/show.html.twig', [
  68.             'machine' => $machine,
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/{id}/edit", name="machine_edit", methods={"GET", "POST"})
  73.      */
  74.     public function edit(Request $requestMachine $machineMachineRepository $machineRepository): Response
  75.     {
  76.         $form $this->createForm(MachineType::class, $machine);
  77.         $form->handleRequest($request);
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $machineRepository->add($machinetrue);
  80.             $this->addFlash('success''Machine updated.');
  81.             return $this->redirectToRoute('machine_edit', ['id' =>$machine->getId()], Response::HTTP_SEE_OTHER);
  82.         }
  83.         return $this->renderForm('machine/edit.html.twig', [
  84.             'machine' => $machine,
  85.             'form' => $form,
  86.         ]);
  87.     }
  88.     /**
  89.      * @Route("/{id}/paper_size_matrix_index", name="paper_size_matrix_index")
  90.      */
  91.     public function paper_size_matrix_index(Request $requestMachine $machineEntityManagerInterface $entityManager): Response
  92.     {
  93.         //Paper size matrix available
  94.         $paperSizes $machine->getPaperSizes();
  95.         //$machinePaperSizeMatrices = $machine->getMachinePaperSizeMatrices();
  96.         //$entityManager->initializeObject($machinePaperSizeMatrices);
  97.         //dump($machinePaperSizeMatrices);
  98.         //Check if the paper size matrix exists
  99. /*        foreach ($paperSizes as $paperSize){
  100.             $
  101.         }
  102.         dump($papersizes);*/
  103.         /*foreach ($machinePaperSizeMatrices as $machinePaperSizeMatrix){
  104.             foreach ($paperSizes as $paperSize){
  105.                 if ($machinePaperSizeMatrix->getPaperSize()->getName() == $machinePaperSize->getName()){
  106.                 }
  107.             }
  108.         }*/
  109.         $paperSizesAvailable = [];
  110.         return $this->render('machine/machine_paper_size_matrix_index.html.twig', [
  111.             'machine' => $machine,
  112.             'paperSizes' => $paperSizes
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/paper_size_matrix_edit/{id}", name="paper_size_matrix_edit")
  117.      */
  118.     public function paper_size_matrix_edit(Request $requestMachinePaperSizeMatrix $machinePaperSizeMatrix,
  119.                                            MachinePaperSizeMatrixRepository$machinePaperSizeMatrixRepository): Response
  120.     {
  121.         //dump($machinePaperSizeMatrix);
  122.         $form $this->createForm(MachinePaperSizeMatrixType::class, $machinePaperSizeMatrix);
  123.         $form->handleRequest($request);
  124.         if ($form->isSubmitted() && $form->isValid()) {
  125.             $machinePaperSizeMatrixRepository->add($machinePaperSizeMatrixtrue);
  126.             $machine $machinePaperSizeMatrix->getMachine();
  127.             return $this->redirectToRoute('paper_size_matrix_index', ['id' => $machine->getId()], Response::HTTP_SEE_OTHER);
  128.         }
  129.         return $this->renderForm('machine/machine_paper_size_matrix_edit.html.twig', [
  130.             'machinePaperSizeMatrix' => $machinePaperSizeMatrix,
  131.             'machine' => $machinePaperSizeMatrix->getMachine(),
  132.             'form' => $form
  133.         ]);
  134.     }
  135.     /**
  136.      * @Route("/paper_size_matrix_new/{id}/paper_size/{paper_size_id}", name="paper_size_matrix_new")
  137.      * @Entity("paperSize", expr="repository.find(paper_size_id)")
  138.      */
  139.     public function machine_paper_size_matrix_new(Request $requestMachine $machine,
  140.         PaperSize $paperSizeMachinePaperSizeMatrixRepository $machinePaperSizeMatrixRepository
  141.         ): Response
  142.     {
  143.         //$paperSizeId = $request->request->get("paper_size_id");
  144.         $machinePaperSizeMatrix = new MachinePaperSizeMatrix();
  145.         $form $this->createForm(MachinePaperSizeMatrixType::class, $machinePaperSizeMatrix);
  146.         $form->handleRequest($request);
  147.         if ($form->isSubmitted() && $form->isValid()) {
  148.             $machinePaperSizeMatrix->setMachine($machine);
  149.             $machinePaperSizeMatrix->setPaperSize($paperSize);
  150.             $machinePaperSizeMatrixRepository->add($machinePaperSizeMatrixtrue);
  151.             return $this->redirectToRoute('paper_size_matrix_index', ['id' => $machine->getId()], Response::HTTP_SEE_OTHER);
  152.         }
  153.         return $this->renderForm('machine/machine_paper_size_matrix_new.html.twig', [
  154.             'machinePaperSizeMatrix' => $machinePaperSizeMatrix,
  155.             'machine' => $machine,
  156.             'paperSize' => $paperSize,
  157.             'form' => $form
  158.         ]);
  159.     }
  160.     /**
  161.      * @Route("/{id}", name="machine_delete", methods={"POST"})
  162.      */
  163.     public function delete(Request $requestMachine $machineMachineRepository $machineRepository): Response
  164.     {
  165.         if ($this->isCsrfTokenValid('delete'.$machine->getId(), $request->request->get('_token'))) {
  166.             $this->addFlash('success''Machine ID: '.$machine->getId().' deleted.');
  167.             $machineRepository->remove($machinetrue);
  168.         }
  169.         return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
  170.     }
  171.     /**
  172.      * @Route("/{id}/duplicate", name="machine_duplicate", methods={"POST"})
  173.      */
  174.     public function duplicate(Request $requestMachine $machineMachineRepository $machineRepository,
  175.                               EntityManagerInterface $em): Response
  176.     {
  177.         $newMachine = clone $machine;
  178.         $newMachine->setCode($machine->getCode().' '.time());
  179.         $now = new \DateTime('now');
  180.         $newMachine->setCreatedAt($now);
  181.         $newMachine->setUpdatedAt($now);
  182.         $em->persist($newMachine);
  183.         $em->flush();
  184.         $this->addFlash('success''Machine created');
  185.         return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
  186.     }
  187. }