From: Tim Otten Date: Fri, 19 May 2023 10:16:03 +0000 (-0700) Subject: (REF) civicrm_data - Extract civicrm_dedupe_rule_group and civicrm_dedupe_rule X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=dd378682475dd6bf113d65d9077552ba27572a90;p=civicrm-core.git (REF) civicrm_data - Extract civicrm_dedupe_rule_group and civicrm_dedupe_rule --- diff --git a/CRM/Core/CodeGen/DedupeRule.php b/CRM/Core/CodeGen/DedupeRule.php new file mode 100644 index 0000000000..800fccbdb8 --- /dev/null +++ b/CRM/Core/CodeGen/DedupeRule.php @@ -0,0 +1,63 @@ +metadata['name'] = $name; + $og->var = '@drgid'; + return $og; + } + + /** + * @param array $fields + * List of DedupeRuleGroup fields/values. + * Ex: ['is_reserved' => 0, 'description' => 'Store the stuff'] + * @return $this + */ + public function addMetadata(array $fields): CRM_Core_CodeGen_DedupeRule { + $this->metadata = array_merge($this->metadata, $fields); + return $this; + } + + public function toArray(): array { + $result = []; + foreach ($this->rows as $row) { + $row = $this->applySyncRules($row); + $result[] = array_merge(['dedupe_rule_group_id' => new CRM_Utils_SQL_Literal($this->var)], $this->defaults, $row); + } + return $result; + } + + public function toSQL(): string { + $result = ''; + $result .= CRM_Utils_SQL_Insert::into('civicrm_dedupe_rule_group') + ->row($this->metadata) + ->toSQL() . ";\n"; + + $rows = $this->toArray(); + if ($rows) { + $result .= CRM_Utils_SQL_Select::from('civicrm_dedupe_rule_group') + ->select("{$this->var} := max(id)") + ->where('name = @NAME', ['NAME' => $this->metadata['name']]) + ->toSQL() . ";\n"; + + $result .= CRM_Utils_SQL_Insert::into('civicrm_dedupe_rule') + ->allowLiterals() + ->rows($rows) + ->toSQL() . ";\n"; + } + return $result; + } + +} diff --git a/sql/civicrm_data/civicrm_dedupe_rule/HouseholdSupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/HouseholdSupervised.sqldata.php new file mode 100644 index 0000000000..b907f0cadd --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/HouseholdSupervised.sqldata.php @@ -0,0 +1,13 @@ +addMetadata([ + 'contact_type' => 'Household', + 'threshold' => 10, + 'used' => 'Supervised', + 'title' => ts('Name and Email'), + 'is_reserved' => 0, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'household_name', 10], + ['civicrm_email', 'email', 10], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/HouseholdUnsupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/HouseholdUnsupervised.sqldata.php new file mode 100644 index 0000000000..37499430e6 --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/HouseholdUnsupervised.sqldata.php @@ -0,0 +1,13 @@ +addMetadata([ + 'contact_type' => 'Household', + 'threshold' => 10, + 'used' => 'Unsupervised', + 'title' => ts('Name and Email'), + 'is_reserved' => 0, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'household_name', 10], + ['civicrm_email', 'email', 10], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/IndividualGeneral.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/IndividualGeneral.sqldata.php new file mode 100644 index 0000000000..762682e1b9 --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/IndividualGeneral.sqldata.php @@ -0,0 +1,17 @@ +addMetadata([ + 'contact_type' => 'Individual', + 'threshold' => 15, + 'used' => 'General', + 'title' => ts('Name and Address (reserved)'), + 'is_reserved' => 1, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'first_name', '5'], + ['civicrm_contact', 'last_name', '5'], + ['civicrm_address', 'street_address', '5'], + ['civicrm_contact', 'middle_name', '1'], + ['civicrm_contact', 'suffix_id', '1'], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/IndividualSupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/IndividualSupervised.sqldata.php new file mode 100644 index 0000000000..c4ce709b71 --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/IndividualSupervised.sqldata.php @@ -0,0 +1,15 @@ +addMetadata([ + 'contact_type' => 'Individual', + 'threshold' => 20, + 'used' => 'Supervised', + 'title' => ts('Name and Email (reserved)'), + 'is_reserved' => 1, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'first_name', 5], + ['civicrm_contact', 'last_name', 7], + ['civicrm_email', 'email', 10], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/IndividualUnsupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/IndividualUnsupervised.sqldata.php new file mode 100644 index 0000000000..b18f2cdc5c --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/IndividualUnsupervised.sqldata.php @@ -0,0 +1,13 @@ +addMetadata([ + 'contact_type' => 'Individual', + 'threshold' => 10, + 'used' => 'Unsupervised', + 'title' => ts('Email (reserved)'), + 'is_reserved' => 1, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_email', 'email', 10], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/OrganizationSupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/OrganizationSupervised.sqldata.php new file mode 100644 index 0000000000..556feea24a --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/OrganizationSupervised.sqldata.php @@ -0,0 +1,13 @@ +addMetadata([ + 'contact_type' => 'Organization', + 'threshold' => 10, + 'used' => 'Supervised', + 'title' => ts('Name and Email'), + 'is_reserved' => 0, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'organization_name', 10], + ['civicrm_email', 'email', 10], + ]); diff --git a/sql/civicrm_data/civicrm_dedupe_rule/OrganizationUnsupervised.sqldata.php b/sql/civicrm_data/civicrm_dedupe_rule/OrganizationUnsupervised.sqldata.php new file mode 100644 index 0000000000..f1fe0c3368 --- /dev/null +++ b/sql/civicrm_data/civicrm_dedupe_rule/OrganizationUnsupervised.sqldata.php @@ -0,0 +1,13 @@ +addMetadata([ + 'contact_type' => 'Organization', + 'threshold' => 10, + 'used' => 'Unsupervised', + 'title' => ts('Name and Email'), + 'is_reserved' => 0, + ]) + ->addValueTable(['rule_table', 'rule_field', 'rule_weight'], [ + ['civicrm_contact', 'organization_name', 10], + ['civicrm_email', 'email', 10], + ]); diff --git a/xml/templates/civicrm_data.tpl b/xml/templates/civicrm_data.tpl index e9b4cba688..b4b56d37d4 100644 --- a/xml/templates/civicrm_data.tpl +++ b/xml/templates/civicrm_data.tpl @@ -78,69 +78,13 @@ SELECT @opCost := value FROM civicrm_option_value WHERE name = 'Cost of Sales' a echo (include "sql/civicrm_data/civicrm_payment_processor_type.sqldata.php")->toSQL(); {/php} --- the fuzzy default dedupe rules --- IndividualSupervised uses hard-coded optimized query (CRM_Dedupe_BAO_QueryBuilder_IndividualSupervised) -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Individual', 20, 'Supervised', 'IndividualSupervised', '{ts escape="sql"}Name and Email (reserved){/ts}', 1); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'first_name', 5), - (@drgid, 'civicrm_contact', 'last_name', 7), - (@drgid, 'civicrm_email' , 'email', 10); - -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Organization', 10, 'Supervised', 'OrganizationSupervised', '{ts escape="sql"}Name and Email{/ts}', 0); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'organization_name', 10), - (@drgid, 'civicrm_email' , 'email', 10); - -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Household', 10, 'Supervised', 'HouseholdSupervised', '{ts escape="sql"}Name and Email{/ts}', 0); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'household_name', 10), - (@drgid, 'civicrm_email' , 'email', 10); - --- the strict dedupe rules --- IndividualUnsupervised uses hard-coded optimized query (CRM_Dedupe_BAO_QueryBuilder_IndividualUnsupervised) -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Individual', 10, 'Unsupervised', 'IndividualUnsupervised', '{ts escape="sql"}Email (reserved){/ts}', 1); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_email', 'email', 10); - -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Organization', 10, 'Unsupervised', 'OrganizationUnsupervised', '{ts escape="sql"}Name and Email{/ts}', 0); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'organization_name', 10), - (@drgid, 'civicrm_email' , 'email', 10); - -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Household', 10, 'Unsupervised', 'HouseholdUnsupervised', '{ts escape="sql"}Name and Email{/ts}', 0); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'household_name', 10), - (@drgid, 'civicrm_email' , 'email', 10); - --- IndividualGeneral uses hard-coded optimized query (CRM_Dedupe_BAO_QueryBuilder_IndividualGeneral) -INSERT INTO civicrm_dedupe_rule_group (contact_type, threshold, used, name, title, is_reserved) -VALUES ('Individual', 15, 'General', 'IndividualGeneral', '{ts escape="sql"}Name and Address (reserved){/ts}', 1); - -SELECT @drgid := MAX(id) FROM civicrm_dedupe_rule_group; -INSERT INTO civicrm_dedupe_rule (dedupe_rule_group_id, rule_table, rule_field, rule_weight) -VALUES (@drgid, 'civicrm_contact', 'first_name', '5'), - (@drgid, 'civicrm_contact', 'last_name', '5'), - (@drgid, 'civicrm_address', 'street_address', '5'), - (@drgid, 'civicrm_contact', 'middle_name', '1'), - (@drgid, 'civicrm_contact', 'suffix_id', '1'); +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/IndividualSupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/OrganizationSupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/HouseholdSupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/IndividualUnsupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/OrganizationUnsupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/HouseholdUnsupervised.sqldata.php")->toSQL();{/php} +{php}echo (include "sql/civicrm_data/civicrm_dedupe_rule/IndividualGeneral.sqldata.php")->toSQL();{/php} {php}echo (include "sql/civicrm_data/civicrm_county.sqldata.php")->toSQL();{/php}