Merge remote-tracking branch 'origin/upgrade-sf3' into upgrade-sf3

This commit is contained in:
Julien Fastré 2018-07-05 14:02:38 +02:00
commit 5acfbd0cf1
3 changed files with 83 additions and 26 deletions

View File

@ -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,54 @@ 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);
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 +328,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 +443,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());
}
}

View File

@ -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();
}

View File

@ -17,7 +17,7 @@
<ul class="tab-nav">
{% for menu in menus %}
<li class="">
<a href="{{ menu.uri }}" >{{ menu.label }}</a>
<a href="{{ menu.uri }}" >{{ menu.label|upper }}</a>
</li>
{% endfor %}
</ul>