mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
render custom fields choice in differents columns
This commit is contained in:
parent
7c8f095de3
commit
c0834353d5
@ -21,6 +21,7 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|||||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||||
|
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a list of peoples
|
* Render a list of peoples
|
||||||
@ -61,6 +62,8 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
|
|||||||
'address_postcode_code', 'address_country_name', 'address_country_code'
|
'address_postcode_code', 'address_country_name', 'address_country_code'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private $slugs = [];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
TranslatorInterface $translator,
|
TranslatorInterface $translator,
|
||||||
@ -263,25 +266,55 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
|
|||||||
|
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// for fields which are custom fields
|
return $this->getLabelForCustomField($key, $values, $data);
|
||||||
/* @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');
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}
|
* {@inheritDoc}
|
||||||
@ -296,29 +329,46 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface
|
|||||||
foreach ($data['fields'] as $key) {
|
foreach ($data['fields'] as $key) {
|
||||||
if (in_array($key, $this->fields)) {
|
if (in_array($key, $this->fields)) {
|
||||||
$fields[] = $key;
|
$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
|
* 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
|
* @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)
|
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) {
|
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());
|
$slug = $this->slugToDQL($cf->getSlug());
|
||||||
$qb->addSelect(
|
$qb->addSelect(
|
||||||
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
|
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
|
||||||
$slug, $slug));
|
$slug, $slug));
|
||||||
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
|
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
|
||||||
//$qb->setParameter(sprintf('name%s', $slug), $cf->getSlug());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user