mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-03 21:34:59 +00:00
update export & aggregator & filter to new api + add tests
This commit is contained in:
@@ -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)]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user