mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
234 lines
7.0 KiB
PHP
234 lines
7.0 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\ActivityStatsVoter;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
use Doctrine\ORM\Query\Expr\Join;
|
|
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
*/
|
|
class ActivityReasonAggregator implements AggregatorInterface,
|
|
ExportElementValidatedInterface
|
|
{
|
|
/**
|
|
*
|
|
* @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
|
|
$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
|
|
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', ChoiceType::class, array(
|
|
'choices' => array(
|
|
'By reason' => 'reasons',
|
|
'By category of reason' => 'categories'
|
|
),
|
|
'multiple' => false,
|
|
'expanded' => true,
|
|
'label' => 'Reason\'s level'
|
|
));
|
|
}
|
|
|
|
public function validateForm($data, ExecutionContextInterface $context)
|
|
{
|
|
if ($data['level'] === null) {
|
|
$context->buildViolation("The reasons's level should not be empty")
|
|
->addViolation();
|
|
}
|
|
}
|
|
|
|
public function getTitle()
|
|
{
|
|
return "Aggregate by activity reason";
|
|
}
|
|
|
|
public function addRole()
|
|
{
|
|
return new Role(ActivityStatsVoter::STATS);
|
|
}
|
|
|
|
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':
|
|
/* @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':
|
|
$c = $this->categoryRepository->find($value);
|
|
|
|
return $this->stringHelper->localize($c->getName());
|
|
break;
|
|
// no need for a default : the default was already set above
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
}
|
|
|
|
}
|