Skip to content

Save person model attribute data. #290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -212,6 +212,23 @@ public function finalize(int $id, \App\Model\Entity\Petition $petition): bool
$cxn
);

/****** PERSON ******/
// Filter the MVEAS Attributes and keep the field name
$personAttributes = (new Collection($supportedAttributes))->filter(function($attr, $key) {
return isset($attr['model']) && $attr['model'] == 'Person';
})->toArray();
$personAttributes = array_keys($personAttributes);

// Get all the fields/values required to build the PrersonRole
$fieldsForPerson = $attributesCollection->filter(function($attr, $key) use ($personAttributes) {
return in_array($attr['enrollment_attribute']['attribute'], $personAttributes);
})->toArray();

if($People->saveAttributes($person->id, $fieldsForPerson) === false) {
$cxn->rollback();
throw new \RuntimeException(__d('error', 'save', ['Person']));
}

// Save the Date Of Birth. This is the only one that is single valued
// and goes under the Person

Expand Down
34 changes: 34 additions & 0 deletions app/src/Model/Table/PeopleTable.php
Expand Up @@ -737,4 +737,38 @@ public function validationDefault(Validator $validator): Validator {

return $validator;
}


/**
* Save attributes for a person entity.
*
* @param int $personId The ID of the person entity to update.
* @param array $fields An array of fields to update, where each field is expected to have an
* `enrollment_attribute` with an `attribute` property, and a `value` property containing the value to update.
* @return \App\Model\Entity\Person The updated person entity after saving.
* @throws \Cake\Datasource\Exception\RecordNotFoundException If the person with the given ID does not exist.
* @throws \RuntimeException If the person entity could not be saved.
* @since COmanage Registry v5.1.0
*/
public function saveAttributes(int $personId, array $fields): \App\Model\Entity\Person
{

$person = $this->get($personId);
foreach ($fields as $field) {
$attribute = $field->enrollment_attribute->attribute;
$value = $field->value;
if($attribute === 'date_of_birth' && is_string($value)) {
// While we're here, make sure it's in YYYY-MM-DD format. This should fail
// if the inbound attribute is invalid.
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $value);

if($dob) {
$value = $dob->format('Y-m-d');
}
}
$person->$attribute = $value;
}

return $this->save($person);
}
}