Skip to content

Commit

Permalink
Fix Petition Attributes Form rendering (#241)
Browse files Browse the repository at this point in the history
* Fix Petition Attributes Form rendering

* populate autoview vars in the petition attribute collection view

* AttributeCollectors/dispatch.inc fix fields configurations.

* Add field description. Add FieldHelper support to Attribute selection view.

* Fix field sorting.Fix type dropdowns.
  • Loading branch information
Ioannis committed Dec 15, 2024
1 parent b2b4fdc commit 7fb0393
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,104 +211,123 @@ public function supportedAttributes(string $format='full'): array {
// Single valued Person attributes
$attrs['date_of_birth'] = [
'label' => __d('field', 'date_of_birth'),
'model' => 'Person'
'model' => 'Person',
'fieldType' => 'date'
];

// Single valued Person Role attributes

$attrs['affiliation_type_id'] = [
'label' => __d('field', 'affiliation'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'string'
];

$attrs['cou_id'] = [
'label' => __d('controller', 'Cous', [1]),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'integer'
];

$attrs['department'] = [
'label' => __d('field', 'department'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'string'
];

$attrs['manager_person_id'] = [
'label' => __d('field', 'manager'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'integer'
];

$attrs['organization'] = [
'label' => __d('field', 'organization'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'string'
];

$attrs['sponsor_person_id'] = [
'label' => __d('field', 'sponsor'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'integer'
];

$attrs['title'] = [
'label' => __d('field', 'title'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'string'
];

$attrs['valid_from'] = [
'label' => __d('field', 'valid_from'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'datetime'
];

$attrs['valid_through'] = [
'label' => __d('field', 'valid_through'),
'model' => 'PersonRole'
'model' => 'PersonRole',
'fieldType' => 'datetime'
];

// MVEAs, which might attach to the Person or Person Role or both

$attrs['address'] = [
'label' => __d('controller', 'Addresses', [1]),
'mveaModel' => 'Addresses',
'mveaParents' => ['Person', 'PersonRole']
'mveaParents' => ['Person', 'PersonRole'],
'fieldType' => 'string',
'autoViewVar' => 'addressTypes',
];

$attrs['adHocAttribute'] = [
'label' => __d('controller', 'AdHocAttributes', [1]),
'mveaModel' => 'AdHocAttributes',
'mveaParents' => ['Person', 'PersonRole']
'mveaParents' => ['Person', 'PersonRole'],
'fieldType' => 'string'
];

$attrs['emailAddress'] = [
'label' => __d('controller', 'EmailAddresses', [1]),
'mveaModel' => 'EmailAddresses',
'mveaParents' => ['Person']
'mveaParents' => ['Person'],
'fieldType' => 'string'
];

$attrs['identifier'] = [
'label' => __d('controller', 'Identifiers', [1]),
'mveaModel' => 'Identifiers',
'mveaParents' => ['Person']
'mveaParents' => ['Person'],
'fieldType' => 'string'
];

$attrs['name'] = [
'label' => __d('controller', 'Names', [1]),
'mveaModel' => 'Names',
'mveaParents' => ['Person']
'mveaParents' => ['Person'],
'fieldType' => 'string'
];

$attrs['pronoun'] = [
'label' => __d('controller', 'Pronouns', [1]),
'mveaModel' => 'Pronouns',
'mveaParents' => ['Person']
'mveaParents' => ['Person'],
'fieldType' => 'string'
];

$attrs['telephoneNumber'] = [
'label' => __d('controller', 'TelephoneNumbers', [1]),
'mveaModel' => 'TelephoneNumbers',
'mveaParents' => ['Person', 'PersonRole']
'mveaParents' => ['Person', 'PersonRole'],
'fieldType' => 'string'
];

$attrs['url'] = [
'label' => __d('controller', 'Urls', [1]),
'mveaModel' => 'Urls',
'mveaParents' => ['Person']
'mveaParents' => ['Person'],
'fieldType' => 'string'
];

// Group memberships, as reflected by the Group ID for the membership to be created in
Expand All @@ -318,19 +337,22 @@ public function supportedAttributes(string $format='full'): array {
// to attach the membership to, but the label says Group Member because that'll be
// more obvious
'label' => __d('controller', 'GroupMembers', [1]),
'model' => 'Group'
'model' => 'Group',
'fieldType' => 'integer'
];

// Attributes that are only stored in the Petition

$attrs['petition_text'] = [
'label' => __d('core_enroller', 'field.EnrollmentAttributes.petition_text'),
'model' => 'Petition'
'model' => 'Petition',
'fieldType' => 'string'
];

$attrs['petition_textarea'] = [
'label' => __d('core_enroller', 'field.EnrollmentAttributes.petition_textarea'),
'model' => 'Petition'
'model' => 'Petition',
'fieldType' => 'text'
];

switch($format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,53 @@

declare(strict_types = 1);

use App\Lib\Enum\StatusEnum;
use \Cake\Utility\Inflector;

// This view is intended to work with dispatch
if($vv_action !== 'dispatch') {
return;
}

// Make the Form fields editable
$this->Field->enableFormEditMode();
// Populate the AutoViewVars. These are the same we do for the EnrollmentAttributes configuration view
$this->Petition->populateAutoViewVars();
// We just populated the AutoViewVars. Add them to the current context
extract($this->viewVars);

// Sort according to configuration
$vv_enrollment_attributes_sorted = $vv_enrollment_attributes->sortBy(function ($attribute) {
return $attribute->ordr;
}, SORT_ASC)->toList();

// Iterate over the attributes and render
foreach($vv_enrollment_attributes_sorted as $attr) {
// Do not render if this is not active
if ($attr->status !== StatusEnum::Active) {
continue;
}

// Get the static configuration of my attribute
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);

foreach($vv_enrollment_attributes as $attr) {
// Field Options array
$options = [];


// 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,
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 rerendering 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]);

Expand All @@ -46,13 +82,55 @@ foreach($vv_enrollment_attributes as $attr) {
}
}

// Construct the field arguments
$formArguments = [
// We prefix the attribute ID with a string because Cake seems to sometimes have
// problems with field names that are purely integers (even if cast to strings)
'fieldName' => 'field-' . $attr->id,
'fieldLabel' => $attr->label, // fieldLabel is only applicable to checkboxes
'fieldType' => $supportedAttributes['fieldType'],
'fieldDescription' => $attr->description,
'fieldNameAlias' => $attr->attribute // the field name to its enrollment attribute field name
];


/*
* Get the values for the attributes ending with _id
* Supported for attributes: group_id, cou_id, affiliation_type_id
*/
if(str_ends_with($attr->attribute, '_id')) {
$suffix = substr($attr->attribute, 0, -3);
$suffix = Inflector::pluralize(Inflector::camelize($suffix)) ;
$defaultValuesPopulated = 'defaultValue' . $suffix;
if ($this->get($defaultValuesPopulated) !== null) {
$formArguments['fieldType'] = 'select';
$formArguments['fieldSelectOptions'] = $this->get($defaultValuesPopulated);
}
}

// READ-ONLY
if (isset($attr->modifiable) && !$attr->modifiable) {
$options['readonly'] = true;
}

// REQUIRED
if (isset($attr->required) && $attr->required) {
$options['required'] = true;
}

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

// HIDDEN Field
// We print directly, we do not delegate to the element for further processing
if ($attr->hidden) {
// In case this is a hidden field, we need to get only the value
print $this->Form->hidden($formArguments['fieldName'], ['value' => $options['default']]);
continue;
}

// Print the element
print $this->element('form/listItem', [
'arguments' => [
// We prefix the attribute ID with a string because Cake seems to sometimes have
// problems with field names that are purely integers (even if cast to strings)
'fieldName' => 'field-' . $attr->id,
'fieldOptions' => $options,
'fieldLabel' => $attr->label
]
'arguments' => $formArguments
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $hidden = [
* Include a "cancel" button next to the "save" button
*/

$this->set('vv_include_cancel',true);
$this->set('vv_include_cancel', true);

/*
* Common Fields for all attributes
Expand Down
Loading

0 comments on commit 7fb0393

Please sign in to comment.