From: Coleman Watts Date: Mon, 2 Jan 2017 06:32:41 +0000 (-0500) Subject: CRM-19816 - Improve activity search filters X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1196e08698ba51c175dda54a2ee4356b24fe1ab0;p=civicrm-core.git CRM-19816 - Improve activity search filters --- diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 96ad5253e6..bf57eda13e 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -223,6 +223,54 @@ function _civicrm_api3_activity_create_spec(&$params) { } +/** + * Specify Metadata for get. + * + * @param array $params + */ +function _civicrm_api3_activity_get_spec(&$params) { + $params['tag_id'] = array( + 'name' => 'tag_id', + 'title' => 'Tags', + 'description' => 'Find activities with specified tags.', + 'type' => 1, + 'FKClassName' => 'CRM_Core_DAO_Tag', + 'FKApiName' => 'Tag', + ); + $params['case_id'] = array( + 'name' => 'case_id', + 'title' => 'Cases', + 'description' => 'Find activities within specified cases.', + 'type' => 1, + 'FKClassName' => 'CRM_Case_DAO_Case', + 'FKApiName' => 'Case', + ); + $params['target_contact_id'] = array( + 'name' => 'target_contact_id', + 'title' => 'Target Contact ID', + 'description' => 'Find activities with specified target contact.', + 'type' => 1, + 'FKClassName' => 'CRM_Contact_DAO_Contact', + 'FKApiName' => 'Contact', + ); + $params['source_contact_id'] = array( + 'name' => 'source_contact_id', + 'title' => 'Source Contact ID', + 'description' => 'Find activities with specified source contact.', + 'type' => 1, + 'FKClassName' => 'CRM_Contact_DAO_Contact', + 'FKApiName' => 'Contact', + ); + $params['assignee_contact_id'] = array( + 'name' => 'assignee_contact_id', + 'title' => 'Assignee Contact ID', + 'description' => 'Find activities with specified assignee contact.', + 'type' => 1, + 'FKClassName' => 'CRM_Contact_DAO_Contact', + 'FKApiName' => 'Contact', + ); +} + /** * Gets a CiviCRM activity according to parameters. * @@ -266,6 +314,7 @@ function civicrm_api3_activity_get($params) { } else { $sql = CRM_Utils_SQL_Select::fragment(); + // Support search by activity_contact $options = civicrm_api3('ActivityContact', 'getoptions', array('field' => 'record_type_id')); $options = $options['values']; $activityContactOptions = array( @@ -275,17 +324,31 @@ function civicrm_api3_activity_get($params) { ); foreach ($activityContactOptions as $activityContactName => $activityContactValue) { if (!empty($params[$activityContactName])) { - // If the intent is to have multiple joins -- one for each relation -- then you would - // need different table aliases. Consider replacing 'ac' and passing in a '!alias' param, - // with a different value for each relation. - $sql->join( - 'activity_' . $activityContactName, - 'LEFT JOIN civicrm_activity_contact ac ON a.id = ac.activity_id AND ac.record_type_id = #typeId', - array('typeId' => $activityContactValue) + if (!is_array($params[$activityContactName])) { + $params[$activityContactName] = array('=' => $params[$activityContactName]); + } + $clause = \CRM_Core_DAO::createSQLFilter('contact_id', $params[$activityContactName]); + $sql->where('a.id IN (SELECT activity_id FROM civicrm_activity_contact WHERE record_type_id = #typeId AND !clause)', + array('#typeId' => $activityContactValue, '!clause' => $clause) ); - $sql->where('ac.contact_id IN (#cid)', array( - 'cid' => $params[$activityContactName], - )); + } + } + if (!empty($params['tag_id'])) { + if (!is_array($params['tag_id'])) { + $params['tag_id'] = array('=' => $params['tag_id']); + } + $clause = \CRM_Core_DAO::createSQLFilter('tag_id', $params['tag_id']); + if ($clause) { + $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['case_id'])) { + if (!is_array($params['case_id'])) { + $params['case_id'] = array('=' => $params['case_id']); + } + $clause = \CRM_Core_DAO::createSQLFilter('case_id', $params['case_id']); + if ($clause) { + $sql->where('a.id IN (SELECT activity_id FROM civicrm_case_activity WHERE !clause)', array('!clause' => $clause)); } } $activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql); diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index 3b4f5b508f..afe7e74b88 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -680,13 +680,12 @@ class api_v3_ActivityTest extends CiviUnitTestCase { * Test that activity.get api works when filtering on subject. */ public function testActivityGetSubjectFilter() { - $subject = 'test activity ' . __FUNCTION__; + $subject = 'test activity ' . __FUNCTION__ . mt_rand(); $params = $this->_params; $params['subject'] = $subject; $activity = $this->callAPISuccess('Activity', 'Create', $params); $activityget = $this->callAPISuccess('activity', 'getsingle', array( 'subject' => $subject, - 'id' => $activity['id'], )); $this->assertEquals($activityget['subject'], $subject); } @@ -695,17 +694,29 @@ class api_v3_ActivityTest extends CiviUnitTestCase { * Test that activity.get api works when filtering on details. */ public function testActivityGetDetailsFilter() { - $details = 'test activity ' . __FUNCTION__; + $details = 'test activity ' . __FUNCTION__ . mt_rand(); $params = $this->_params; $params['details'] = $details; $activity = $this->callAPISuccess('Activity', 'Create', $params); $activityget = $this->callAPISuccess('activity', 'getsingle', array( 'details' => $details, - 'id' => $activity['id'], )); $this->assertEquals($activityget['details'], $details); } + /** + * Test that activity.get api works when filtering on tag. + */ + public function testActivityGetTagFilter() { + $tag = $this->callAPISuccess('Tag', 'create', array('name' => mt_rand(), '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( + 'tag_id' => $tag['id'], + )); + $this->assertEquals($activityget['id'], $activity['id']); + } + /** * test that get functioning does filtering. */