/** * Returns a RecordEntity for an id * @param $record_id * @return RecordEntity|null */ public static function getValidRecordWithId($record_id) { try { $record = Record::getRepository()->findOneBy(['is_hidden' => false, 'id' => $record_id]); if (!empty($record)) { return $record; } else { return null; } } catch (Exception $e) { return null; } }
/** * 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; } }
/** * Parses the data/field info and saves it into database * @todo: extract * @todo: Update records updated_by * * @since 0.0.4 */ public function actionData() { $em = DB::getEntityManager(); $data = $this->parseRequest(['record_id' => 0, 'field_id' => 0, 'value' => null, 'is_default' => null]); $response['error'] = null; /** @var RecordEntity $record */ if ($data['record_id'] > 0 && ($record = Record::getValidRecordWithId($data['record_id'])) != null) { if (in_array($data['field_id'], [FIELD_GIVEN_NAME, FIELD_MIDDLE_NAME, FIELD_LAST_NAME])) { if ($data['value'] !== null) { if ($data['field_id'] == FIELD_GIVEN_NAME) { $record->getPerson()->setGivenName($data['value']); } elseif ($data['field_id'] == FIELD_MIDDLE_NAME) { $record->getPerson()->setMiddleName($data['value']); } elseif ($data['field_id'] == FIELD_LAST_NAME) { $record->getPerson()->setLastName($data['value']); } $em->flush(); } else { $response['error'] = ['id' => 1, 'description' => 'Value cannot be equal to null.']; } /** @var FieldEntity $field */ } elseif ($data['field_id'] > 0 && ($field = Field::getValidFieldWithId($data['field_id'])) != null) { if ($data['value'] !== null) { $dataObject = $record->findOrCreateData($data['field_id']); switch ($field->getType()) { case 1: $dataObject->setInt(intval($data['value'])); break; case 2: if ($field->hasDefault()) { if ($data['is_default'] != null) { $dataObject->setIsDefault(true); if ($field->isMultiple()) { for ($i = 0; $i < count($data['value']); $i++) { $data['value'][$i] = intval($data['value'][$i]); } $dataObject->setLongText(serialize($data['value'])); } else { $dataObject->setInt(intval($data['value'])); } } else { $dataObject->setIsDefault(false); $dataObject->setVarchar($data['value']); } } elseif ($field->isMultiple()) { $dataObject->setLongText(serialize($data['value'])); } else { $dataObject->setVarchar($data['value']); } break; case 3: $date = new DateTime($data['value']); $dataObject->setDateTime($date); break; case 4: $dataObject->setLongText($data['value']); break; } $em->flush(); } else { $response['error'] = $this->getJSONError(1, 'Value cannot be equal to null.'); } } else { $response['error'] = $this->getJSONError(1, 'Supplied field ID is invalid.'); } } else { $response['error'] = $this->getJSONError(1, 'Supplied record ID is invalid.'); } echo json_encode($response); }
/** * 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); }