Converting the field value when saving to a respondent track

@param array $currentValue The current value
@param array $fieldData The other values loaded so far
@return mixed the new value
514668
public function onFieldDataSave($currentValue, array $fieldData)
    {
        if ((null === $currentValue) ||
                ($currentValue instanceof \Zend_Db_Expr) ||
                \MUtil_String::startsWith($currentValue, 'current_', true)) {
            return $currentValue;
        }

        $saveFormat = $this->getStorageFormat();

        if ($currentValue instanceof \Zend_Date) {
            return $currentValue->toString($saveFormat);

        } else {
            $displayFormat = $this->getDateFormat();

            $saveDate = \MUtil_Date::ifDate($currentValue, array($displayFormat, $saveFormat, \Gems_Tracker::DB_DATETIME_FORMAT));
            if ($saveDate instanceof \Zend_Date) {
                return $saveDate->toString($saveFormat);
            }
        }

        return (string) $currentValue;
    }
853011
public function field($fieldName, $fieldValue = '', $fieldBoost = null) {
        if(empty($fieldName)) {
            throw new \InvalidArgumentException('Please provide a fieldname');
        }
        $field = array(
			'name'=> $fieldName,
			'value' => $fieldValue
        );
        if(!empty($fieldBoost)) {
            $field['boost'] = $fieldBoost;
        }
        $this->fields[] = $field;
        return $this;
    }
463552
public function onFieldDataLoad($currentValue, array $fieldData)
    {
        if (empty($currentValue)) {
            return null;
        }

        return new \MUtil_Date($currentValue, $this->getStorageFormat());
    }
585331
protected function applyHtmlSpecialCharsOnSingleFieldValue($fieldValue)
    {
        if (is_array($fieldValue)) {
            foreach ($fieldValue as $key => $fieldValueItem) {
                $fieldValue[$key] = htmlspecialchars($fieldValueItem, null, null, false);
            }
        } else {
            $fieldValue = htmlspecialchars($fieldValue, null, null, false);
        }

        return $fieldValue;
    }
315291
private function fieldToProperty($fieldValue, AbstractField $annotation)
    {
        $fieldType = $annotation->type;

        switch ($fieldType) {
            case 'string':
                $propertyValue = strval($fieldValue);
                break;
            case 'bool':
                $propertyValue = boolval($fieldValue);
                break;
            case 'int':
                $propertyValue = intval($fieldValue);
                break;
            case 'float':
                $propertyValue = floatval($fieldValue);
                break;
            case 'array':
                $propertyValue = is_array($fieldValue) ? $fieldValue : (array)($fieldValue);
                break;
            case 'date':
                /** @var \DateTime $fieldValue */
                $propertyValue = ($fieldValue instanceof \DateTime) ? new UTCDateTime($fieldValue) : $fieldValue;
                break;
            case 'id':
                $propertyValue = $fieldValue instanceof ObjectId ? $fieldValue : new ObjectId();
                break;
            case 'embedOne':
                $propertyValue = $this->embedOneToProperty($fieldValue, $annotation);
                break;
            case 'embedMany':
                $propertyValue = $this->embedManyToProperty((array)$fieldValue, $annotation);
                break;
            case 'raw':
            default:
                $propertyValue = $fieldValue;
                break;
        }

        return $propertyValue;
    }
1311628
public function serialize(FieldValue $fieldValue, array $options = [])
    {
        $serialized = [
            'id' => $fieldValue->getUuid(),
        ];

        if (in_array(Options::SERIALIZE_MINIMAL, $options)) {
            $serialized = array_merge($serialized, [
                'field' => [
                    'id' => $fieldValue->getField()->getUuid(),
                ],
                'fieldFacetValue' => $this->fieldFacetValueSerializer->serialize($fieldValue->getFieldFacetValue(), ['minimal']),
            ]);
        } else {
            $serialized = array_merge($serialized, [
                'field' => $this->fieldSerializer->serialize($fieldValue->getField(), [Options::SERIALIZE_MINIMAL]),
                'fieldFacetValue' => $this->fieldFacetValueSerializer->serialize($fieldValue->getFieldFacetValue(), ['minimal']),
            ]);
        }

        return $serialized;
    }
390804
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
    {
        if (empty($value->dataText)) {
            // Special case for anonymous user
            return;
        }
        $fieldValue->data = $this->parseLegacyXml($value->dataText);
    }
429181
protected function _render_radio($data, $current_value)
    {
        if (isset($data['name'])) {
            $name = $data['name'];
        } else {
            throw new MissingFieldException('name');
        }

        if (isset($data['values'])) {
            $values = $data['values'];
        } else {
            throw new MissingFieldException('values');
        }

        $r = PHP_EOL;
        /** @noinspection ForeachSourceInspection */
        foreach ($values as $value => $label) {
            /** @noinspection TypeUnsafeComparisonInspection */
            $checked = ($current_value === '' && $value === '') || ($current_value !== '' && $value == $current_value);
            $r .= '  <label><input type="radio"'
                . " name=\"$name\" value=\"$value\" "
                . ($checked ? 'checked="checked"' : '')
                . '/><span>' . htmlspecialchars($label) . '</span></label>' . PHP_EOL;

        }

        return $r;
    }
