mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
exports: add Composition Filter and Aggregator in Household exports
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Export\Aggregator\HouseholdAggregators;
|
||||
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Chill\PersonBundle\Repository\Household\HouseholdCompositionTypeRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class CompositionAggregator implements AggregatorInterface
|
||||
{
|
||||
private HouseholdCompositionTypeRepository $typeRepository;
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
HouseholdCompositionTypeRepository $typeRepository,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
) {
|
||||
$this->typeRepository = $typeRepository;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
return function ($value): string {
|
||||
if ($value === '_header') {
|
||||
return 'Composition';
|
||||
}
|
||||
|
||||
$c = $this->typeRepository->find($value);
|
||||
|
||||
return $this->translatableStringHelper->localize(
|
||||
$c->getLabel()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getQueryKeys($data): array
|
||||
{
|
||||
return ['composition_aggregator'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
// TODO: Implement buildForm() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return 'Group by composition';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function addRole()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
$qb
|
||||
->join('household.compositions', 'composition')
|
||||
//->join('composition.householdCompositionType', 'type')
|
||||
;
|
||||
|
||||
$qb->addSelect('IDENTITY(composition.householdCompositionType) AS composition_aggregator');
|
||||
|
||||
$groupBy = $qb->getDQLPart('groupBy');
|
||||
|
||||
if (!empty($groupBy)) {
|
||||
$qb->addGroupBy('composition_aggregator');
|
||||
} else {
|
||||
$qb->groupBy('composition_aggregator');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function applyOn(): string
|
||||
{
|
||||
return Declarations::HOUSEHOLD_TYPE;
|
||||
}
|
||||
}
|
@@ -106,6 +106,7 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
|
||||
->join('acp.participations', 'acppart')
|
||||
->join('acppart.person', 'person')
|
||||
->join('person.householdParticipations', 'householdmember')
|
||||
->join('householdmember.household', 'household')
|
||||
;
|
||||
|
||||
$qb->select('COUNT(DISTINCT householdmember.household) AS export_result');
|
||||
|
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Export\Filter\HouseholdFilters;
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdCompositionType;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Doctrine\ORM\Query\Expr\Andx;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class CompositionFilter implements FilterInterface
|
||||
{
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
) {
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('accepted_composition', EntityType::class, [
|
||||
'class' => HouseholdCompositionType::class,
|
||||
'choice_label' => function (HouseholdCompositionType $type) {
|
||||
return $this->translatableStringHelper->localize(
|
||||
$type->getLabel()
|
||||
);
|
||||
},
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return 'Filter by composition';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function describeAction($data, $format = 'string'): array
|
||||
{
|
||||
$compositions = [];
|
||||
|
||||
foreach ($data['accepted_composition'] as $c) {
|
||||
$compositions[] = $this->translatableStringHelper->localize(
|
||||
$c->getLabel()
|
||||
);
|
||||
}
|
||||
|
||||
return ['Filtered by composition: only %compositions%', [
|
||||
'%compositions%' => implode(", ou ", $compositions)
|
||||
]];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function addRole()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
$qb
|
||||
->join('household.compositions', 'composition')
|
||||
//->join('composition.householdCompositionType', 'type')
|
||||
;
|
||||
|
||||
$where = $qb->getDQLPart('where');
|
||||
|
||||
$clause = $qb->expr()->in('composition.householdCompositionType', ':compositions');
|
||||
|
||||
if ($where instanceof Andx) {
|
||||
$where->add($clause);
|
||||
} else {
|
||||
$where = $qb->expr()->andX($clause);
|
||||
}
|
||||
|
||||
$qb->add('where', $where);
|
||||
$qb->setParameter('compositions', $data['accepted_composition']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function applyOn(): string
|
||||
{
|
||||
return Declarations::HOUSEHOLD_TYPE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user