Fix implicit variable creation.

This commit is contained in:
Pol Dellaiera
2021-11-09 15:25:45 +01:00
parent a1b381a3ea
commit 5e5dcaefe7
21 changed files with 464 additions and 581 deletions

View File

@@ -1,6 +1,6 @@
<?php
/*
*
*
*/
namespace Chill\ReportBundle\Export\Export;
@@ -27,7 +27,7 @@ use Chill\MainBundle\Entity\User;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
/**
*
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
@@ -38,27 +38,27 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
* @var CustomFieldsGroup
*/
protected $customfieldsGroup;
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var CustomFieldProvider
*/
protected $customFieldProvider;
protected $em;
protected $fields = array(
'person_id', 'person_firstName', 'person_lastName', 'person_birthdate',
'person_placeOfBirth', 'person_gender', 'person_memo', 'person_email', 'person_phonenumber',
@@ -67,11 +67,11 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
'person_address_postcode_code', 'person_address_country_name', 'person_address_country_code',
'report_id', 'report_user', 'report_date', 'report_scope'
);
protected $slugs = [];
function __construct(
CustomFieldsGroup $customfieldsGroup,
CustomFieldsGroup $customfieldsGroup,
TranslatableStringHelper $translatableStringHelper,
TranslatorInterface $translator,
CustomFieldProvider $customFieldProvider,
@@ -84,18 +84,18 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
$this->em = $em;
}
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
{
$choices = array_combine($this->fields, $this->fields);
foreach ($this->getCustomFields() as $cf) {
$choices
[$this->translatableStringHelper->localize($cf->getName())]
=
[$this->translatableStringHelper->localize($cf->getName())]
=
$cf->getSlug();
}
// Add a checkbox to select fields
$builder->add('fields', ChoiceType::class, array(
'multiple' => true,
@@ -133,7 +133,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
}
))]
));
// add a date field for addresses
$builder->add('address_date', ChillDateType::class, array(
'label' => "Address valid at this date",
@@ -142,14 +142,14 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
'block_name' => 'list_export_form_address_date'
));
}
public function validateForm($data, ExecutionContextInterface $context)
{
// get the field starting with address_
$addressFields = array_filter(function($el) {
return substr($el, 0, 8) === 'address_';
}, $this->fields);
// check if there is one field starting with address in data
if (count(array_intersect($data['fields'], $addressFields)) > 0) {
// if a field address is checked, the date must not be empty
@@ -161,10 +161,10 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
}
}
}
/**
* Get custom fields associated with person
*
*
* @return CustomField[]
*/
private function getCustomFields()
@@ -184,7 +184,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
{
return $this->translator->trans(
"Generate list of report '%type%'",
[
[
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName())
]
);
@@ -192,13 +192,12 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
/**
* {@inheritDoc}
*
*
* @param type $key
* @param array $values
* @param type $data
* @return type
*/
public function getLabels($key, array $values, $data)
public function getLabels($key, array $values, $data): \Closure
{
switch ($key) {
case 'person_birthdate':
@@ -206,27 +205,27 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
// for birthdate or report date, we have to transform the string into a date
// to format the date correctly.
return function($value) use ($key) {
if ($value === '_header') {
return $key === 'person_birthdate' ? 'birthdate' : 'report_date';
if ($value === '_header') {
return $key === 'person_birthdate' ? 'birthdate' : 'report_date';
}
if (empty($value))
{
return "";
}
if ($key === 'person_birthdate') {
$date = \DateTime::createFromFormat('Y-m-d', $value);
} else {
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $value);
}
// check that the creation could occurs.
if ($date === false) {
if ($date === false) {
throw new \Exception(sprintf("The value %s could "
. "not be converted to %s", $value, \DateTime::class));
}
return $date->format('d-m-Y');
return $date->format('d-m-Y');
};
case 'report_scope':
$qb = $this->em->getRepository(Scope::class)
@@ -236,17 +235,18 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
->where($qb->expr()->in('s.id', $values))
;
$rows = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
$scopes = [];
foreach($rows as $row) {
$scopes[$row['id']] = $this->translatableStringHelper
->localize($row['name']);
$scopes[$row['id']] = $this->translatableStringHelper->localize($row['name']);
}
return function($value) use ($scopes) {
return function($value) use ($scopes): string {
if ($value === '_header') {
return 'circle';
}
return $scopes[$value];
};
case 'report_user':
@@ -257,71 +257,73 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
->where($qb->expr()->in('u.id', $values))
;
$rows = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
$users = [];
foreach($rows as $row) {
$users[$row['id']] = $row['username'];
}
return function($value) use ($users) {
return function($value) use ($users): string {
if ($value === '_header') {
return 'user';
}
return $users[$value];
};
case 'person_gender' :
// for gender, we have to translate men/women statement
return function($value) {
if ($value === '_header') { return 'gender'; }
return $this->translator->trans($value);
};
case 'person_countryOfBirth':
case 'person_nationality':
$countryRepository = $this->em
->getRepository('ChillMainBundle:Country');
// load all countries in a single query
$countryRepository->findBy(array('countryCode' => $values));
return function($value) use ($key, $countryRepository) {
if ($value === '_header') { return \strtolower($key); }
if ($value === NULL) {
if ($value === NULL) {
return $this->translator->trans('no data');
}
$country = $countryRepository->find($value);
return $this->translatableStringHelper->localize(
$country->getName());
};
case 'person_address_country_name':
return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
if ($value === NULL) {
return '';
}
return $this->translatableStringHelper->localize(json_decode($value, true));
};
default:
// for fields which are associated with person
if (in_array($key, $this->fields)) {
return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
if ($value === '_header') { return \strtolower($key); }
return $value;
return $value;
};
} else {
return $this->getLabelForCustomField($key, $values, $data);
}
}
}
private function getLabelForCustomField($key, array $values, $data)
{
// for fields which are custom fields
@@ -329,38 +331,38 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
$cf = $this->em
->getRepository(CustomField::class)
->findOneBy(array('slug' => $this->DQLToSlug($key)));
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
$defaultFunction = function($value) use ($cf) {
if ($value === '_header') {
return $this->translatableStringHelper->localize($cf->getName());
return $this->translatableStringHelper->localize($cf->getName());
}
return $this->customFieldProvider
->getCustomFieldByType($cf->getType())
->render(json_decode($value, true), $cf, 'csv');
};
if ($cfType instanceof CustomFieldChoice and $cfType->isMultiple($cf)) {
return function($value) use ($cf, $cfType, $key) {
$slugChoice = $this->extractInfosFromSlug($key)['additionnalInfos']['choiceSlug'];
$decoded = \json_decode($value, true);
if ($value === '_header') {
$label = $cfType->getChoices($cf)[$slugChoice];
return $this->translatableStringHelper->localize($cf->getName())
.' | '.$label;
}
if ($slugChoice === '_other' and $cfType->isChecked($cf, $choiceSlug, $decoded)) {
return $cfType->extractOtherValue($cf, $decoded);
} else {
return $cfType->isChecked($cf, $slugChoice, $decoded);
}
};
} else {
return $defaultFunction;
}
@@ -369,44 +371,44 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
public function getQueryKeys($data)
{
$fields = array();
foreach ($data['fields'] as $key) {
if (in_array($key, $this->fields)) {
$fields[] = $key;
}
}
// add the key from slugs and return
return \array_merge($fields, \array_keys($this->slugs));
}
/**
* clean a slug to be usable by DQL
*
* @param string $slugsanitize
*
* @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 = [])
{
$uid = 'slug_'.\uniqid();
$this->slugs[$uid] = [
'slug' => $slug,
'type' => $type,
'additionnalInfos' => $additionalInfos
];
return $uid;
}
private function DQLToSlug($cleanedSlug)
{
{
return $this->slugs[$cleanedSlug]['slug'];
}
/**
*
*
* @param type $cleanedSlug
* @return an array with keys = 'slug', 'type', 'additionnalInfo'
*/
@@ -424,7 +426,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
{
return $this->translator->trans(
"List for report '%type%'",
[
[
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName())
]
);
@@ -438,22 +440,22 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
{
$centers = array_map(function($el) { return $el['center']; }, $acl);
// throw an error if any fields are present
if (!\array_key_exists('fields', $data)) {
throw new \Doctrine\DBAL\Exception\InvalidArgumentException("any fields "
. "have been checked");
}
$qb = $this->em->createQueryBuilder();
// process fields which are not custom fields
foreach ($this->fields as $f) {
// do not add fields which are not selected
if (!\in_array($f, $data['fields'])) {
continue;
}
// add a column to the query for each field
switch ($f) {
case 'person_countryOfBirth':
@@ -470,11 +472,11 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
case 'person_address_country_code':
// remove 'person_'
$suffix = \substr($f, 7);
$qb->addSelect(sprintf(
'GET_PERSON_ADDRESS_%s(person.id, :address_date) AS %s',
// get the part after address_
strtoupper(substr($suffix, 8)),
strtoupper(substr($suffix, 8)),
$f));
$qb->setParameter('address_date', $data['address_date']);
break;
@@ -487,7 +489,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
default:
$prefix = \substr($f, 0, 7);
$suffix = \substr($f, 7);
switch($prefix) {
case 'person_':
$qb->addSelect(sprintf('person.%s as %s', $suffix, $f));
@@ -502,22 +504,22 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
}
}
// process fields which are custom fields
foreach ($this->getCustomFields() as $cf) {
// do not add custom fields which are not selected
if (!\in_array($cf->getSlug(), $data['fields'])) {
continue;
}
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
// if is multiple, split into multiple columns
if ($cfType instanceof CustomFieldChoice and $cfType->isMultiple($cf)) {
foreach($cfType->getChoices($cf) as $choiceSlug => $label) {
$slug = $this->slugToDQL($cf->getSlug(), 'choice', [ 'choiceSlug' => $choiceSlug ]);
$qb->addSelect(
sprintf('GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
sprintf('GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
}
@@ -525,12 +527,12 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
// not multiple, add a single column
$slug = $this->slugToDQL($cf->getSlug());
$qb->addSelect(
sprintf('GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
sprintf('GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
}
}
$qb
->from(Report::class, 'report')
->leftJoin('report.person', 'person')
@@ -540,7 +542,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
->andWhere('center IN (:authorized_centers)')
->setParameter('authorized_centers', $centers);
;
return $qb;
}