mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,113 +1,125 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Champs-Libres <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\CRUD\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use function array_merge;
|
||||
|
||||
/**
|
||||
* Class EntityPersonCRUDController
|
||||
* CRUD Controller for entities attached to a Person
|
||||
*
|
||||
* @package Chill\PersonBundle\CRUD\Controller
|
||||
* CRUD Controller for entities attached to a Person.
|
||||
*/
|
||||
class EntityPersonCRUDController extends CRUDController
|
||||
{
|
||||
/**
|
||||
* Extract the person from the request
|
||||
*
|
||||
* the person parameter will be `person_id` and must be
|
||||
* Override the base method to add a filtering step to a person.
|
||||
*
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
protected function buildQueryEntities(string $action, Request $request)
|
||||
{
|
||||
$qb = parent::buildQueryEntities($action, $request);
|
||||
|
||||
return $this->filterQueryEntitiesByPerson($action, $qb, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Chill\MainBundle\CRUD\Controller\string|string $action
|
||||
*/
|
||||
protected function createEntity($action, Request $request): object
|
||||
{
|
||||
$entity = parent::createEntity($action, $request);
|
||||
|
||||
$person = $this->getPerson($request);
|
||||
|
||||
$entity->setPerson($person);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a where clause to the buildQuery.
|
||||
*
|
||||
* @param \Chill\PersonBundle\CRUD\Controller\QueryBuilder $qb
|
||||
*
|
||||
* @return \Chill\PersonBundle\CRUD\Controller\QueryBuilder
|
||||
*/
|
||||
protected function filterQueryEntitiesByPerson(string $action, QueryBuilder $qb, Request $request): QueryBuilder
|
||||
{
|
||||
$qb->andWhere($qb->expr()->eq('e.person', ':person'));
|
||||
$qb->setParameter('person', $this->getPerson($request));
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $entity
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = []): 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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* 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')) {
|
||||
if (false === $request->query->has('person_id')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$person = $this->getDoctrine()
|
||||
->getRepository(Person::class)
|
||||
->find($request->query->getInt('person_id'))
|
||||
;
|
||||
|
||||
if (NULL === $person) {
|
||||
->find($request->query->getInt('person_id'));
|
||||
|
||||
if (null === $person) {
|
||||
throw $this->createNotFoundException('the person with this id is not found');
|
||||
}
|
||||
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Chill\MainBundle\CRUD\Controller\string|string $action
|
||||
* @param Request $request
|
||||
* @return object
|
||||
*/
|
||||
protected function createEntity($action, Request $request): object
|
||||
{
|
||||
$entity = parent::createEntity($action, $request);
|
||||
|
||||
$person = $this->getPerson($request);
|
||||
|
||||
$entity->setPerson($person);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param mixed $entity
|
||||
* @param Request $request
|
||||
* @param array $defaultTemplateParameters
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param mixed $entity
|
||||
* @param Request $request
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTemplateFor($action, $entity, Request $request)
|
||||
@@ -115,83 +127,59 @@ class EntityPersonCRUDController extends CRUDController
|
||||
if ($this->hasCustomTemplate($action, $entity, $request)) {
|
||||
return $this->getActionConfig($action)['template'];
|
||||
}
|
||||
|
||||
|
||||
switch ($action) {
|
||||
case 'new':
|
||||
return '@ChillPerson/CRUD/new.html.twig';
|
||||
case 'edit':
|
||||
|
||||
case 'edit':
|
||||
return '@ChillPerson/CRUD/edit.html.twig';
|
||||
|
||||
case 'view':
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param mixed $entity
|
||||
* @param \Symfony\Component\Form\FormInterface $form
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
||||
*/
|
||||
protected function onBeforeRedirectAfterSubmission(string $action, $entity, \Symfony\Component\Form\FormInterface $form, Request $request)
|
||||
{
|
||||
$next = $request->request->get("submit", "save-and-close");
|
||||
|
||||
$next = $request->request->get('submit', 'save-and-close');
|
||||
|
||||
switch ($next) {
|
||||
case "save-and-close":
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index', [
|
||||
'person_id' => $this->getPerson($request)->getId()
|
||||
case 'save-and-close':
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_index', [
|
||||
'person_id' => $this->getPerson($request)->getId(),
|
||||
]);
|
||||
case "save-and-new":
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_new', [
|
||||
'person_id' => $this->getPerson($request)->getId()
|
||||
|
||||
case 'save-and-new':
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_new', [
|
||||
'person_id' => $this->getPerson($request)->getId(),
|
||||
]);
|
||||
case "new":
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', [
|
||||
|
||||
case 'new':
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', [
|
||||
'id' => $entity->getId(),
|
||||
'person_id' => $this->getPerson($request)->getId()
|
||||
'person_id' => $this->getPerson($request)->getId(),
|
||||
]);
|
||||
|
||||
default:
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', [
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', [
|
||||
'id' => $entity->getId(),
|
||||
'person_id' => $this->getPerson($request)->getId()
|
||||
]);
|
||||
'person_id' => $this->getPerson($request)->getId(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the base method to add a filtering step to a person.
|
||||
*
|
||||
* @param string $action
|
||||
* @param Request $request
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
protected function buildQueryEntities(string $action, Request $request)
|
||||
{
|
||||
$qb = parent::buildQueryEntities($action, $request);
|
||||
|
||||
return $this->filterQueryEntitiesByPerson($action, $qb, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a where clause to the buildQuery
|
||||
*
|
||||
* @param string $action
|
||||
* @param \Chill\PersonBundle\CRUD\Controller\QueryBuilder $qb
|
||||
* @param Request $request
|
||||
* @return \Chill\PersonBundle\CRUD\Controller\QueryBuilder
|
||||
*/
|
||||
protected function filterQueryEntitiesByPerson(string $action, QueryBuilder $qb, Request $request): QueryBuilder
|
||||
{
|
||||
$qb->andWhere($qb->expr()->eq('e.person', ':person'));
|
||||
$qb->setParameter('person', $this->getPerson($request));
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
@@ -1,44 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\CRUD\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use BadMethodCallException;
|
||||
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use LogicException;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class OneToOneEntityPersonCRUDController extends CRUDController
|
||||
{
|
||||
protected function getTemplateFor($action, $entity, Request $request)
|
||||
protected function generateRedirectOnCreateRoute($action, Request $request, $entity)
|
||||
{
|
||||
if (!empty($this->crudConfig[$action]['template'])) {
|
||||
return $this->crudConfig[$action]['template'];
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'new':
|
||||
return '@ChillPerson/CRUD/new.html.twig';
|
||||
case 'edit':
|
||||
return '@ChillPerson/CRUD/edit.html.twig';
|
||||
case 'index':
|
||||
return '@ChillPerson/CRUD/index.html.twig';
|
||||
default:
|
||||
throw new \LogicException("the view for action $action is not "
|
||||
. "defined. You should override ".__METHOD__." to add this "
|
||||
. "action");
|
||||
}
|
||||
throw new BadMethodCallException('Not implemented yet.');
|
||||
}
|
||||
|
||||
protected function getEntity($action, $id, Request $request): ?object
|
||||
{
|
||||
$entity = parent::getEntity($action, $id, $request);
|
||||
|
||||
if (NULL === $entity) {
|
||||
if (null === $entity) {
|
||||
$entity = $this->createEntity($action, $request);
|
||||
$person = $this->getDoctrine()
|
||||
->getManager()
|
||||
@@ -51,23 +44,40 @@ class OneToOneEntityPersonCRUDController extends CRUDController
|
||||
return $entity;
|
||||
}
|
||||
|
||||
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
|
||||
protected function getTemplateFor($action, $entity, Request $request)
|
||||
{
|
||||
$this->getDoctrine()->getManager()->persist($entity);
|
||||
if (!empty($this->crudConfig[$action]['template'])) {
|
||||
return $this->crudConfig[$action]['template'];
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'new':
|
||||
return '@ChillPerson/CRUD/new.html.twig';
|
||||
|
||||
case 'edit':
|
||||
return '@ChillPerson/CRUD/edit.html.twig';
|
||||
|
||||
case 'index':
|
||||
return '@ChillPerson/CRUD/index.html.twig';
|
||||
|
||||
default:
|
||||
throw new LogicException("the view for action {$action} is not "
|
||||
. 'defined. You should override ' . __METHOD__ . ' to add this '
|
||||
. 'action');
|
||||
}
|
||||
}
|
||||
|
||||
protected function onPostFetchEntity($action, Request $request, $entity): ?Response
|
||||
{
|
||||
if (FALSE === $this->getDoctrine()->getManager()->contains($entity)) {
|
||||
if (false === $this->getDoctrine()->getManager()->contains($entity)) {
|
||||
return new RedirectResponse($this->generateRedirectOnCreateRoute($action, $request, $entity));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function generateRedirectOnCreateRoute($action, Request $request, $entity)
|
||||
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
throw new BadMethodCallException('Not implemented yet.');
|
||||
$this->getDoctrine()->getManager()->persist($entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user