From cfcf91326666bafc945e98fd6f076e6bc375866d Mon Sep 17 00:00:00 2001 From: yashodha Date: Mon, 22 Jun 2015 15:43:43 +0530 Subject: [PATCH] CRM-16665: add followup functionality for email ---------------------------------------- * CRM-16665: Add followup activity to "Send Email" form https://issues.civicrm.org/jira/browse/CRM-16665 --- CRM/Contact/Form/Task/EmailCommon.php | 86 +++++++++++++++++-- templates/CRM/Activity/Form/Activity.tpl | 29 +------ templates/CRM/Activity/Form/FollowUp.tpl | 30 +++++++ templates/CRM/Case/Form/Activity.tpl | 32 +------ .../CRM/Contact/Form/Task/EmailCommon.tpl | 1 + 5 files changed, 114 insertions(+), 64 deletions(-) create mode 100644 templates/CRM/Activity/Form/FollowUp.tpl diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index e36ed8646c..870fb2ef4e 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -313,6 +313,51 @@ class CRM_Contact_Form_Task_EmailCommon { $form->addDefaultButtons(ts('Send Email'), 'upload'); } + $fields = array( + 'followup_assignee_contact_id' => array( + 'type' => 'entityRef', + 'label' => ts('Assigned to'), + 'attributes' => array( + 'multiple' => TRUE, + 'create' => TRUE, + 'api' => array('params' => array('is_deceased' => 0)), + ), + ), + 'followup_activity_type_id' => array( + 'type' => 'select', + 'label' => ts('Followup Activity'), + 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant::ActivityType(FALSE), + 'extra' => array('class' => 'crm-select2'), + ), + 'followup_activity_subject' => array( + 'type' => 'text', + 'label' => ts('Subject'), + 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', + 'subject' + ), + ), + ); + + //add followup date + $form->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime')); + + foreach ($fields as $field => $values) { + if (!empty($fields[$field])) { + $attribute = CRM_Utils_Array::value('attributes', $values); + $required = !empty($values['required']); + + if ($values['type'] == 'select' && empty($attribute)) { + $form->addSelect($field, array('entity' => 'activity'), $required); + } + elseif ($values['type'] == 'entityRef') { + $form->addEntityRef($field, $values['label'], $attribute, $required); + } + else { + $form->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array::value('extra', $values)); + } + } + } + $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js'); } @@ -364,7 +409,6 @@ class CRM_Contact_Form_Task_EmailCommon { // check and ensure that $formValues = $form->controller->exportValues($form->getName()); - $fromEmail = $formValues['fromEmailAddress']; $from = CRM_Utils_Array::value($fromEmail, $form->_emails); $subject = $formValues['subject']; @@ -440,7 +484,6 @@ class CRM_Contact_Form_Task_EmailCommon { // format contact details array to handle multiple emails from same contact $formattedContactDetails = array(); $tempEmails = array(); - foreach ($form->_contactIds as $key => $contactId) { // if we dont have details on this contactID, we should ignore // potentially this is due to the contact not wanting to receive email @@ -476,12 +519,45 @@ class CRM_Contact_Form_Task_EmailCommon { $additionalDetails ); + $followupStatus = ''; if ($sent) { + $followupActivity = NULL; + if (!empty( $formValues['followup_activity_type_id'] )) { + $params['followup_activity_type_id'] = $formValues['followup_activity_type_id']; + $params['followup_activity_subject'] = $formValues['followup_activity_subject']; + $params['followup_date'] = $formValues['followup_date']; + $params['followup_date_time'] = $formValues['followup_date_time']; + $params['target_contact_id'] = $form->_contactIds; + $params['followup_assignee_contact_id'] = explode( ',', $formValues['followup_assignee_contact_id']); + $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activityId, $params); + $followupStatus = ts('A followup activity has been scheduled.'); + + if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification')) { + if ($followupActivity) { + $mailToFollowupContacts = array(); + $assignee = array($followupActivity->id); + $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($assignee, TRUE, FALSE); + foreach ($assigneeContacts as $values) { + $mailToFollowupContacts[$values['email']] = $values; + } + + if (!CRM_Utils_array::crmIsEmptyArray($mailToFollowupContacts)) { + $ics = new CRM_Activity_BAO_ICalendar($followupActivity); + $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $followupActivity->id); + $ics->addAttachment($attachments, $mailToFollowupContacts); + CRM_Case_BAO_Case::sendActivityCopy(NULL, $followupActivity->id, $mailToFollowupContacts, $attachments, NULL); + $ics->cleanup(); + $followupStatus .= '
' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s)."); + } + } + } + } + $count_success = count($form->_toContactDetails); - CRM_Core_Session::setStatus(ts('One message was sent successfully.', array( - 'plural' => '%count messages were sent successfully.', + CRM_Core_Session::setStatus(ts('One message was sent successfully. ', array( + 'plural' => '%count messages were sent successfully. ', 'count' => $count_success, - )), ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success'); + )). $followupStatus, ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $count_success)), 'success'); } // Display the name and number of contacts for those email is not sent. diff --git a/templates/CRM/Activity/Form/Activity.tpl b/templates/CRM/Activity/Form/Activity.tpl index aacceb61f8..8d0d60b2be 100644 --- a/templates/CRM/Activity/Form/Activity.tpl +++ b/templates/CRM/Activity/Form/Activity.tpl @@ -217,34 +217,7 @@ {if $action neq 4} {* Don't include "Schedule Follow-up" section in View mode. *} - + {include file="CRM/Activity/Form/FollowUp.tpl" type=""} {literal}