/**
  * @param RecordEntity $record
  * @param FieldEntity $field
  * @return mixed
  */
 public static function getFormattedField($record, $field)
 {
     $fieldData = $record->findOrCreateData($field->getId());
     return Data::getFormattedData($fieldData);
 }
 /**
  * Action responsible for handling requests related to fields
  * @todo: extract.
  * @param string $action
  */
 public function actionField($action = null)
 {
     $response['error'] = null;
     if ($action == 'update') {
         $data = $this->parseRequest(['type' => null, 'id' => 0, 'value' => null]);
         if (!empty($data['type']) && $data['id'] > 0 && !empty($data['value'])) {
             if (in_array($data['type'], ['name', 'defaults'])) {
                 /** @var FieldEntity $field */
                 $field = Field::getValidFieldWithId($data['id']);
                 if ($field != null) {
                     $em = DB::getEntityManager();
                     if ($data['type'] == 'name') {
                         $field->setName($data['value']);
                     } elseif ($data['type'] == 'defaults') {
                         //@todo: Anything but this
                         $length = max(count($data['value']), count($field->getDefaults()));
                         $defaults = $field->getDefaults();
                         for ($i = 0; $i < $length; $i++) {
                             if (count($field->getDefaults()) > $i) {
                                 $default = $defaults[$i];
                                 if (count($data['value']) > $i) {
                                     $default->setValue($data['value'][$i]);
                                     $default->setOrder($i);
                                 } else {
                                     $em->remove($default);
                                 }
                             } else {
                                 $default = new DefaultEntity();
                                 $default->setField($field);
                                 $default->setValue($data['value'][$i]);
                                 $default->setOrder($i);
                                 $em->persist($default);
                             }
                         }
                         $em->flush();
                     }
                     $em->flush();
                 } else {
                     $error['id'] = 0;
                     $error['description'] = 'Invalid ID.';
                 }
             } else {
                 $error['id'] = 0;
                 $error['description'] = 'Invalid field type.';
             }
         } else {
             $error['id'] = 0;
             $error['description'] = 'Missing post request parameters.';
         }
     } elseif ($action == 'add') {
         $data = $this->parseRequest(['name' => null, 'type' => null]);
         if (!empty($data['name']) && !empty($data['type'])) {
             $field = new FieldEntity();
             $field->setOrganisation(Apollo::getInstance()->getUser()->getOrganisation());
             $field->setName($data['name']);
             switch ($data['type']) {
                 case 'integer':
                     $field->setType(1);
                     break;
                 case 'single':
                     $field->setType(2);
                     break;
                 case 'multiple':
                     $field->setType(2);
                     $field->setIsMultiple(true);
                     break;
                 case 'dropdown':
                     $field->setType(2);
                     $field->setHasDefault(true);
                     break;
                 case 'dropdown-other':
                     $field->setType(2);
                     $field->setHasDefault(true);
                     $field->setAllowOther(true);
                     break;
                 case 'dropdown-multiple':
                     $field->setType(2);
                     $field->setHasDefault(true);
                     $field->setIsMultiple(true);
                     break;
                 case 'date':
                     $field->setType(3);
                     break;
                 case 'text':
                     $field->setType(4);
                     break;
             }
             DB::getEntityManager()->persist($field);
             DB::getEntityManager()->flush();
             if (in_array($data['type'], ['dropdown', 'dropdown-other', 'dropdown-multiple'])) {
                 $default = new DefaultEntity();
                 $default->setField($field);
                 $default->setOrder(0);
                 $default->setValue('Default value');
                 DB::getEntityManager()->persist($default);
                 DB::getEntityManager()->flush();
             }
         } else {
             $error['id'] = 0;
             $error['description'] = 'Missing post request parameters.';
         }
     } elseif ($action == 'hide') {
         $data = $this->parseRequest(['id' => 0]);
         $field = Field::getValidFieldWithId($data['id']);
         if ($field != null) {
             if (!$field->isEssential()) {
                 $field->setIsHidden(true);
                 DB::getEntityManager()->flush();
             } else {
                 $error['id'] = 0;
                 $error['description'] = 'Field is marked as essential and hence cannot be hidden.';
             }
         } else {
             $error['id'] = 0;
             $error['description'] = 'Invalid ID.';
         }
     } else {
         $error['id'] = 0;
         $error['description'] = 'Invalid action.';
         $response['error'] = $error;
     }
     echo json_encode($response);
 }