<?php
namespace App\Controller;
use App\Entity\Machine;
use App\Entity\MachinePaperSizeMatrix;
use App\Entity\PaperSize;
use App\Form\MachinePaperSizeMatrixType;
use App\Form\MachineType;
use App\Repository\MachinePaperSizeMatrixRepository;
use App\Repository\MachineRepository;
use App\Repository\PaperSizeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
/**
* @Route("/admin/machine")
*/
class MachineController extends AbstractController
{
/**
* @Route("/", name="machine_index", methods={"GET"})
*/
public function index(MachineRepository $machineRepository,
PaginatorInterface $paginator,Request $request,
EntityManagerInterface $em): Response
{
$queryString = trim($request->query->get('query'));
$query = $machineRepository->findByNameOrCodeQuery($queryString);
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20, /*limit per page*/
[
'defaultSortFieldName' => 'm.createdAt',
'defaultSortDirection' => 'desc'
]
);
return $this->render('machine/index.html.twig', [
'pagination' => $pagination
]);
}
/**
* @Route("/new", name="machine_new", methods={"GET", "POST"})
*/
public function new(Request $request, MachineRepository $machineRepository): Response
{
$machine = new Machine();
$form = $this->createForm(MachineType::class, $machine);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$machineRepository->add($machine, true);
return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('machine/new.html.twig', [
'machine' => $machine,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="machine_show", methods={"GET"})
*/
public function show(Machine $machine): Response
{
return $this->render('machine/show.html.twig', [
'machine' => $machine,
]);
}
/**
* @Route("/{id}/edit", name="machine_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Machine $machine, MachineRepository $machineRepository): Response
{
$form = $this->createForm(MachineType::class, $machine);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$machineRepository->add($machine, true);
$this->addFlash('success', 'Machine updated.');
return $this->redirectToRoute('machine_edit', ['id' =>$machine->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('machine/edit.html.twig', [
'machine' => $machine,
'form' => $form,
]);
}
/**
* @Route("/{id}/paper_size_matrix_index", name="paper_size_matrix_index")
*/
public function paper_size_matrix_index(Request $request, Machine $machine, EntityManagerInterface $entityManager): Response
{
//Paper size matrix available
$paperSizes = $machine->getPaperSizes();
//$machinePaperSizeMatrices = $machine->getMachinePaperSizeMatrices();
//$entityManager->initializeObject($machinePaperSizeMatrices);
//dump($machinePaperSizeMatrices);
//Check if the paper size matrix exists
/* foreach ($paperSizes as $paperSize){
$
}
dump($papersizes);*/
/*foreach ($machinePaperSizeMatrices as $machinePaperSizeMatrix){
foreach ($paperSizes as $paperSize){
if ($machinePaperSizeMatrix->getPaperSize()->getName() == $machinePaperSize->getName()){
}
}
}*/
$paperSizesAvailable = [];
return $this->render('machine/machine_paper_size_matrix_index.html.twig', [
'machine' => $machine,
'paperSizes' => $paperSizes
]);
}
/**
* @Route("/paper_size_matrix_edit/{id}", name="paper_size_matrix_edit")
*/
public function paper_size_matrix_edit(Request $request, MachinePaperSizeMatrix $machinePaperSizeMatrix,
MachinePaperSizeMatrixRepository$machinePaperSizeMatrixRepository): Response
{
//dump($machinePaperSizeMatrix);
$form = $this->createForm(MachinePaperSizeMatrixType::class, $machinePaperSizeMatrix);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$machinePaperSizeMatrixRepository->add($machinePaperSizeMatrix, true);
$machine = $machinePaperSizeMatrix->getMachine();
return $this->redirectToRoute('paper_size_matrix_index', ['id' => $machine->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('machine/machine_paper_size_matrix_edit.html.twig', [
'machinePaperSizeMatrix' => $machinePaperSizeMatrix,
'machine' => $machinePaperSizeMatrix->getMachine(),
'form' => $form
]);
}
/**
* @Route("/paper_size_matrix_new/{id}/paper_size/{paper_size_id}", name="paper_size_matrix_new")
* @Entity("paperSize", expr="repository.find(paper_size_id)")
*/
public function machine_paper_size_matrix_new(Request $request, Machine $machine,
PaperSize $paperSize, MachinePaperSizeMatrixRepository $machinePaperSizeMatrixRepository
): Response
{
//$paperSizeId = $request->request->get("paper_size_id");
$machinePaperSizeMatrix = new MachinePaperSizeMatrix();
$form = $this->createForm(MachinePaperSizeMatrixType::class, $machinePaperSizeMatrix);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$machinePaperSizeMatrix->setMachine($machine);
$machinePaperSizeMatrix->setPaperSize($paperSize);
$machinePaperSizeMatrixRepository->add($machinePaperSizeMatrix, true);
return $this->redirectToRoute('paper_size_matrix_index', ['id' => $machine->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('machine/machine_paper_size_matrix_new.html.twig', [
'machinePaperSizeMatrix' => $machinePaperSizeMatrix,
'machine' => $machine,
'paperSize' => $paperSize,
'form' => $form
]);
}
/**
* @Route("/{id}", name="machine_delete", methods={"POST"})
*/
public function delete(Request $request, Machine $machine, MachineRepository $machineRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$machine->getId(), $request->request->get('_token'))) {
$this->addFlash('success', 'Machine ID: '.$machine->getId().' deleted.');
$machineRepository->remove($machine, true);
}
return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}/duplicate", name="machine_duplicate", methods={"POST"})
*/
public function duplicate(Request $request, Machine $machine, MachineRepository $machineRepository,
EntityManagerInterface $em): Response
{
$newMachine = clone $machine;
$newMachine->setCode($machine->getCode().' '.time());
$now = new \DateTime('now');
$newMachine->setCreatedAt($now);
$newMachine->setUpdatedAt($now);
$em->persist($newMachine);
$em->flush();
$this->addFlash('success', 'Machine created');
return $this->redirectToRoute('machine_index', [], Response::HTTP_SEE_OTHER);
}
}