Skip to content

Commit

Permalink
Attribute Collection view FieldGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Nov 5, 2024
1 parent 00ef4c9 commit 6fe842f
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function initialize(array $config): void {
'type' => 'enum',
'class' => 'RequiredAddressFieldsEnum'
],
'addressGroupedFields' => [
'type' => 'enum',
'class' => 'GroupedAddressFieldsEnum'
],
'addressTypes' => [
'type' => 'type',
'attribute' => 'Addresses.type'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* COmanage Registry Fields Set 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;

// 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 = str_replace(' ', '_', Inflector::underscore($field));
$label = Inflector::humanize($field);

// 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-$field-$attr->id",
'fieldLabel' => $attr->label, // fieldLabel is only applicable to checkboxes
'fieldType' => $supportedAttributes['fieldType'],
'fieldNameAlias' => $attr->attribute // the field name to its enrollment attribute field name
];
?>

<div class="fieldset-field">
<label for="<?= $field ?>"><?= $label ?></label>
<?= \in_array($field, $required_fields_list, true) ? $this->element('form/requiredSpan') : ''?>
<div class="input text">
<?= $this->Field->formField(...$formArguments) ?>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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;

?>

<?php foreach($groupedFieldsArray as $idx => $fields): ?>
<div class="fieldset-subgroup">
<?php
foreach($fields as $field) {
// Remove the field we render from the permitted list.
$permitted_fields_list = $permitted_fields_list->filter(fn($value) => str_replace(' ', '_', Inflector::underscore($value)) !== $field);
include($vv_template_path . DS . 'fieldset-field.inc');
}
?>
</div>
<?php endforeach; ?>

<?php

// For all the remaining fields we do not need a group
foreach ($permitted_fields_list as $field) {
include($vv_template_path . DS . 'fieldset-field.inc');
}
?>


Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

declare(strict_types = 1);

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

// This field is called attribute_type and not attribute_type_id because we want this
Expand All @@ -38,20 +37,53 @@ $mveaAutoPopulatedVariable = $attr->attribute . 'Types';
// dropdown list
$fieldLabel = 'Not found';
if ($this->get($mveaAutoPopulatedVariable) !== null) {
$fieldLabel = $this->get($mveaAutoPopulatedVariable)[$attr->attribute_type] . ' ' . ucfirst($attr->attribute);
$fieldLabel = $this->get($mveaAutoPopulatedVariable)[$attr->attribute_type]
. ' '
. Inflector::humanize(Inflector::underscore($attr->attribute));
}

$required_fields_variable_name = "required_fields_$attr->attribute";
$permitted_fields_variable_name = "permitted_fields_$attr->attribute";
$required_fields_variable_name = 'required_fields_' . Inflector::underscore($attr->attribute);
$permitted_fields_variable_name = 'permitted_fields_' . Inflector::underscore($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]);
}

// Required fields
// - first look at the enrollment flow level
// - if not set look at the co level
$required_fields = match(true) {
// Enrollment Flow
!empty($attr?->attribute_required_fields) => $attr?->attribute_required_fields,
// Co Level
!empty($cosettings[0][$required_fields_variable_name]) => $cosettings[0][$required_fields_variable_name],
// XXX Do i need CMP level here?
default => ''
};
// List of the required fields
$required_fields_list = explode(',', $required_fields);

// Address has not permitted fields configuration at CO level. And according to our design,
// the permitted fields are found in CoSettings. As a result, if we do not have any we need to get
// For all the attributes that have no
// permitted configuration at the CO level we will get all the fields that we support
$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));
}

// Grouped fields
$groupedFieldsVar = $attr->attribute . 'GroupedFields';
$groupedFieldsArray = [];
if(!empty($$groupedFieldsVar)) {
$groupedFieldsArray = collection(array_keys($$groupedFieldsVar))->map(static fn($fields) => explode(',', $fields))->toArray();
}
// Now that we have the permitted fields array and the grouped fields array, we need to put the permitted fields to groups

?>

<li class="fields-<?= $attr->attribute?>">
Expand All @@ -60,30 +92,16 @@ if (!empty($cosettings[0][$permitted_fields_variable_name])) {
<?= $fieldLabel ?>
</legend>
<div class="fieldset-info">

<?php foreach (explode(',', $attr->attribute_required_fields) as $field) : ?>
<?php if(!\in_array($field, $permitted_fields_list, true)) { continue; } ?>
<?php
// 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-$field-$attr->id",
'fieldLabel' => $attr->label, // fieldLabel is only applicable to checkboxes
'fieldType' => $supportedAttributes['fieldType'],
'fieldNameAlias' => $attr->attribute // the field name to its enrollment attribute field name
];
?>

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

<?php
if(!empty($groupedFieldsArray)) {
include( $vv_template_path . DS . 'fieldset-group.inc');
} else {
// fieldset-fields
foreach ($permitted_fields_list as $field) {
include($vv_template_path . DS . 'fieldset-field.inc');
}
}
?>
</div>
</fieldset>
</li>
17 changes: 13 additions & 4 deletions app/resources/locales/en_US/enumeration.po
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,20 @@ msgstr "Postal Code"
msgid "RequiredAddressFieldsEnum.street"
msgstr "Street"

msgid "RequiredAddressFieldsEnum.street,locality,state,postal_code"
msgstr "Street, City, State, Postal Code"
msgid "RequiredAddressFieldsEnum.street,room,locality,state,postal_code"
msgstr "Street, Room, Locality, City, State, Postal Code"

msgid "RequiredAddressFieldsEnum.street,locality,state,postal_code,country"
msgstr "Street, City, State, Postal Code, Country"
msgid "RequiredAddressFieldsEnum.street,room,locality,state,postal_code,country"
msgstr "Street, Room, Locality, City, State, Postal Code, Country"

msgid "GroupedAddressFieldsEnum.street,room"
msgstr "Street, Room"

msgid "GroupedAddressFieldsEnum.city,locality"
msgstr "City, Locality"

msgid "GroupedAddressFieldsEnum.state,postal_code,country"
msgstr "State, Postal Code, Country"

msgid "RequiredNameFieldsEnum.given"
msgstr "Given"
Expand Down
38 changes: 38 additions & 0 deletions app/src/Lib/Enum/GroupedAddressFieldsEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* COmanage Registry Required Address Fields Enum
*
* 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);

namespace App\Lib\Enum;

class GroupedAddressFieldsEnum extends StandardEnum {
// The order needs to be from local to country. Since this is the
// ordering that will be used for rendering
const StreetRoom = 'street,room';
const CityLocality = 'city,locality';
const StatePostalCountry = 'state,postal_code,country';
}

0 comments on commit 6fe842f

Please sign in to comment.