[export] fix data type from form: either collection or array

This commit is contained in:
Julien Fastré 2023-11-05 23:08:13 +01:00
parent b2aa465b03
commit 6fd80f9f2e
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
17 changed files with 51 additions and 12 deletions

View File

@ -0,0 +1,5 @@
kind: Fixed
body: Fix "problem during download" on some filters, which used a wrong data type
time: 2023-11-05T23:04:26.346839079+01:00
custom:
Issue: "183"

View File

@ -80,7 +80,7 @@ class ActivityTypeFilter implements ExportElementValidatedInterface, FilterInter
// collect all the reasons'name used in this filter in one array // collect all the reasons'name used in this filter in one array
$reasonsNames = array_map( $reasonsNames = array_map(
fn (ActivityType $t): string => $this->translatableStringHelper->localize($t->getName()), fn (ActivityType $t): string => $this->translatableStringHelper->localize($t->getName()),
$this->activityTypeRepository->findBy(['id' => $data['types']->toArray()]) $this->activityTypeRepository->findBy(['id' => $data['types'] instanceof \Doctrine\Common\Collections\Collection ? $data['types']->toArray() : $data['types']])
); );
return ['Filtered by activity type: only %list%', [ return ['Filtered by activity type: only %list%', [

View File

@ -17,6 +17,7 @@ use Chill\ActivityBundle\Repository\ActivityReasonRepository;
use Chill\MainBundle\Export\ExportElementValidatedInterface; use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query\Expr; use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@ -79,7 +80,7 @@ class ActivityReasonFilter implements ExportElementValidatedInterface, FilterInt
// collect all the reasons'name used in this filter in one array // collect all the reasons'name used in this filter in one array
$reasonsNames = array_map( $reasonsNames = array_map(
fn (ActivityReason $r): string => '"'.$this->translatableStringHelper->localize($r->getName()).'"', fn (ActivityReason $r): string => '"'.$this->translatableStringHelper->localize($r->getName()).'"',
$this->activityReasonRepository->findBy(['id' => $data['reasons']->toArray()]) $this->activityReasonRepository->findBy(['id' => $data['reasons'] instanceof Collection ? $data['reasons']->toArray() : $data['reasons']])
); );
return [ return [

View File

@ -17,6 +17,7 @@ use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -80,7 +81,7 @@ class UsersJobFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()), fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()),
$data['jobs']->toArray() $data['jobs'] instanceof Collection ? $data['jobs']->toArray() : $data['jobs']
) )
), ),
]]; ]];

View File

@ -18,6 +18,7 @@ use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -83,7 +84,7 @@ class UsersScopeFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()), fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()),
$data['scopes']->toArray() $data['scopes'] instanceof Collection ? $data['scopes']->toArray() : $data['scopes']
) )
), ),
]]; ]];

View File

@ -52,6 +52,7 @@ final class ActivityReasonFilterTest extends AbstractFilterTest
public function getFormData() public function getFormData()
{ {
self::bootKernel(); self::bootKernel();
$data = [];
$em = self::$container $em = self::$container
->get(EntityManagerInterface::class); ->get(EntityManagerInterface::class);
@ -62,10 +63,13 @@ final class ActivityReasonFilterTest extends AbstractFilterTest
// generate an array of 5 different combination of results // generate an array of 5 different combination of results
for ($i = 0; 5 > $i; ++$i) { for ($i = 0; 5 > $i; ++$i) {
yield ['reasons' => new ArrayCollection(array_splice($reasons, ($i + 1) * -1))]; $data[] = ['reasons' => new ArrayCollection(array_splice($reasons, ($i + 1) * -1))];
$data[] = ['reasons' => array_splice($reasons, ($i + 1) * -1)];
} }
self::ensureKernelShutdown(); self::ensureKernelShutdown();
return $data;
} }
public function getQueryBuilders(): iterable public function getQueryBuilders(): iterable

View File

@ -57,6 +57,9 @@ final class ActivityTypeFilterTest extends AbstractFilterTest
$data[] = [ $data[] = [
'types' => new ArrayCollection([$a]), 'types' => new ArrayCollection([$a]),
]; ];
/*$data[] = [
'types' => [$a],
];*/
} }
return $data; return $data;

View File

