From 985f48904d8e260a236cc6168a60554c8b8a089c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 20 Mar 2015 20:21:16 -0400 Subject: [PATCH] CRM-16112 - Support group & tag filter options in api The contact get "group_id" option seems to have no effect, so I removed it. --- CRM/Contact/BAO/Contact.php | 11 +++++++++++ CRM/Core/BAO/EntityTag.php | 17 ++++++++++++----- api/v3/Contact.php | 13 ++++++++----- api/v3/utils.php | 8 ++++++-- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 26cef83f16..e67fac0c3d 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -3329,6 +3329,17 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) return CRM_Contact_BAO_ContactType::getSelectElements(); } break; + + // The contact api supports some related entities so we'll honor that by fetching their options + case 'group_id': + case 'group': + return CRM_Contact_BAO_GroupContact::buildOptions('group_id', $context, $props); + + case 'tag_id': + case 'tag': + $props['entity_table'] = 'civicrm_contact'; + return CRM_Core_BAO_EntityTag::buildOptions('tag_id', $context, $props); + } return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); } diff --git a/CRM/Core/BAO/EntityTag.php b/CRM/Core/BAO/EntityTag.php index 2064277944..482203e230 100644 --- a/CRM/Core/BAO/EntityTag.php +++ b/CRM/Core/BAO/EntityTag.php @@ -403,14 +403,21 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag { public static function buildOptions($fieldName, $context = NULL, $props = array()) { $params = array(); - $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); + if ($fieldName == 'tag' || $fieldName == 'tag_id') { + if (!empty($props['entity_table'])) { + $entity = CRM_Utils_Type::escape($props['entity_table'], 'String'); + $params[] = "used_for LIKE '%$entity%'"; + } - // Output tag list as nested hierarchy - // TODO: This will only work when api.entity is "entity_tag". What about others? - if (($fieldName == 'tag' || $fieldName == 'tag_id') && ($context == 'search' || $context == 'create')) { - $options = CRM_Core_BAO_Tag::getTags('civicrm_contact', CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- '); + // Output tag list as nested hierarchy + // TODO: This will only work when api.entity is "entity_tag". What about others? + if ($context == 'search' || $context == 'create') { + return CRM_Core_BAO_Tag::getTags(CRM_Utils_Array::value('entity_table', $props, 'civicrm_contact'), CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- '); + } } + $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); + return $options; } diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 96e88dfa99..f98f0c2f83 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -277,14 +277,17 @@ function _civicrm_api3_contact_get_spec(&$params) { 'title' => 'Primary Instant Messenger ID', 'type' => CRM_Utils_Type::T_INT, ); - $params['group_id'] = array( - 'title' => 'Group Memberships (filter)', - ); $params['group'] = array( - 'title' => 'Group Memberships (filter, array)', + 'title' => 'Group', + 'pseudoconstant' => array( + 'table' => 'civicrm_group', + ), ); $params['tag'] = array( - 'title' => 'Assigned tags (filter, array)', + 'title' => 'Tags', + 'pseudoconstant' => array( + 'table' => 'civicrm_tag', + ), ); $params['birth_date_low'] = array('name' => 'birth_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or greater than')); $params['birth_date_high'] = array('name' => 'birth_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or less than')); diff --git a/api/v3/utils.php b/api/v3/utils.php index aa680fb628..9cc7f0e2e4 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2185,7 +2185,7 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN * @return bool|string * fieldName or FALSE if the field does not exist */ -function _civicrm_api3_api_resolve_alias($entity, $fieldName) { +function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create') { if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) { return $fieldName; } @@ -2194,7 +2194,7 @@ function _civicrm_api3_api_resolve_alias($entity, $fieldName) { } $result = civicrm_api($entity, 'getfields', array( 'version' => 3, - 'action' => 'create', + 'action' => $action, )); $meta = $result['values']; if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) { @@ -2211,6 +2211,10 @@ function _civicrm_api3_api_resolve_alias($entity, $fieldName) { return $info['name']; } } + // Create didn't work, try with get + if ($action == 'create') { + return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get'); + } return FALSE; } -- 2.25.1