move queries in repository

This commit is contained in:
Tchama 2019-05-02 15:10:16 +02:00
parent c2b5e0f767
commit 55afcc2184
7 changed files with 120 additions and 38 deletions

View File

@ -24,6 +24,7 @@ namespace Chill\EventBundle\Controller;
use Chill\EventBundle\Entity\Participation; use Chill\EventBundle\Entity\Participation;
use Chill\EventBundle\Form\Type\PickEventType; use Chill\EventBundle\Form\Type\PickEventType;
use Chill\EventBundle\Repository\ParticipationRepository;
use Chill\EventBundle\Security\Authorization\EventVoter; use Chill\EventBundle\Security\Authorization\EventVoter;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
@ -342,6 +343,7 @@ class EventController extends Controller
* *
* @param $person_id * @param $person_id
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ORM\NonUniqueResultException
*/ */
public function listByPersonAction($person_id) public function listByPersonAction($person_id)
{ {
@ -356,46 +358,27 @@ class EventController extends Controller
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$reachablesCircles = $this->authorizationHelper $reachablesCircles = $this->authorizationHelper->getReachableCircles(
->getReachableCircles( $this->getUser(),
$this->getUser(), new Role(EventVoter::SEE),
new Role(EventVoter::SEE), $person->getCenter()
$person->getCenter() );
);
$total = $em $total = $em->getRepository('ChillEventBundle:Participation')->countByPerson($person_id);
->createQuery("
SELECT COUNT (participation.id)
FROM ChillEventBundle:Participation participation
WHERE participation.person = :person_id
")
->setParameter(':person_id', $person_id)
->getSingleScalarResult();
/** /**
* @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory * @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory
*/ */
$paginatorFactory = $this->get('chill_main.paginator_factory'); $paginatorFactory = $this->get('chill_main.paginator_factory');
$paginator = $paginatorFactory->create($total); $paginator = $paginatorFactory->create($total);
$participations = $em $participations = $em->getRepository('ChillEventBundle:Participation')->findByPersonInCircle(
->createQuery(" $person_id,
SELECT participation $reachablesCircles,
FROM ChillEventBundle:Participation participation $paginator->getCurrentPage()->getFirstItemNumber(),
JOIN participation.event event $paginator->getItemsPerPage()
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()
;
$privacyEvent = new PrivacyEvent($person, array( $privacyEvent = new PrivacyEvent($person, array(
'element_class' => Participation::class, 'element_class' => Participation::class,
'action' => 'list' 'action' => 'list'

View File

@ -111,12 +111,18 @@ class EventType extends AbstractType
}, },
)) ))
->add('type', PickEventTypeType::class, array( ->add('type', PickEventTypeType::class, array(
'placeholder' => '' 'placeholder' => 'Pick a type of event',
'attr' => array(
'class' => ''
)
)) ))
->add('moderator', UserPickerType::class, array( ->add('moderator', UserPickerType::class, array(
'center' => $options['center'], 'center' => $options['center'],
'role' => $options['role'], 'role' => $options['role'],
'placeholder' => '', 'placeholder' => 'Pick a moderator',
'attr' => array(
'class' => ''
),
'required' => false 'required' => false
)) ))
; ;

View File

@ -166,7 +166,7 @@ class PickEventType extends AbstractType
*/ */
protected function filterCenters(Options $options) protected function filterCenters(Options $options)
{ {
// option role
if ($options['role'] === NULL) { if ($options['role'] === NULL) {
$centers = array_map( $centers = array_map(
function (GroupCenter $g) { function (GroupCenter $g) {
@ -181,6 +181,7 @@ class PickEventType extends AbstractType
); );
} }
// option center
if ($options['centers'] === NULL) if ($options['centers'] === NULL)
{ {
// we select all selected centers // we select all selected centers

View File

@ -0,0 +1,83 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <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\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()
;
}
}

View File

@ -1,6 +1,7 @@
Chill\EventBundle\Entity\Participation: Chill\EventBundle\Entity\Participation:
type: entity type: entity
table: chill_event_participation table: chill_event_participation
repositoryClass: Chill\EventBundle\Repository\ParticipationRepository
id: id:
id: id:
type: integer type: integer

View File

@ -5,7 +5,13 @@ services:
factory: ['@doctrine.orm.entity_manager', getRepository] factory: ['@doctrine.orm.entity_manager', getRepository]
arguments: arguments:
- 'Chill\EventBundle\Entity\Event' - '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: chill_event.repository.role:
class: Doctrine\ORM\EntityRepository class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository] factory: ['@doctrine.orm.entity_manager', getRepository]

View File

@ -68,4 +68,6 @@ futur: futur
Show the event: Voir l'événement Show the event: Voir l'événement
Subscribe an event: Ajouter un événement Subscribe an event: Ajouter un événement
Pick an event: Choisir un événement Pick an event: Choisir un événement
Pick a type of event: Choisir un type d'événement
Pick a moderator: Choisir un animateur