exports: add a new ClosingMotive aggregator

This commit is contained in:
2022-08-08 16:36:01 +02:00
parent 643f37509f
commit ec7325ebbd
3 changed files with 112 additions and 1 deletions

View File

@@ -0,0 +1,103 @@
<?php
namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Repository\AccompanyingPeriod\ClosingMotiveRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class ClosingMotiveAggregator implements AggregatorInterface
{
private EntityManagerInterface $em;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
EntityManagerInterface $em,
TranslatableStringHelper $translatableStringHelper
) {
$this->motiveRepository = $em->getRepository(ClosingMotive::class);
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @inheritDoc
*/
public function getLabels($key, array $values, $data)
{
return function ($value): string {
if ('_header' === $value) {
return 'Closing motive';
}
$cm = $this->motiveRepository->find($value);
return $this->translatableStringHelper->localize(
$cm->getName()
);
};
}
/**
* @inheritDoc
*/
public function getQueryKeys($data)
{
return ['closingmotive_aggregator'];
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Group by closing motive';
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->join('acp.closingMotive', 'cm');
$qb->addSelect('IDENTITY(acp.closingMotive) AS closingmotive_aggregator');
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
$qb->addGroupBy('closingmotive_aggregator');
} else {
$qb->groupBy('closingmotive_aggregator');
}
}
/**
* @inheritDoc
*/
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}