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\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,21 +358,13 @@ class EventController extends Controller
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$reachablesCircles = $this->authorizationHelper
->getReachableCircles(
$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
@ -378,23 +372,12 @@ class EventController extends Controller
$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,

View File

@ -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
))
;

View File

@ -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

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:
type: entity
table: chill_event_participation
repositoryClass: Chill\EventBundle\Repository\ParticipationRepository
id:
id:
type: integer

View File

@ -6,6 +6,12 @@ services:
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]

View File

@ -69,3 +69,5 @@ Show the event: Voir l'événement
Subscribe an event: Ajouter 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