Skip to content

Commit

Permalink
Refactored required and permitted calculations.Moved attribute collec…
Browse files Browse the repository at this point in the history
…tions parts to elements.
  • Loading branch information
Ioannis committed Nov 5, 2024
1 parent e23d343 commit 2ffa55c
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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') {
Expand All @@ -54,13 +53,12 @@ foreach($vv_enrollment_attributes_sorted as $attr) {
continue;
}

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

if(empty($attr->attribute_required_fields)) {
include( $vv_template_path . DS . 'attribute.inc');
} else {
include( $vv_template_path . DS . 'multifield_attribute.inc');
// Fieldset with legend for MVEAs
if(\in_array($attr->attribute, ['name', 'address', 'telephoneNumber'], true)) {
print $this->element('CoreEnroller.mveas/mvea-fieldset', compact('attr'));
continue;
}

// Default
print $this->element('CoreEnroller.field', compact('attr'));
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ if (\in_array($attribute_type, ['adHocAttribute'], true)) {
* Attribute Language field
*
* Supported for attributes: name, address
* todo: probably should go away. The attribute required fields could become hidden and store what we have in CoSettings
* at the time we saved the required fields.
*/
if(\in_array($attribute_type, ['name', 'address', 'pronoun'], true)) {
if(\in_array($attribute_type, ['address', 'pronoun'], true)) {
$requiredFieldsPopulated = $attribute_type . 'RequiredFields';
// Pronouns have no required fields at the moment, as a result we will not render any.
if ($this->get($requiredFieldsPopulated) !== null) {
Expand Down
110 changes: 110 additions & 0 deletions app/plugins/CoreEnroller/templates/element/field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* COmanage Registry Attribute Field
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

use \Cake\Utility\Inflector;

// $attr: Obj

// Field Options array
$options = [];

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

// 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 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
]);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* COmanage Registry Fields Set Field
* COmanage Registry Fieldset Field Used for MVEAs
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
Expand All @@ -25,14 +25,24 @@
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/


declare(strict_types = 1);

use \Cake\Utility\Inflector;

// We get the Permitted Address fields from the resources. As a result we need to
// bring them to a proper form. The following action will have not affect the rest of the fields
// $field: string
// $attr: object

$field = str_replace(' ', '_', Inflector::underscore($field));
$label = Inflector::humanize($field);
$isRequiredFromValidationRule = false;
$supportedAttributes = $this->Petition->getSupportedEnrollmentAttribute($attr->attribute);

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

// Construct the field arguments
$formArguments = [
Expand All @@ -47,8 +57,11 @@

<div class="fieldset-field">
<label for="<?= $field ?>"><?= $label ?></label>
<?= \in_array($field, $required_fields_list, true) ? $this->element('form/requiredSpan') : ''?>
<!-- --><?php //= \in_array($field, $required_fields_list, true) ? $this->element('form/requiredSpan') : ''?>
<?= $isRequiredFromValidationRule ? $this->element('form/requiredSpan') : ''?>
<div class="input text">
<?= $this->Field->formField(...$formArguments) ?>
</div>
</div>
</div>


Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* COmanage Registry Fields Set Group
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

use \Cake\Utility\Inflector;

// $attr: object


// Grouped fields
$groupedFieldsVar = $attr->attribute . 'GroupedFields';
$groupedFieldsArray = [];
if(!empty($$groupedFieldsVar)) {
$groupedFieldsArray = collection(array_keys($$groupedFieldsVar))->map(static fn($fields) => explode(',', $fields))->toArray();
}

$permitted_fields_list = [];
$permitted_fields_variable_name = 'permitted_fields_' . Inflector::underscore($attr->attribute);
if (!empty($cosettings[0][$permitted_fields_variable_name])) {
$permitted_fields_list = explode(',', $cosettings[0][$permitted_fields_variable_name]);
}
// Address has no permitted fields configuration at CO level. And according to our design,
// the permitted fields are found in CoSettings.
// For all the attributes that have no permitted configuration at the CO level,
// we will get all the fields that we support
// We will construct the list of permitted fields from the list of required fields.
// The permitted fields will be the set that contains all the required fields.
$requiredDropDownListVar = $attr->attribute . 'RequiredFields';
if(empty($permitted_fields_list) && !empty($$requiredDropDownListVar)) {
usort($$requiredDropDownListVar, static fn($a, $b) => count(explode(',', $a)) <=> count(explode(',', $b)));
$permitted_fields = end($$requiredDropDownListVar);
$permitted_fields_list = collection(explode(',', $permitted_fields))->map(fn($value) => trim($value));
}

?>

<?php foreach($groupedFieldsArray as $idx => $fields): ?>
<div class="fieldset-subgroup">
<?php
$permitted_fields_list = $permitted_fields_list->map(static fn($value) => str_replace(' ', '_', Inflector::underscore($value)));
foreach($fields as $field) {
if($permitted_fields_list->contains($field)) {
// Print the element
print $this->element('CoreEnroller.mveas/fieldset-field', compact('field', 'attr'));
}
// Remove the field we render from the permitted list.
$permitted_fields_list = $permitted_fields_list->filter(fn($value) => $value !== $field);
}
?>
</div>
<?php endforeach; ?>

<?php

// For all the remaining fields we do not need a group
foreach ($permitted_fields_list as $field) {
print $this->element('CoreEnroller.mveas/fieldset-field', compact('field', 'attr'));
}
?>


Loading

0 comments on commit 2ffa55c

Please sign in to comment.