fix deprecations: use fqcn instead of 'choices'

This commit is contained in:
nobohan
2018-04-04 15:55:45 +02:00
parent 65e7354437
commit b84ee91652
7 changed files with 100 additions and 92 deletions

View File

@@ -3,17 +3,17 @@
/*
* Chill is a software for social workers
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -31,9 +31,10 @@ use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
use Symfony\Bridge\Twig\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
*
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Marc Ducobu <marc@champs-libes.coop>
@@ -49,7 +50,7 @@ class CustomFieldChoice extends AbstractCustomField
private $defaultLocales;
/**
*
*
* @var TwigEngine
*/
private $templating;
@@ -58,9 +59,9 @@ class CustomFieldChoice extends AbstractCustomField
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/
private $translatableStringHelper;
public function __construct(
Translator $translator,
Translator $translator,
TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper)
{
@@ -68,7 +69,7 @@ class CustomFieldChoice extends AbstractCustomField
$this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
}
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
{
//prepare choices
@@ -80,7 +81,7 @@ class CustomFieldChoice extends AbstractCustomField
$choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']);
}
}
//prepare $options
$options = array(
'multiple' => $customFieldOptions[self::MULTIPLE],
@@ -104,13 +105,13 @@ class CustomFieldChoice extends AbstractCustomField
new ChoiceWithOtherType($otherValueLabel),
$options)
->addModelTransformer(new CustomFieldDataTransformer($this, $customField)));
} else { //if allow_other = false
//we add the 'expanded' to options
$options['expanded'] = $customFieldOptions[self::EXPANDED];
$builder->add(
$builder->create($customField->getSlug(), 'choice', $options)
$builder->create($customField->getSlug(), ChoiceType::class, $options)
->addModelTransformer(new CustomFieldDataTransformer($this, $customField))
);
}
@@ -119,7 +120,7 @@ class CustomFieldChoice extends AbstractCustomField
public function buildOptionsForm(FormBuilderInterface $builder)
{
$builder
->add(self::MULTIPLE, 'choice', array(
->add(self::MULTIPLE, ChoiceType::class, array(
'expanded' => true,
'multiple' => false,
'choices' => array(
@@ -128,7 +129,7 @@ class CustomFieldChoice extends AbstractCustomField
'empty_data' => '0',
'label' => 'Multiplicity'
))
->add(self::EXPANDED, 'choice', array(
->add(self::EXPANDED, ChoiceType::class, array(
'expanded' => true,
'multiple' => false,
'choices' => array(
@@ -137,7 +138,7 @@ class CustomFieldChoice extends AbstractCustomField
'empty_data' => '0',
'label' => 'Choice display'
))
->add(self::ALLOW_OTHER, 'choice', array(
->add(self::ALLOW_OTHER, ChoiceType::class, array(
'label' => 'Allow other',
'choices' => array(
'0' => 'No',
@@ -152,7 +153,7 @@ class CustomFieldChoice extends AbstractCustomField
'type' => new ChoicesListType($this->defaultLocales),
'allow_add' => true
));
return $builder;
}
@@ -160,7 +161,7 @@ class CustomFieldChoice extends AbstractCustomField
{
// we always have to adapt to what the current data should be
$options = $customField->getOptions();
if ($options[self::MULTIPLE]) {
return $this->deserializeToMultiple($serialized, $options[self::ALLOW_OTHER]);
} else {
@@ -168,62 +169,62 @@ class CustomFieldChoice extends AbstractCustomField
}
return $serialized;
}
private function deserializeToUnique($serialized, $allowOther)
{
$value = $this->guessValue($serialized);
// set in a single value. We must have a single string
$fixedValue = is_array($value) ?
$fixedValue = is_array($value) ?
// check if the array has an element, if not replace by empty string
count($value) > 0 ? end($value) : ''
:
count($value) > 0 ? end($value) : ''
:
$value;
if ($allowOther) {
return $this->deserializeWithAllowOther($serialized, $fixedValue);
} else {
return $fixedValue;
}
}
/**
* deserialized the data from the database to a multiple
* deserialized the data from the database to a multiple
* field
*
*
* @param mixed $serialized
* @param boolean $allowOther
*/
private function deserializeToMultiple($serialized, $allowOther)
{
$value = $this->guessValue($serialized);
// set in an array : we want a multiple
$fixedValue = is_array($value) ? $value : array($value);
if ($allowOther) {
return $this->deserializeWithAllowOther($serialized, $fixedValue);
} else {
return $fixedValue;
}
}
private function deserializeWithAllowOther($serialized, $value)
{
$existingOther = isset($serialized['_other']) ? $serialized['_other'] : '';
return array(
'_other' => $existingOther,
'_choices' => $value
);
}
/**
* Guess the value from the representation of it.
*
*
* If the value had an 'allow_other' = true option, the returned value
* **is not** the content of the _other field, but the `_other` string.
*
*
* @param array|string $value
* @return mixed
* @throws \LogicException if the case is not covered by this
@@ -233,7 +234,7 @@ class CustomFieldChoice extends AbstractCustomField
if ($value === NULL) {
return NULL;
}
if (!is_array($value)) {
return $value;
} else {
@@ -245,7 +246,7 @@ class CustomFieldChoice extends AbstractCustomField
return $value;
}
}
throw \LogicException("This case is not expected.");
}
@@ -253,13 +254,13 @@ class CustomFieldChoice extends AbstractCustomField
{
return 'Choices';
}
public function isEmptyValue($value, CustomField $customField)
{
if ($value === NULL) {
return true;
}
// if multiple choice OR multiple/single choice with other
if (is_array($value))
{
@@ -284,7 +285,7 @@ class CustomFieldChoice extends AbstractCustomField
}
/**
*
*
* @internal this function is able to receive data whichever is the value of "other", "multiple"
* @param mixed $value
* @param CustomField $customField
@@ -294,13 +295,13 @@ class CustomFieldChoice extends AbstractCustomField
{
//extract the data. They are under a _choice key if they are stored with allow_other
$data = (isset($value['_choices'])) ? $value['_choices'] : $value;
$selected = (is_array($data)) ? $data : array($data);
$selected = (is_array($data)) ? $data : array($data);
$choices = $customField->getOptions()[self::CHOICES];
if (in_array('_other', $selected)){
$choices[] = array('name' => $value['_other'], 'slug' => '_other');
}
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig';
if($documentType == 'csv') {
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig';
@@ -309,7 +310,7 @@ class CustomFieldChoice extends AbstractCustomField
return $this->templating
->render($template,
array(
'choices' => $choices,
'choices' => $choices,
'selected' => $selected,
'multiple' => $customField->getOptions()[self::MULTIPLE],
'expanded' => $customField->getOptions()[self::EXPANDED],