From 30238b5e769383c435af786fa4b574bee43e6bd5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 2 Apr 2022 19:26:51 -0400 Subject: [PATCH] ContactType - Prefer icons over image_URL This is an intermediary step toward removing the image_URL field. The UI now uses css-based icons when available, and only falls back to image_URL when an icon hasn't been set for a given sub-type. Fixes dev/user-interface#26 --- CRM/Contact/BAO/Contact/Utils.php | 66 +++++++++++++++---------------- CRM/Contact/BAO/ContactType.php | 2 +- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index 16ffcc0cd1..0e8610d065 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -19,9 +19,9 @@ use Civi\Api4\Contact; class CRM_Contact_BAO_Contact_Utils { /** - * Given a contact type, get the contact image. + * Given a contact type or sub_type(s), generate markup for the contact type icon. * - * @param string $contactType + * @param string $contactTypes * Contact type. * @param bool $urlOnly * If we need to return only image url. @@ -35,47 +35,43 @@ class CRM_Contact_BAO_Contact_Utils { * @return string * @throws \CRM_Core_Exception */ - public static function getImage($contactType, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE, $contactUrl = NULL) { - - static $imageInfo = []; + public static function getImage($contactTypes, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE, $contactUrl = NULL) { + // Ensure string data is unserialized + $contactTypes = CRM_Utils_Array::explodePadded($contactTypes); - $contactType = CRM_Utils_Array::explodePadded($contactType); - $contactType = $contactType[0]; + $allContactTypeInfo = \CRM_Contact_BAO_ContactType::getAllContactTypes(); - if (!array_key_exists($contactType, $imageInfo)) { - $imageInfo[$contactType] = []; + $imageInfo = ['url' => NULL, 'image' => NULL]; - $typeInfo = []; - $params = ['name' => $contactType]; - CRM_Contact_BAO_ContactType::retrieve($params, $typeInfo); + foreach ($contactTypes as $contactType) { + $typeInfo = $allContactTypeInfo[$contactType]; + // Prefer the first type/subtype with an icon + if (!empty($typeInfo['icon'])) { + break; + } + // Fall back to using image_URL if no subtypes have an icon if (!empty($typeInfo['image_URL'])) { $imageUrl = $typeInfo['image_URL']; - $config = CRM_Core_Config::singleton(); if (!preg_match("/^(\/|(http(s)?:)).+$/i", $imageUrl)) { - $imageUrl = $config->resourceBase . $imageUrl; + $imageUrl = CRM_Core_Config::singleton()->resourceBase . $imageUrl; } - $imageInfo[$contactType]['image'] = "
"; - $imageInfo[$contactType]['url'] = $imageUrl; + $imageInfo['image'] = "
"; + $imageInfo['url'] = $imageUrl; } - else { - if (!empty($typeInfo['parent_id'])) { - $type = CRM_Contact_BAO_ContactType::getBasicType($typeInfo['name']) . '-subtype'; - } - else { - $type = $typeInfo['name'] ?? NULL; - } + } - // do not add title since it hides contact name - if ($addProfileOverlay) { - $imageInfo[$contactType]['image'] = "
"; - } - else { - $imageInfo[$contactType]['image'] = "
"; - } - $imageInfo[$contactType]['url'] = NULL; - } + // If subtype doesn't have an image or an icon, use the parent type + if (empty($imageUrl) && empty($typeInfo['icon']) && !empty($typeInfo['parent'])) { + $typeInfo = $allContactTypeInfo[$typeInfo['parent']]; + } + + // Prefer icon over image + if (!empty($typeInfo['icon'])) { + // do not add title since it hides contact name + $title = $addProfileOverlay ? '' : htmlspecialchars($typeInfo['label']); + $imageInfo['image'] = ''; } if ($addProfileOverlay) { @@ -91,13 +87,13 @@ class CRM_Contact_BAO_Contact_Utils { "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4&is_show_email_task=1" ); - $imageInfo[$contactType]['summary-link'] = '' . $imageInfo[$contactType]['image'] . ''; + $imageInfo['summary-link'] = '' . $imageInfo['image'] . ''; } else { - $imageInfo[$contactType]['summary-link'] = $imageInfo[$contactType]['image']; + $imageInfo['summary-link'] = $imageInfo['image']; } - return $urlOnly ? $imageInfo[$contactType]['url'] : $imageInfo[$contactType]['summary-link']; + return $urlOnly ? $imageInfo['url'] : $imageInfo['summary-link']; } /** diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index a881d1f6cf..d9bb3e1b2f 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -862,7 +862,7 @@ WHERE ($subtypeClause)"; * @return array * @throws \API_Exception */ - protected static function getAllContactTypes() { + public static function getAllContactTypes() { $cache = Civi::cache('contactTypes'); $cacheKey = 'all_' . $GLOBALS['tsLocale']; $contactTypes = $cache->get($cacheKey); -- 2.25.1