From 2d35bc781cdb66f440261fce17166734414b780d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 6 Jan 2022 12:17:29 -0500 Subject: [PATCH] REF - Use `CRM_Contact_BAO_ContactType::basicTypes()` instead of hardcoded lists Makes code more flexible/forgiving if new contact types are added in the future, or if existing contact types are disabled. --- CRM/Contact/Form/Contact.php | 4 +--- CRM/Contact/Import/Form/MapField.php | 2 +- CRM/Contact/Import/Parser/Contact.php | 5 +---- CRM/Core/BAO/CustomField.php | 4 ++-- CRM/Core/BAO/Mapping.php | 4 ++-- CRM/Core/BAO/UFField.php | 2 +- CRM/Core/BAO/UFGroup.php | 7 +++---- CRM/Dedupe/BAO/DedupeRule.php | 2 +- CRM/Dedupe/BAO/DedupeRuleGroup.php | 2 +- CRM/Report/Form.php | 2 +- CRM/UF/Form/Field.php | 4 ++-- CRM/UF/Page/ProfileEditor.php | 2 +- CRM/Utils/Migrate/Export.php | 2 +- CRM/Utils/Migrate/Import.php | 2 +- api/v3/utils.php | 2 +- ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php | 2 +- ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php | 4 ++-- 17 files changed, 23 insertions(+), 29 deletions(-) diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 7168f0a9f8..0da2759780 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -161,9 +161,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { $this->_contactType = CRM_Utils_Request::retrieve('ct', 'String', $this, TRUE, NULL, 'REQUEST' ); - if (!in_array($this->_contactType, - ['Individual', 'Household', 'Organization'] - ) + if (!in_array($this->_contactType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE) ) { CRM_Core_Error::statusBounce(ts('Could not get a contact id and/or contact type')); } diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 7e3ffd33fc..c85e9bbae8 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -114,7 +114,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { if ($this->_onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { //Mark Dedupe Rule Fields as required, since it's used in matching contact - foreach (['Individual', 'Household', 'Organization'] as $cType) { + foreach (CRM_Contact_BAO_ContactType::basicTypes() as $cType) { $ruleParams = [ 'contact_type' => $cType, 'used' => 'Unsupervised', diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 59dafa4560..36d581f43e 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -1003,7 +1003,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { } // get array of subtypes - CRM-18708 - if (in_array($csType, ['Individual', 'Organization', 'Household'])) { + if (in_array($csType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $csType = self::getSubtypes($params['contact_type']); } @@ -2115,9 +2115,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { $requiredCheck = FALSE; } if ($requiredCheck) { - if (isset($params['id'])) { - $required = ['Individual', 'Household', 'Organization']; - } $required = [ 'Individual' => [ ['first_name', 'last_name'], diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 984afff29a..060b31b965 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -408,7 +408,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $value = NULL; foreach ($customDataType as $dataType) { if (array_key_exists($dataType, CRM_Core_SelectValues::customGroupExtends())) { - if (in_array($dataType, ['Individual', 'Household', 'Organization'])) { + if (in_array($dataType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' "; } else { @@ -2672,7 +2672,7 @@ WHERE cf.id = %1 AND cg.is_multiple = 1"; */ public function getEntity() { $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends'); - return in_array($entity, ['Individual', 'Household', 'Organization']) ? 'Contact' : $entity; + return in_array($entity, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE) ? 'Contact' : $entity; } /** diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 5c2d883ac8..c186fc6764 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -1014,11 +1014,11 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Test\Ho return $fields; } - $types = ['Individual', 'Organization', 'Household']; + $types = CRM_Contact_BAO_ContactType::basicTypes(TRUE); foreach ($params['mapper'] as $key => $value) { $contactType = NULL; foreach ($value as $k => $v) { - if (in_array($v[0], $types)) { + if (in_array($v[0], $types, TRUE)) { if ($contactType && $contactType != $v[0]) { throw new CRM_Core_Exception(ts("Cannot have two clauses with different types: %1, %2", [1 => $contactType, 2 => $v[0]] diff --git a/CRM/Core/BAO/UFField.php b/CRM/Core/BAO/UFField.php index b52c3b19c5..3f057c0aff 100644 --- a/CRM/Core/BAO/UFField.php +++ b/CRM/Core/BAO/UFField.php @@ -414,7 +414,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1"; } } - $contactTypes = ['Individual', 'Household', 'Organization']; + $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(TRUE); $subTypes = CRM_Contact_BAO_ContactType::subTypes(); $profileTypeComponent = array_intersect($components, $profileTypes); diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index ad9c7d1a01..60e916f11f 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -3340,8 +3340,7 @@ SELECT group_id public static function checkForMixProfiles($profileIds) { $mixProfile = FALSE; - $contactTypes = ['Individual', 'Household', 'Organization']; - $subTypes = CRM_Contact_BAO_ContactType::subTypes(); + $contactTypes = CRM_Contact_BAO_ContactType::basicTypes(TRUE); $components = ['Contribution', 'Participant', 'Membership', 'Activity']; @@ -3352,7 +3351,7 @@ SELECT group_id if ($profileType == 'Contact') { continue; } - if (in_array($profileType, $contactTypes)) { + if (in_array($profileType, $contactTypes, TRUE)) { if (!isset($typeCount['ctype'][$profileType])) { $typeCount['ctype'][$profileType] = 1; } @@ -3363,7 +3362,7 @@ SELECT group_id break; } } - elseif (in_array($profileType, $components)) { + elseif (in_array($profileType, $components, TRUE)) { $mixProfile = TRUE; break; } diff --git a/CRM/Dedupe/BAO/DedupeRule.php b/CRM/Dedupe/BAO/DedupeRule.php index a1d9d63944..5cd159c7b2 100644 --- a/CRM/Dedupe/BAO/DedupeRule.php +++ b/CRM/Dedupe/BAO/DedupeRule.php @@ -241,7 +241,7 @@ class CRM_Dedupe_BAO_DedupeRule extends CRM_Dedupe_DAO_DedupeRule { if (!$entity) { // This means we have stored a custom field rather than an entity name in rule_table, figure out the entity. $entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table, 'return' => 'extends']); - if (in_array($entity, ['Individual', 'Household', 'Organization'])) { + if (in_array($entity, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $entity = 'Contact'; } $fieldName = 'custom_' . civicrm_api3('CustomField', 'getvalue', ['column_name' => $fieldName, 'return' => 'id']); diff --git a/CRM/Dedupe/BAO/DedupeRuleGroup.php b/CRM/Dedupe/BAO/DedupeRuleGroup.php index 4dedd05586..e40118c783 100644 --- a/CRM/Dedupe/BAO/DedupeRuleGroup.php +++ b/CRM/Dedupe/BAO/DedupeRuleGroup.php @@ -87,7 +87,7 @@ class CRM_Dedupe_BAO_DedupeRuleGroup extends CRM_Dedupe_DAO_DedupeRuleGroup { 'civicrm_website', ]; - foreach (['Individual', 'Organization', 'Household'] as $ctype) { + foreach (CRM_Contact_BAO_ContactType::basicTypes() as $ctype) { // take the table.field pairs and their titles from importableFields() if the table is supported foreach (CRM_Contact_BAO_Contact::importableFields($ctype) as $iField) { if (isset($iField['where'])) { diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index 6b6d37768d..78ba43e17b 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -5426,7 +5426,7 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a */ protected function getContactColumns($options = []) { $defaultOptions = [ - 'custom_fields' => ['Individual', 'Contact', 'Organization'], + 'custom_fields' => CRM_Contact_BAO_ContactType::basicTypes(), 'fields_defaults' => ['display_name', 'id'], 'order_bys_defaults' => ['sort_name ASC'], 'contact_type' => NULL, diff --git a/CRM/UF/Form/Field.php b/CRM/UF/Form/Field.php index 43509401ab..b639b71b14 100644 --- a/CRM/UF/Form/Field.php +++ b/CRM/UF/Form/Field.php @@ -968,8 +968,8 @@ class CRM_UF_Form_Field extends CRM_Core_Form { } elseif ( CRM_Utils_Array::value(1, $fields['field_name']) == 'contact_sub_type' && - !in_array($profileType, ['Individual', 'Household', 'Organization']) && - !in_array($profileType, CRM_Contact_BAO_ContactType::subTypes()) + !in_array($profileType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE) && + !in_array($profileType, CRM_Contact_BAO_ContactType::subTypes(), TRUE) ) { $errors['field_name'] = ts('Cannot add or update profile field Contact Subtype as profile type is not one of Individual, Household or Organization.'); } diff --git a/CRM/UF/Page/ProfileEditor.php b/CRM/UF/Page/ProfileEditor.php index 061b7ce742..8222086dda 100644 --- a/CRM/UF/Page/ProfileEditor.php +++ b/CRM/UF/Page/ProfileEditor.php @@ -210,7 +210,7 @@ class CRM_UF_Page_ProfileEditor extends CRM_Core_Page { } // Adding the oddball "formatting" field here because there's no other place to put it - foreach (['Individual', 'Organization', 'Household'] as $type) { + foreach (CRM_Contact_BAO_ContactType::basicTypes() as $type) { if (isset($civiSchema[$type . 'Model'])) { $civiSchema[$type . 'Model']['schema'] += [ 'formatting' => [ diff --git a/CRM/Utils/Migrate/Export.php b/CRM/Utils/Migrate/Export.php index 7dc6b17d7f..9e88893bd1 100644 --- a/CRM/Utils/Migrate/Export.php +++ b/CRM/Utils/Migrate/Export.php @@ -483,7 +483,7 @@ class CRM_Utils_Migrate_Export { } $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1)); $values = []; - if (in_array($object->extends, ['Individual', 'Organization', 'Household'])) { + if (in_array($object->extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $key = 'contact_type'; $values = $types; } diff --git a/CRM/Utils/Migrate/Import.php b/CRM/Utils/Migrate/Import.php index 819fcc0ea0..aa4b937ef9 100644 --- a/CRM/Utils/Migrate/Import.php +++ b/CRM/Utils/Migrate/Import.php @@ -221,7 +221,7 @@ WHERE v.option_group_id = %1 $valueIDs[] = $relTypeId; } } - elseif (in_array($customGroup->extends, ['Individual', 'Organization', 'Household'])) { + elseif (in_array($customGroup->extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $valueIDs = $optionValues; } elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) { diff --git a/api/v3/utils.php b/api/v3/utils.php index 3272066ef7..19a3040c99 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1096,7 +1096,7 @@ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entity $values['custom'] = []; $checkCheckBoxField = FALSE; $entity = $extends; - if (in_array($extends, ['Household', 'Individual', 'Organization'])) { + if (in_array($extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $entity = 'Contact'; } diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index 6ac1d03503..7038b42321 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -109,7 +109,7 @@ class AfformAdminMeta { 'select' => ['name', 'label', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type', 'fk_entity', 'readonly'], 'where' => [['input_type', 'IS NOT NULL']], ]; - if (in_array($entityName, ['Individual', 'Household', 'Organization'])) { + if (in_array($entityName, \CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { $params['values']['contact_type'] = $entityName; $entityName = 'Contact'; } diff --git a/ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php b/ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php index 5c73354997..b38fc04c6c 100644 --- a/ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php +++ b/ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php @@ -144,7 +144,7 @@ class LoadAdminData extends \Civi\Api4\Generic\AbstractAction { $scanBlocks($info['definition']['layout']); } - if (array_intersect($entities, ['Individual', 'Household', 'Organization'])) { + if (array_intersect($entities, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) { $entities[] = 'Contact'; } @@ -204,7 +204,7 @@ class LoadAdminData extends \Civi\Api4\Generic\AbstractAction { // Optimization - since contact fields are a combination of these three, // we'll combine them client-side rather than sending them via ajax. - elseif (array_intersect($entities, ['Individual', 'Household', 'Organization'])) { + elseif (array_intersect($entities, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) { $entities = array_diff($entities, ['Contact']); } -- 2.25.1