diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 99fd066f7390..1811b2e206a3 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -1403,6 +1403,8 @@ public static function deprecatedGetActivitySQLClause($input) { * Contact ids. * @param string $additionalDetails * The additional information of CC and BCC appended to the activity Details. + * @param array $contributionIds + * @param int $campaignId * * @return array * ( sent, activityId) if any email is sent and activityId @@ -1420,7 +1422,8 @@ public static function sendEmail( $bcc = NULL, $contactIds = NULL, $additionalDetails = NULL, - $contributionIds = NULL + $contributionIds = NULL, + $campaignId = NULL ) { // get the contact details of logged in contact, which we set as from email if ($userID == NULL) { @@ -1469,6 +1472,7 @@ public static function sendEmail( 'details' => $details, // FIXME: check for name Completed and get ID from that lookup 'status_id' => 2, + 'campaign_id' => $campaignId, ); // CRM-5916: strip [case #…] before saving the activity (if present in subject) diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index 7f202d89d6e5..c19af80809c0 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -370,6 +370,9 @@ public static function buildQuickForm(&$form) { } } + //Added for CRM-15984: Add campaign field + CRM_Campaign_BAO_Campaign::addCampaign($form); + $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header'); } @@ -531,7 +534,8 @@ public static function postProcess(&$form) { $bcc, array_keys($form->_toContactDetails), $additionalDetails, - $contributionIds + $contributionIds, + CRM_Utils_Array::value('campaign_id', $formValues) ); $followupStatus = ''; diff --git a/templates/CRM/Contact/Form/Task/Email.tpl b/templates/CRM/Contact/Form/Task/Email.tpl index e6d25a59f67d..304ee5ea1efe 100644 --- a/templates/CRM/Contact/Form/Task/Email.tpl +++ b/templates/CRM/Contact/Form/Task/Email.tpl @@ -79,6 +79,8 @@ {help id="id-token-subject" tplFile=$tplFile isAdmin=$isAdmin file="CRM/Contact/Form/Task/Email.hlp"} + {* CRM-15984 --add campaign to email activities *} + {include file="CRM/Campaign/Form/addCampaignToComponent.tpl" campaignTrClass="crm-contactEmail-form-block-campaign_id"} {include file="CRM/Contact/Form/Task/EmailCommon.tpl"} diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 91b0af02b371..1477ec266981 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -15,7 +15,13 @@ public function setUp() { * Clean up after tests. */ public function tearDown() { - $tablesToTruncate = array('civicrm_activity', 'civicrm_activity_contact', 'civicrm_email'); + $tablesToTruncate = array( + 'civicrm_activity', + 'civicrm_activity_contact', + 'civicrm_uf_match', + 'civicrm_campaign', + 'civicrm_email', + ); $this->quickCleanup($tablesToTruncate); $this->cleanUpAfterACLs(); parent::tearDown(); @@ -808,4 +814,109 @@ protected function setUpForActivityDashboardTests() { ); } + public function testSendEmailBasic() { + $contactId = $this->individualCreate(); + + // create a logged in USER since the code references it for sendEmail user. + $this->createLoggedInUser(); + $session = CRM_Core_Session::singleton(); + $loggedInUser = $session->get('userID'); + + $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); + $contactDetailsIntersectKeys = array( + 'contact_id' => '', + 'sort_name' => '', + 'display_name' => '', + 'do_not_email' => '', + 'preferred_mail_format' => '', + 'is_deceased' => '', + 'email' => '', + 'on_hold' => '', + ); + $contactDetails = array( + array_intersect_key($contact, $contactDetailsIntersectKeys), + ); + + $subject = __FUNCTION__ . ' subject'; + $html = __FUNCTION__ . ' html'; + $text = __FUNCTION__ . ' text'; + $userID = $loggedInUser; + + list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( + $contactDetails, + $subject, + $text, + $html, + $contact['email'], + $userID, + $from = __FUNCTION__ . '@example.com' + ); + + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); + $details = "-ALTERNATIVE ITEM 0- +$html +-ALTERNATIVE ITEM 1- +$text +-ALTERNATIVE END- +"; + $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); + $this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.'); + } + + public function testSendEmailWithCampaign() { + // Create a contact and contactDetails array. + $contactId = $this->individualCreate(); + + // create a logged in USER since the code references it for sendEmail user. + $this->createLoggedInUser(); + $session = CRM_Core_Session::singleton(); + $loggedInUser = $session->get('userID'); + + $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); + $contactDetailsIntersectKeys = array( + 'contact_id' => '', + 'sort_name' => '', + 'display_name' => '', + 'do_not_email' => '', + 'preferred_mail_format' => '', + 'is_deceased' => '', + 'email' => '', + 'on_hold' => '', + ); + $contactDetails = array( + array_intersect_key($contact, $contactDetailsIntersectKeys), + ); + + // Create a campaign. + $result = $this->civicrm_api('Campaign', 'create', array( + 'version' => $this->_apiversion, + 'title' => __FUNCTION__ . ' campaign', + )); + $campaign_id = $result['id']; + + $subject = __FUNCTION__ . ' subject'; + $html = __FUNCTION__ . ' html'; + $text = __FUNCTION__ . ' text'; + $userID = $loggedInUser; + + list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( + $contactDetails, + $subject, + $text, + $html, + $contact['email'], + $userID, + $from = __FUNCTION__ . '@example.com', + $attachments = NULL, + $cc = NULL, + $bcc = NULL, + $contactIds = NULL, + $additionalDetails = NULL, + NULL, + $campaign_id + ); + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); + $this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.'); + } + }