mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-14 14:24:24 +00:00
improve exports & add new filter, export, aggregators
This commit is contained in:
parent
b36ec02b65
commit
67c8c19885
@ -26,6 +26,7 @@ use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -76,15 +77,23 @@ class ActivityReasonAggregator implements AggregatorInterface
|
||||
}
|
||||
|
||||
$qb->addSelect($elem.' as '.$alias);
|
||||
|
||||
// make a jointure only if needed
|
||||
// add a join to reasons only if needed
|
||||
if (array_key_exists('activity', $qb->getDQLPart('join'))) {
|
||||
// we want to avoid multiple join on same object
|
||||
if (!$this->checkJoinAlreadyDefined($qb->getDQLPart('join')['activity'], 'reasons')) {
|
||||
$qb->add('join', new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons'));
|
||||
}
|
||||
} else {
|
||||
$qb->join('activity.reasons', 'reasons');
|
||||
$join = $qb->getDQLPart('join');
|
||||
if (
|
||||
(array_key_exists('activity', $join)
|
||||
&&
|
||||
!$this->checkJoinAlreadyDefined($join['activity'], 'reasons')
|
||||
)
|
||||
OR
|
||||
(! array_key_exists('activity', $join))
|
||||
) {
|
||||
$qb->add(
|
||||
'join',
|
||||
array('activity' =>
|
||||
new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons')
|
||||
),
|
||||
true);
|
||||
}
|
||||
|
||||
// join category if necessary
|
||||
@ -178,19 +187,21 @@ class ActivityReasonAggregator implements AggregatorInterface
|
||||
|
||||
switch ($data['level']) {
|
||||
case 'reasons':
|
||||
$n = $this->reasonRepository->find($value)
|
||||
->getName()
|
||||
/* @var $r \Chill\ActivityBundle\Entity\ActivityReason */
|
||||
$r = $this->reasonRepository->find($value);
|
||||
|
||||
return $this->stringHelper->localize($r->getCategory()->getName())
|
||||
." > "
|
||||
. $this->stringHelper->localize($r->getName());
|
||||
;
|
||||
break;
|
||||
case 'categories':
|
||||
$n = $this->categoryRepository->find($value)
|
||||
->getName()
|
||||
;
|
||||
$c = $this->categoryRepository->find($value);
|
||||
|
||||
return $this->stringHelper->localize($c->getName());
|
||||
break;
|
||||
// no need for a default : the default was already set above
|
||||
}
|
||||
|
||||
return $this->stringHelper->localize($n);
|
||||
};
|
||||
|
||||
}
|
||||
|
287
Export/Export/ListActivity.php
Normal file
287
Export/Export/ListActivity.php
Normal file
@ -0,0 +1,287 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 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\ActivityBundle\Export\Export;
|
||||
|
||||
use Chill\MainBundle\Export\ListInterface;
|
||||
use Chill\ActivityBundle\Entity\ActivityReason;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\ActivityBundle\Entity\ActivityType;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Doctrine\ORM\Query;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Create a list for all activities
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ListActivity implements ListInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
protected $fields = array(
|
||||
'id',
|
||||
'date',
|
||||
'durationTime',
|
||||
'attendee',
|
||||
'list_reasons',
|
||||
'user_username',
|
||||
'circle_name',
|
||||
'type_name',
|
||||
'person_firstname',
|
||||
'person_lastname',
|
||||
'person_id'
|
||||
);
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
TranslatorInterface $translator,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
)
|
||||
{
|
||||
$this->entityManager = $em;
|
||||
$this->translator = $translator;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param FormBuilderInterface $builder
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('fields', ChoiceType::class, array(
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => array_combine($this->fields, $this->fields),
|
||||
'label' => 'Fields to include in export',
|
||||
'constraints' => [new Callback(array(
|
||||
'callback' => function($selected, ExecutionContextInterface $context) {
|
||||
if (count($selected) === 0) {
|
||||
$context->buildViolation('You must select at least one element')
|
||||
->atPath('fields')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
))]
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function getAllowedFormattersTypes()
|
||||
{
|
||||
return array(FormatterInterface::TYPE_LIST);
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return "List activities";
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
switch ($key)
|
||||
{
|
||||
case 'date' :
|
||||
return function($value) {
|
||||
if ($value === '_header') return 'date';
|
||||
|
||||
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $value);
|
||||
|
||||
return $date->format('d-m-Y');
|
||||
};
|
||||
case 'attendee':
|
||||
return function($value) {
|
||||
if ($value === '_header') return 'attendee';
|
||||
|
||||
return $value ? 1 : 0;
|
||||
};
|
||||
case 'list_reasons' :
|
||||
/* @var $activityReasonsRepository EntityRepository */
|
||||
$activityRepository = $this->entityManager
|
||||
->getRepository('ChillActivityBundle:Activity');
|
||||
|
||||
return function($value) use ($activityRepository) {
|
||||
if ($value === '_header') return 'activity reasons';
|
||||
|
||||
$activity = $activityRepository
|
||||
->find($value);
|
||||
|
||||
return implode(", ", array_map(function(ActivityReason $r) {
|
||||
|
||||
return '"'.
|
||||
$this->translatableStringHelper->localize($r->getCategory()->getName())
|
||||
.' > '.
|
||||
$this->translatableStringHelper->localize($r->getName())
|
||||
.'"';
|
||||
}, $activity->getReasons()->toArray()));
|
||||
};
|
||||
case 'circle_name' :
|
||||
return function($value) {
|
||||
if ($value === '_header') return 'circle';
|
||||
|
||||
return $this->translatableStringHelper
|
||||
->localize(json_decode($value, true));
|
||||
};
|
||||
case 'type_name' :
|
||||
return function($value) {
|
||||
if ($value === '_header') return 'activity type';
|
||||
|
||||
return $this->translatableStringHelper
|
||||
->localize(json_decode($value, true));
|
||||
};
|
||||
default:
|
||||
return function($value) use ($key) {
|
||||
if ($value === '_header') return $key;
|
||||
|
||||
return $value;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
return $data['fields'];
|
||||
}
|
||||
|
||||
public function getResult($query, $data)
|
||||
{
|
||||
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'List activities';
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return 'activity';
|
||||
}
|
||||
|
||||
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
|
||||
{
|
||||
$centers = array_map(function($el) { return $el['center']; }, $acl);
|
||||
|
||||
// throw an error if any fields are present
|
||||
if (!\array_key_exists('fields', $data)) {
|
||||
throw new \Doctrine\DBAL\Exception\InvalidArgumentException("any fields "
|
||||
. "have been checked");
|
||||
}
|
||||
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
|
||||
$qb
|
||||
->from('ChillActivityBundle:Activity', 'activity')
|
||||
->join('activity.person', 'person')
|
||||
->join('person.center', 'center')
|
||||
->andWhere('center IN (:authorized_centers)')
|
||||
->setParameter('authorized_centers', $centers);
|
||||
;
|
||||
|
||||
foreach ($this->fields as $f) {
|
||||
if (in_array($f, $data['fields'])) {
|
||||
switch ($f) {
|
||||
case 'id':
|
||||
$qb->addSelect('activity.id AS id');
|
||||
break;
|
||||
case 'person_firstname':
|
||||
$qb->addSelect('person.firstName AS person_firstname');
|
||||
break;
|
||||
case 'person_lastname':
|
||||
$qb->addSelect('person.lastName AS person_lastname');
|
||||
break;
|
||||
case 'person_id':
|
||||
$qb->addSelect('person.id AS person_id');
|
||||
break;
|
||||
case 'user_username':
|
||||
$qb->join('activity.user', 'user');
|
||||
$qb->addSelect('user.username AS user_username');
|
||||
break;
|
||||
case 'circle_name':
|
||||
$qb->join('activity.scope', 'circle');
|
||||
$qb->addSelect('circle.name AS circle_name');
|
||||
break;
|
||||
case 'type_name':
|
||||
$qb->join('activity.type', 'type');
|
||||
$qb->addSelect('type.name AS type_name');
|
||||
break;
|
||||
case 'list_reasons':
|
||||
// this is a trick... The reasons is filled with the
|
||||
// activity id which will be used to load reasons
|
||||
$qb->addSelect('activity.id AS list_reasons');
|
||||
break;
|
||||
default:
|
||||
$qb->addSelect(sprintf('activity.%s as %s', $f, $f));
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function requiredRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::LISTS);
|
||||
}
|
||||
|
||||
public function supportsModifiers()
|
||||
{
|
||||
return array('activity', 'person');
|
||||
}
|
||||
|
||||
}
|
132
Export/Filter/ActivityDateFilter.php
Normal file
132
Export/Filter/ActivityDateFilter.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 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\ActivityBundle\Export\Filter;
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Chill\MainBundle\Form\Type\Export\FilterType;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ActivityDateFilter implements FilterInterface
|
||||
{
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data)
|
||||
{
|
||||
$where = $qb->getDQLPart('where');
|
||||
$clause = $qb->expr()->between('activity.date', ':date_from',
|
||||
':date_to');
|
||||
|
||||
if ($where instanceof Expr\Andx) {
|
||||
$where->add($clause);
|
||||
} else {
|
||||
$where = $qb->expr()->andX($clause);
|
||||
}
|
||||
|
||||
$qb->add('where', $where);
|
||||
$qb->setParameter('date_from', $data['date_from']);
|
||||
$qb->setParameter('date_to', $data['date_to']);
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return 'activity';
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('date_from', DateType::class, array(
|
||||
'label' => "Activities after this date",
|
||||
'data' => new \DateTime(),
|
||||
'attr' => array('class' => 'datepicker'),
|
||||
'widget'=> 'single_text',
|
||||
'format' => 'dd-MM-yyyy',
|
||||
));
|
||||
|
||||
$builder->add('date_to', DateType::class, array(
|
||||
'label' => "Activities before this date",
|
||||
'data' => new \DateTime(),
|
||||
'attr' => array('class' => 'datepicker'),
|
||||
'widget'=> 'single_text',
|
||||
'format' => 'dd-MM-yyyy',
|
||||
));
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
|
||||
/* @var $filterForm \Symfony\Component\Form\FormInterface */
|
||||
$filterForm = $event->getForm()->getParent();
|
||||
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
||||
|
||||
if ($enabled === true) {
|
||||
// if the filter is enabled, add some validation
|
||||
$form = $event->getForm();
|
||||
$date_from = $form->get('date_from')->getData();
|
||||
$date_to = $form->get('date_to')->getData();
|
||||
|
||||
// check that fields are not empty
|
||||
if ($date_from === null) {
|
||||
$form->get('date_from')->addError(new FormError('This field '
|
||||
. 'should not be empty'));
|
||||
}
|
||||
if ($date_to === null) {
|
||||
$form->get('date_to')->addError(new FormError('This field '
|
||||
. 'should not be empty'));
|
||||
}
|
||||
|
||||
// check that date_from is before date_to
|
||||
if (
|
||||
($date_from !== null && $date_to !== null)
|
||||
&&
|
||||
$date_from >= $date_to
|
||||
) {
|
||||
$form->get('date_to')->addError(new FormError('This date '
|
||||
. 'should be after the date given in "activities after" field'));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return array(
|
||||
"Filtered by date of activity: only between %date_from% and %date_to%",
|
||||
array(
|
||||
"%date_from%" => $data['date_from']->format('d-m-Y'),
|
||||
'%date_to%' => $data['date_to']->format('d-m-Y')
|
||||
));
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return "Filtered by date activity";
|
||||
}
|
||||
|
||||
}
|
@ -27,7 +27,7 @@ use Chill\ActivityBundle\Entity\ActivityReason;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
|
||||
@ -65,14 +65,21 @@ class ActivityReasonFilter implements FilterInterface
|
||||
$where = $qb->getDQLPart('where');
|
||||
$join = $qb->getDQLPart('join');
|
||||
$clause = $qb->expr()->in('reasons', ':selected_activity_reasons');
|
||||
//dump($join);
|
||||
// add a join to reasons only if needed
|
||||
if (array_key_exists('activity', $join)) {
|
||||
// we want to avoid multiple join on same object
|
||||
if (!$this->checkJoinAlreadyDefined($join['activity'])) {
|
||||
$qb->add('join', new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons'));
|
||||
}
|
||||
} else {
|
||||
$qb->join('activity.reasons', 'reasons');
|
||||
if (
|
||||
(array_key_exists('activity', $join)
|
||||
&&
|
||||
!$this->checkJoinAlreadyDefined($join['activity'], 'reasons')
|
||||
)
|
||||
OR
|
||||
(! array_key_exists('activity', $join))
|
||||
) {
|
||||
$qb->add(
|
||||
'join',
|
||||
array('activity' => new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons')),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if ($where instanceof Expr\Andx) {
|
||||
@ -91,10 +98,10 @@ class ActivityReasonFilter implements FilterInterface
|
||||
* @param Join[] $joins
|
||||
* @return boolean
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins)
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === 'reasons') {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -132,7 +139,7 @@ class ActivityReasonFilter implements FilterInterface
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityVoter::SEE);
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
|
205
Export/Filter/PersonHavingActivityBetweenDateFilter.php
Normal file
205
Export/Filter/PersonHavingActivityBetweenDateFilter.php
Normal file
@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 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\ActivityBundle\Export\Filter;
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Chill\MainBundle\Form\Type\Export\FilterType;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Chill\ActivityBundle\Entity\ActivityReason;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class PersonHavingActivityBetweenDateFilter implements FilterInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $activityReasonRepository;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
EntityRepository $activityReasonRepository
|
||||
) {
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->activityReasonRepository = $activityReasonRepository;
|
||||
}
|
||||
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data)
|
||||
{
|
||||
// create a query for activity
|
||||
$sqb = $qb->getEntityManager()->createQueryBuilder();
|
||||
$sqb->select("person_person_having_activity.id")
|
||||
->from("ChillActivityBundle:Activity", "activity_person_having_activity")
|
||||
->join("activity_person_having_activity.person", "person_person_having_activity")
|
||||
;
|
||||
// add clause between date
|
||||
$sqb->where("activity_person_having_activity.date BETWEEN "
|
||||
. ":person_having_activity_between_date_from"
|
||||
. " AND "
|
||||
. ":person_having_activity_between_date_to");
|
||||
// add clause activity reason
|
||||
$sqb->join('activity_person_having_activity.reasons',
|
||||
'reasons_person_having_activity');
|
||||
$sqb->andWhere(
|
||||
$sqb->expr()->in(
|
||||
'reasons_person_having_activity',
|
||||
":person_having_activity_reasons")
|
||||
);
|
||||
|
||||
$where = $qb->getDQLPart('where');
|
||||
$clause = $qb->expr()->in('person.id', $sqb->getDQL());
|
||||
|
||||
if ($where instanceof Expr\Andx) {
|
||||
$where->add($clause);
|
||||
} else {
|
||||
$where = $qb->expr()->andX($clause);
|
||||
}
|
||||
|
||||
$qb->add('where', $where);
|
||||
$qb->setParameter('person_having_activity_between_date_from',
|
||||
$data['date_from']);
|
||||
$qb->setParameter('person_having_activity_between_date_to',
|
||||
$data['date_to']);
|
||||
$qb->setParameter('person_having_activity_reasons', $data['reasons']);
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return Declarations::PERSON_IMPLIED_IN;
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('date_from', DateType::class, array(
|
||||
'label' => "Implied in an activity after this date",
|
||||
'data' => new \DateTime(),
|
||||
'attr' => array('class' => 'datepicker'),
|
||||
'widget'=> 'single_text',
|
||||
'format' => 'dd-MM-yyyy',
|
||||
));
|
||||
|
||||
$builder->add('date_to', DateType::class, array(
|
||||
'label' => "Implied in an activity before this date",
|
||||
'data' => new \DateTime(),
|
||||
'attr' => array('class' => 'datepicker'),
|
||||
'widget'=> 'single_text',
|
||||
'format' => 'dd-MM-yyyy',
|
||||
));
|
||||
|
||||
$builder->add('reasons', EntityType::class, array(
|
||||
'class' => 'ChillActivityBundle:ActivityReason',
|
||||
'choice_label' => function (ActivityReason $reason) {
|
||||
return $this->translatableStringHelper
|
||||
->localize($reason->getName());
|
||||
},
|
||||
'group_by' => function(ActivityReason $reason) {
|
||||
return $this->translatableStringHelper
|
||||
->localize($reason->getCategory()->getName());
|
||||
},
|
||||
'data' => $this->activityReasonRepository->findAll(),
|
||||
'multiple' => true,
|
||||
'expanded' => false,
|
||||
'label' => "Activity reasons for those activities"
|
||||
));
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
|
||||
/* @var $filterForm \Symfony\Component\Form\FormInterface */
|
||||
$filterForm = $event->getForm()->getParent();
|
||||
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
||||
|
||||
if ($enabled === true) {
|
||||
// if the filter is enabled, add some validation
|
||||
$form = $event->getForm();
|
||||
$date_from = $form->get('date_from')->getData();
|
||||
$date_to = $form->get('date_to')->getData();
|
||||
|
||||
// check that fields are not empty
|
||||
if ($date_from === null) {
|
||||
$form->get('date_from')->addError(new FormError('This field '
|
||||
. 'should not be empty'));
|
||||
}
|
||||
if ($date_to === null) {
|
||||
$form->get('date_to')->addError(new FormError('This field '
|
||||
. 'should not be empty'));
|
||||
}
|
||||
|
||||
// check that date_from is before date_to
|
||||
if (
|
||||
($date_from !== null && $date_to !== null)
|
||||
&&
|
||||
$date_from >= $date_to
|
||||
) {
|
||||
$form->get('date_to')->addError(new FormError('This date '
|
||||
. 'should be after the date given in "Implied in an '
|
||||
. 'activity after this date" field'));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return array(
|
||||
"Filtered by person having an activity between %date_from% and "
|
||||
. "%date_to% with reasons %reasons_name%",
|
||||
array(
|
||||
"%date_from%" => $data['date_from']->format('d-m-Y'),
|
||||
'%date_to%' => $data['date_to']->format('d-m-Y'),
|
||||
"%reasons_name%" => implode(", ", array_map(
|
||||
function (ActivityReason $r) {
|
||||
return '"'.$this->translatableStringHelper->
|
||||
localize($r->getName()).'"';
|
||||
},
|
||||
$data['reasons']))
|
||||
));
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return "Filtered by person having an activity in a period";
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,15 @@ services:
|
||||
tags:
|
||||
- { name: chill.export, alias: 'count_activity' }
|
||||
|
||||
chill.activity.export.list_activity:
|
||||
class: Chill\ActivityBundle\Export\Export\ListActivity
|
||||
arguments:
|
||||
- "@doctrine.orm.entity_manager"
|
||||
- "@translator"
|
||||
- "@chill.main.helper.translatable_string"
|
||||
tags:
|
||||
- { name: chill.export, alias: 'list_activity' }
|
||||
|
||||
chill.activity.export.reason_filter:
|
||||
class: Chill\ActivityBundle\Export\Filter\ActivityReasonFilter
|
||||
arguments:
|
||||
@ -14,6 +23,21 @@ services:
|
||||
tags:
|
||||
- { name: chill.export_filter, alias: 'activity_reason_filter' }
|
||||
|
||||
chill.activity.export.date_filter:
|
||||
class: Chill\ActivityBundle\Export\Filter\ActivityDateFilter
|
||||
tags:
|
||||
- { name: chill.export_filter, alias: 'activity_date_filter' }
|
||||
|
||||
chill.activity.export.person_having_an_activity_between_date_filter:
|
||||
class: Chill\ActivityBundle\Export\Filter\PersonHavingActivityBetweenDateFilter
|
||||
arguments:
|
||||
- "@chill.main.helper.translatable_string"
|
||||
- "@chill_activity.repository.reason"
|
||||
tags:
|
||||
- #0 register as a filter
|
||||
name: chill.export_filter
|
||||
alias: 'activity_person_having_ac_bw_date_filter'
|
||||
|
||||
chill.activity.export.reason_aggregator:
|
||||
class: Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator
|
||||
arguments:
|
||||
@ -21,4 +45,4 @@ services:
|
||||
- "@chill_activity.repository.reason"
|
||||
- "@chill.main.helper.translatable_string"
|
||||
tags:
|
||||
- { name: chill.export_aggregator, alias: activity_reason }
|
||||
- { name: chill.export_aggregator, alias: activity_reason_aggregator }
|
||||
|
@ -103,6 +103,12 @@ Count activities by various parameters.: Compte le nombre d'activités enregistr
|
||||
Filter by reason: Filtrer par sujet d'activité
|
||||
'Filtered by reasons: only %list%': 'Filtré par sujet: seulement %list%'
|
||||
|
||||
|
||||
"Filtered by date of activity: only between %date_from% and %date_to%": "Filtré par date de l'activité: uniquement entre %date_from% et %date_to%"
|
||||
|
||||
Filtered by person having an activity between %date_from% and %date_to% with reasons %reasons_name%: Filtré par personnes associées à une activité entre %date_from% et %date_to% avec les sujets %reasons_name%
|
||||
Activity reasons for those activities: Sujets de ces activités
|
||||
|
||||
#aggregators
|
||||
Aggregate by activity reason: Aggréger par sujet de l'activité
|
||||
By reason: Par sujet
|
||||
|
93
Tests/Export/Export/ListActivityTest.php
Normal file
93
Tests/Export/Export/ListActivityTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 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\ActivityBundle\Tests\Export\Export;
|
||||
|
||||
use Chill\MainBundle\Test\Export\AbstractExportTest;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ListActivityTest extends AbstractExportTest
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ActivityBundle\Export\Export\ListActivity
|
||||
*/
|
||||
private $export;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
/* @var $container \Symfony\Component\DependencyInjection\ContainerInterface */
|
||||
$container = self::$kernel->getContainer();
|
||||
|
||||
$this->export = $container->get('chill.activity.export.list_activity');
|
||||
|
||||
// add a fake request with a default locale (used in translatable string)
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$request = $prophet->prophesize();
|
||||
$request->willExtend(\Symfony\Component\HttpFoundation\Request::class);
|
||||
$request->getLocale()->willReturn('fr');
|
||||
|
||||
$container->get('request_stack')
|
||||
->push($request->reveal());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getFormData()
|
||||
{
|
||||
return array(
|
||||
array('fields' => array(
|
||||
'id',
|
||||
'date',
|
||||
'durationTime',
|
||||
'attendee',
|
||||
'user_username',
|
||||
'circle_name',
|
||||
'type_name',
|
||||
'person_firstname',
|
||||
'person_lastname',
|
||||
'person_id'
|
||||
)),
|
||||
array('fields' => array(
|
||||
'id',
|
||||
'list_reasons'
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
public function getModifiersCombination()
|
||||
{
|
||||
return array(
|
||||
array('activity'),
|
||||
array('activity', 'person')
|
||||
);
|
||||
}
|
||||
|
||||
public function getExport()
|
||||
{
|
||||
return $this->export;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user