From 2c9e4446d34a87f19d4614e6633bc4f6b9188eb3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 13 Sep 2017 11:09:50 -0400 Subject: [PATCH 1/2] CRM-21182 - Activity API: Fetch case fields via the join syntax --- api/v3/Activity.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 924bfe8cb410..45b39dea0f58 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -251,6 +251,7 @@ function _civicrm_api3_activity_get_spec(&$params) { 'type' => CRM_Utils_Type::T_INT, 'FKClassName' => 'CRM_Case_DAO_Case', 'FKApiName' => 'Case', + 'supports_joins' => TRUE, ); $params['contact_id'] = array( 'title' => 'Activity Contact ID', @@ -481,11 +482,16 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options) } $tagGet = array('tag_id', 'entity_id'); + $caseGet = $caseIds = array(); foreach (array_keys($returns) as $key) { if (strpos($key, 'tag_id.') === 0) { $tagGet[] = $key; $returns['tag_id'] = 1; } + if (strpos($key, 'case_id.') === 0) { + $caseGet[] = str_replace('case_id.', '', $key); + $returns['case_id'] = 1; + } } foreach ($returns as $n => $v) { @@ -548,6 +554,7 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options) array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES))); while ($dao->fetch()) { $activities[$dao->activity_id]['case_id'][] = $dao->case_id; + $caseIds[$dao->case_id] = $dao->case_id; } break; @@ -564,6 +571,29 @@ function _civicrm_api3_activity_get_formatResult($params, $activities, $options) } } + // Fetch case fields via the join syntax + // Note this is limited to the first case if the activity belongs to more than one + if ($caseGet && $caseIds) { + $cases = civicrm_api3('Case', 'get', array( + 'id' => array('IN' => $caseIds), + 'options' => array('limit' => 0), + 'check_permissions' => !empty($params['check_permissions']), + 'return' => $caseGet, + )); + foreach ($activities as &$activity) { + if (!empty($activity['case_id'])) { + $case = CRM_Utils_Array::value($activity['case_id'][0], $cases['values']); + if ($case) { + foreach ($case as $key => $value) { + if ($key != 'id') { + $activity['case_id.' . $key] = $value; + } + } + } + } + } + } + // Legacy extras if (!empty($params['contact_id'])) { $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get'); From 5394ee605f5e67bb0b2a0c5606cff0f78b3d5f9c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 14 Sep 2017 17:47:26 -0400 Subject: [PATCH 2/2] CRM-21182 - Add test for fetching case fields in activity.get --- tests/phpunit/api/v3/ActivityCaseTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/phpunit/api/v3/ActivityCaseTest.php b/tests/phpunit/api/v3/ActivityCaseTest.php index 9f8a2a31e990..2a8187b0026a 100644 --- a/tests/phpunit/api/v3/ActivityCaseTest.php +++ b/tests/phpunit/api/v3/ActivityCaseTest.php @@ -77,4 +77,15 @@ public function testGet() { $this->assertEquals(array(), array_intersect($getByCaseId_ids, $getByCaseNull_ids)); } + public function testActivityGetWithCaseInfo() { + $activities = $this->callAPISuccess('Activity', 'get', array( + 'sequential' => 1, + 'case_id' => $this->_case['id'], + 'return' => array('case_id', 'case_id.subject'), + )); + $this->assertEquals(__CLASS__, $activities['values'][0]['case_id.subject']); + // Note - case_id is always an array + $this->assertEquals($this->_case['id'], $activities['values'][0]['case_id'][0]); + } + }