CRM-16665: add followup functionality for email
authoryashodha <yashodha.chaku@webaccessglobal.com>
Mon, 22 Jun 2015 10:13:43 +0000 (15:43 +0530)
committeryashodha <yashodha.chaku@webaccessglobal.com>
Mon, 22 Jun 2015 10:13:43 +0000 (15:43 +0530)
----------------------------------------
* CRM-16665: Add followup activity to "Send Email" form
  https://issues.civicrm.org/jira/browse/CRM-16665

CRM/Contact/Form/Task/EmailCommon.php
templates/CRM/Activity/Form/Activity.tpl
templates/CRM/Activity/Form/FollowUp.tpl [new file with mode: 0644]
templates/CRM/Case/Form/Activity.tpl
templates/CRM/Contact/Form/Task/EmailCommon.tpl

index e36ed8646c71d5c40f216988f51dea992078c17a..870fb2ef4e3867b9fa4c5b665db222b116e163dc 100644 (file)
@@ -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 .= '<br />' . 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.
index aacceb61f8424c24d32e695dfdc6c37d9852b402..8d0d60b2be56d15fa30f50eb8acb373fea606cfa 100644 (file)
   {if $action neq 4} {* Don't include "Schedule Follow-up" section in View mode. *}
   <tr class="crm-activity-form-block-schedule_followup">
     <td colspan="2">
-      <div class="crm-accordion-wrapper collapsed">
-        <div class="crm-accordion-header">
-          {ts}Schedule Follow-up{/ts}
-        </div><!-- /.crm-accordion-header -->
-        <div class="crm-accordion-body">
-          <table class="form-layout-compressed">
-            <tr><td class="label">{ts}Schedule Follow-up Activity{/ts}</td>
-              <td>{$form.followup_activity_type_id.html}&nbsp;&nbsp;{ts}on{/ts}
-              {include file="CRM/common/jcalendar.tpl" elementName=followup_date}
-              </td>
-            </tr>
-            <tr>
-              <td class="label">{$form.followup_activity_subject.label}</td>
-              <td>{$form.followup_activity_subject.html|crmAddClass:huge}</td>
-            </tr>
-              <tr>
-                  <td class="label">
-                    {$form.followup_assignee_contact_id.label}
-                    {edit}
-                    {/edit}
-                  </td>
-                  <td>
-                    {$form.followup_assignee_contact_id.html}
-                  </td>
-              </tr>
-          </table>
-        </div><!-- /.crm-accordion-body -->
-      </div><!-- /.crm-accordion-wrapper -->
+      {include file="CRM/Activity/Form/FollowUp.tpl" type=""}
       {literal}
         <script type="text/javascript">
           CRM.$(function($) {
diff --git a/templates/CRM/Activity/Form/FollowUp.tpl b/templates/CRM/Activity/Form/FollowUp.tpl
new file mode 100644 (file)
index 0000000..5968454
--- /dev/null
@@ -0,0 +1,30 @@
+<div id="follow-up" class="crm-accordion-wrapper collapsed">
+  <div class="crm-accordion-header">
+     {ts}Schedule Follow-up{/ts}
+  </div><!-- /.crm-accordion-header -->
+  <div class="crm-accordion-body">
+    <table class="form-layout-compressed">
+      <tr class="crm-{$type}activity-form-block-followup_activity_type_id">
+       <td class="label">{ts}Schedule Follow-up Activity{/ts}</td>
+        <td>{$form.followup_activity_type_id.html}&nbsp;&nbsp;{ts}on{/ts}
+        {include file="CRM/common/jcalendar.tpl" elementName=followup_date}
+        </td>
+      </tr>
+      <tr class="crm-{$type}activity-form-block-followup_activity_subject">
+        <td class="label">{$form.followup_activity_subject.label}</td>
+        <td>{$form.followup_activity_subject.html|crmAddClass:huge}</td>
+      </tr>
+      <tr class="crm-{$type}activity-form-block-followup_assignee_contact_id">
+        <td class="label">
+        {$form.followup_assignee_contact_id.label}
+        {edit}
+        {/edit}
+        </td>
+        <td>
+          {$form.followup_assignee_contact_id.html}
+        </td>
+      </tr>
+    </table>
+  </div><!-- /.crm-accordion-body -->
+</div><!-- /.crm-accordion-wrapper -->
+     
\ No newline at end of file
index 413878d027bcd7e15e987c99cc2d5aff23b59dda..a11869b66800d4c82aa7d3b6b316e9f4a5f90f70 100644 (file)
     {/if}
   <tr class="crm-case-activity-form-block-schedule_followup">
     <td colspan="2">
-
-      <div id="follow-up" class="crm-accordion-wrapper collapsed">
-        <div class="crm-accordion-header">
-          {ts}Schedule Follow-up{/ts}
-        </div><!-- /.crm-accordion-header -->
-        <div class="crm-accordion-body">
-
-          <table class="form-layout-compressed">
-            <tr class="crm-case-activity-form-block-followup_activity_type_id">
-              <td class="label">{ts}Schedule Follow-up Activity{/ts}</td>
-              <td>{$form.followup_activity_type_id.html}&nbsp;&nbsp;{ts}on{/ts}
-              {include file="CRM/common/jcalendar.tpl" elementName=followup_date}
-              </td>
-            </tr>
-            <tr class="crm-case-activity-form-block-followup_activity_subject">
-              <td class="label">{$form.followup_activity_subject.label}</td>
-              <td>{$form.followup_activity_subject.html|crmAddClass:huge}</td>
-            </tr>
-            <tr>
-              <td class="label">
-                {$form.followup_assignee_contact_id.label}
-                {edit}
-                {/edit}
-              </td>
-              <td>
-                {$form.followup_assignee_contact_id.html}
-              </td>
-            </tr>
-          </table>
-        </div><!-- /.crm-accordion-body -->
-      </div><!-- /.crm-accordion-wrapper -->
+    {include file="CRM/Activity/Form/FollowUp.tpl" type="case-"}
     </td>
   </tr>
   {* Suppress activity status and priority for changes to status, case type and start date. PostProc will force status to completed. *}
index 1d876350a44848671f9ccc53c64ccc87efaf037b..cfd49f3e62496f273810518c8efe9001b1bf3a8d 100644 (file)
@@ -79,3 +79,4 @@
 
 {include file="CRM/Mailing/Form/InsertTokens.tpl"}
 
+{include file="CRM/Activity/Form/FollowUp.tpl" type='email-'}
\ No newline at end of file