collection-crud/src/Controller/PreviouslyOwnedCameraController.php

115 lines
3.3 KiB
PHP
Raw Normal View History

2017-11-30 15:06:13 -05:00
<?php
namespace App\Controller;
2017-11-30 15:06:13 -05:00
use App\Entity\PreviouslyOwnedCamera;
use App\Form\PreviouslyOwnedCameraType;
2017-11-30 15:06:13 -05:00
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2018-07-11 09:18:46 -04:00
use Symfony\Component\Routing\Annotation\Route;
2018-02-14 15:08:03 -05:00
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request};
2017-11-30 15:06:13 -05:00
/**
* Previouslyownedcamera controller.
*
* @Route("previously-owned-camera")
*/
class PreviouslyOwnedCameraController extends Controller
{
/**
* Lists all previouslyOwnedCamera entities.
*
2018-07-11 09:18:46 -04:00
* @Route("/", name="previously-owned-camera_index", methods={"GET"})
2018-01-04 10:42:36 -05:00
* @throws \UnexpectedValueException
2017-11-30 15:06:13 -05:00
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$previouslyOwnedCameras = $em->getRepository('App:PreviouslyOwnedCamera')->findBy([], [
2017-11-30 15:06:13 -05:00
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
return $this->render('previouslyownedcamera/index.html.twig', array(
'previouslyOwnedCameras' => $previouslyOwnedCameras,
));
}
/**
* Finds and displays a previouslyOwnedCamera entity.
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}", name="previously-owned-camera_show", methods={"GET"})
2017-11-30 15:06:13 -05:00
*/
public function showAction(PreviouslyOwnedCamera $previouslyOwnedCamera)
{
return $this->render('previouslyownedcamera/show.html.twig', array(
'previouslyOwnedCamera' => $previouslyOwnedCamera,
));
}
/**
* Displays a form to edit an existing previouslyOwnedCamera entity.
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}/edit", name="previously-owned-camera_edit", methods={"GET", "POST"})
2018-01-04 10:42:36 -05:00
* @throws \LogicException
2017-11-30 15:06:13 -05:00
*/
public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera)
{
2018-01-04 10:42:36 -05:00
$editForm = $this->createForm(PreviouslyOwnedCameraType::class, $previouslyOwnedCamera);
2017-11-30 15:06:13 -05:00
$editForm->handleRequest($request);
2018-02-14 15:08:03 -05:00
$reacquireForm = $this->createReacquireForm($previouslyOwnedCamera);
2017-11-30 15:06:13 -05:00
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('previously-owned-camera_edit', array('id' => $previouslyOwnedCamera->getId()));
}
return $this->render('previouslyownedcamera/edit.html.twig', array(
'previouslyOwnedCamera' => $previouslyOwnedCamera,
'edit_form' => $editForm->createView(),
2018-02-14 15:08:03 -05:00
'reacquire_form' => $reacquireForm->createView()
2017-11-30 15:06:13 -05:00
));
}
2018-02-14 15:08:03 -05:00
/**
* Moves a camera to the previouslyOwned table
*
2018-07-11 09:18:46 -04:00
* @Route("/{id}/reacquire", name="previously-owned-camera_reacquire", methods={"POST"})
2018-02-14 15:08:03 -05:00
* @param Request $request
* @param PreviouslyOwnedCamera $camera
* @throws \LogicException
* @throws \Doctrine\ORM\ORMInvalidArgumentException
* @return RedirectResponse
*/
public function reacquireAction(Request $request, PreviouslyOwnedCamera $camera): RedirectResponse
{
$form = $this->createReacquireForm($camera);
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(PreviouslyOwnedCamera::class);
$repository->reacquire($camera);
return $this->redirectToRoute('previously-owned-camera_index');
}
/**
* Creates a form to move
*
* @param PreviouslyOwnedCamera $camera The camera entity
*
* @return FormInterface
*/
private function createReacquireForm(PreviouslyOwnedCamera $camera): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('previously-owned-camera_reacquire', ['id' => $camera->getId()]))
->setMethod('POST')
->getForm();
}
2017-11-30 15:06:13 -05:00
}