fix bug in changing custom field option

bug description :

> if you change the options of a custom field choice (i.e. from a
> multiple to a single, removing or adding allow_other), the data
> representation change and do not match with the expected
> representation of the form.

This commit fix this bug by switching the data representation to the
current options.
This commit is contained in:
2015-12-17 23:50:24 +01:00
parent 9867cca632
commit 24f9db6ae7
2 changed files with 453 additions and 0 deletions

View File

@@ -167,8 +167,96 @@ class CustomFieldChoice extends AbstractCustomField
public function deserialize($serialized, CustomField $customField)
{
// 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 {
return $this->deserializeToUnique($serialized, $options[self::ALLOW_OTHER]);
}
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) ?
// check if the array has an element, if not replace by empty string
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
* 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
*/
private function guessValue($value)
{
if ($value === NULL) {
return NULL;
}
if (!is_array($value)) {
return $value;
} else {
// we have a field with "allow other"
if (isset($value['_choices'])) {
return $value['_choices'];
} else {
// we have a field with "multiple"
return $value;
}
}
throw \LogicException("This case is not expected.");
}
public function getName()
{