@ -17,6 +17,7 @@ use Chill\AsideActivityBundle\Repository\AsideActivityCategoryRepository;
use Chill\AsideActivityBundle\Templating\Entity\CategoryRender; use Chill\AsideActivityBundle\Templating\Entity\CategoryRender;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -76,7 +77,7 @@ class ByActivityTypeFilter implements FilterInterface
{ {
$types = array_map( $types = array_map(
fn (AsideActivityCategory $t): string => $this->translatableStringHelper->localize($t->getTitle()), fn (AsideActivityCategory $t): string => $this->translatableStringHelper->localize($t->getTitle()),
$data['types']->toArray() $data['types'] instanceof Collection ? $data['types']->toArray() : $data['types']
); );
return ['export.filter.Filtered by aside activity type: only %type%', [ return ['export.filter.Filtered by aside activity type: only %type%', [

View File

@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickUserLocationType; use Chill\MainBundle\Form\Type\PickUserLocationType;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
@ -54,7 +55,12 @@ final readonly class ByLocationFilter implements FilterInterface
public function describeAction($data, $format = 'string'): array public function describeAction($data, $format = 'string'): array
{ {
$locations = $data['locations']->map(fn (Location $l): string => $l->getName()); $extractFunction = fn (Location $l): string => $l->getName();
if ($data['locations'] instanceof Collection) {
$locations = $data['locations']->map($extractFunction);
} else {
$locations = array_map($extractFunction, $data['locations']);
}
return ['export.filter.Filtered by aside activity location: only %location%', [ return ['export.filter.Filtered by aside activity location: only %location%', [
'%location%' => implode(', ', $locations), '%location%' => implode(', ', $locations),

View File

@ -17,6 +17,7 @@ use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -80,7 +81,7 @@ class ByUserJobFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()), fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()),
$data['jobs']->toArray() $data['jobs'] instanceof Collection ? $data['jobs']->toArray() : $data['jobs']
) )
), ),
]]; ]];

View File

@ -18,6 +18,7 @@ use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -83,7 +84,7 @@ class ByUserScopeFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()), fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()),
$data['scopes']->toArray() $data['scopes'] instanceof Collection ? $data['scopes']->toArray() : $data['scopes']
) )
), ),
]]; ]];

View File

@ -16,6 +16,7 @@ use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Form\Type\PickSocialIssueType; use Chill\PersonBundle\Form\Type\PickSocialIssueType;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -50,7 +51,9 @@ class SocialIssueFilter implements FilterInterface
$qb->andWhere($clause) $qb->andWhere($clause)
->setParameter( ->setParameter(
'socialissues', 'socialissues',
SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues']->toArray()) SocialIssue::getDescendantsWithThisForIssues(
$data['accepted_socialissues'] instanceof Collection ? $data['accepted_socialissues']->toArray() : $data['accepted_socialissues']
)
); );
} }

View File

@ -17,6 +17,7 @@ use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\UserJobRepositoryInterface; use Chill\MainBundle\Repository\UserJobRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@ -105,7 +106,7 @@ class UserJobFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()), fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()),
$data['jobs']->toArray() $data['jobs'] instanceof Collection ? $data['jobs']->toArray() : $data['jobs']
) )
), ),
], ],

View File

@ -17,6 +17,7 @@ use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@ -104,7 +105,7 @@ class UserScopeFilter implements FilterInterface
', ', ', ',
array_map( array_map(
fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()), fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()),
$data['scopes']->toArray() $data['scopes'] instanceof Collection ? $data['scopes']->toArray() : $data['scopes']
) )
), ),
], ],

View File

@ -52,6 +52,7 @@ final class SocialIssueFilterTest extends AbstractFilterTest
->getResult(); ->getResult();
yield ['accepted_socialissues' => new ArrayCollection($array)]; yield ['accepted_socialissues' => new ArrayCollection($array)];
yield ['accepted_socialissues' => $array];
} }
public function getQueryBuilders(): array public function getQueryBuilders(): array

View File

@ -52,6 +52,11 @@ final class UserJobFilterTest extends AbstractFilterTest
'jobs' => new ArrayCollection($jobs), 'jobs' => new ArrayCollection($jobs),
'date_calc' => new RollingDate(RollingDate::T_TODAY), 'date_calc' => new RollingDate(RollingDate::T_TODAY),
]; ];
yield [
'jobs' => $jobs,
'date_calc' => new RollingDate(RollingDate::T_TODAY),
];
} }
public function getQueryBuilders(): array public function getQueryBuilders(): array

View File

@ -53,6 +53,10 @@ final class UserScopeFilterTest extends AbstractFilterTest
'date_calc' => new RollingDate(RollingDate::T_TODAY), 'date_calc' => new RollingDate(RollingDate::T_TODAY),
'scopes' => new ArrayCollection($scopes), 'scopes' => new ArrayCollection($scopes),
], ],
[
'date_calc' => new RollingDate(RollingDate::T_TODAY),
'scopes' => $scopes,
],
]; ];
} }