1137347
public function saveFields(array $fieldData)
    {
        $trackEngine = $this->getTrackEngine();

        if (! $trackEngine) {
            return 0;
        }

        //\MUtil_Echo::track($fieldData);
        $this->_fieldData = $this->_mergeFieldValues($fieldData, $this->getFieldData(), $trackEngine);

        $changed = $trackEngine->getFieldsDefinition()->saveFields($this->_respTrackId, $this->_fieldData);

        if ($changed) {
            $this->_ensureFieldData(true);
        }

        $this->handleFieldUpdate();

        return $changed;
    }
142551
protected function _setFieldData($fieldName, $fieldValue, $dataType = Field::T_TEXT)
    {
        $longFieldName = $this->_getFieldLongName($fieldName);
        //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);

        // doing this because in lazy loaded lists on first load it is harmful to have initialised fields but not yet set
        // situation: only first article is loaded fully for "select oxid from oxarticles"
        //if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
        //    return;


        //in non lazy loading case we just add a field and do not care about it more
        if (!$this->_blUseLazyLoading
            && !$this->isPropertyLoaded($longFieldName)
        ) {
            $fieldsList = $this->_getAllFields(true);
            if (isset($fieldsList[strtolower($fieldName)])) {
                $this->_addField($fieldName, $this->_getFieldStatus($fieldName));
            }
        }
        // if we have a double field we replace "," with "." in case somebody enters it in european format
        $isPropertyLoaded = $this->isPropertyLoaded($longFieldName);
        if ($isPropertyLoaded
            && isset($this->$longFieldName->fldtype)
            && $this->$longFieldName->fldtype == 'double'
        ) {
            $fieldValue = str_replace(',', '.', $fieldValue);
        }

        // isset is REQUIRED here not to use getter
        if ($isPropertyLoaded
            && is_object($this->$longFieldName)
        ) {
            $this->$longFieldName->setValue($fieldValue, $dataType);
        } else {
            $this->$longFieldName = new Field($fieldValue, $dataType);
        }
    }
1286675
public function createField($fieldName, $defaultValue = null)
    {
        /** @var OptionsetField $field */
        $field = parent::createField($fieldName, $defaultValue);
        $field->setSource($this->Options()->map()->toArray());
        $field->setValue($defaultValue);
        return $field;
    }
572961
@Override
    public boolean updateValuesForField(String currentValue) {
        Utils.require(m_dbObj.getObjectID().equals(currentValue), "Object ID cannot be changed");
        return false;
    }
466820
public function fromPersistenceValue(FieldValue $fieldValue)
    {
        if ($fieldValue->data === null) {
            return $this->getEmptyValue();
        }

        // Restored data comes in $data, since it has already been processed
        // there might be more data in the persistence value than needed here
        $result = $this->fromHash(
            array(
                'id' => (isset($fieldValue->data['id'])
                    ? $fieldValue->data['id']
                    : null),
                'alternativeText' => (isset($fieldValue->data['alternativeText'])
                    ? $fieldValue->data['alternativeText']
                    : null),
                'fileName' => (isset($fieldValue->data['fileName'])
                    ? $fieldValue->data['fileName']
                    : null),
                'fileSize' => (isset($fieldValue->data['fileSize'])
                    ? $fieldValue->data['fileSize']
                    : null),
                'uri' => (isset($fieldValue->data['uri'])
                    ? $fieldValue->data['uri']
                    : null),
                'imageId' => (isset($fieldValue->data['imageId'])
                    ? $fieldValue->data['imageId']
                    : null),
                'width' => (isset($fieldValue->data['width'])
                    ? $fieldValue->data['width']
                    : null),
                'height' => (isset($fieldValue->data['height'])
                    ? $fieldValue->data['height']
                    : null),
            )
        );

        return $result;
    }
1129103
public function convertFieldValueFromForm($data)
    {
        if ($data === null) {
            return null;
        }

        $fileData = array(
            'inputUri' => $data->getRealPath(),
            'fileName' => $data->getClientOriginalName(),
            'fileSize' => $data->getSize(),
        );

        return new FileValue($fileData);
    }
311542
public function fromPersistenceValue(FieldValue $fieldValue)
    {
        if ($fieldValue->externalData === null) {
            return $this->getEmptyValue();
        }

        return new Value(
            $fieldValue->externalData,
            $fieldValue->data['text']
        );
    }
324277
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
    {
        $fieldDefinition = $data->fieldDefinition;
        $formConfig = $fieldForm->getConfig();

        $fieldForm
            ->add(
                $formConfig->getFormFactory()->createBuilder()
                    ->create(
                        'value',
                        $this->formType,
                        [
                            'required' => $fieldDefinition->isRequired,
                            'label' => $fieldDefinition->getName(),
                        ]
                    )
                    ->addModelTransformer(new FieldValueTransformer($this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier)))
                    // Deactivate auto-initialize as we're not on the root form.
                    ->setAutoInitialize(false)
                    ->getForm()
            );
    }
411000
public function addValueData(AttributeInterface $attribute, $valueData, array $context = null)
    {
        $this->checkAttribute($attribute);
        if (!$attribute->isCollection()) {
            $m = "Cannot append data to a non-collection attribute '{$attribute->getCode()}'";
            throw new InvalidValueDataException($m);
        }
        $newValue = $this->createValue($attribute, $context);
        $position = -1;
        foreach ($this->getInternalValues($attribute, $context) as $value) {
            $position = max($position, $value->getPosition());
        }
        $newValue->setPosition($position + 1);
        $this->setInternalValueData($attribute, $newValue, $valueData);

        return $this;
    }