REF - Use `CRM_Contact_BAO_ContactType::basicTypes()` instead of hardcoded lists
authorColeman Watts <coleman@civicrm.org>
Thu, 6 Jan 2022 17:17:29 +0000 (12:17 -0500)
committerColeman Watts <coleman@civicrm.org>
Thu, 6 Jan 2022 19:37:57 +0000 (14:37 -0500)
Makes code more flexible/forgiving if new contact types are added in the future,
or if existing contact types are disabled.

17 files changed:
CRM/Contact/Form/Contact.php
CRM/Contact/Import/Form/MapField.php
CRM/Contact/Import/Parser/Contact.php
CRM/Core/BAO/CustomField.php
CRM/Core/BAO/Mapping.php
CRM/Core/BAO/UFField.php
CRM/Core/BAO/UFGroup.php
CRM/Dedupe/BAO/DedupeRule.php
CRM/Dedupe/BAO/DedupeRuleGroup.php
CRM/Report/Form.php
CRM/UF/Form/Field.php
CRM/UF/Page/ProfileEditor.php
CRM/Utils/Migrate/Export.php
CRM/Utils/Migrate/Import.php
api/v3/utils.php
ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php

index 7168f0a9f804ee086c73e42e80c0d7b64f04376e..0da2759780a70f10294d5fa4eb3acf407c687e3d 100644 (file)
@@ -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'));
       }
index 7e3ffd33fc8a1a3198a4221d76cbcb5f3a382c18..c85e9bbae852a84df44a7e4502d7b8e27be1b9fd 100644 (file)
@@ -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',
index 59dafa45603802f3487f536cfe0ffb44a70f776d..36d581f43e061ed8f7ce026e3f4c6c5ec8bacf80 100644 (file)
@@ -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'],
index 984afff29a5984ec3a37445a55f9830f8488f4d8..060b31b965bd8f56a966ca0801dc033dbbe8c695 100644 (file)
@@ -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;
   }
 
   /**
index 5c2d883ac8549761d8f3114864740ce6293f497c..c186fc67647bbfd7fdfae0fe28467e43010da3f5 100644 (file)
@@ -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]]
index b52c3b19c5853e3d40178f0516216758b1846a70..3f057c0aff65d7ac87a0e4279bf1e534f85cf7a0 100644 (file)
@@ -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);
index ad9c7d1a018115dfa3f71aefe69698a639b61c5f..60e916f11ff94e223969ad77f9a2a41c0727a20c 100644 (file)
@@ -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;
       }
index a1d9d6394476ef14b7552ad611a01477ed89c0b4..5cd159c7b26fc400cd2a5778fcb917b52f807f92 100644 (file)
@@ -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']);
index 4dedd05586b0ac3ae595191e5228c6e88c0c779c..e40118c7831ca21716e4156838a22feec7e04b11 100644 (file)
@@ -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'])) {
index 6b6d37768da0a4d35f1ffa7f7fd34790c7249272..78ba43e17b3c252539c3a5f6c74a0c8864ecfa49 100644 (file)
@@ -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,
index 43509401ab041472664103b07e36f93d05ae01fa..b639b71b1496c6bd894744c0d72702ae938ee4d5 100644 (file)
@@ -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.');
         }
index 061b7ce742bc9082e922f67ce3c2d33bf5e6eeee..8222086ddabc19939b3f3f4393de514de9fa50db 100644 (file)
@@ -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' => [
index 7dc6b17d7f67b6babea222205437a2a21f9414bb..9e88893bd13ce140ba2b94d70b5f60d6c48f942c 100644 (file)
@@ -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;
             }
index 819fcc0ea005c47c66039cde840319c7b6dc42ea..aa4b937ef91e77443c913536be3fae3951b600a0 100644 (file)
@@ -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'])) {
index 3272066ef75adfc181636cb22deb3773e0528006..19a3040c99f8a0673c61462f21adc8d34ca733c7 100644 (file)
@@ -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';
   }
 
index 6ac1d0350339d2e588b2705cf4f34453a0c7c921..7038b42321ddc187aa3c63b521328746555e1011 100644 (file)
@@ -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';
     }
index 5c73354997c1d09a7574e440dcb6e8b761ad8ba8..b38fc04c6c58664abeb14138cfadb1250c9b2220 100644 (file)
@@ -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']);
     }