Commit c0834353 authored by Julien Fastré's avatar Julien Fastré

render custom fields choice in differents columns

parent 7c8f095d
......@@ -21,6 +21,7 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
/**
* Render a list of peoples
......@@ -61,6 +62,8 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
'address_postcode_code', 'address_country_name', 'address_country_code'
);
private $slugs = [];
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
......@@ -263,25 +266,55 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
};
} else {
// for fields which are custom fields
/* @var $cf CustomField */
$cf = $this->entityManager
->getRepository(CustomField::class)
->findOneBy(array('slug' => $this->DQLToSlug($key)));
return 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');
};
return $this->getLabelForCustomField($key, $values, $data);
}
}
}
private function getLabelForCustomField($key, array $values, $data)
{
// for fields which are custom fields
/* @var $cf CustomField */
$cf = $this->entityManager
->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->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);
dump($decoded);
dump($slugChoice);
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;
}
}
/**
* {@inheritDoc}
......@@ -296,29 +329,46 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
foreach ($data['fields'] as $key) {
if (in_array($key, $this->fields)) {
$fields[] = $key;
} else {
// this should be a slug from custom field, we have to clean it
$fields[] = $this->slugToDQL($key);
}
}
return $fields;
// 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 $type the type of the customfield, if required (currently only for choices)
* @return string
*/
private function slugToDQL($slug)
private function slugToDQL($slug, $type = "default", array $additionalInfos = [])
{
return "cf____".\str_replace("-", "____", $slug);
$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'
*/
private function extractInfosFromSlug($slug)
{
return \str_replace("____", "-", \substr($cleanedSlug, 6));
return $this->slugs[$slug];
}
/**
......@@ -394,13 +444,21 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
}
foreach ($this->getCustomFields() as $cf) {
if (in_array($cf->getSlug(), $data['fields'])) {
$cfType = $this->customFieldProvider->getCustomFieldByType($cf->getType());
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(person.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
}
} else {
$slug = $this->slugToDQL($cf->getSlug());
$qb->addSelect(
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
//$qb->setParameter(sprintf('name%s', $slug), $cf->getSlug());
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment