Skip to content

Commit

Permalink
Cfm 324 attribute collector enroller plugin save form (#279)
Browse files Browse the repository at this point in the history
* Save petition attribute collection data

Save form and display.php render

phpv8.2 deprecation

Petition preview

parse people picker value

Petition Preview new structure

Add collapsable behavior

fix people link

Fix disabled fields. Fix affiliation_type petition render.

More improvements

Fixing Petition resume view

* Fix access to array item

* Fix, partially, the AttributeCollectors finalize function.

* Fix miscalculated tab link process

* Rename view variable creating conflict

* Fix attribute conflict.Fix BasicAttributesCollector dispaly.

* Render hidden column for Enrollment Attributes

* Move display.php to elements

* basic attribute collectors view refactor

* Fix enrollment attributes ordering

* fix typo

* Fix basic Attributes Sets
  • Loading branch information
Ioannis authored Feb 6, 2025
1 parent 9e5dcdb commit 670a9db
Show file tree
Hide file tree
Showing 32 changed files with 444 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
namespace CoreEnroller\Controller;

use App\Controller\StandardEnrollerController;
use App\Lib\Enum\PetitionStatusEnum;
use Cake\Event\EventInterface;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;

class AttributeCollectorsController extends StandardEnrollerController {
public $paginate = [
Expand Down Expand Up @@ -121,16 +121,4 @@ public function dispatch(string $id) {

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

/**
* Display information about this Step.
*
* @since COmanage Registry v5.0.0
* @param string $id Attribute Collector ID
*/

public function display(string $id) {
debug("display something for this petition");
debug($this->getPetition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,4 @@ public function dispatch(string $id) {

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

/**
* Display information about this Step.
*
* @since COmanage Registry v5.1.0
* @param string $id Attribute Collector ID
*/

public function display(string $id) {
$petition = $this->getPetition();

$this->set('vv_petition_basic_attribute_set', $this->BasicAttributeCollectors
->PetitionBasicAttributeSets
->find()
->where(['PetitionBasicAttributeSets.petition_id' => $petition->id])
->firstOrFail());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,4 @@ public function dispatch(string $id) {

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

/**
* Display information about this Step.
*
* @since COmanage Registry v5.1.0
* @param string $id Email Verifiers ID
*/

public function display(string $id) {
$petition = $this->getPetition();

$PetitionVerifications = TableRegistry::getTableLocator()->get('CoreEnroller.PetitionVerifications');

// Because Petition Verifications are not tracked on a per-step basis, we just pull all
// associated with the Petition

$this->set('vv_pv', $PetitionVerifications->find()
->where(['PetitionVerifications.petition_id' => $petition->id])
->contain(['Verifications'])
->all());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ public function dispatch(string $id) {
}
}

/**
* Display information about this Step.
*
* @since COmanage Registry v5.1.0
* @param string $id Invitation Accepters ID
*/

public function display(string $id) {
$petition = $this->getPetition();

$PetitionIdentifiers = TableRegistry::getTableLocator()->get('CoreEnroller.PetitionIdentifiers');

$this->set('vv_pi', $PetitionIdentifiers->find()->where(['petition_id' => $petition->id])->first());
}

/**
* Indicate whether this Controller will handle some or all authnz.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,4 @@ public function dispatch(string $id) {

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

/**
* Display information about this Step.
*
* @since COmanage Registry v5.1.0
* @param string $id Invitation Accepters ID
*/

public function display(string $id) {
$petition = $this->getPetition();

$PetitionAcceptances = TableRegistry::getTableLocator()->get('CoreEnroller.PetitionAcceptances');

$this->set('vv_pa', $PetitionAcceptances->find()->where(['petition_id' => $petition->id])->first());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace CoreEnroller\Model\Table;

use App\Model\Entity\Petition;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
Expand Down Expand Up @@ -117,21 +118,32 @@ public function initialize(array $config): void {

/**
* Perform steps necessary to finalize the Petition.
*
* @since COmanage Registry v5.0.0
* @param int $id Attribute Collector ID
* @param int $petitionId Petition ID
*
* @param int $id Attribute Collector ID
* @param Petition $petition
* @return bool true on success
* @since COmanage Registry v5.1.0
*/

public function finalize(int $id, int $petitionId) {
// XXX convert Petition Attributes to operational Attributes
// keep in mind this can be called multiple times if the plugin is
// instantiated more than once in the Enrollment Flow ($id will be different)
// any errors should be logged or otherwise managed, returning false
// or throwing an error will NOT prevent the petition from finalizing
public function finalize(int $id, \App\Model\Entity\Petition $petition) {
$cfg = $this->get($id);

if(empty($petition->enrollee_person_id)) {
throw new \InvalidArgumentException(__d('error', 'Petitions.enrollee.notfound', [$petition->id]));
}

$People = TableRegistry::getTableLocator()->get('People');

$person = $People->get($petition->enrollee_person_id);

debug("in AttributeCollector finalize");
$attributes = $this->EnrollmentAttributes
->PetitionAttributes
->find()
->where(['petition_id' => $petition->id])
->firstOrFail();

// XXX Should i save the primary name?
// XXX Should i save the email?
return true;
}
Expand Down Expand Up @@ -166,15 +178,33 @@ public function upsert(int $id, int $petitionId, array $attributes) {

foreach($attributes as $enrollmentAttributeLabel => $value) {
// Remove field- prefix from the form field name
$enrollmentAttributeId = (int)substr($enrollmentAttributeLabel, 6);
$fieldToParts = explode('-', $enrollmentAttributeLabel);
// MVAs have multiple columns. For example a name has:
// given, surname, prefix, ...
$columnName = null;
if (count($fieldToParts) > 2) {
// There is a type ID in the middle
$columnName = $fieldToParts[1];
}
// The enrollment Attribute ID is the last part
$enrollmentAttributeId = (int)array_pop($fieldToParts);

// The people picker will send a complex string and not just the id. We need to extract it ourselves.
$re = '/^.*\(ID: (\d+)\)$/m';
preg_match_all($re, $value, $matches, PREG_SET_ORDER, 0);
if(!empty($matches)) {
$value = $matches[0][1];
}

$newAttribute = [
'petition_id' => $petitionId,
'enrollment_attribute_id' => $enrollmentAttributeId,
'value' => $value
'value' => $value,
// This is the column name for the attributes that consist of multiple fields, like the name or the address
'column_name' => $columnName,
];

if(array_key_exists($enrollmentAttributeId, $currentAttributes)) {
if(\array_key_exists($enrollmentAttributeId, $currentAttributes)) {
// This is an update of an existing attribute

$newAttribute['id'] = $currentAttributes[$enrollmentAttributeId];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function finalize(int $id, \App\Model\Entity\Petition $petition) {
// At this point there is a Person record allocated and stored in the Petition,
// but it doesn't have any attributes on it, including a Primary Name.
// We assume we're the only attribute collector, so we'll force a Primary Name
// based on the Basic Attribtues, and create a skeletal role.
// based on the Basic Attributes, and create a skeletal role.

if(empty($petition->enrollee_person_id)) {
throw new \InvalidArgumentException(__d('error', 'Petitions.enrollee.notfound', [$petition->id]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function validationDefault(Validator $validator): Validator {
$validator->notEmptyString('enrollment_attribute_id');

$this->registerStringValidation($validator, $schema, 'value', false);
$this->registerStringValidation($validator, $schema, 'column', false);

return $validator;
}
Expand Down
3 changes: 2 additions & 1 deletion app/plugins/CoreEnroller/src/config/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"id": {},
"petition_id": {},
"enrollment_attribute_id": { "type": "integer", "foreignkey": { "table": "enrollment_attributes", "column": "id" } },
"value": { "type": "string", "size": 160 }
"value": { "type": "string", "size": 160 },
"column_name": { "notnull": false, "type": "string", "size": 64 }
},
"indexes": {
"petition_attributes_i1": { "columns": [ "petition_id" ] },
Expand Down

This file was deleted.

24 changes: 0 additions & 24 deletions app/plugins/CoreEnroller/templates/EmailVerifiers/display.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ $indexColumns = [
'required' => [
'type' => 'boolean',
'class' => 'YesBooleanEnum'
],
'hidden' => [
'type' => 'boolean',
'class' => 'YesBooleanEnum'
]
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ use \Cake\Utility\Inflector;
$attribute_type = null;
if($vv_action == 'add') {
$attribute_type = $this->request->getQuery('attribute_type');
} else if($vv_action == 'edit' && !empty($vv_obj)
) {
} else if($vv_action == 'edit' && !empty($vv_obj)) {
$attribute_type = $vv_obj->attribute;
}

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion app/plugins/CoreEnroller/templates/element/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@
'formArguments' => $formArguments
]),
// Default use case
default => $this->element('form/listItem', ['arguments' => $formArguments])
default => $this->Field->getElementsForDisabledInput('form/listItem', $formArguments)
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,41 @@
$isRequiredFromValidationRule = false;
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);

// Field Options array
$options = [];

if(isset($supportedAttributes['mveaModel'])) {
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);
$modelTable = $this->Petition->getTable($supportedAttributes['mveaModel']);
$isRequiredFromValidationRule = !$modelTable->getValidator()->field($field)->isEmptyAllowed();
}

// Do we have a default value configured?
// Either a value or an Environmental Variable,
// Each default value is mutually exclusive to the rest. We do not have to worry about a conflict.
$options['default'] = match(true) {
isset($attr->default_value) => $attr->default_value,
// XXX The $attr->default_value_env_name for the name attribute is tricky. Since the name has many values.
// Check the EnvSource plugin
isset($attr->default_value_env_name)
&& getenv($attr->default_value_env_name) !== false => getenv($attr->default_value_env_name),
isset($attr->default_value_datetime) => $attr->default_value_datetime,
default => ''
};

// If we are re-rendering the Petition, override the default value with whatever
// was previously saved
if(!empty($vv_petition_attributes)) {
$curEntity = $vv_petition_attributes->firstMatch([
'enrollment_attribute_id' => $attr->id,
'column_name' => $field
]);

if(!empty($curEntity->value)) {
$options['default'] = $curEntity->value;
}
}

// Construct the field arguments
$formArguments = [
// We prefix the attribute ID with a string because Cake seems to sometimes have
Expand All @@ -53,6 +82,9 @@
'fieldType' => $modelTable->getSchema()->getColumn($field)['type'],
'fieldNameAlias' => $attr->attribute // the field name to its enrollment attribute field name
];

// Set the final fieldOptions
$formArguments['fieldOptions'] = $options;
?>

<div class="fieldset-field <?= "fields-$field"?>">
Expand Down
Loading

0 comments on commit 670a9db

Please sign in to comment.