Skip to content

Commit

Permalink
REF - Use CRM_Contact_BAO_ContactType::basicTypes() instead of hard…
Browse files Browse the repository at this point in the history
…coded lists

Makes code more flexible/forgiving if new contact types are added in the future,
or if existing contact types are disabled.
  • Loading branch information
colemanw committed Jan 6, 2022
1 parent bf2fc66 commit 2d35bc7
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 29 deletions.
4 changes: 1 addition & 3 deletions CRM/Contact/Form/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ public function preProcess() {
$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'));
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function preProcess() {

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',
Expand Down
5 changes: 1 addition & 4 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ public static function isErrorInCustomData($params, &$errorMessage, $csType = NU
}

// 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']);
}

Expand Down Expand Up @@ -2115,9 +2115,6 @@ public function deprecated_contact_check_params(
$requiredCheck = FALSE;
}
if ($requiredCheck) {
if (isset($params['id'])) {
$required = ['Individual', 'Household', 'Organization'];
}
$required = [
'Individual' => [
['first_name', 'last_name'],
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public static function &getFields(
$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 {
Expand Down Expand Up @@ -2672,7 +2672,7 @@ public static function isSerialized($field) {
*/
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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,11 @@ public static function formattedFields(&$params, $row = FALSE) {
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]]
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/UFField.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public static function checkContactActivityProfileTypeByGroupType($ufGroupType)
}
}

$contactTypes = ['Individual', 'Household', 'Organization'];
$contactTypes = CRM_Contact_BAO_ContactType::basicTypes(TRUE);
$subTypes = CRM_Contact_BAO_ContactType::subTypes();

$profileTypeComponent = array_intersect($components, $profileTypes);
Expand Down
7 changes: 3 additions & 4 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3340,8 +3340,7 @@ public static function getDoubleOptInGroupIds(&$params, $contactId = NULL) {
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'];

Expand All @@ -3352,7 +3351,7 @@ public static function checkForMixProfiles($profileIds) {
if ($profileType == 'Contact') {
continue;
}
if (in_array($profileType, $contactTypes)) {
if (in_array($profileType, $contactTypes, TRUE)) {
if (!isset($typeCount['ctype'][$profileType])) {
$typeCount['ctype'][$profileType] = 1;
}
Expand All @@ -3363,7 +3362,7 @@ public static function checkForMixProfiles($profileIds) {
break;
}
}
elseif (in_array($profileType, $components)) {
elseif (in_array($profileType, $components, TRUE)) {
$mixProfile = TRUE;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Dedupe/BAO/DedupeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function getFieldType($fieldName) {
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']);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Dedupe/BAO/DedupeRuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function supportedFields($requestedType) {
'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'])) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5426,7 +5426,7 @@ protected function getColumns($type, $options = []) {
*/
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,
Expand Down
4 changes: 2 additions & 2 deletions CRM/UF/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,8 @@ public static function formRule($fields, $files, $self) {
}
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.');
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/UF/Page/ProfileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static function getSchema($entityTypes) {
}

// 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' => [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Migrate/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public function exportDAO($objectName, $object, $mappedFields) {
}
$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;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Migrate/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function customGroups(&$xml, &$idMap) {
$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'])) {
Expand Down
2 changes: 1 addition & 1 deletion api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
2 changes: 1 addition & 1 deletion ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function getFields($entityName, $params = []) {
'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';
}
Expand Down
4 changes: 2 additions & 2 deletions ext/afform/admin/Civi/Api4/Action/Afform/LoadAdminData.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {
$scanBlocks($info['definition']['layout']);
}

if (array_intersect($entities, ['Individual', 'Household', 'Organization'])) {
if (array_intersect($entities, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) {
$entities[] = 'Contact';
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public function _run(\Civi\Api4\Generic\Result $result) {

// 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']);
}

Expand Down

0 comments on commit 2d35bc7

Please sign in to comment.