mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-11 02:09:50 +00:00
fix: SA: Fix "...Switch condition type..." rule.
Also fix a critical bug in `ReportList.php`. SA stands for Static Analysis.
This commit is contained in:
parent
7462babbeb
commit
5a85d497ab
@ -160,46 +160,6 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php
|
path: src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'person_address_country_name' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'person_birthdate' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'person_countryOfBirth' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'person_gender' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'person_nationality' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'report_date' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'report_scope' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Switch condition type \\(Chill\\\\ReportBundle\\\\Export\\\\Export\\\\type\\) does not match case condition 'report_user' \\(string\\)\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/Bundle/ChillReportBundle/Export/Export/ReportList.php
|
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Chill\\\\TaskBundle\\\\Entity\\\\RecurringTask\\:\\:__construct\\(\\) does not call parent constructor from Chill\\\\TaskBundle\\\\Entity\\\\AbstractTask\\.$#"
|
message: "#^Chill\\\\TaskBundle\\\\Entity\\\\RecurringTask\\:\\:__construct\\(\\) does not call parent constructor from Chill\\\\TaskBundle\\\\Entity\\\\AbstractTask\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
|
||||||
*
|
declare(strict_types=1);
|
||||||
*/
|
|
||||||
namespace Chill\ReportBundle\Export\Export;
|
namespace Chill\ReportBundle\Export\Export;
|
||||||
|
|
||||||
use Chill\MainBundle\Export\ListInterface;
|
use Chill\MainBundle\Export\ListInterface;
|
||||||
@ -9,6 +9,7 @@ use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
|||||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||||
use Chill\ReportBundle\Security\Authorization\ReportVoter;
|
use Chill\ReportBundle\Security\Authorization\ReportVoter;
|
||||||
use Chill\PersonBundle\Export\Declarations;
|
use Chill\PersonBundle\Export\Declarations;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\Security\Core\Role\Role;
|
use Symfony\Component\Security\Core\Role\Role;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||||
@ -26,49 +27,28 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
||||||
*/
|
|
||||||
class ReportList implements ListInterface, ExportElementValidatedInterface
|
class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||||
{
|
{
|
||||||
/**
|
protected CustomFieldsGroup $customfieldsGroup;
|
||||||
*
|
|
||||||
* @var CustomFieldsGroup
|
|
||||||
*/
|
|
||||||
protected $customfieldsGroup;
|
|
||||||
|
|
||||||
/**
|
protected TranslatableStringHelper $translatableStringHelper;
|
||||||
*
|
|
||||||
* @var TranslatableStringHelper
|
|
||||||
*/
|
|
||||||
protected $translatableStringHelper;
|
|
||||||
|
|
||||||
/**
|
protected TranslatorInterface $translator;
|
||||||
*
|
|
||||||
* @var TranslatorInterface
|
|
||||||
*/
|
|
||||||
protected $translator;
|
|
||||||
|
|
||||||
/**
|
protected CustomFieldProvider $customFieldProvider;
|
||||||
*
|
|
||||||
* @var CustomFieldProvider
|
|
||||||
*/
|
|
||||||
protected $customFieldProvider;
|
|
||||||
|
|
||||||
protected $em;
|
protected EntityManagerInterface $em;
|
||||||
|
|
||||||
protected $fields = array(
|
protected array $fields = [
|
||||||
'person_id', 'person_firstName', 'person_lastName', 'person_birthdate',
|
'person_id', 'person_firstName', 'person_lastName', 'person_birthdate',
|
||||||
'person_placeOfBirth', 'person_gender', 'person_memo', 'person_email', 'person_phonenumber',
|
'person_placeOfBirth', 'person_gender', 'person_memo', 'person_email', 'person_phonenumber',
|
||||||
'person_countryOfBirth', 'person_nationality', 'person_address_street_address_1',
|
'person_countryOfBirth', 'person_nationality', 'person_address_street_address_1',
|
||||||
'person_address_street_address_2', 'person_address_valid_from', 'person_address_postcode_label',
|
'person_address_street_address_2', 'person_address_valid_from', 'person_address_postcode_label',
|
||||||
'person_address_postcode_code', 'person_address_country_name', 'person_address_country_code',
|
'person_address_postcode_code', 'person_address_country_name', 'person_address_country_code',
|
||||||
'report_id', 'report_user', 'report_date', 'report_scope'
|
'report_id', 'report_user', 'report_date', 'report_scope'
|
||||||
);
|
];
|
||||||
|
|
||||||
protected $slugs = [];
|
protected array $slugs = [];
|
||||||
|
|
||||||
function __construct(
|
function __construct(
|
||||||
CustomFieldsGroup $customfieldsGroup,
|
CustomFieldsGroup $customfieldsGroup,
|
||||||
@ -85,7 +65,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
public function buildForm(FormBuilderInterface $builder)
|
||||||
{
|
{
|
||||||
$choices = array_combine($this->fields, $this->fields);
|
$choices = array_combine($this->fields, $this->fields);
|
||||||
|
|
||||||
@ -97,7 +77,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add a checkbox to select fields
|
// Add a checkbox to select fields
|
||||||
$builder->add('fields', ChoiceType::class, array(
|
$builder->add('fields', ChoiceType::class, [
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
'expanded' => true,
|
'expanded' => true,
|
||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
@ -106,11 +86,11 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
// add a 'data-display-target' for address fields
|
// add a 'data-display-target' for address fields
|
||||||
if (substr($val, 0, 8) === 'address_') {
|
if (substr($val, 0, 8) === 'address_') {
|
||||||
return ['data-display-target' => 'address_date'];
|
return ['data-display-target' => 'address_date'];
|
||||||
} else {
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
},
|
},
|
||||||
'choice_label' => function($key, $label) {
|
'choice_label' => function(string $key, string $label): string {
|
||||||
switch (\substr($key, 0, 7)) {
|
switch (\substr($key, 0, 7)) {
|
||||||
case 'person_':
|
case 'person_':
|
||||||
return $this->translator->trans(\substr($key, 7, \strlen($key) - 7)).
|
return $this->translator->trans(\substr($key, 7, \strlen($key) - 7)).
|
||||||
@ -123,7 +103,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
' ('.$this->translator->trans("Report's question").')';;
|
' ('.$this->translator->trans("Report's question").')';;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'constraints' => [new Callback(array(
|
'constraints' => [new Callback([
|
||||||
'callback' => function($selected, ExecutionContextInterface $context) {
|
'callback' => function($selected, ExecutionContextInterface $context) {
|
||||||
if (count($selected) === 0) {
|
if (count($selected) === 0) {
|
||||||
$context->buildViolation('You must select at least one element')
|
$context->buildViolation('You must select at least one element')
|
||||||
@ -131,24 +111,25 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
->addViolation();
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
))]
|
])]
|
||||||
));
|
]);
|
||||||
|
|
||||||
// add a date field for addresses
|
// add a date field for addresses
|
||||||
$builder->add('address_date', ChillDateType::class, array(
|
$builder->add('address_date', ChillDateType::class, [
|
||||||
'label' => "Address valid at this date",
|
'label' => "Address valid at this date",
|
||||||
'data' => new \DateTime(),
|
'data' => new \DateTime(),
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'block_name' => 'list_export_form_address_date'
|
'block_name' => 'list_export_form_address_date'
|
||||||
));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validateForm($data, ExecutionContextInterface $context)
|
public function validateForm($data, ExecutionContextInterface $context)
|
||||||
{
|
{
|
||||||
// get the field starting with address_
|
// get the field starting with address_
|
||||||
$addressFields = array_filter(function($el) {
|
$addressFields = array_filter(
|
||||||
return substr($el, 0, 8) === 'address_';
|
$this->fields,
|
||||||
}, $this->fields);
|
static fn(string $el): bool => substr($el, 0, 8) === 'address_'
|
||||||
|
);
|
||||||
|
|
||||||
// check if there is one field starting with address in data
|
// check if there is one field starting with address in data
|
||||||
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
|
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
|
||||||
@ -177,7 +158,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
|
|
||||||
public function getAllowedFormattersTypes()
|
public function getAllowedFormattersTypes()
|
||||||
{
|
{
|
||||||
return array(FormatterInterface::TYPE_LIST);
|
return [FormatterInterface::TYPE_LIST];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDescription()
|
public function getDescription()
|
||||||
@ -190,13 +171,6 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* @param type $key
|
|
||||||
* @param array $values
|
|
||||||
* @param type $data
|
|
||||||
*/
|
|
||||||
public function getLabels($key, array $values, $data): \Closure
|
public function getLabels($key, array $values, $data): \Closure
|
||||||
{
|
{
|
||||||
switch ($key) {
|
switch ($key) {
|
||||||
@ -284,7 +258,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
->getRepository('ChillMainBundle:Country');
|
->getRepository('ChillMainBundle:Country');
|
||||||
|
|
||||||
// load all countries in a single query
|
// load all countries in a single query
|
||||||
$countryRepository->findBy(array('countryCode' => $values));
|
$countryRepository->findBy(['countryCode' => $values]);
|
||||||
|
|
||||||
return function($value) use ($key, $countryRepository) {
|
return function($value) use ($key, $countryRepository) {
|
||||||
if ($value === '_header') { return \strtolower($key); }
|
if ($value === '_header') { return \strtolower($key); }
|
||||||
@ -317,9 +291,9 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
return $value;
|
return $value;
|
||||||
|
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
return $this->getLabelForCustomField($key, $values, $data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->getLabelForCustomField($key, $values, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -330,7 +304,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
/* @var $cf CustomField */
|
/* @var $cf CustomField */
|
||||||
$cf = $this->em
|
$cf = $this->em
|
||||||
->getRepository(CustomField::class)
|
->getRepository(CustomField::class)
|
||||||
->findOneBy(array('slug' => $this->DQLToSlug($key)));
|
->findOneBy(['slug' => $this->DQLToSlug($key)]);
|
||||||
|
|
||||||
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
|
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
|
||||||
$defaultFunction = function($value) use ($cf) {
|
$defaultFunction = function($value) use ($cf) {
|
||||||
@ -358,19 +332,19 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
|
|
||||||
if ($slugChoice === '_other' and $cfType->isChecked($cf, $choiceSlug, $decoded)) {
|
if ($slugChoice === '_other' and $cfType->isChecked($cf, $choiceSlug, $decoded)) {
|
||||||
return $cfType->extractOtherValue($cf, $decoded);
|
return $cfType->extractOtherValue($cf, $decoded);
|
||||||
} else {
|
|
||||||
return $cfType->isChecked($cf, $slugChoice, $decoded);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $cfType->isChecked($cf, $slugChoice, $decoded);
|
||||||
};
|
};
|
||||||
|
|
||||||
} else {
|
|
||||||
return $defaultFunction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $defaultFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getQueryKeys($data)
|
public function getQueryKeys($data)
|
||||||
{
|
{
|
||||||
$fields = array();
|
$fields = [];
|
||||||
|
|
||||||
foreach ($data['fields'] as $key) {
|
foreach ($data['fields'] as $key) {
|
||||||
if (in_array($key, $this->fields)) {
|
if (in_array($key, $this->fields)) {
|
||||||
@ -382,16 +356,9 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
return \array_merge($fields, \array_keys($this->slugs));
|
return \array_merge($fields, \array_keys($this->slugs));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* clean a slug to be usable by DQL
|
|
||||||
*
|
|
||||||
* @param string $slugsanitize
|
|
||||||
* @param string $type the type of the customfield, if required (currently only for choices)
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function slugToDQL($slug, $type = "default", array $additionalInfos = [])
|
private function slugToDQL($slug, $type = "default", array $additionalInfos = [])
|
||||||
{
|
{
|
||||||
$uid = 'slug_'.\uniqid();
|
$uid = 'slug_' . \uniqid('', true);
|
||||||
|
|
||||||
$this->slugs[$uid] = [
|
$this->slugs[$uid] = [
|
||||||
'slug' => $slug,
|
'slug' => $slug,
|
||||||
@ -407,11 +374,6 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
return $this->slugs[$cleanedSlug]['slug'];
|
return $this->slugs[$cleanedSlug]['slug'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param type $cleanedSlug
|
|
||||||
* @return an array with keys = 'slug', 'type', 'additionnalInfo'
|
|
||||||
*/
|
|
||||||
private function extractInfosFromSlug($slug)
|
private function extractInfosFromSlug($slug)
|
||||||
{
|
{
|
||||||
return $this->slugs[$slug];
|
return $this->slugs[$slug];
|
||||||
@ -437,7 +399,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
return 'report';
|
return 'report';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
|
public function initiateQuery(array $requiredModifiers, array $acl, array $data = [])
|
||||||
{
|
{
|
||||||
$centers = array_map(function($el) { return $el['center']; }, $acl);
|
$centers = array_map(function($el) { return $el['center']; }, $acl);
|
||||||
|
|
||||||
@ -533,7 +495,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$qb
|
return $qb
|
||||||
->from(Report::class, 'report')
|
->from(Report::class, 'report')
|
||||||
->leftJoin('report.person', 'person')
|
->leftJoin('report.person', 'person')
|
||||||
->join('person.center', 'center')
|
->join('person.center', 'center')
|
||||||
@ -541,9 +503,6 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
|||||||
->setParameter('cFGroup', $this->customfieldsGroup)
|
->setParameter('cFGroup', $this->customfieldsGroup)
|
||||||
->andWhere('center IN (:authorized_centers)')
|
->andWhere('center IN (:authorized_centers)')
|
||||||
->setParameter('authorized_centers', $centers);
|
->setParameter('authorized_centers', $centers);
|
||||||
;
|
|
||||||
|
|
||||||
return $qb;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function requiredRole()
|
public function requiredRole()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user