diff --git a/app/resources/locales/en_US/default.po b/app/resources/locales/en_US/default.po index b7da898d9..002b643d3 100644 --- a/app/resources/locales/en_US/default.po +++ b/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" diff --git a/app/src/Controller/AttributeMappingsController.php b/app/src/Controller/AttributeMappingsController.php index 808d7d968..65eda7c60 100644 --- a/app/src/Controller/AttributeMappingsController.php +++ b/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 */ diff --git a/app/src/Controller/AttributesController.php b/app/src/Controller/AttributesController.php index 4885f641f..98b1bba0c 100644 --- a/app/src/Controller/AttributesController.php +++ b/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 ]; diff --git a/app/src/Model/Table/AttributesTable.php b/app/src/Model/Table/AttributesTable.php index 91025b704..37507df61 100644 --- a/app/src/Model/Table/AttributesTable.php +++ b/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. * diff --git a/app/templates/Attributes/columns.inc b/app/templates/Attributes/columns.inc index 7eb1c6dbb..16adb55dc 100644 --- a/app/templates/Attributes/columns.inc +++ b/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') + ] ]; \ No newline at end of file