Skip to content

Commit

Permalink
[REF] Cleanup function for retrieving contact types.
Browse files Browse the repository at this point in the history
This switches to using the apiv4 as part of trying to eliminate calls to executeQuery where the DAO is passed in
like

```
       = CRM_Core_DAO::executeQuery(, [],
        FALSE, 'CRM_Contact_DAO_ContactType'
      );
```

Note that this file seems to be the only place this is done.

Also note that I added tests first & the changes to the test highlight what is changed in the PR in the output - namely
- stricter type casting (courtesy apiv4)
- same keys present for all types
  • Loading branch information
eileenmcnaughton committed Jun 21, 2020
1 parent c67d09e commit ce718f7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 42 deletions.
93 changes: 51 additions & 42 deletions CRM/Contact/BAO/ContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static function isActive($contactType) {
/**
* Retrieve basic contact type information.
*
* @todo - call getAllContactTypes & return filtered results.
*
* @param bool $includeInactive
*
* @return array
Expand Down Expand Up @@ -111,6 +113,8 @@ public static function basicTypePairs($all = FALSE, $key = 'name') {
/**
* Retrieve all subtypes Information.
*
* @todo - call getAllContactTypes & return filtered results.
*
* @param array $contactType
* ..
* @param bool $all
Expand Down Expand Up @@ -217,54 +221,26 @@ public static function contactTypes($all = FALSE) {
/**
* Retrieve info array about all types i.e basic + subtypes.
*
* @todo deprecate calling this with $all = TRUE in favour of getAllContactTypes
* & ideally add getActiveContactTypes & call that from this fully
* deprecated function.
*
* @param bool $all
*
* @return array
* Array of basic types + all subtypes.
* @throws \API_Exception
*/
public static function contactTypeInfo($all = FALSE) {

if (!isset(Civi::$statics[__CLASS__]['contactTypeInfo'])) {
Civi::$statics[__CLASS__]['contactTypeInfo'] = [];
}
$_cache = &Civi::$statics[__CLASS__]['contactTypeInfo'];

$argString = $all ? 'CRM_CT_CTI_1' : 'CRM_CT_CTI_0';
if (!array_key_exists($argString, $_cache)) {
$cache = CRM_Utils_Cache::singleton();
$_cache[$argString] = $cache->get($argString);
if (!$_cache[$argString]) {
$_cache[$argString] = [];

$sql = '
SELECT type.*, parent.name as parent, parent.label as parent_label
FROM civicrm_contact_type type
LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id';

if ($all === FALSE) {
$sql .= ' WHERE type.is_active = 1';
}

$dao = CRM_Core_DAO::executeQuery($sql,
[],
FALSE,
'CRM_Contact_DAO_ContactType'
);
while ($dao->fetch()) {
$value = [];
CRM_Core_DAO::storeValues($dao, $value);
if (array_key_exists('parent_id', $value)) {
$value['parent'] = $dao->parent;
$value['parent_label'] = $dao->parent_label;
}
$_cache[$argString][$dao->name] = $value;
$contactTypes = self::getAllContactTypes();
if (!$all) {
foreach ($contactTypes as $index => $value) {
if (!$value['is_active']) {
unset($contactTypes[$index]);
}

$cache->set($argString, $_cache[$argString]);
}
}

return $_cache[$argString];
return $contactTypes;
}

/**
Expand All @@ -276,6 +252,7 @@ public static function contactTypeInfo($all = FALSE) {
*
* @return array
* Array of basictypes with name as 'built-in name' and 'label' as value
* @throws \API_Exception
*/
public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) {
$types = self::contactTypeInfo($all);
Expand Down Expand Up @@ -325,6 +302,7 @@ public static function getSelectElements(
$_cache = [];
}

// @todo - call getAllContactTypes & return filtered results.
$argString = $all ? 'CRM_CT_GSE_1' : 'CRM_CT_GSE_0';
$argString .= $isSeparator ? '_1' : '_0';
$argString .= $separator;
Expand Down Expand Up @@ -658,7 +636,8 @@ public static function setIsActive($id, $is_active) {
/**
* @param string $typeName
*
* @return mixed
* @return string
* @throws \API_Exception
*/
public static function getLabel($typeName) {
$types = self::contactTypeInfo(TRUE);
Expand Down Expand Up @@ -804,14 +783,16 @@ public static function getSubtypeCustomPair($contactType, $subtypeSet = []) {

/**
* Function that does something.
* @todo what does this function do?
*
* @param int $contactID
* @param $contactType
* @param string $contactType
* @param array $oldSubtypeSet
* @param array $newSubtypeSet
*
* @return bool
* @throws \CRM_Core_Exception
*
* @todo what does this function do?
*/
public static function deleteCustomSetForSubtypeMigration(
$contactID,
Expand Down Expand Up @@ -841,6 +822,8 @@ public static function deleteCustomSetForSubtypeMigration(
* @param array $subtypesToPreserve
*
* @return bool
*
* @throws \CRM_Core_Exception
*/
public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypesToPreserve = []) {
if (!$gID or empty($subtypes)) {
Expand Down Expand Up @@ -886,11 +869,37 @@ public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypes
* Entity id.
*
* @return null|string
*
* @throws \CRM_Core_Exception
*/
public static function deleteCustomRowsForEntityID($customTable, $entityID) {
$customTable = CRM_Utils_Type::escape($customTable, 'String');
$query = "DELETE FROM {$customTable} WHERE entity_id = %1";
return CRM_Core_DAO::singleValueQuery($query, [1 => [$entityID, 'Integer']]);
}

/**
* Get all contact types, leveraging caching.
*
* @return array
*
* @throws \API_Exception
*/
protected static function getAllContactTypes() {
if (!Civi::cache('contactTypes')->has('all')) {
$contactTypes = (array) ContactType::get()->setCheckPermissions(FALSE)
->setSelect(['id', 'name', 'label', 'description', 'is_active', 'is_reserved', 'image_URL', 'parent_id', 'parent_id:name', 'parent_id:label'])
->execute()->indexBy('name');

foreach ($contactTypes as $id => $contactType) {
$contactTypes[$id]['parent'] = $contactType['parent_id:name'];
$contactTypes[$id]['parent_label'] = $contactType['parent_id:label'];
unset($contactTypes[$id]['parent_id:name'], $contactTypes[$id]['parent_id:label']);
}
Civi::cache('contactTypes')->set('all', $contactTypes);
}
$contactTypes = Civi::cache('contactTypes')->get('all');
return $contactTypes;
}

}
33 changes: 33 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public function getExpectedContactTypes() {
'label' => 'Individual',
'is_active' => '1',
'is_reserved' => '1',
'description' => NULL,
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
'image_URL' => NULL,
],
'Household' =>
[
Expand All @@ -141,6 +146,11 @@ public function getExpectedContactTypes() {
'label' => 'Household',
'is_active' => '1',
'is_reserved' => '1',
'description' => NULL,
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
'image_URL' => NULL,
],
'Organization' =>
[
Expand All @@ -149,6 +159,11 @@ public function getExpectedContactTypes() {
'label' => 'Organization',
'is_active' => '1',
'is_reserved' => '1',
'description' => NULL,
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
'image_URL' => NULL,
],
'Student' =>
[
Expand All @@ -158,8 +173,10 @@ public function getExpectedContactTypes() {
'parent_id' => '1',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Individual',
'parent_label' => 'Individual',
'image_URL' => NULL,
],
'Parent' =>
[
Expand All @@ -169,8 +186,10 @@ public function getExpectedContactTypes() {
'parent_id' => '1',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Individual',
'parent_label' => 'Individual',
'image_URL' => NULL,
],
'Staff' =>
[
Expand All @@ -180,8 +199,10 @@ public function getExpectedContactTypes() {
'parent_id' => '1',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Individual',
'parent_label' => 'Individual',
'image_URL' => NULL,
],
'Team' =>
[
Expand All @@ -191,8 +212,10 @@ public function getExpectedContactTypes() {
'parent_id' => '3',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Organization',
'parent_label' => 'Organization',
'image_URL' => NULL,
],
'Sponsor' =>
[
Expand All @@ -202,8 +225,10 @@ public function getExpectedContactTypes() {
'parent_id' => '3',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Organization',
'parent_label' => 'Organization',
'image_URL' => NULL,
],
'sub1_individual' =>
[
Expand All @@ -213,8 +238,10 @@ public function getExpectedContactTypes() {
'parent_id' => '1',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Individual',
'parent_label' => 'Individual',
'image_URL' => NULL,
],
'sub2_individual' =>
[
Expand All @@ -224,8 +251,10 @@ public function getExpectedContactTypes() {
'parent_id' => '1',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Individual',
'parent_label' => 'Individual',
'image_URL' => NULL,
],
'sub_organization' =>
[
Expand All @@ -235,8 +264,10 @@ public function getExpectedContactTypes() {
'parent_id' => '3',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Organization',
'parent_label' => 'Organization',
'image_URL' => NULL,
],
'sub_household' =>
[
Expand All @@ -246,8 +277,10 @@ public function getExpectedContactTypes() {
'parent_id' => '2',
'is_active' => '1',
'is_reserved' => '0',
'description' => NULL,
'parent' => 'Household',
'parent_label' => 'Household',
'image_URL' => NULL,
],
];
}
Expand Down

0 comments on commit ce718f7

Please sign in to comment.