Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pre-defined default attributes (CO-1677)
Benn Oshrin committed Nov 25, 2022
1 parent 1027902 commit b5c9bab
Showing 5 changed files with 159 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/resources/locales/en_US/default.po
@@ -602,6 +602,9 @@ msgstr "Are you sure you wish to assign this Reference ID?"
msgid "match.op.AttributeMappings.install.nicknames.en"
msgstr "Install English Nicknames"

msgid "match.op.Attributes.install"
msgstr "Install Default Attributes"

msgid "match.op.build"
msgstr "Build"

@@ -747,6 +750,9 @@ msgstr "View {0}"
msgid "match.rs.AttributeMappings.install"
msgstr "Attribute Mapping successfully installed"

msgid "match.rs.Attributes.install"
msgstr "Default Attributes successfully installed"

msgid "match.rs.build"
msgstr "Matchgrid schema successfully applied"

2 changes: 1 addition & 1 deletion app/src/Controller/AttributeMappingsController.php
@@ -38,7 +38,7 @@ class AttributeMappingsController extends StandardController {
];

/**
* Handle an edit action for a Standard object.
* Install default Attribute Mappings.
*
* @since COmanage Match v1.0.0
*/
20 changes: 20 additions & 0 deletions app/src/Controller/AttributesController.php
@@ -36,6 +36,25 @@ class AttributesController extends StandardController {
]
];

/**
* Install default Attributes.
*
* @since COmanage Match v1.1.0
*/

public function install() {
try {
$this->Attributes->install($this->cur_mg->id);

$this->Flash->success(__('match.rs.Attributes.install'));
}
catch(Exception $e) {
$this->Flash->error($e->getMessage());
}

return $this->generateRedirect();
}

/**
* Authorization for this Controller, called by Auth component
* - postcondition: $vv_permissions set with calculated permissions for this Controller
@@ -58,6 +77,7 @@ public function isAuthorized(Array $user) {
'duplicate' => $platformAdmin || $mgAdmin,
'edit' => $platformAdmin || $mgAdmin,
'index' => $platformAdmin || $mgAdmin,
'install' => $platformAdmin || $mgAdmin,
'view' => false
];

123 changes: 123 additions & 0 deletions app/src/Model/Table/AttributesTable.php
@@ -61,6 +61,7 @@ public function initialize(array $config): void {
$this->setDisplayField('name');

$this->setPrimaryLink('matchgrid_id');
$this->setAllowUnkeyedPrimaryLink(['install']);
$this->setRequiresMatchgrid(true);

$this->setAutoViewVars([
@@ -77,6 +78,128 @@ public function initialize(array $config): void {
]);
}

/**
* Install a pre-configured set of Attributes.
*
* @param int $matchgridId Matchgrid ID
* @throws \RuntimeException
*/

public function install(int $matchgridId) {
// First create an "official" attribute group, if one doesn't already exist

$groupObj = $this->AttributeGroups->find('all', [
'conditions' => [
'matchgrid_id' => $matchgridId,
'name' => 'official'
]])
->first();

if(empty($groupObj)) {
// No existing Attribute Group, create one

$group = [
'matchgrid_id' => $matchgridId,
'name' => 'official'
];

$groupObj = $this->AttributeGroups->newEntity($group);

if(!$this->AttributeGroups->save($groupObj)) {
throw new \RuntimeException(__('match.er.save', ["AttributeGroups"]));
}
}

// Now add (any missing) default attributes. Populating "false" instead of "0"
// causes Cake/PHP to not provide those values to validation, which subsequently fails.

$attrs = [
[
'matchgrid_id' => $matchgridId,
'attribute_group_id' => $groupObj->id,
'name' => 'firstname',
'api_name' => 'names:given',
'alphanumeric' => 0,
'case_sensitive' => 0,
'invalidates' => 0,
'null_equivalents' => 0,
'search_distance' => 2,
'search_exact' => 1,
'search_substr_from' => null,
'search_substr_for' => null,
'attribute_map_id' => null,
'index_display' => 1
],
[
'matchgrid_id' => $matchgridId,
'attribute_group_id' => $groupObj->id,
'name' => 'lastname',
'api_name' => 'names:family',
'alphanumeric' => 0,
'case_sensitive' => 0,
'invalidates' => 0,
'null_equivalents' => 0,
'search_distance' => 2,
'search_exact' => 1,
'search_substr_from' => null,
'search_substr_for' => null,
'attribute_map_id' => null,
'index_display' => 1
],
[
'matchgrid_id' => $matchgridId,
'attribute_group_id' => null,
'name' => 'dob',
'api_name' => 'dateOfBirth',
'alphanumeric' => 1,
'case_sensitive' => 0,
'invalidates' => 0,
'null_equivalents' => 1,
'search_distance' => 2,
'search_exact' => 1,
'search_substr_from' => null,
'search_substr_for' => null,
'attribute_map_id' => null,
'index_display' => 0
],
[
'matchgrid_id' => $matchgridId,
'attribute_group_id' => null,
'name' => 'nationalid',
'api_name' => 'identifiers:identifier/national',
'alphanumeric' => 1,
'case_sensitive' => 0,
'invalidates' => 0,
'null_equivalents' => 0,
'search_distance' => 2,
'search_exact' => 1,
'search_substr_from' => null,
'search_substr_for' => null,
'attribute_map_id' => null,
'index_display' => 0
]
];

foreach($attrs as $attr) {
$attrObj = $this->find('all', [
'conditions' => [
'matchgrid_id' => $matchgridId,
'name' => $attr['name']
]])
->first();

if(empty($attrObj)) {
// No existing Attribute, create one

$attrObj = $this->newEntity($attr);

if(!$this->save($attrObj)) {
throw new \RuntimeException(__('match.er.save', ["Attributes"]));
}
}
}
}

/**
* Set validation rules.
*
9 changes: 9 additions & 0 deletions app/templates/Attributes/columns.inc
@@ -33,4 +33,13 @@ $indexColumns = [
'attribute_group_id' => [
'type' => 'fk'
]
];

$topLinks= [
[
'action' => 'install',
'class' => 'buildbutton',
'icon' => 'file_download',
'label' => __('match.op.Attributes.install')
]
];

0 comments on commit b5c9bab

Please sign in to comment.