/**
  * Given a record, this returns the record as a "row"
  * @param RecordEntity $record
  * @return array|null
  */
 private function getRecordDataAsArray($record)
 {
     try {
         $name = Person::getNameAsArray($record->getPerson());
         $essential = Record::getFormattedFields($record, true);
         $non_essential = Record::getFormattedFields($record, false);
         $activityNames = Person::getActivityNames($record->getPerson());
         $formattedNames = Data::concatMultiple($activityNames);
         $strings = $name;
         $strings = array_merge($strings, Data::formattedDataArrayToString($essential));
         $strings = array_merge($strings, Data::formattedDataArrayToString($non_essential));
         $strings = array_merge($strings, [$formattedNames]);
         return $strings;
     } catch (Exception $e) {
         return null;
     }
 }
 /**
  * Returns the data field or creates it if it does not exist
  *
  * @param int $field_id
  * @return DataEntity
  * @since 0.0.7 Now takes into account the type of the field
  * @since 0.0.6
  */
 public function findOrCreateData($field_id)
 {
     /**
      * @var FieldEntity $field
      */
     $field = Field::getRepository()->find($field_id);
     $data = Data::getRepository()->findOneBy(['record' => $this->getId(), 'field' => $field_id]);
     if ($data == null) {
         $data = new DataEntity();
         $data->setRecord($this);
         $data->setField($field);
         $data->setUpdatedBy(Apollo::getInstance()->getConsole()->getEntity());
         if ($field->hasDefault()) {
             if ($field->isMultiple()) {
                 $data->setLongText(serialize([0]));
             } else {
                 $data->setInt(0);
             }
             $data->setIsDefault(true);
         } else {
             if ($field->isMultiple()) {
                 $value = [''];
                 $data->setLongText(serialize($value));
             }
         }
         DB::getEntityManager()->persist($data);
         DB::getEntityManager()->flush();
     }
     return $data;
 }
 /**
  * @param RecordEntity $record
  * @param FieldEntity $field
  * @return mixed
  */
 public static function getFormattedField($record, $field)
 {
     $fieldData = $record->findOrCreateData($field->getId());
     return Data::getFormattedData($fieldData);
 }
 /**
  * Returns JSON containing information used to build the edit view for a record
  * @todo: Put all of the field formatting in the field component
  * @since 0.0.9
  */
 public function actionRecordEdit()
 {
     $data = $this->parseRequest(['id' => 0]);
     $response['error'] = null;
     /**
      * @var RecordEntity $record
      */
     if ($data['id'] > 0 && ($record = Record::getValidRecordWithId($data['id']))) {
         Record::prepare($record);
         $response['essential'] = Record::getFormattedData($record);
         $fieldsEditData = [];
         /**
          * @var FieldEntity[] $fields
          */
         $fields = Field::getValidFields();
         foreach ($fields as $field) {
             $fieldEditData['id'] = $field->getId();
             $fieldEditData['name'] = $field->getName();
             $fieldEditData['type'] = $field->getType();
             $fieldEditData['has_default'] = $field->hasDefault();
             $fieldEditData['allow_other'] = $field->isAllowOther();
             $fieldEditData['is_multiple'] = $field->isMultiple();
             $defaults = $field->getDefaults();
             $defaultArray = [];
             foreach ($defaults as $default) {
                 $defaultArray[] = $default->getValue();
             }
             $fieldEditData['defaults'] = $defaultArray;
             $fieldData = $record->findOrCreateData($field->getId());
             if ($field->hasDefault()) {
                 if ($field->isMultiple()) {
                     $value = unserialize($fieldData->getLongText());
                 } else {
                     if ($fieldData->isDefault() || !$field->isAllowOther()) {
                         $value = $fieldData->getInt();
                     } else {
                         $value = $fieldData->getVarchar();
                     }
                 }
             } else {
                 if ($field->isMultiple()) {
                     $value = unserialize($fieldData->getLongText());
                 } else {
                     $value = Data::serialize($fieldData);
                 }
             }
             $fieldEditData['value'] = $value;
             $fieldsEditData[] = $fieldEditData;
         }
         $response['data'] = $fieldsEditData;
     } else {
         $response['error'] = ['id' => 1, 'description' => 'The supplied ID is invalid!'];
     }
     echo json_encode($response);
 }