Skip to content

Commit

Permalink
Save person model attribute data. (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis authored Feb 12, 2025
1 parent 4f9d8af commit a4c6e63
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}

0 comments on commit a4c6e63

Please sign in to comment.