Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ msgstr "Charset must consist of UPPER case characters only."
msgid "error.EmailVerifiers.verified"
msgstr "Requested address is already verified"

msgid "error.EnrollmentAttributes.single_valued_exists"
msgstr "An Enrollment Attribute for {0} is already configured for this Attribute Collector"

msgid "error.PetitionAcceptances.exists"
msgstr "An Invitation for this Petition already exists"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function dispatch(string $id) {
attributes: array_diff_key($this->request->getData(), ['petition_id' => true, 'token' => true])
);

$this->persistPetitionCouIdFromCollectedAttributes((int)$petition->id, (int)$id);

// On success, indicate the step is completed and generate a redirect
// to the next step

Expand Down Expand Up @@ -96,8 +98,54 @@ public function dispatch(string $id) {
->PetitionAttributes
->find()
->where(['petition_id' => $petition->id])
->contain(['EnrollmentAttributes'])
->all());

$this->render('/Standard/dispatch');
}

/**
* If the Attribute Collector collected a COU for later context (eg approvals),
* persist Petition.cou_id during dispatch (not during finalization).
*
* @since COmanage Registry v5.3.0
* @param int $petitionId
* @param int|null $attributeCollectorId Attribute Collector ID
* @return void
*/
protected function persistPetitionCouIdFromCollectedAttributes(int $petitionId, ?int $attributeCollectorId = null): void {
$petitionAttributesTable = $this->AttributeCollectors
->EnrollmentAttributes
->PetitionAttributes;

// Fetch petition attributes + enrollment attribute data
$query = $petitionAttributesTable
->find()
->where(['PetitionAttributes.petition_id' => $petitionId])
->contain(['EnrollmentAttributes']);

if($attributeCollectorId !== null) {
$query->matching('EnrollmentAttributes', function($q) use ($attributeCollectorId) {
return $q->where(['EnrollmentAttributes.attribute_collector_id' => $attributeCollectorId]);
});
}

$attrs = $query->toArray();

// Find the "cou_id" attribute with a non-empty value.
// Note: Each Attribute Collector is restricted to at most one cou_id attribute
// via ruleSingleValuedAttributeUnique, so selecting the first non-empty match is deterministic.
$couAttr = collection($attrs)
->filter(fn($pa) => $pa->enrollment_attribute?->attribute === 'cou_id' && !empty($pa->value))
->first();

if(!$couAttr) {
return;
}

$petitionsTable = $this->fetchTable('Petitions');
$petition = $petitionsTable->get($petitionId);
$petition->cou_id = (int)$couAttr->value;
$petitionsTable->saveOrFail($petition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ public function hydrate(int $id, \App\Model\Entity\Petition $petition): bool
$attributes = $this->EnrollmentAttributes
->PetitionAttributes
->find()
->where(['petition_id' => $petition->id])
->where(['PetitionAttributes.petition_id' => $petition->id])
->matching('EnrollmentAttributes', function($q) use ($id) {
return $q->where(['EnrollmentAttributes.attribute_collector_id' => $id]);
})
->contain(
['EnrollmentAttributes' => ['AttributeTypes']]
)->toArray();
Expand Down Expand Up @@ -270,6 +273,14 @@ public function hydrate(int $id, \App\Model\Entity\Petition $petition): bool
if(!empty($fieldsForPersonRole)) {
$personRoleObj = TableRegistry::getTableLocator()->get('PersonRoles');
$role = $personRoleObj->saveAttributeCollectorPetitionAttributes((int)$person->id, $fieldsForPersonRole);
} else {
$personRoleObj = TableRegistry::getTableLocator()->get('PersonRoles');
$role = $personRoleObj->find()
->where([
'person_id' => (int)$person->id,
'source_external_identity_role_id IS' => null,
])
->first();
}

/********* MVEAS **************/
Expand All @@ -291,7 +302,7 @@ public function hydrate(int $id, \App\Model\Entity\Petition $petition): bool

// MVEAs for Role
// Save the MVEAs for the PersonRole only if we collected something meaningful for it, and the role was created
if(!empty($fieldsForPersonRole)) {
if(!empty($role)) {
$this->handleMveaAttributes(
$person,
$role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace CoreEnroller\Model\Table;

use Cake\Event\EventInterface;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use \App\Lib\Enum\GroupTypeEnum;
Expand Down Expand Up @@ -193,6 +194,54 @@ public function initialize(array $config): void {
]);
}

/**
* Define business rules.
*
* @since COmanage Registry v5.2.0
* @param RulesChecker $rules RulesChecker object
* @return RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker {
// AR-EnrollmentAttribute-1 Single-valued Person Role attributes may only be configured
// once per Attribute Collector.
$rules->add([$this, 'ruleSingleValuedAttributeUnique'],
'singleValuedAttributeUnique',
['errorField' => 'attribute']);

return $rules;
}

/**
* Application Rule to ensure single-valued Person and Person Role attributes are unique per Attribute Collector.
*
* @since COmanage Registry v5.3.0
* @param \Cake\Datasource\EntityInterface $entity
* @param array $options
* @return bool|string
*/
public function ruleSingleValuedAttributeUnique($entity, array $options): bool|string {
if(empty($entity->attribute) || empty($entity->attribute_collector_id)) {
return true;
}

$supported = $this->supportedAttributes();
if(!empty($supported[$entity->attribute]['model']) && in_array($supported[$entity->attribute]['model'], ['PersonRole', 'Person'])) {
$conditions = [
'attribute_collector_id' => $entity->attribute_collector_id,
'attribute' => $entity->attribute
];
if(!empty($entity->id)) {
$conditions['id !='] = $entity->id;
}
$exists = $this->find()->where($conditions)->first();
if($exists) {
return __d('core_enroller', 'error.EnrollmentAttributes.single_valued_exists', [$supported[$entity->attribute]['label'] ?? $entity->attribute]);
}
}

return true;
}

/**
* Obtain the set of supported attributes.
*
Expand Down
4 changes: 4 additions & 0 deletions app/plugins/CoreEnroller/templates/element/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
if(!empty($vv_petition_attributes)) {
$curEntity = $vv_petition_attributes->firstMatch(['enrollment_attribute_id' => $attr->id]);

if(empty($curEntity)) {
$curEntity = $vv_petition_attributes->firstMatch(['enrollment_attribute.attribute' => $attr->attribute]);
}

if(!empty($curEntity->value)) {
$options['default'] = $curEntity->value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
'column_name' => $field
]);

if(empty($curEntity)) {
$curEntity = $vv_petition_attributes->firstMatch([
'enrollment_attribute.attribute' => $attr->attribute,
'column_name' => $field
]);
}

if(!empty($curEntity->value)) {
$options['default'] = $curEntity->value;
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/Model/Table/PersonRolesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,18 @@ public function saveAttributeCollectorPetitionAttributes(int $personId, array $f
$role[$attribute] = $value;
}

$existingRole = $this->find()
->where([
'person_id' => $personId,
'source_external_identity_role_id IS' => null,
])
->first();

if(!empty($existingRole)) {
$existingRole = $this->patchEntity($existingRole, $role);
return $this->saveOrFail($existingRole);
}

return $this->saveOrFail($this->newEntity($role));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.