diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ca43b6e..c3b010ccc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,12 +81,6 @@ Version 1.5.9 - [closing motive] Add an admin section for closing motives ; <<<<<<< HEAD -Master branch -============= - -- [CRUD] add step delete - -======= Version 1.5.10 ============== @@ -103,3 +97,10 @@ Version 1.5.12 - [addresses] add a homeless to person's addresses, and this information into person list +Master branch +============= + + +- [CRUD] add step delete +- [CRUD] improve index view in person CRUD + diff --git a/CRUD/Controller/EntityPersonCRUDController.php b/CRUD/Controller/EntityPersonCRUDController.php index c99ba0b9e..aae821b3c 100644 --- a/CRUD/Controller/EntityPersonCRUDController.php +++ b/CRUD/Controller/EntityPersonCRUDController.php @@ -27,14 +27,26 @@ use Chill\PersonBundle\Entity\Person; */ class EntityPersonCRUDController extends CRUDController { - protected function createEntity($action, Request $request): object + /** + * Extract the person from the request + * + * the person parameter will be `person_id` and must be + * present in the query + * + * If the parameter is not set, this method will return null. + * + * If the person id does not exists, the method will throw a + * Symfony\Component\HttpKernel\Exception\NotFoundHttpException + * + * @param Request $request + * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the person with given id is not found + */ + protected function getPerson(Request $request): ?Person { if (FALSE === $request->query->has('person_id')) { - throw $this->createNotFoundException('the person is not set'); + return null; } - $entity = parent::createEntity($action, $request); - $person = $this->getDoctrine() ->getRepository(Person::class) ->find($request->query->getInt('person_id')) @@ -44,11 +56,38 @@ class EntityPersonCRUDController extends CRUDController throw $this->createNotFoundException('the person with this id is not found'); } + return $person; + } + + protected function createEntity($action, Request $request): object + { + $entity = parent::createEntity($action, $request); + + $person = $this->getPerson($request); + $entity->setPerson($person); return $entity; } + protected function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = array()): array + { + $person = $this->getPerson($request); + + if (NULL === $person) { + throw new \Exception("the `person_id` parameter is not set in the query. " + . "You should set it or override the current method to allow another " + . "behaviour: ".__METHOD__); + } + + return parent::generateTemplateParameter( + $action, + $entity, + $request, + \array_merge([ 'person' => $person ], $defaultTemplateParameters) + ); + } + protected function getTemplateFor($action, $entity, Request $request) { if ($this->hasCustomTemplate($action, $entity, $request)) { @@ -64,6 +103,8 @@ class EntityPersonCRUDController extends CRUDController return '@ChillPerson/CRUD/view.html.twig'; case 'delete': return '@ChillPerson/CRUD/delete.html.twig'; + case 'index': + return '@ChillPerson/CRUD/index.html.twig'; default: return parent::getTemplateFor($action, $entity, $request); } @@ -76,7 +117,7 @@ class EntityPersonCRUDController extends CRUDController switch ($next) { case "save-and-close": return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index', [ - 'id' => $entity->getPerson()->getId() + 'person_id' => $entity->getPerson()->getId() ]); case "save-and-new": return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_new', [ @@ -88,4 +129,6 @@ class EntityPersonCRUDController extends CRUDController ]); } } + + } diff --git a/Resources/views/CRUD/index.html.twig b/Resources/views/CRUD/index.html.twig new file mode 100644 index 000000000..994e25e1d --- /dev/null +++ b/Resources/views/CRUD/index.html.twig @@ -0,0 +1,15 @@ +{% extends '@ChillPerson/layout.html.twig' %} + +{% set activeRouteKey = '' %} + +{% block title %}{{ ('crud.' ~ crud_name ~ '.index.title')|trans({'%crud_name%': crud_name}) }}{% endblock %} + +{% block personcontent %} + {% embed '@ChillMain/CRUD/_index.html.twig' %} + {% block add_new %} +
  • + {{ ('crud.'~crud_name~'.index.add_new')|trans( {'%crud_name%': crud_name} ) }} +
  • + {% endblock %} + {% endembed %} +{% endblock personcontent %}