From 3d7ec2ebde04fe5e0b347852897194ecc3c12b85 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 15 Feb 2018 09:48:57 -0500 Subject: [PATCH] Add Film CRUD --- src/Controller/FilmController.php | 147 ++++++++++++++++++++++++++++++ src/Entity/Film.php | 28 ++++-- src/Form/FilmType.php | 47 ++++++++++ templates/film/edit.html.twig | 26 ++++++ templates/film/index.html.twig | 58 ++++++++++++ templates/film/new.html.twig | 22 +++++ templates/film/show.html.twig | 77 ++++++++++++++++ templates/header.html.twig | 3 + 8 files changed, 398 insertions(+), 10 deletions(-) create mode 100644 src/Controller/FilmController.php create mode 100644 src/Form/FilmType.php create mode 100644 templates/film/edit.html.twig create mode 100644 templates/film/index.html.twig create mode 100644 templates/film/new.html.twig create mode 100644 templates/film/show.html.twig diff --git a/src/Controller/FilmController.php b/src/Controller/FilmController.php new file mode 100644 index 0000000..23189d6 --- /dev/null +++ b/src/Controller/FilmController.php @@ -0,0 +1,147 @@ +getDoctrine()->getManager(); + + $films = $em->getRepository('App:Film')->findBy([], [ + 'brand' => 'ASC', + 'productLine' => 'ASC', + 'filmFormat' => 'ASC', + ]); + + return $this->render('film/index.html.twig', array( + 'films' => $films, + )); + } + + /** + * Creates a new film entity. + * + * @Route("/new", name="film_new") + * @Method({"GET", "POST"}) + */ + public function newAction(Request $request) + { + $film = new Film(); + $form = $this->createForm(FilmType::class, $film); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($film); + $em->flush(); + + return $this->redirectToRoute('film_show', array('id' => $film->getId())); + } + + return $this->render('film/new.html.twig', array( + 'film' => $film, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a film entity. + * + * @Route("/{id}", name="film_show") + * @Method("GET") + */ + public function showAction(Film $film) + { + $deleteForm = $this->createDeleteForm($film); + + return $this->render('film/show.html.twig', array( + 'film' => $film, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing film entity. + * + * @Route("/{id}/edit", name="film_edit") + * @Method({"GET", "POST"}) + * @throws \LogicException + */ + public function editAction(Request $request, Film $film) + { + $deleteForm = $this->createDeleteForm($film); + + $editForm = $this->createForm(FilmType::class, $film); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('film_edit', array('id' => $film->getId())); + } + + return $this->render('film/edit.html.twig', array( + 'film' => $film, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Deletes a film entity. + * + * @Route("/{id}", name="film_delete") + * @Method("DELETE") + * @throws \LogicException + */ + public function deleteAction(Request $request, Film $film) + { + $form = $this->createDeleteForm($film); + $form->handleRequest($request); + + $em = $this->getDoctrine()->getManager(); + $em->remove($film); + $em->flush(); + + return $this->redirectToRoute('film_index'); + } + + /** + * Creates a form to delete a film entity. + * + * @param Film $film The film entity + * + * @return \Symfony\Component\Form\FormInterface The form + */ + private function createDeleteForm(Film $film): FormInterface + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('film_delete', ['id' => $film->getId()])) + ->setMethod('DELETE') + ->getForm(); + } +} diff --git a/src/Entity/Film.php b/src/Entity/Film.php index 02483ab..073dfac 100644 --- a/src/Entity/Film.php +++ b/src/Entity/Film.php @@ -98,10 +98,18 @@ class Film { */ private $notes; + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + /** * @return string */ - public function getBrand(): string + public function getBrand(): ?string { return $this->brand; } @@ -119,7 +127,7 @@ class Film { /** * @return string */ - public function getProductLine(): string + public function getProductLine(): ?string { return $this->productLine; } @@ -137,7 +145,7 @@ class Film { /** * @return string */ - public function getFilmName(): string + public function getFilmName(): ?string { return $this->filmName; } @@ -155,7 +163,7 @@ class Film { /** * @return int */ - public function getFilmSpeedAsa(): int + public function getFilmSpeedAsa(): ?int { return $this->filmSpeedAsa; } @@ -173,7 +181,7 @@ class Film { /** * @return int */ - public function getFilmSpeedDin(): int + public function getFilmSpeedDin(): ?int { return $this->filmSpeedDin; } @@ -191,7 +199,7 @@ class Film { /** * @return string */ - public function getFilmFormat(): string + public function getFilmFormat(): ?string { return $this->filmFormat; } @@ -209,7 +217,7 @@ class Film { /** * @return string */ - public function getFilmBase(): string + public function getFilmBase(): ?string { return $this->filmBase; } @@ -227,7 +235,7 @@ class Film { /** * @return int */ - public function getUnusedRolls(): int + public function getUnusedRolls(): ?int { return $this->unusedRolls; } @@ -245,7 +253,7 @@ class Film { /** * @return int */ - public function getDevelopedRolls(): int + public function getDevelopedRolls(): ?int { return $this->developedRolls; } @@ -263,7 +271,7 @@ class Film { /** * @return string */ - public function getChemistry(): string + public function getChemistry(): ?string { return $this->chemistry; } diff --git a/src/Form/FilmType.php b/src/Form/FilmType.php new file mode 100644 index 0000000..151e9d8 --- /dev/null +++ b/src/Form/FilmType.php @@ -0,0 +1,47 @@ +add('brand') + ->add('productLine') + ->add('filmName') + ->add('filmSpeedAsa') + ->add('filmSpeedDin') + ->add('filmFormat') + ->add('filmBase') + ->add('unusedRolls') + ->add('developedRolls') + ->add('chemistry') + ->add('notes'); + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults(array( + 'data_class' => Film::class + )); + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix(): string + { + return 'camerabundle_film'; + } +} diff --git a/templates/film/edit.html.twig b/templates/film/edit.html.twig new file mode 100644 index 0000000..2f47a77 --- /dev/null +++ b/templates/film/edit.html.twig @@ -0,0 +1,26 @@ +{% extends 'form.html.twig' %} + +{% block form %} +

Edit Film

+ +
+ +
+ +
+ {{ form_start(edit_form) }} + {{ form_widget(edit_form) }} + + {{ form_end(edit_form) }} + +
+ + {{ form_start(delete_form) }} + + {{ form_end(delete_form) }} +
+{% endblock %} diff --git a/templates/film/index.html.twig b/templates/film/index.html.twig new file mode 100644 index 0000000..b0636ae --- /dev/null +++ b/templates/film/index.html.twig @@ -0,0 +1,58 @@ +{% extends 'base.html.twig' %} + +{% block body %} +

Film

+ +
+ +
+ + + + + + + + + + + + + + + + + + + + {% for film in films %} + + + + + + + + + + + + + + + {% endfor %} + +
ActionsIdBrandProduct LineFilm NameFilm SpeedFilm FormatFilm BaseUnused RollsDeveloped RollsChemistryNotes
+ + {{ film.id }}{{ film.brand }}{{ film.productLine }}{{ film.filmName }}{{ film.filmSpeedAsa }}/{{ film.filmSpeedDin }}°{{ film.filmFormat }}{{ film.filmBase }}{{ film.unusedRolls }}{{ film.developedRolls }}{{ film.chemistry }}{{ film.notes }}
+ + +{% endblock %} diff --git a/templates/film/new.html.twig b/templates/film/new.html.twig new file mode 100644 index 0000000..80b8f7c --- /dev/null +++ b/templates/film/new.html.twig @@ -0,0 +1,22 @@ +{% extends 'form.html.twig' %} + +{% block form %} +

Add a Film

+ +
+ +
+ +
+ {{ form_start(form) }} + {{ form_widget(form) }} + + {{ form_end(form) }} +
+ + +{% endblock %} diff --git a/templates/film/show.html.twig b/templates/film/show.html.twig new file mode 100644 index 0000000..2206001 --- /dev/null +++ b/templates/film/show.html.twig @@ -0,0 +1,77 @@ +{% extends 'form.html.twig' %} + +{% block form %} +

Film

+ +
+ + + +
+ + + {{ form_start(delete_form) }} + + {{ form_end(delete_form) }} +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id{{ film.id }}
Brand{{ film.brand }}
Product Line{{ film.productLine }}
Film Name{{ film.filmName }}
Film Speed{{ film.filmSpeedAsa }}/{{ film.filmSpeedDin }}°
Film Format{{ film.filmFormat }}
Film Base{{ film.filmBase }}
# of Unused Rolls{{ film.unusedRolls }}
# of Developed Rolls{{ film.developedRolls }}
Chemistry{{ film.chemistry }}
Notes{{ film.notes }}
+
+ + +{% endblock %} diff --git a/templates/header.html.twig b/templates/header.html.twig index bd213f3..add3ddf 100644 --- a/templates/header.html.twig +++ b/templates/header.html.twig @@ -23,6 +23,9 @@ Lenses +
  • + Film +
  • Camera Types