From 3c65e2d58546d33d27a8a85de101c57bdc233a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 2 Jan 2016 16:47:55 +0100 Subject: [PATCH] [export] add an export 'count activity' and first filters - reason aggregator - reason filter - count activity export --- Export/Aggregator/ReasonAggregator.php | 88 ++++++++++++++++++++++++ Export/Export/CountActivity.php | 77 +++++++++++++++++++++ Export/Filter/ActivityReasonFilter.php | 92 ++++++++++++++++++++++++++ Resources/config/services.yml | 19 +++++- 4 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 Export/Aggregator/ReasonAggregator.php create mode 100644 Export/Export/CountActivity.php create mode 100644 Export/Filter/ActivityReasonFilter.php diff --git a/Export/Aggregator/ReasonAggregator.php b/Export/Aggregator/ReasonAggregator.php new file mode 100644 index 000000000..e24457525 --- /dev/null +++ b/Export/Aggregator/ReasonAggregator.php @@ -0,0 +1,88 @@ + + * + * 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\ActivityBundle\Export\Aggregator; + +use Symfony\Component\Form\FormBuilderInterface; +use Doctrine\ORM\QueryBuilder; +use Chill\MainBundle\Export\AggregatorInterface; + +/** + * + * + * @author Julien Fastré + */ +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"; + } + +} diff --git a/Export/Export/CountActivity.php b/Export/Export/CountActivity.php new file mode 100644 index 000000000..237f58ca6 --- /dev/null +++ b/Export/Export/CountActivity.php @@ -0,0 +1,77 @@ + + * + * 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\ActivityBundle\Export\Export; + +use Chill\MainBundle\Export\ExportInterface; +use Doctrine\ORM\Query\Expr\Join; +use Doctrine\ORM\QueryBuilder; + +/** + * + * + * @author Julien Fastré + */ +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'); + } + +} diff --git a/Export/Filter/ActivityReasonFilter.php b/Export/Filter/ActivityReasonFilter.php new file mode 100644 index 000000000..6b7ad68f7 --- /dev/null +++ b/Export/Filter/ActivityReasonFilter.php @@ -0,0 +1,92 @@ + + * + * 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\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é + */ +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() + { + + } + +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 39ba42839..090ff55ac 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -46,4 +46,21 @@ services: - '@chill.main.security.authorization.helper' - '@security.token_storage' tags: - - { name: chill.timeline, context: 'person' } \ No newline at end of file + - { 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 } \ No newline at end of file