From 40875a9126598e09ecf1cef2608d64d4b7ce3975 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 17 Feb 2017 14:06:31 -0500 Subject: [PATCH] CRM-19816 - Support activity tag joins and activity attachments --- api/v3/Activity.php | 42 +++++++++++++++++++++++-- templates/CRM/Admin/Page/APIExplorer.js | 2 +- tests/phpunit/api/v3/ActivityTest.php | 42 +++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 80f411fe6e..ba87a024a0 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -235,6 +235,14 @@ function _civicrm_api3_activity_get_spec(&$params) { 'type' => 1, 'FKClassName' => 'CRM_Core_DAO_Tag', 'FKApiName' => 'Tag', + 'supports_joins' => TRUE, + ); + $params['file_id'] = array( + 'title' => 'Attached Files', + 'description' => 'Find activities with attached files.', + 'type' => 1, + 'FKClassName' => 'CRM_Core_DAO_File', + 'FKApiName' => 'File', ); $params['case_id'] = array( 'title' => 'Cases', @@ -341,6 +349,16 @@ function civicrm_api3_activity_get($params) { $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause)); } } + if (!empty($params['file_id'])) { + _civicrm_api3_validate_integer($params, 'file_id', $extraFieldSpecs['file_id'], 'Activity'); + if (!is_array($params['file_id'])) { + $params['file_id'] = array('=' => $params['file_id']); + } + $clause = \CRM_Core_DAO::createSQLFilter('file_id', $params['file_id']); + if ($clause) { + $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_file WHERE entity_table = "civicrm_activity" AND !clause)', array('!clause' => $clause)); + } + } if (!empty($params['case_id'])) { _civicrm_api3_validate_integer($params, 'case_id', $extraFieldSpecs['case_id'], 'Activity'); if (!is_array($params['case_id'])) { @@ -401,6 +419,14 @@ function _civicrm_api3_activity_get_formatResult($params, $activities) { $returns['assignee_contact_id'] = 1; } + $tagGet = array('tag_id', 'entity_id'); + foreach (array_keys($returns) as $key) { + if (strpos($key, 'tag_id.') === 0) { + $tagGet[] = $key; + $returns['tag_id'] = 1; + } + } + foreach ($returns as $n => $v) { switch ($n) { case 'assignee_contact_id': @@ -438,11 +464,21 @@ function _civicrm_api3_activity_get_formatResult($params, $activities) { $tags = civicrm_api3('EntityTag', 'get', array( 'entity_table' => 'civicrm_activity', 'entity_id' => array('IN' => array_keys($activities)), - 'return' => array('tag_id', 'entity_id'), + 'return' => $tagGet, 'options' => array('limit' => 0), )); foreach ($tags['values'] as $tag) { - $activities[$tag['entity_id']]['tag_id'][] = (int) $tag['tag_id']; + $key = (int) $tag['entity_id']; + unset($tag['entity_id'], $tag['id']); + $activities[$key]['tag_id'][$tag['tag_id']] = $tag; + } + break; + + case 'file_id': + $dao = CRM_Core_DAO::executeQuery("SELECT entity_id, file_id FROM civicrm_entity_file WHERE entity_table = 'civicrm_activity' AND entity_id IN (%1)", + array(1 => array(implode(',', array_keys($activities)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES))); + while ($dao->fetch()) { + $activities[$dao->entity_id]['file_id'][] = $dao->file_id; } break; @@ -453,7 +489,7 @@ function _civicrm_api3_activity_get_formatResult($params, $activities) { } } - // Legacy ouput + // Legacy extras if (!empty($params['contact_id'])) { $statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get'); $typeOptions = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate'); diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 9ce6482920..b99f9eaa50 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -886,7 +886,7 @@ var name = prefix + field.name; addJoinInfo(field, name); var entity = field.FKApiName; - if (entity && field.is_core_field) { + if (entity && (field.is_core_field || field.supports_joins)) { joinable[name] = { title: field.title + ' (' + field.FKApiName + ')', entity: entity, diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index 526e03dfd8..5ce55f7c0f 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -717,6 +717,48 @@ class api_v3_ActivityTest extends CiviUnitTestCase { $this->assertEquals($activityget['id'], $activity['id']); } + /** + * Return tag info + */ + public function testJoinOnTags() { + $tagName = 'act_tag_nm_' . mt_rand(); + $tagDescription = 'act_tag_ds_' . mt_rand(); + $tagColor = '#' . substr(md5(mt_rand()), 0, 6); + $tag = $this->callAPISuccess('Tag', 'create', array('name' => $tagName, 'color' => $tagColor, 'description' => $tagDescription, 'used_for' => 'Activities')); + $activity = $this->callAPISuccess('Activity', 'Create', $this->_params); + $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id'])); + $activityget = $this->callAPISuccess('activity', 'getsingle', array( + 'id' => $activity['id'], + 'return' => array('tag_id.name', 'tag_id.description', 'tag_id.color'), + )); + $this->assertEquals($tagName, $activityget['tag_id'][$tag['id']]['tag_id.name']); + $this->assertEquals($tagColor, $activityget['tag_id'][$tag['id']]['tag_id.color']); + $this->assertEquals($tagDescription, $activityget['tag_id'][$tag['id']]['tag_id.description']); + } + + + /** + * Test that activity.get api works to filter on and return files. + */ + public function testActivityGetFile() { + $activity = $this->callAPISuccess('Activity', 'create', $this->_params); + $activity2 = $this->callAPISuccess('Activity', 'create', $this->_params2); + $file = $this->callAPISuccess('Attachment', 'create', array( + 'name' => 'actAttachment.txt', + 'mime_type' => 'text/plain', + 'description' => 'My test description', + 'content' => 'My test content', + 'entity_table' => 'civicrm_activity', + 'entity_id' => $activity2['id'], + )); + $activityget = $this->callAPISuccess('activity', 'getsingle', array( + 'file_id' => $file['id'], + 'return' => 'file_id', + )); + $this->assertEquals($activityget['id'], $activity2['id']); + $this->assertEquals($file['id'], $activityget['file_id'][0]); + } + /** * test that get functioning does filtering. */ -- 2.25.1