diff --git a/app/src/Model/Table/AdHocAttributesTable.php b/app/src/Model/Table/AdHocAttributesTable.php index c7ad2c1e1..c08deb814 100644 --- a/app/src/Model/Table/AdHocAttributesTable.php +++ b/app/src/Model/Table/AdHocAttributesTable.php @@ -167,10 +167,19 @@ public function validationDefault(Validator $validator): Validator { public function saveAttributeCollectorPetitionAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool { foreach ($fields as $idx => $field) { - // Check if this has already been saved + $tag = $field->enrollment_attribute->attribute_tag; + $value = $field->value; + + if ( + $field->enrollment_attribute->required !== true + && ($tag === null || trim((string)$tag) === '') + ) { + continue; + } + $adhoc = [ - 'tag' => $field->enrollment_attribute->attribute_tag, - 'value' => $field->value + 'tag' => $tag, + 'value' => $value ]; if($parentModel === 'Person') { diff --git a/app/src/Model/Table/EmailAddressesTable.php b/app/src/Model/Table/EmailAddressesTable.php index 32e126a9d..7174dfa62 100644 --- a/app/src/Model/Table/EmailAddressesTable.php +++ b/app/src/Model/Table/EmailAddressesTable.php @@ -386,10 +386,19 @@ public function validationDefault(Validator $validator): Validator { public function saveAttributeCollectorPetitionAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool { foreach ($fields as $idx => $field) { - // Check if this has already been saved + // Email Addresses can be optional, but if they are not provided we have to skip save since empty email values are not allowed. + $value = $field->value; + + if ( + $field->enrollment_attribute->required !== true + && ($value === null || trim((string)$value) === '') + ) { + continue; + } + $email = [ 'person_id' => $personId, - 'mail' => $field->value, + 'mail' => $value, 'type_id' => $field->enrollment_attribute->attribute_type, ]; diff --git a/app/src/Model/Table/IdentifiersTable.php b/app/src/Model/Table/IdentifiersTable.php index 642c79ab7..13f6505cf 100644 --- a/app/src/Model/Table/IdentifiersTable.php +++ b/app/src/Model/Table/IdentifiersTable.php @@ -469,4 +469,38 @@ public function validationDefault(Validator $validator): Validator { return $validator; } + + /** + * Save attributes for a given person and optionally their role. + * + * @since COmanage Registry v5.2.0 + * @param int $personId Identifier for the person + * @param int|null $roleId Identifier for the role (nullable) + * @param string $parentModel Name of the parent model + * @param array $fields Array of attributes to save + * @return bool True on success, false otherwise + */ + public function saveAttributeCollectorPetitionAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool + { + foreach ($fields as $idx => $field) { + // Identifiers can be optional, but if they are not provided we have to skip save since empty strings are not allowed. + $value = $field->value; + + if ( + $field->enrollment_attribute->required !== true + && ($value === null || trim((string)$value) === '') + ) { + continue; + } + + $identifier = [ + 'person_id' => $personId, + 'identifier' => $value, + 'type_id' => $field->enrollment_attribute->attribute_type, + ]; + + $this->saveOrFail($this->newEntity($identifier)); + } + return true; + } } \ No newline at end of file diff --git a/app/src/Model/Table/PronounsTable.php b/app/src/Model/Table/PronounsTable.php index 8e03ff219..16bab5ce3 100644 --- a/app/src/Model/Table/PronounsTable.php +++ b/app/src/Model/Table/PronounsTable.php @@ -194,10 +194,19 @@ public function validationDefault(Validator $validator): Validator { public function saveAttributeCollectorPetitionAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool { foreach ($fields as $idx => $field) { - // Check if this has already been saved + // Pronouns can be optional, but if they are not provided we have to skip save since empty strings are not allowed. + $value = $field->value; + + if ( + $field->enrollment_attribute->required !== true + && ($value === null || trim((string)$value) === '') + ) { + continue; + } + $pronoun = [ 'person_id' => $personId, - 'pronouns' => $field->value, + 'pronouns' => $value, 'language' => $field->enrollment_attribute->attribute_language, 'type_id' => $field->enrollment_attribute->attribute_type, ]; diff --git a/app/src/Model/Table/UrlsTable.php b/app/src/Model/Table/UrlsTable.php index 6e4b60580..2e7a22db1 100644 --- a/app/src/Model/Table/UrlsTable.php +++ b/app/src/Model/Table/UrlsTable.php @@ -212,10 +212,19 @@ public function validationDefault(Validator $validator): Validator { public function saveAttributeCollectorPetitionAttributes(int $personId, ?int $roleId, string $parentModel, array $fields): bool { foreach ($fields as $idx => $field) { - // Check if this has already been saved + // URLs can be optional, but if they are not provided we have to skip save since empty URL strings are not allowed. + $value = $field->value; + + if ( + $field->enrollment_attribute->required !== true + && ($value === null || trim((string)$value) === '') + ) { + continue; + } + $url = [ 'person_id' => $personId, - 'url' => $field->value, + 'url' => $value, 'type_id' => $field->enrollment_attribute->attribute_type, ];