From 6e1bb60cac668fc94c3746d8afd101367140d367 Mon Sep 17 00:00:00 2001 From: Nileema Date: Tue, 4 Feb 2014 19:00:05 +0530 Subject: [PATCH] --HR-253 Activity.get API - Filter by target_contact_id --- CRM/Activity/BAO/Activity.php | 13 ++++++++ CRM/Activity/BAO/ActivityContact.php | 4 +++ CRM/Core/DAO.php | 2 ++ api/v3/utils.php | 1 + tests/phpunit/api/v3/ActivityTest.php | 44 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index c8f890f838..938c8d8eba 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -2589,5 +2589,18 @@ INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n return self::getActivityContact($activityId, $sourceID); } + function setApiFilter(&$params) { + if (CRM_Utils_Array::value('target_contact_id', $params)) { + $this->selectAdd(); + $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); + $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); + $obj = new CRM_Activity_BAO_ActivityContact(); + $params['return.target_contact_id'] = 1; + $this->joinAdd($obj, 'LEFT'); + $this->selectAdd('civicrm_activity.*'); + $this->whereAdd(" civicrm_activity_contact.contact_id = {$params['target_contact_id']} AND civicrm_activity_contact.record_type_id = {$targetID}"); + } + } + } diff --git a/CRM/Activity/BAO/ActivityContact.php b/CRM/Activity/BAO/ActivityContact.php index fd57585252..1d5aff9834 100644 --- a/CRM/Activity/BAO/ActivityContact.php +++ b/CRM/Activity/BAO/ActivityContact.php @@ -142,4 +142,8 @@ AND civicrm_contact.is_deleted = 0 return $activityContact; } + function links() { + $link = array('activity_id' => 'civicrm_activity:id'); + return $link; + } } diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index fbd2f6c468..ae59e51092 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1955,4 +1955,6 @@ EOS; return substr($string, 0, $length - 8) . "_{$md5string}"; } + function setApiFilter(&$params) {} + } diff --git a/api/v3/utils.php b/api/v3/utils.php index d5b54dd75a..ce2a2d8efb 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -559,6 +559,7 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) { $dao->selectAdd($allfields[$uniqueVal]); } } + $dao->setApiFilter($params); } /** diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index b2840bb2a2..9603e81988 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -547,6 +547,50 @@ class api_v3_ActivityTest extends CiviUnitTestCase { $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']); $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id'])); } + + + /** + * Test civicrm_activity_get() with filter target_contact_id + */ + function testActivityGetTargetFilter() { + $params = $this->_params; + $contact1Params = array( + 'first_name' => 'John', + 'middle_name' => 'J.', + 'last_name' => 'Anderson', + 'prefix_id' => 3, + 'suffix_id' => 3, + 'email' => 'john_anderson@civicrm.org', + 'contact_type' => 'Individual', + ); + + $contact1 = $this->individualCreate($contact1Params); + $contact2Params = array( + 'first_name' => 'Michal', + 'middle_name' => 'J.', + 'last_name' => 'Anderson', + 'prefix_id' => 3, + 'suffix_id' => 3, + 'email' => 'michal_anderson@civicrm.org', + 'contact_type' => 'Individual', + ); + + $contact2 = $this->individualCreate($contact2Params); + + $params['assignee_contact_id'] = array($contact1, $contact2); + $params['target_contact_id'] = array($contact2 => $contact2); + $activity = $this->callAPISuccess('Activity', 'Create', $params); + + $activityget = $this->callAPISuccess('Activity', 'get', array('id' => $activity['id'], 'target_contact_id' => $contact2,'return.target_contact_id' => 1)); + $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__); + $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__); + + $activityget = $this->callAPISuccess('activity', 'get', array('target_contact_id' => $this->_contactID,'return.target_contact_id' => 1,'id' => $activity['id'])); + if ($activityget['count'] > 0) { + $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__); + } + } + /* * test that get functioning does filtering */ -- 2.25.1