Add Film CRUD

This commit is contained in:
Timothy Warren 2018-02-15 09:48:57 -05:00
parent cbdccc0fb1
commit 3d7ec2ebde
8 changed files with 398 additions and 10 deletions

View File

@ -0,0 +1,147 @@
<?php
namespace App\Controller;
use App\Entity\Film;
use App\Form\FilmType;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMInvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Film controller.
*
* @Route("film")
*/
class FilmController extends Controller
{
/**
* Lists all film entities.
*
* @Route("/", name="film_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->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();
}
}

View File

@ -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;
}

47
src/Form/FilmType.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace App\Form;
use App\Entity\Film;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FilmType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->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';
}
}

View File

@ -0,0 +1,26 @@
{% extends 'form.html.twig' %}
{% block form %}
<h1>Edit Film</h1>
<div class="small callout">
<ul>
<li>
<a href="{{ path('film_index') }}">Back to the list</a>
</li>
</ul>
</div>
<div class="large primary callout">
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
<button type="submit" class="success button">Update</button>
{{ form_end(edit_form) }}
<hr />
{{ form_start(delete_form) }}
<button type="submit" class="alert button">Delete</button>
{{ form_end(delete_form) }}
</div>
{% endblock %}

View File

@ -0,0 +1,58 @@
{% extends 'base.html.twig' %}
{% block body %}
<h2>Film</h2>
<div class="small callout primary">
<ul>
<li>
<a href="{{ path('film_new') }}">Add a Film</a>
</li>
</ul>
</div>
<table class="hover scroll stack">
<thead>
<tr>
<th>Actions</th>
<th>Id</th>
<th>Brand</th>
<th>Product Line</th>
<th>Film Name</th>
<th>Film Speed</th>
<th>Film Format</th>
<th>Film Base</th>
<th>Unused Rolls</th>
<th>Developed Rolls</th>
<th>Chemistry</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
{% for film in films %}
<tr>
<td>
<ul>
<li>
<a href="{{ path('film_edit', { 'id': film.id }) }}">edit</a>
</li>
</ul>
</td>
<td><a href="{{ path('film_show', { 'id': film.id }) }}">{{ film.id }}</a></td>
<td>{{ film.brand }}</td>
<td>{{ film.productLine }}</td>
<td>{{ film.filmName }}</td>
<td>{{ film.filmSpeedAsa }}/{{ film.filmSpeedDin }}&deg;</td>
<td>{{ film.filmFormat }}</td>
<td>{{ film.filmBase }}</td>
<td>{{ film.unusedRolls }}</td>
<td>{{ film.developedRolls }}</td>
<td>{{ film.chemistry }}</td>
<td>{{ film.notes }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -0,0 +1,22 @@
{% extends 'form.html.twig' %}
{% block form %}
<h2>Add a Film</h2>
<div class="small callout">
<ul>
<li>
<a href="{{ path('film_index') }}">Back to the list</a>
</li>
</ul>
</div>
<div class="large primary callout">
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="success button">Add</button>
{{ form_end(form) }}
</div>
{% endblock %}

View File

@ -0,0 +1,77 @@
{% extends 'form.html.twig' %}
{% block form %}
<h2>Film</h2>
<div class="callout">
<ul>
<li>
<a href="{{ path('film_index') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('film_edit', { 'id': film.id }) }}">Edit</a>
</li>
</ul>
<hr />
{{ form_start(delete_form) }}
<button type="submit" class="alert button">Delete Film</button>
{{ form_end(delete_form) }}
</div>
<div class="large primary callout">
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ film.id }}</td>
</tr>
<tr>
<th>Brand</th>
<td>{{ film.brand }}</td>
</tr>
<tr>
<th>Product Line</th>
<td>{{ film.productLine }}</td>
</tr>
<tr>
<th>Film Name</th>
<td>{{ film.filmName }}</td>
</tr>
<tr>
<th>Film Speed</th>
<td>{{ film.filmSpeedAsa }}/{{ film.filmSpeedDin }}&deg;</td>
</tr>
<tr>
<th>Film Format</th>
<td>{{ film.filmFormat }}</td>
</tr>
<tr>
<th>Film Base</th>
<td>{{ film.filmBase }}</td>
</tr>
<tr>
<th># of Unused Rolls</th>
<td>{{ film.unusedRolls }}</td>
</tr>
<tr>
<th># of Developed Rolls</th>
<td>{{ film.developedRolls }}</td>
</tr>
<tr>
<th>Chemistry</th>
<td>{{ film.chemistry }}</td>
</tr>
<tr>
<th>Notes</th>
<td>{{ film.notes }}</td>
</tr>
</tbody>
</table>
</div>
{% endblock %}

View File

@ -23,6 +23,9 @@
<a href="{{ path('previously-owned-lens_index') }}">Lenses</a>
</li>
<li class="menu-text">Meta</li>
<li class="{{ route starts with 'film_' ? 'is-active' }}">
<a href="{{ path('film_index') }}">Film</a>
</li>
<li class="{{ route starts with 'camera-type_' ? 'is-active' }}">
<a href="{{ path('camera-type_index') }}">Camera Types</a>
</li>