Skip to content

Commit

Permalink
Merge pull request #10979 from colemanw/CRM-21182
Browse files Browse the repository at this point in the history
CRM-21182 - Activity API: Fetch case fields via the join syntax
  • Loading branch information
colemanw authored Sep 15, 2017
2 parents db3a540 + 5394ee6 commit 30d7235
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/v3/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/api/v3/ActivityCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

}

0 comments on commit 30d7235

Please sign in to comment.