diff --git a/app/plugins/CoreEnroller/src/Model/Table/EnrollmentAttributesTable.php b/app/plugins/CoreEnroller/src/Model/Table/EnrollmentAttributesTable.php index cee5f79a1..707a34062 100644 --- a/app/plugins/CoreEnroller/src/Model/Table/EnrollmentAttributesTable.php +++ b/app/plugins/CoreEnroller/src/Model/Table/EnrollmentAttributesTable.php @@ -155,7 +155,12 @@ public function initialize(array $config): void { 'urlTypes' => [ 'type' => 'type', 'attribute' => 'Urls.type' - ] + ], + // Required for attribute collection + 'cosettings' => [ + 'type' => 'auxiliary', + 'model' => 'CoSettings' + ], ]); $this->setLayout([ 'index' => 'iframe', diff --git a/app/plugins/CoreEnroller/templates/AttributeCollectors/attribute.inc b/app/plugins/CoreEnroller/templates/AttributeCollectors/attribute.inc new file mode 100644 index 000000000..d8dec4a3c --- /dev/null +++ b/app/plugins/CoreEnroller/templates/AttributeCollectors/attribute.inc @@ -0,0 +1,107 @@ +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 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]); + + 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 + // 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']]); +} else { + // Print the element + print $this->element('form/listItem', [ + 'arguments' => $formArguments + ]); +} \ No newline at end of file diff --git a/app/plugins/CoreEnroller/templates/AttributeCollectors/dispatch.inc b/app/plugins/CoreEnroller/templates/AttributeCollectors/dispatch.inc index a90b43221..92b35e72b 100644 --- a/app/plugins/CoreEnroller/templates/AttributeCollectors/dispatch.inc +++ b/app/plugins/CoreEnroller/templates/AttributeCollectors/dispatch.inc @@ -57,80 +57,10 @@ foreach($vv_enrollment_attributes_sorted as $attr) { // Get the static configuration of my attribute $supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute); - // 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]); - - 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 - // 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; + if(empty($attr->attribute_required_fields)) { + include( $vv_template_path . DS . 'attribute.inc'); + } else { + include( $vv_template_path . DS . 'multifield_attribute.inc'); } - // Print the element - print $this->element('form/listItem', [ - 'arguments' => $formArguments - ]); } \ No newline at end of file diff --git a/app/plugins/CoreEnroller/templates/AttributeCollectors/multifield_attribute.inc b/app/plugins/CoreEnroller/templates/AttributeCollectors/multifield_attribute.inc new file mode 100644 index 000000000..e9bd8019e --- /dev/null +++ b/app/plugins/CoreEnroller/templates/AttributeCollectors/multifield_attribute.inc @@ -0,0 +1,89 @@ +attribute . 'Types'; +// Check if this mvea model supports types, if it does then render the attribute type +// dropdown list +$fieldLabel = 'Not found'; +if ($this->get($mveaAutoPopulatedVariable) !== null) { + $fieldLabel = $this->get($mveaAutoPopulatedVariable)[$attr->attribute_type] . ' ' . ucfirst($attr->attribute); +} + +$required_fields_variable_name = "required_fields_$attr->attribute"; +$permitted_fields_variable_name = "permitted_fields_$attr->attribute"; +$permitted_fields_list = []; +$required_fields_list = []; +if (!empty($cosettings[0][$required_fields_variable_name])) { + $required_fields_list = explode(',', $cosettings[0][$required_fields_variable_name]); +} + +if (!empty($cosettings[0][$permitted_fields_variable_name])) { + $permitted_fields_list = explode(',', $cosettings[0][$permitted_fields_variable_name]); +} +?> + +