* * 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; use Symfony\Component\Security\Core\Role\Role; use Chill\ActivityBundle\Security\Authorization\ActivityVoter; /** * * * @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"; } public function addRole() { return new Role(ActivityVoter::SEE); } public function getLabels($key, array $values, $data) { return array_combine($values, $values); } public function getQueryKeys($data) { // add select element if ($data['level'] === 'reason') { return array('activity_reason_id'); } elseif ($data['level'] === 'category') { return array ('activity_category_id'); } else { throw new \RuntimeException('the data provided are not recognised'); } } }