From 55afcc2184b10db5204685b3e6cfa93a5d8fd8b3 Mon Sep 17 00:00:00 2001 From: Tchama Date: Thu, 2 May 2019 15:10:16 +0200 Subject: [PATCH] move queries in repository --- Controller/EventController.php | 49 ++++------- Form/EventType.php | 10 ++- Form/Type/PickEventType.php | 3 +- Repository/ParticipationRepository.php | 83 +++++++++++++++++++ .../config/doctrine/Participation.orm.yml | 1 + Resources/config/services/repositories.yml | 8 +- Resources/translations/messages.fr.yml | 4 +- 7 files changed, 120 insertions(+), 38 deletions(-) create mode 100644 Repository/ParticipationRepository.php diff --git a/Controller/EventController.php b/Controller/EventController.php index f96dd0f50..3b0d2e93e 100644 --- a/Controller/EventController.php +++ b/Controller/EventController.php @@ -24,6 +24,7 @@ namespace Chill\EventBundle\Controller; use Chill\EventBundle\Entity\Participation; use Chill\EventBundle\Form\Type\PickEventType; +use Chill\EventBundle\Repository\ParticipationRepository; use Chill\EventBundle\Security\Authorization\EventVoter; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\Person; @@ -342,6 +343,7 @@ class EventController extends Controller * * @param $person_id * @return \Symfony\Component\HttpFoundation\Response + * @throws \Doctrine\ORM\NonUniqueResultException */ public function listByPersonAction($person_id) { @@ -356,46 +358,27 @@ class EventController extends Controller $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); - $reachablesCircles = $this->authorizationHelper - ->getReachableCircles( - $this->getUser(), - new Role(EventVoter::SEE), - $person->getCenter() - ); + $reachablesCircles = $this->authorizationHelper->getReachableCircles( + $this->getUser(), + new Role(EventVoter::SEE), + $person->getCenter() + ); - $total = $em - ->createQuery(" - SELECT COUNT (participation.id) - FROM ChillEventBundle:Participation participation - WHERE participation.person = :person_id - ") - ->setParameter(':person_id', $person_id) - ->getSingleScalarResult(); + $total = $em->getRepository('ChillEventBundle:Participation')->countByPerson($person_id); /** * @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory */ $paginatorFactory = $this->get('chill_main.paginator_factory'); $paginator = $paginatorFactory->create($total); - - $participations = $em - ->createQuery(" - SELECT participation - FROM ChillEventBundle:Participation participation - JOIN participation.event event - WHERE participation.person = :person_id - AND event.circle IN (:reachable_circles) - ORDER BY event.date ASC - ") - ->setParameters(array( - ':person_id' => $person_id, - ':reachable_circles' => $reachablesCircles - )) - ->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber()) - ->setMaxResults($paginator->getItemsPerPage()) - ->getResult() - ; - + + $participations = $em->getRepository('ChillEventBundle:Participation')->findByPersonInCircle( + $person_id, + $reachablesCircles, + $paginator->getCurrentPage()->getFirstItemNumber(), + $paginator->getItemsPerPage() + ); + $privacyEvent = new PrivacyEvent($person, array( 'element_class' => Participation::class, 'action' => 'list' diff --git a/Form/EventType.php b/Form/EventType.php index b08d902e7..fcf35eaea 100644 --- a/Form/EventType.php +++ b/Form/EventType.php @@ -111,12 +111,18 @@ class EventType extends AbstractType }, )) ->add('type', PickEventTypeType::class, array( - 'placeholder' => '' + 'placeholder' => 'Pick a type of event', + 'attr' => array( + 'class' => '' + ) )) ->add('moderator', UserPickerType::class, array( 'center' => $options['center'], 'role' => $options['role'], - 'placeholder' => '', + 'placeholder' => 'Pick a moderator', + 'attr' => array( + 'class' => '' + ), 'required' => false )) ; diff --git a/Form/Type/PickEventType.php b/Form/Type/PickEventType.php index 71f4d9b56..9d9d2bc96 100644 --- a/Form/Type/PickEventType.php +++ b/Form/Type/PickEventType.php @@ -166,7 +166,7 @@ class PickEventType extends AbstractType */ protected function filterCenters(Options $options) { - + // option role if ($options['role'] === NULL) { $centers = array_map( function (GroupCenter $g) { @@ -181,6 +181,7 @@ class PickEventType extends AbstractType ); } + // option center if ($options['centers'] === NULL) { // we select all selected centers diff --git a/Repository/ParticipationRepository.php b/Repository/ParticipationRepository.php new file mode 100644 index 000000000..29bd09e9a --- /dev/null +++ b/Repository/ParticipationRepository.php @@ -0,0 +1,83 @@ +, + * + * 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 . + */ + +namespace Chill\EventBundle\Repository; + +use Doctrine\ORM\EntityRepository; + +/** + * Class ParticipationRepository + * + * @package Chill\EventBundle\Repository + * @author Mathieu Jaumotte jaum_mathieu@collectifs.net + */ +class ParticipationRepository extends EntityRepository +{ + + /** + * Count number of participations per person + * + * @param $person_id + * @return mixed + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function countByPerson($person_id) + { + return $this->createQueryBuilder('p') + ->select('COUNT (p.id)') + ->where('p.id = :person_id') + ->setParameter(':person_id', $person_id) + ->getQuery() + ->getSingleScalarResult() + ; + } + + + /** + * Return paginated participations for a person and in reachables circles + * + * @param $person_id + * @param $reachablesCircles + * @param $first + * @param $max + * @return mixed + */ + public function findByPersonInCircle($person_id, $reachablesCircles, $first, $max) + { + return $this->createQueryBuilder('p') + ->join('p.event', 'e') + ->where('p.person = :person_id') + ->andWhere('e.circle IN (:reachable_circles)') + ->orderBy('e.date', 'ASC') + ->setParameters(array( + ':person_id' => $person_id, + ':reachable_circles' => $reachablesCircles + )) + ->setFirstResult($first) + ->setMaxResults($max) + ->getQuery() + ->getResult() + ; + } + + + +} diff --git a/Resources/config/doctrine/Participation.orm.yml b/Resources/config/doctrine/Participation.orm.yml index 24858afc4..377dd54fe 100644 --- a/Resources/config/doctrine/Participation.orm.yml +++ b/Resources/config/doctrine/Participation.orm.yml @@ -1,6 +1,7 @@ Chill\EventBundle\Entity\Participation: type: entity table: chill_event_participation + repositoryClass: Chill\EventBundle\Repository\ParticipationRepository id: id: type: integer diff --git a/Resources/config/services/repositories.yml b/Resources/config/services/repositories.yml index ca050ed4b..4d627f625 100644 --- a/Resources/config/services/repositories.yml +++ b/Resources/config/services/repositories.yml @@ -5,7 +5,13 @@ services: factory: ['@doctrine.orm.entity_manager', getRepository] arguments: - 'Chill\EventBundle\Entity\Event' - + + chill_event.repository.participation: + class: Chill\EventBundle\Repository\ParticipationRepository + factory: ['@doctrine.orm.entity_manager', getRepository] + arguments: + - 'Chill\EventBundle\Entity\Participation' + chill_event.repository.role: class: Doctrine\ORM\EntityRepository factory: ['@doctrine.orm.entity_manager', getRepository] diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 2bfc74ac9..53a79c3e1 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -68,4 +68,6 @@ futur: futur Show the event: Voir l'événement Subscribe an event: Ajouter un événement -Pick an event: Choisir un événement \ No newline at end of file +Pick an event: Choisir un événement +Pick a type of event: Choisir un type d'événement +Pick a moderator: Choisir un animateur