CRM-16112 - Support group & tag filter options in api
authorColeman Watts <coleman@civicrm.org>
Sat, 21 Mar 2015 00:21:16 +0000 (20:21 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 21 Mar 2015 00:31:53 +0000 (20:31 -0400)
The contact get "group_id" option seems to have no effect, so I removed it.

CRM/Contact/BAO/Contact.php
CRM/Core/BAO/EntityTag.php
api/v3/Contact.php
api/v3/utils.php

index 26cef83f16af3012875ec54174ecc74199b67e2a..e67fac0c3dc0bb4d6a8db70b05b8c3f7c5e7dc60 100644 (file)
@@ -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);
   }
index 2064277944b02cb02d945deb69ffcea4674997c7..482203e230e0872fa4233c0958be18bb91ca2d72 100644 (file)
@@ -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;
   }
 
index 96e88dfa995119227dbc1c599ae371f26888208e..f98f0c2f836f645c74ed85792d4459f3f20d2ff2 100644 (file)
@@ -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'));
index aa680fb628776f795856d2f89b5b0072688f41f0..9cc7f0e2e4414d82ec3878855dd278b40830a524 100644 (file)
@@ -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;
 }