household position aggregator for person

This commit is contained in:
2022-08-16 15:07:03 +02:00
parent bde623e806
commit 4c91bf5084
3 changed files with 136 additions and 1 deletions

View File

@@ -0,0 +1,125 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Export\Aggregator\PersonAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Repository\Household\PositionRepository;
use DateTime;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final class HouseholdPositionAggregator implements AggregatorInterface, ExportElementValidatedInterface
{
private TranslatorInterface $translator;
private PositionRepository $positionRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(TranslatorInterface $translator, TranslatableStringHelper $translatableStringHelper, PositionRepository $positionRepository)
{
$this->translator = $translator;
$this->positionRepository = $positionRepository;
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole()
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->resetDQLPart('from');
$qb->from('ChillPersonBundle:Household\HouseholdMember', 'hm');
$qb->join('hm.person', 'person');
$qb->join('person.center', 'center');
$qb->andWhere($qb->expr()->andX(
$qb->expr()->lte('hm.startDate', ':date'),
$qb->expr()->orX(
$qb->expr()->isNull('hm.endDate'),
$qb->expr()->gte('hm.endDate', ':date')
)
));
$qb->setParameter('date', $data['date_position']);
$qb->addSelect('IDENTITY(hm.position) AS household_position_aggregator');
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
$qb->addGroupBy('household_position_aggregator');
} else {
$qb->groupBy('household_position_aggregator');
}
}
public function applyOn()
{
return 'person';
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('date_position', ChillDateType::class, [
'label' => 'Household position in relation to this date',
'data' => new DateTime(),
]);
}
public function getLabels($key, array $values, $data)
{
return function ($value) {
if ('_header' === $value) {
return 'Household position';
}
if (null === $value) {
return $this->translator->trans('without data');
}
$p = $this->positionRepository->find($value);
return $this->translatableStringHelper->localize($p->getLabel());
};
}
public function getQueryKeys($data)
{
return [
'household_position_aggregator',
];
}
public function getTitle()
{
return 'Aggregate by household position';
}
public function validateForm($data, ExecutionContextInterface $context)
{
if (null === $data['date_position']) {
$context->buildViolation('The date should not be empty')
->addViolation();
}
}
}