chill-bundles/CRUD/Controller/EntityPersonCRUDController.php

92 lines
3.3 KiB
PHP

<?php
/*
* Copyright (C) 2016-2019 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\CRUD\Controller;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Chill\PersonBundle\Entity\Person;
/**
* CRUD Controller for entities attached to a Person
*/
class EntityPersonCRUDController extends CRUDController
{
protected function createEntity($action, Request $request): object
{
if (FALSE === $request->query->has('person_id')) {
throw $this->createNotFoundException('the person is not set');
}
$entity = parent::createEntity($action, $request);
$person = $this->getDoctrine()
->getRepository(Person::class)
->find($request->query->getInt('person_id'))
;
if (NULL === $person) {
throw $this->createNotFoundException('the person with this id is not found');
}
$entity->setPerson($person);
return $entity;
}
protected function getTemplateFor($action, $entity, Request $request)
{
if ($this->hasCustomTemplate($action, $entity, $request)) {
return $this->getActionConfig($action)['template'];
}
switch ($action) {
case 'new':
return '@ChillPerson/CRUD/new.html.twig';
case 'edit':
return '@ChillPerson/CRUD/edit.html.twig';
case 'view':
return '@ChillPerson/CRUD/view.html.twig';
case 'delete':
return '@ChillPerson/CRUD/delete.html.twig';
default:
return parent::getTemplateFor($action, $entity, $request);
}
}
protected function onBeforeRedirectAfterSubmission(string $action, $entity, \Symfony\Component\Form\FormInterface $form, Request $request)
{
$next = $request->request->get("submit", "save-and-close");
switch ($next) {
case "save-and-close":
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index', [
'id' => $entity->getPerson()->getId()
]);
case "save-and-new":
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_new', [
'person_id' => $entity->getPerson()->getId()
]);
default:
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', [
'id' => $entity->getId()
]);
}
}
}