mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,42 +1,56 @@
|
||||
<?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\ReportBundle\Export\Export;
|
||||
|
||||
use Chill\MainBundle\Export\ListInterface;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Chill\MainBundle\Export\ListInterface;
|
||||
use Chill\MainBundle\Form\Type\ChillDateType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\ReportBundle\Security\Authorization\ReportVoter;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Chill\ReportBundle\Security\Authorization\ReportVoter;
|
||||
use Closure;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Exception;
|
||||
use LogicException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Chill\MainBundle\Form\Type\ChillDateType;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Doctrine\ORM\Query;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function array_merge;
|
||||
use function strlen;
|
||||
use function strtolower;
|
||||
use function ucfirst;
|
||||
use function uniqid;
|
||||
|
||||
class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
{
|
||||
protected CustomFieldsGroup $customfieldsGroup;
|
||||
|
||||
protected TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
protected CustomFieldProvider $customFieldProvider;
|
||||
|
||||
protected CustomFieldsGroup $customfieldsGroup;
|
||||
|
||||
protected EntityManagerInterface $em;
|
||||
|
||||
protected array $fields = [
|
||||
@@ -45,12 +59,16 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
'person_countryOfBirth', 'person_nationality', 'person_address_street_address_1',
|
||||
'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',
|
||||
'report_id', 'report_user', 'report_date', 'report_scope'
|
||||
'report_id', 'report_user', 'report_date', 'report_scope',
|
||||
];
|
||||
|
||||
protected array $slugs = [];
|
||||
|
||||
function __construct(
|
||||
protected TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
CustomFieldsGroup $customfieldsGroup,
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
@@ -64,14 +82,12 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$choices = array_combine($this->fields, $this->fields);
|
||||
$choices = array_combine($this->fields, $this->fields);
|
||||
|
||||
foreach ($this->getCustomFields() as $cf) {
|
||||
$choices
|
||||
[$this->translatableStringHelper->localize($cf->getName())]
|
||||
$choices[$this->translatableStringHelper->localize($cf->getName())]
|
||||
=
|
||||
$cf->getSlug();
|
||||
}
|
||||
@@ -81,8 +97,8 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => $choices,
|
||||
'label' => 'Fields to include in export',
|
||||
'choice_attr' => function($val, $key, $index) {
|
||||
'label' => 'Fields to include in export',
|
||||
'choice_attr' => function ($val, $key, $index) {
|
||||
// add a 'data-display-target' for address fields
|
||||
if (substr($val, 0, 8) === 'address_') {
|
||||
return ['data-display-target' => 'address_date'];
|
||||
@@ -90,72 +106,41 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
|
||||
return [];
|
||||
},
|
||||
'choice_label' => function(string $key, string $label): string {
|
||||
'choice_label' => function (string $key, string $label): string {
|
||||
switch (\substr($key, 0, 7)) {
|
||||
case 'person_':
|
||||
return $this->translator->trans(\substr($key, 7, \strlen($key) - 7)).
|
||||
' ('.$this->translator->trans('Person').')';
|
||||
return $this->translator->trans(\substr($key, 7, strlen($key) - 7)) .
|
||||
' (' . $this->translator->trans('Person') . ')';
|
||||
|
||||
case 'report_':
|
||||
return $this->translator->trans(\ucfirst(\substr($key, 7, \strlen($key) - 7))).
|
||||
' ('.$this->translator->trans('Report').')';
|
||||
return $this->translator->trans(ucfirst(\substr($key, 7, strlen($key) - 7))) .
|
||||
' (' . $this->translator->trans('Report') . ')';
|
||||
|
||||
default:
|
||||
return $label.
|
||||
' ('.$this->translator->trans("Report's question").')';;
|
||||
return $label .
|
||||
' (' . $this->translator->trans("Report's question") . ')';
|
||||
}
|
||||
},
|
||||
'constraints' => [new Callback([
|
||||
'callback' => function($selected, ExecutionContextInterface $context) {
|
||||
'callback' => function ($selected, ExecutionContextInterface $context) {
|
||||
if (count($selected) === 0) {
|
||||
$context->buildViolation('You must select at least one element')
|
||||
->atPath('fields')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
])]
|
||||
},
|
||||
])],
|
||||
]);
|
||||
|
||||
// add a date field for addresses
|
||||
$builder->add('address_date', ChillDateType::class, [
|
||||
'label' => "Address valid at this date",
|
||||
'data' => new \DateTime(),
|
||||
'label' => 'Address valid at this date',
|
||||
'data' => new DateTime(),
|
||||
'required' => false,
|
||||
'block_name' => 'list_export_form_address_date'
|
||||
'block_name' => 'list_export_form_address_date',
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
// get the field starting with address_
|
||||
$addressFields = array_filter(
|
||||
$this->fields,
|
||||
static fn(string $el): bool => substr($el, 0, 8) === 'address_'
|
||||
);
|
||||
|
||||
// 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
|
||||
if (empty($data['address_date'])) {
|
||||
$context
|
||||
->buildViolation("You must set this date if an address is checked")
|
||||
->atPath('address_date')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom fields associated with person
|
||||
*
|
||||
* @return CustomField[]
|
||||
*/
|
||||
private function getCustomFields()
|
||||
{
|
||||
return \array_filter($this->customfieldsGroup
|
||||
->getCustomFields()->toArray(), function(CustomField $cf) {
|
||||
return $cf->getType() !== 'title';
|
||||
});
|
||||
}
|
||||
|
||||
public function getAllowedFormattersTypes()
|
||||
{
|
||||
return [FormatterInterface::TYPE_LIST];
|
||||
@@ -166,92 +151,95 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
return $this->translator->trans(
|
||||
"Generate list of report '%type%'",
|
||||
[
|
||||
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName())
|
||||
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data): \Closure
|
||||
public function getLabels($key, array $values, $data): Closure
|
||||
{
|
||||
switch ($key) {
|
||||
case 'person_birthdate':
|
||||
case 'report_date':
|
||||
// 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';
|
||||
return function ($value) use ($key) {
|
||||
if ('_header' === $value) {
|
||||
return 'person_birthdate' === $key ? 'birthdate' : 'report_date';
|
||||
}
|
||||
|
||||
if (empty($value))
|
||||
{
|
||||
return "";
|
||||
if (empty($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($key === 'person_birthdate') {
|
||||
$date = \DateTime::createFromFormat('Y-m-d', $value);
|
||||
if ('person_birthdate' === $key) {
|
||||
$date = DateTime::createFromFormat('Y-m-d', $value);
|
||||
} else {
|
||||
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $value);
|
||||
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value);
|
||||
}
|
||||
// check that the creation could occurs.
|
||||
if ($date === false) {
|
||||
throw new \Exception(sprintf("The value %s could "
|
||||
. "not be converted to %s", $value, \DateTime::class));
|
||||
if (false === $date) {
|
||||
throw new Exception(sprintf('The value %s could '
|
||||
. 'not be converted to %s', $value, DateTime::class));
|
||||
}
|
||||
|
||||
return $date->format('d-m-Y');
|
||||
};
|
||||
|
||||
case 'report_scope':
|
||||
$qb = $this->em->getRepository(Scope::class)
|
||||
->createQueryBuilder('s');
|
||||
$qb->addSelect('s.name')
|
||||
->addSelect('s.id')
|
||||
->where($qb->expr()->in('s.id', $values))
|
||||
;
|
||||
->where($qb->expr()->in('s.id', $values));
|
||||
$rows = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
$scopes = [];
|
||||
|
||||
foreach($rows as $row) {
|
||||
foreach ($rows as $row) {
|
||||
$scopes[$row['id']] = $this->translatableStringHelper->localize($row['name']);
|
||||
}
|
||||
|
||||
return function($value) use ($scopes): string {
|
||||
if ($value === '_header') {
|
||||
return function ($value) use ($scopes): string {
|
||||
if ('_header' === $value) {
|
||||
return 'circle';
|
||||
}
|
||||
|
||||
return $scopes[$value];
|
||||
};
|
||||
|
||||
case 'report_user':
|
||||
$qb = $this->em->getRepository(User::class)
|
||||
->createQueryBuilder('u');
|
||||
$qb->addSelect('u.username')
|
||||
->addSelect('u.id')
|
||||
->where($qb->expr()->in('u.id', $values))
|
||||
;
|
||||
->where($qb->expr()->in('u.id', $values));
|
||||
$rows = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
$users = [];
|
||||
|
||||
foreach($rows as $row) {
|
||||
foreach ($rows as $row) {
|
||||
$users[$row['id']] = $row['username'];
|
||||
}
|
||||
|
||||
return function($value) use ($users): string {
|
||||
if ($value === '_header') {
|
||||
return function ($value) use ($users): string {
|
||||
if ('_header' === $value) {
|
||||
return 'user';
|
||||
}
|
||||
|
||||
return $users[$value];
|
||||
};
|
||||
case 'person_gender' :
|
||||
|
||||
case 'person_gender':
|
||||
// for gender, we have to translate men/women statement
|
||||
return function($value) {
|
||||
if ($value === '_header') { return 'gender'; }
|
||||
return function ($value) {
|
||||
if ('_header' === $value) {
|
||||
return 'gender';
|
||||
}
|
||||
|
||||
return $this->translator->trans($value);
|
||||
};
|
||||
|
||||
case 'person_countryOfBirth':
|
||||
case 'person_nationality':
|
||||
$countryRepository = $this->em
|
||||
@@ -260,86 +248,49 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
// load all countries in a single query
|
||||
$countryRepository->findBy(['countryCode' => $values]);
|
||||
|
||||
return function($value) use ($key, $countryRepository) {
|
||||
if ($value === '_header') { return \strtolower($key); }
|
||||
return function ($value) use ($key, $countryRepository) {
|
||||
if ('_header' === $value) {
|
||||
return strtolower($key);
|
||||
}
|
||||
|
||||
if ($value === NULL) {
|
||||
if (null === $value) {
|
||||
return $this->translator->trans('no data');
|
||||
}
|
||||
|
||||
$country = $countryRepository->find($value);
|
||||
|
||||
return $this->translatableStringHelper->localize(
|
||||
$country->getName());
|
||||
$country->getName()
|
||||
);
|
||||
};
|
||||
case 'person_address_country_name':
|
||||
return function($value) use ($key) {
|
||||
if ($value === '_header') { return \strtolower($key); }
|
||||
|
||||
if ($value === NULL) {
|
||||
case 'person_address_country_name':
|
||||
return function ($value) use ($key) {
|
||||
if ('_header' === $value) {
|
||||
return strtolower($key);
|
||||
}
|
||||
|
||||
if (null === $value) {
|
||||
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); }
|
||||
return function ($value) use ($key) {
|
||||
if ('_header' === $value) {
|
||||
return strtolower($key);
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
return $this->getLabelForCustomField($key, $values, $data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function getLabelForCustomField($key, array $values, $data)
|
||||
{
|
||||
// for fields which are custom fields
|
||||
/* @var $cf CustomField */
|
||||
$cf = $this->em
|
||||
->getRepository(CustomField::class)
|
||||
->findOneBy(['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->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);
|
||||
}
|
||||
|
||||
return $cfType->isChecked($cf, $slugChoice, $decoded);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
return $defaultFunction;
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
@@ -353,30 +304,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
}
|
||||
|
||||
// add the key from slugs and return
|
||||
return \array_merge($fields, \array_keys($this->slugs));
|
||||
}
|
||||
|
||||
private function slugToDQL($slug, $type = "default", array $additionalInfos = [])
|
||||
{
|
||||
$uid = 'slug_' . \uniqid('', true);
|
||||
|
||||
$this->slugs[$uid] = [
|
||||
'slug' => $slug,
|
||||
'type' => $type,
|
||||
'additionnalInfos' => $additionalInfos
|
||||
];
|
||||
|
||||
return $uid;
|
||||
}
|
||||
|
||||
private function DQLToSlug($cleanedSlug)
|
||||
{
|
||||
return $this->slugs[$cleanedSlug]['slug'];
|
||||
}
|
||||
|
||||
private function extractInfosFromSlug($slug)
|
||||
{
|
||||
return $this->slugs[$slug];
|
||||
return array_merge($fields, array_keys($this->slugs));
|
||||
}
|
||||
|
||||
public function getResult($query, $data)
|
||||
@@ -389,7 +317,7 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
return $this->translator->trans(
|
||||
"List for report '%type%'",
|
||||
[
|
||||
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName())
|
||||
'%type%' => $this->translatableStringHelper->localize($this->customfieldsGroup->getName()),
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -401,12 +329,12 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
|
||||
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);
|
||||
|
||||
// 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");
|
||||
if (!array_key_exists('fields', $data)) {
|
||||
throw new \Doctrine\DBAL\Exception\InvalidArgumentException('any fields '
|
||||
. 'have been checked');
|
||||
}
|
||||
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
@@ -424,7 +352,9 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
case 'person_nationality':
|
||||
$suffix = \substr($f, 7);
|
||||
$qb->addSelect(sprintf('IDENTITY(person.%s) as %s', $suffix, $f));
|
||||
|
||||
break;
|
||||
|
||||
case 'person_address_street_address_1':
|
||||
case 'person_address_street_address_2':
|
||||
case 'person_address_valid_from':
|
||||
@@ -439,32 +369,42 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
'GET_PERSON_ADDRESS_%s(person.id, :address_date) AS %s',
|
||||
// get the part after address_
|
||||
strtoupper(substr($suffix, 8)),
|
||||
$f));
|
||||
$f
|
||||
));
|
||||
$qb->setParameter('address_date', $data['address_date']);
|
||||
|
||||
break;
|
||||
|
||||
case 'report_scope':
|
||||
$qb->addSelect(sprintf('IDENTITY(report.scope) AS %s', 'report_scope'));
|
||||
|
||||
break;
|
||||
|
||||
case 'report_user':
|
||||
$qb->addSelect(sprintf('IDENTITY(report.user) AS %s', 'report_user'));
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$prefix = \substr($f, 0, 7);
|
||||
$suffix = \substr($f, 7);
|
||||
|
||||
switch($prefix) {
|
||||
switch ($prefix) {
|
||||
case 'person_':
|
||||
$qb->addSelect(sprintf('person.%s as %s', $suffix, $f));
|
||||
|
||||
break;
|
||||
|
||||
case 'report_':
|
||||
$qb->addSelect(sprintf('report.%s as %s', $suffix, $f));
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \LogicException("this prefix $prefix should "
|
||||
. "not be encountered. Full field: $f");
|
||||
throw new LogicException("this prefix {$prefix} should "
|
||||
. "not be encountered. Full field: {$f}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// process fields which are custom fields
|
||||
@@ -478,19 +418,27 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
|
||||
// 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 ]);
|
||||
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',
|
||||
$slug, $slug));
|
||||
sprintf(
|
||||
'GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
|
||||
$slug,
|
||||
$slug
|
||||
)
|
||||
);
|
||||
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
|
||||
}
|
||||
} else {
|
||||
// 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',
|
||||
$slug, $slug));
|
||||
sprintf(
|
||||
'GET_JSON_FIELD_BY_KEY(report.cFData, :slug%s) AS %s',
|
||||
$slug,
|
||||
$slug
|
||||
)
|
||||
);
|
||||
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
|
||||
}
|
||||
}
|
||||
@@ -514,4 +462,102 @@ class ReportList implements ListInterface, ExportElementValidatedInterface
|
||||
{
|
||||
return [Declarations::PERSON_IMPLIED_IN, Declarations::PERSON_TYPE, 'report'];
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
// get the field starting with address_
|
||||
$addressFields = array_filter(
|
||||
$this->fields,
|
||||
static fn (string $el): bool => substr($el, 0, 8) === 'address_'
|
||||
);
|
||||
|
||||
// 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
|
||||
if (empty($data['address_date'])) {
|
||||
$context
|
||||
->buildViolation('You must set this date if an address is checked')
|
||||
->atPath('address_date')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function DQLToSlug($cleanedSlug)
|
||||
{
|
||||
return $this->slugs[$cleanedSlug]['slug'];
|
||||
}
|
||||
|
||||
private function extractInfosFromSlug($slug)
|
||||
{
|
||||
return $this->slugs[$slug];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom fields associated with person.
|
||||
*
|
||||
* @return CustomField[]
|
||||
*/
|
||||
private function getCustomFields()
|
||||
{
|
||||
return \array_filter($this->customfieldsGroup
|
||||
->getCustomFields()->toArray(), function (CustomField $cf) {
|
||||
return $cf->getType() !== 'title';
|
||||
});
|
||||
}
|
||||
|
||||
private function getLabelForCustomField($key, array $values, $data)
|
||||
{
|
||||
// for fields which are custom fields
|
||||
/* @var $cf CustomField */
|
||||
$cf = $this->em
|
||||
->getRepository(CustomField::class)
|
||||
->findOneBy(['slug' => $this->DQLToSlug($key)]);
|
||||
|
||||
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
|
||||
$defaultFunction = function ($value) use ($cf) {
|
||||
if ('_header' === $value) {
|
||||
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 ('_header' === $value) {
|
||||
$label = $cfType->getChoices($cf)[$slugChoice];
|
||||
|
||||
return $this->translatableStringHelper->localize($cf->getName())
|
||||
. ' | ' . $label;
|
||||
}
|
||||
|
||||
if ('_other' === $slugChoice and $cfType->isChecked($cf, $choiceSlug, $decoded)) {
|
||||
return $cfType->extractOtherValue($cf, $decoded);
|
||||
}
|
||||
|
||||
return $cfType->isChecked($cf, $slugChoice, $decoded);
|
||||
};
|
||||
}
|
||||
|
||||
return $defaultFunction;
|
||||
}
|
||||
|
||||
private function slugToDQL($slug, $type = 'default', array $additionalInfos = [])
|
||||
{
|
||||
$uid = 'slug_' . uniqid('', true);
|
||||
|
||||
$this->slugs[$uid] = [
|
||||
'slug' => $slug,
|
||||
'type' => $type,
|
||||
'additionnalInfos' => $additionalInfos,
|
||||
];
|
||||
|
||||
return $uid;
|
||||
}
|
||||
}
|
||||
|
@@ -1,49 +1,46 @@
|
||||
<?php
|
||||
/*
|
||||
*/
|
||||
namespace Chill\ReportBundle\Export\Export;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Chill\MainBundle\Export\ExportElementsProviderInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
|
||||
/**
|
||||
*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ReportBundle\Export\Export;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\MainBundle\Export\ExportElementsProviderInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\ReportBundle\Entity\Report;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class ReportListProvider implements ExportElementsProviderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
protected $customFieldProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
function __construct(
|
||||
EntityManagerInterface $em,
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
CustomFieldProvider $customFieldProvider
|
||||
@@ -54,24 +51,22 @@ class ReportListProvider implements ExportElementsProviderInterface
|
||||
$this->customFieldProvider = $customFieldProvider;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getExportElements()
|
||||
{
|
||||
$groups = $this->em->getRepository(CustomFieldsGroup::class)
|
||||
->findBy([ 'entity' => Report::class ])
|
||||
;
|
||||
->findBy(['entity' => Report::class]);
|
||||
$reports = [];
|
||||
|
||||
|
||||
foreach ($groups as $group) {
|
||||
$reports[$group->getId()] = new ReportList(
|
||||
$group,
|
||||
$group,
|
||||
$this->translatableStringHelper,
|
||||
$this->translator,
|
||||
$this->customFieldProvider,
|
||||
$this->em);
|
||||
$this->em
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return $reports;
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +1,21 @@
|
||||
<?php
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\ReportBundle\Export\Filter;
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Chill\MainBundle\Form\Type\ChillDateType;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ReportDateFilter implements FilterInterface
|
||||
{
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return null;
|
||||
@@ -27,15 +24,18 @@ class ReportDateFilter implements FilterInterface
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data)
|
||||
{
|
||||
$where = $qb->getDQLPart('where');
|
||||
$clause = $qb->expr()->between('report.date', ':report_date_filter_date_from',
|
||||
':report_date_filter_date_to');
|
||||
$clause = $qb->expr()->between(
|
||||
'report.date',
|
||||
':report_date_filter_date_from',
|
||||
':report_date_filter_date_to'
|
||||
);
|
||||
|
||||
if ($where instanceof Expr\Andx) {
|
||||
$where->add($clause);
|
||||
} else {
|
||||
$where = $qb->expr()->andX($clause);
|
||||
}
|
||||
|
||||
|
||||
$qb->add('where', $where);
|
||||
$qb->setParameter('report_date_filter_date_from', $data['date_from']);
|
||||
$qb->setParameter('report_date_filter_date_to', $data['date_to']);
|
||||
@@ -48,24 +48,24 @@ class ReportDateFilter implements FilterInterface
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('date_from', ChillDateType::class, array(
|
||||
'label' => "Report is after this date",
|
||||
'data' => new \DateTime(),
|
||||
));
|
||||
|
||||
$builder->add('date_to', ChillDateType::class, array(
|
||||
'label' => "Report is before this date",
|
||||
'data' => new \DateTime(),
|
||||
));
|
||||
$builder->add('date_from', ChillDateType::class, [
|
||||
'label' => 'Report is after this date',
|
||||
'data' => new DateTime(),
|
||||
]);
|
||||
|
||||
$builder->add('date_to', ChillDateType::class, [
|
||||
'label' => 'Report is before this date',
|
||||
'data' => new DateTime(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return array('Filtered by report\'s date: '
|
||||
. 'between %date_from% and %date_to%', array(
|
||||
return ['Filtered by report\'s date: '
|
||||
. 'between %date_from% and %date_to%', [
|
||||
'%date_from%' => $data['date_from']->format('d-m-Y'),
|
||||
'%date_to%' => $data['date_to']->format('d-m-Y')
|
||||
));
|
||||
'%date_to%' => $data['date_to']->format('d-m-Y'),
|
||||
], ];
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
|
Reference in New Issue
Block a user