[export] add an export 'count activity' and first filters

- reason aggregator
- reason filter
- count activity export
This commit is contained in:
Julien Fastré 2016-01-02 16:47:55 +01:00
parent 65e7a130c5
commit 3c65e2d585
4 changed files with 275 additions and 1 deletions

View File

@ -0,0 +1,88 @@
<?php
/*
* Copyright (C) 2016 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\Aggregator;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\QueryBuilder;
use Chill\MainBundle\Export\AggregatorInterface;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ReasonAggregator implements AggregatorInterface
{
public function alterQuery(QueryBuilder $qb, $data)
{
// add select element
if ($data['level'] === 'reason') {
$elem = 'reason.id';
$alias = 'activity_reason_id';
} elseif ($data['level'] === 'category') {
$elem = 'category.id';
$alias = 'activity_category_id';
} else {
throw new \RuntimeException('the data provided are not recognised');
}
$qb->addSelect($elem.' as '.$alias);
// make a jointure
$qb->join('activity.reason', 'reason');
// join category if necessary
if ($alias === 'activity_category_id') {
$qb->join('reason.category', 'category');
}
// add the "group by" part
$groupBy = $qb->getDQLPart('groupBy');
if (count($groupBy) > 0) {
$qb->addGroupBy($alias);
} else {
$qb->groupBy($alias);
}
}
public function applyOn()
{
return 'activity';
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('level', 'choice', array(
'choices' => array(
'By reason' => 'reason',
'By category of reason' => 'category'
),
'choices_as_values' => true,
'multiple' => false,
'expanded' => true
));
}
public function getTitle()
{
return "Aggregate by activity reason";
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* Copyright (C) 2015 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\ExportInterface;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CountActivity implements ExportInterface
{
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
{
throw new \LogicException('This export does not requires a form.');
}
public function getDescription()
{
return "Count activities";
}
public function getTitle()
{
return "Count activities";
}
public function getType()
{
return 'activity';
}
public function initiateQuery(QueryBuilder $qb, array $requiredModifiers)
{
$qb->select('COUNT(activity.id) as export_count_activity')
->from('ChillActivityBundle:Activity', 'activity')
;
if (in_array('person', $requiredModifiers)) {
$qb->join('activity.person', 'person');
}
return $qb;
}
public function hasForm()
{
return false;
}
public function supportsModifiers()
{
return array('person', 'activity');
}
}

View File

@ -0,0 +1,92 @@
<?php
/*
* Copyright (C) 2016 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 Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\ActivityBundle\Entity\ActivityReason;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ActivityReasonFilter implements FilterInterface
{
/**
*
* @var TranslatableStringHelper
*/
public $translatableStringHelper;
public function __construct(TranslatableStringHelper $helper)
{
$this->translatableStringHelper = $helper;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('activity.reason', ':selected_activity_reasons');
if ($where instanceof Expr\Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('selected_activity_reasons', $data['reasons']);
}
public function applyOn()
{
return 'activity';
}
public function buildForm(FormBuilderInterface $builder)
{
//create a local copy of translatableStringHelper
$helper = $this->translatableStringHelper;
$builder->add('reasons', EntityType::class, array(
'class' => 'ChillActivityBundle:ActivityReason',
'choice_label' => function (ActivityReason $reason) use ($helper) {
return $helper->localize($reason->getName());
},
'group_by' => function(ActivityReason $reason) use ($helper) {
return $helper->localize($reason->getCategory()->getName());
},
'multiple' => true,
'expanded' => false
));
}
public function getTitle()
{
}
}

View File

@ -46,4 +46,21 @@ services:
- '@chill.main.security.authorization.helper' - '@chill.main.security.authorization.helper'
- '@security.token_storage' - '@security.token_storage'
tags: tags:
- { name: chill.timeline, context: 'person' } - { name: chill.timeline, context: 'person' }
chill.activity.export.count_activity:
class: Chill\ActivityBundle\Export\Export\CountActivity
tags:
- { name: chill.export, alias: 'count_activity' }
chill.activity.export.reason_filter:
class: Chill\ActivityBundle\Export\Filter\ActivityReasonFilter
arguments:
- "@chill.main.helper.translatable_string"
tags:
- { name: chill.export_filter, alias: 'activity_reason_filter' }
chill.activity.export.reason_aggregator:
class: Chill\ActivityBundle\Export\Aggregator\ReasonAggregator
tags:
- { name: chill.export_aggregator, alias: activity_reason }