From 7c8f095de3e81390a32fc33467f33130f3e91bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 12 Jun 2018 18:01:14 +0200 Subject: [PATCH 1/4] force uppercase in person menu --- Resources/views/menu.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/menu.html.twig b/Resources/views/menu.html.twig index cd283906b..3c5968b25 100644 --- a/Resources/views/menu.html.twig +++ b/Resources/views/menu.html.twig @@ -17,7 +17,7 @@ \ No newline at end of file From c0834353d545987ef5a0676653e3cf2f6666c146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 12 Jun 2018 18:01:32 +0200 Subject: [PATCH 2/4] render custom fields choice in differents columns --- Export/Export/ListPerson.php | 106 +++++++++++++++++++++++++++-------- 1 file changed, 82 insertions(+), 24 deletions(-) diff --git a/Export/Export/ListPerson.php b/Export/Export/ListPerson.php index 340430f91..0b41c4043 100644 --- a/Export/Export/ListPerson.php +++ b/Export/Export/ListPerson.php @@ -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()); } } From 20b03ee9d138a1693642449a3cd8ff4ea63276ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 13 Jun 2018 16:46:00 +0200 Subject: [PATCH 3/4] remove dump message --- Export/Export/ListPerson.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Export/Export/ListPerson.php b/Export/Export/ListPerson.php index 0b41c4043..4ae95afc0 100644 --- a/Export/Export/ListPerson.php +++ b/Export/Export/ListPerson.php @@ -294,8 +294,7 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface 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]; From 39d70d5d48f457e917a17b89636ad31a3a5e57af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 13 Jun 2018 16:46:20 +0200 Subject: [PATCH 4/4] fix error in validation --- Export/Filter/GenderFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Export/Filter/GenderFilter.php b/Export/Filter/GenderFilter.php index ab551b1f3..dc8697de0 100644 --- a/Export/Filter/GenderFilter.php +++ b/Export/Filter/GenderFilter.php @@ -61,7 +61,7 @@ class GenderFilter implements FilterInterface, public function validateForm($data, ExecutionContextInterface $context) { - if (count($data['accepted_genders']) === 0) { + if ($data['accepted_genders'] === null) { $context->buildViolation("You should select an option") ->addViolation(); }