/**
* Retrieve basic contact type information.
*
- * @todo - call getAllContactTypes & return filtered results.
- *
* @param bool $includeInactive
*
* @return array
* @throws \Civi\API\Exception\UnauthorizedException
*/
public static function basicTypeInfo($includeInactive = FALSE) {
- $cacheKey = 'CRM_CT_BTI_' . (int) $includeInactive;
- $contactTypes = Civi::cache('contactTypes')->get($cacheKey, []);
- if (!$contactTypes) {
- $query = CRM_Utils_SQL_Select::from('civicrm_contact_type')
- ->where('parent_id IS NULL');
- if ($includeInactive === FALSE) {
- $query->where('is_active = 1');
- }
- $dao = CRM_Core_DAO::executeQuery($query->toSQL());
- $contactTypes = array_column($dao->fetchAll(), NULL, 'name');
- Civi::cache('contactTypes')->set($cacheKey, $contactTypes);
- }
- return $contactTypes;
+ return array_filter(self::getAllContactTypes(), function($type) use ($includeInactive) {
+ return empty($type['parent']) && ($includeInactive || $type['is_active']);
+ });
}
/**
}
/**
- *
- * retrieve list of all types i.e basic + subtypes.
+ * Retrieve list of all types i.e basic + subtypes.
*
* @param bool $all
*
*
* @return array
* Array of basic types + all subtypes.
- * @throws \API_Exception
*/
public static function contactTypeInfo($all = FALSE) {
$contactTypes = self::getAllContactTypes();
* @param null $typeName
* @param null $delimiter
*
- * @return array
+ * @return array|string
* Array of basictypes with name as 'built-in name' and 'label' as value
* @throws \API_Exception
*/
*
* @param array|string $subType contact subType.
* @return array|string
- * basicTypes.
*/
public static function getBasicType($subType) {
// @todo - use Cache class - ie like Civi::cache('contactTypes')
* @return bool
*/
public static function hasRelationships($contactId, $contactType) {
- $subTypeClause = NULL;
if (self::isaSubType($contactType)) {
$subType = $contactType;
$contactType = self::getBasicType($subType);
/**
* Get all contact types, leveraging caching.
*
- * @return array
+ * Note, this function is used within APIv4 Entity.get, so must use a
+ * SQL query instead of calling APIv4 to avoid an infinite loop.
*
+ * @return array
* @throws \API_Exception
*/
protected static function getAllContactTypes() {
foreach ($contactTypes as $id => $contactType) {
$contactTypes[$id]['parent'] = $contactType['parent_id'] ? $name_options[$contactType['parent_id']] : NULL;
$contactTypes[$id]['parent_label'] = $contactType['parent_id'] ? $label_options[$contactType['parent_id']] : NULL;
- // Fix types.
+ // Cast int/bool types.
$contactTypes[$id]['id'] = (int) $contactType['id'];
$contactTypes[$id]['parent_id'] = $contactType['parent_id'] ? (int) $contactType['parent_id'] : NULL;
$contactTypes[$id]['is_active'] = (bool) $contactType['is_active'];
$contactTypes[$id]['is_reserved'] = (bool) $contactType['is_reserved'];
- $contactTypes[$id]['description'] = $contactType['description'] ?: NULL;
- $contactTypes[$id]['image_URL'] = $contactType['image_URL'] ?: NULL;
}
$cache->set($cacheKey, $contactTypes);
}
'is_reserved' => FALSE,
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'description' => NULL,
- 'image_URL' => NULL,
+ 'description' => '',
+ 'image_URL' => '',
];
$this->assertEquals($expected, $allTypes);
}
'label' => 'Individual',
'is_active' => TRUE,
'is_reserved' => TRUE,
- 'description' => NULL,
+ 'description' => '',
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Household' =>
[
'label' => 'Household',
'is_active' => TRUE,
'is_reserved' => TRUE,
- 'description' => NULL,
+ 'description' => '',
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Organization' =>
[
'label' => 'Organization',
'is_active' => TRUE,
'is_reserved' => TRUE,
- 'description' => NULL,
+ 'description' => '',
'parent_id' => NULL,
'parent' => NULL,
'parent_label' => NULL,
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Student' =>
[
'parent_id' => 1,
'is_active' => '1',
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Parent' =>
[
'parent_id' => 1,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Staff' =>
[
'parent_id' => 1,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Team' =>
[
'parent_id' => 3,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Organization',
'parent_label' => 'Organization',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'Sponsor' =>
[
'parent_id' => 3,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Organization',
'parent_label' => 'Organization',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'sub1_individual' =>
[
'parent_id' => 1,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'sub2_individual' =>
[
'parent_id' => 1,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Individual',
'parent_label' => 'Individual',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'sub_organization' =>
[
'parent_id' => 3,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Organization',
'parent_label' => 'Organization',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
'sub_household' =>
[
'parent_id' => 2,
'is_active' => TRUE,
'is_reserved' => FALSE,
- 'description' => NULL,
+ 'description' => '',
'parent' => 'Household',
'parent_label' => 'Household',
- 'image_URL' => NULL,
+ 'image_URL' => '',
],
];
}