update export & aggregator & filter to new api + add tests

This commit is contained in:
2017-01-12 15:25:21 +01:00
parent a7dfc5e1bd
commit 2997614a04
10 changed files with 459 additions and 26 deletions

View File

@@ -24,6 +24,8 @@ 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;
/**
*
@@ -32,26 +34,65 @@ use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
*/
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'] === 'reason') {
$elem = 'reason.id';
$alias = 'activity_reason_id';
} elseif ($data['level'] === 'category') {
if ($data['level'] === 'reasons') {
$elem = 'reasons.id';
$alias = 'activity_reasons_id';
} elseif ($data['level'] === 'categories') {
$elem = 'category.id';
$alias = 'activity_category_id';
$alias = 'activity_categories_id';
} else {
throw new \RuntimeException('the data provided are not recognised');
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');
}
// make a jointure
$qb->join('activity.reason', 'reason');
// join category if necessary
if ($alias === 'activity_category_id') {
$qb->join('reason.category', 'category');
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
@@ -63,6 +104,24 @@ class ReasonAggregator implements AggregatorInterface
$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()
{
@@ -73,12 +132,13 @@ class ReasonAggregator implements AggregatorInterface
{
$builder->add('level', 'choice', array(
'choices' => array(
'By reason' => 'reason',
'By category of reason' => 'category'
'By reason' => 'reasons',
'By category of reason' => 'categories'
),
'choices_as_values' => true,
'multiple' => false,
'expanded' => true
'expanded' => true,
'label' => 'Reason\'s level'
));
}
@@ -94,17 +154,54 @@ class ReasonAggregator implements AggregatorInterface
public function getLabels($key, array $values, $data)
{
return array_combine($values, $values);
// 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'] === 'reason') {
return array('activity_reason_id');
} elseif ($data['level'] === 'category') {
return array ('activity_category_id');
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');
}

View File

@@ -102,10 +102,13 @@ class CountActivity implements ExportInterface
throw new \LogicException("the key $key is not used by this export");
}
$labels = array_combine($values, $values);
$labels['_header'] = 'Number of activities';
return $labels;
return function($value) {
return $value === '_header' ?
'Number of activities'
:
$value
;
};
}
public function getQueryKeys($data)

View File

@@ -28,6 +28,8 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr;
use Symfony\Component\Security\Core\Role\Role;
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
/**
*
@@ -40,18 +42,38 @@ class ActivityReasonFilter implements FilterInterface
*
* @var TranslatableStringHelper
*/
public $translatableStringHelper;
protected $translatableStringHelper;
public function __construct(TranslatableStringHelper $helper)
{
/**
* The repository for activity reasons
*
* @var EntityRepository
*/
protected $reasonRepository;
public function __construct(
TranslatableStringHelper $helper,
EntityRepository $reasonRepository
) {
$this->translatableStringHelper = $helper;
$this->reasonRepository = $reasonRepository;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('activity.reason', ':selected_activity_reasons');
$join = $qb->getDQLPart('join');
$clause = $qb->expr()->in('reasons', ':selected_activity_reasons');
// add a join to reasons only if needed
if (array_key_exists('activity', $join)) {
// we want to avoid multiple join on same object
if (!$this->checkJoinAlreadyDefined($join['activity'])) {
$qb->add('join', new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons'));
}
} else {
$qb->join('activity.reasons', 'reasons');
}
if ($where instanceof Expr\Andx) {
$where->add($clause);
@@ -62,6 +84,23 @@ class ActivityReasonFilter implements FilterInterface
$qb->add('where', $where);
$qb->setParameter('selected_activity_reasons', $data['reasons']);
}
/**
* Check if a join between Activity and Reason is already defined
*
* @param Join[] $joins
* @return boolean
*/
private function checkJoinAlreadyDefined(array $joins)
{
foreach ($joins as $join) {
if ($join->getAlias() === 'reasons') {
return true;
}
}
return false;
}
public function applyOn()
{
@@ -95,4 +134,18 @@ class ActivityReasonFilter implements FilterInterface
{
return new Role(ActivityVoter::SEE);
}
public function describeAction($data, $format = 'string')
{
// collect all the reasons'name used in this filter in one array
$reasonsNames = array_map(
function(ActivityReason $r) {
return "\"".$this->translatableStringHelper->localize($r->getName())."\"";
},
$this->reasonRepository->findBy(array('id' => $data['reasons']->toArray()))
);
return array("Filtered by reasons: only %list%",
["%list%" => implode(", ", $reasonsNames)]);
}
}