diff --git a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php index 4a3cfb79..1ecd9bc8 100644 --- a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php +++ b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php @@ -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 diff --git a/app/src/Model/Table/PeopleTable.php b/app/src/Model/Table/PeopleTable.php index e829ecd0..60c3d414 100644 --- a/app/src/Model/Table/PeopleTable.php +++ b/app/src/Model/Table/PeopleTable.php @@ -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); + } } \ No newline at end of file