mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
212 lines
6.4 KiB
PHP
212 lines
6.4 KiB
PHP
<?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;
|
|
use Symfony\Component\Security\Core\Role\Role;
|
|
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
*/
|
|
class ReasonAggregator implements AggregatorInterface
|
|
{
|
|
/**
|
|
*
|
|
* @var EntityRepository
|
|
*/
|
|
protected $categoryRepository;
|
|
|
|
/**
|
|
*
|
|
* @var EntityRepository
|
|
*/
|
|
protected $reasonRepository;
|
|
|
|
/**
|
|
*
|
|
* @var TranslatableStringHelper
|
|
*/
|
|
protected $stringHelper;
|
|
|
|
public function __construct(
|
|
EntityRepository $categoryRepository,
|
|
EntityRepository $reasonRepository,
|
|
TranslatableStringHelper $stringHelper
|
|
) {
|
|
$this->categoryRepository = $categoryRepository;
|
|
$this->reasonRepository = $reasonRepository;
|
|
$this->stringHelper = $stringHelper;
|
|
}
|
|
|
|
public function alterQuery(QueryBuilder $qb, $data)
|
|
{
|
|
// add select element
|
|
if ($data['level'] === 'reasons') {
|
|
$elem = 'reasons.id';
|
|
$alias = 'activity_reasons_id';
|
|
} elseif ($data['level'] === 'categories') {
|
|
$elem = 'category.id';
|
|
$alias = 'activity_categories_id';
|
|
} else {
|
|
throw new \RuntimeException('the data provided are not recognized');
|
|
}
|
|
|
|
$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 category if necessary
|
|
if ($alias === 'activity_categories_id') {
|
|
// add join only if needed
|
|
if (!$this->checkJoinAlreadyDefined($qb->getDQLPart('join')['activity'], 'category')) {
|
|
$qb->join('reasons.category', 'category');
|
|
}
|
|
}
|
|
|
|
// add the "group by" part
|
|
$groupBy = $qb->getDQLPart('groupBy');
|
|
|
|
if (count($groupBy) > 0) {
|
|
$qb->addGroupBy($alias);
|
|
} else {
|
|
$qb->groupBy($alias);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a join between Activity and another alias
|
|
*
|
|
* @param Join[] $joins
|
|
* @param string $alias the alias to search for
|
|
* @return boolean
|
|
*/
|
|
private function checkJoinAlreadyDefined(array $joins, $alias)
|
|
{
|
|
foreach ($joins as $join) {
|
|
if ($join->getAlias() === $alias) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function applyOn()
|
|
{
|
|
return 'activity';
|
|
}
|
|
|
|
public function buildForm(FormBuilderInterface $builder)
|
|
{
|
|
$builder->add('level', 'choice', array(
|
|
'choices' => array(
|
|
'By reason' => 'reasons',
|
|
'By category of reason' => 'categories'
|
|
),
|
|
'choices_as_values' => true,
|
|
'multiple' => false,
|
|
'expanded' => true,
|
|
'label' => 'Reason\'s level'
|
|
));
|
|
}
|
|
|
|
public function getTitle()
|
|
{
|
|
return "Aggregate by activity reason";
|
|
}
|
|
|
|
public function addRole()
|
|
{
|
|
return new Role(ActivityVoter::SEE);
|
|
}
|
|
|
|
public function getLabels($key, array $values, $data)
|
|
{
|
|
// for performance reason, we load data from db only once
|
|
switch ($data['level']) {
|
|
case 'reasons':
|
|
$this->reasonRepository->findBy(array('id' => $values));
|
|
break;
|
|
case 'categories':
|
|
$this->categoryRepository->findBy(array('id' => $values));
|
|
break;
|
|
default:
|
|
throw new \RuntimeException(sprintf("the level data '%s' is invalid",
|
|
$data['level']));
|
|
}
|
|
|
|
return function($value) use ($data) {
|
|
if ($value === '_header') {
|
|
return $data['level'] === 'reasons' ?
|
|
'Group by reasons'
|
|
:
|
|
'Group by categories of reason'
|
|
;
|
|
}
|
|
|
|
switch ($data['level']) {
|
|
case 'reasons':
|
|
$n = $this->reasonRepository->find($value)
|
|
->getName()
|
|
;
|
|
break;
|
|
case 'categories':
|
|
$n = $this->categoryRepository->find($value)
|
|
->getName()
|
|
;
|
|
break;
|
|
// no need for a default : the default was already set above
|
|
}
|
|
|
|
return $this->stringHelper->localize($n);
|
|
};
|
|
|
|
}
|
|
|
|
public function getQueryKeys($data)
|
|
{
|
|
// add select element
|
|
if ($data['level'] === 'reasons') {
|
|
return array('activity_reasons_id');
|
|
} elseif ($data['level'] === 'categories') {
|
|
return array ('activity_categories_id');
|
|
} else {
|
|
throw new \RuntimeException('the data provided are not recognised');
|
|
}
|
|
|
|
}
|
|
|
|
}
|