CRM-13912: added sms support for scheduled reminders
authoryashodha <yashodha.chaku@webaccess.co.in>
Wed, 8 Jan 2014 11:13:15 +0000 (16:43 +0530)
committeryashodha <yashodha.chaku@webaccess.co.in>
Wed, 8 Jan 2014 11:13:15 +0000 (16:43 +0530)
CRM/Admin/Form/ScheduleReminders.php
CRM/Core/BAO/ActionSchedule.php
CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl
templates/CRM/Admin/Form/ScheduleReminders.tpl
xml/schema/Core/ActionSchedule.xml
xml/templates/civicrm_data.tpl

index 07a41c8773c11fbdfbdfdda01db412c79540a400..ccda529b21e6565c911b62418d1c8e90a6e5d097 100644 (file)
@@ -56,6 +56,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
   public function buildQuickForm() {
     parent::buildQuickForm();
     $this->_mappingID = $mappingID = NULL;
+    $providersCount = CRM_SMS_BAO_Provider::activeProviderCount();
 
     if ($this->_action & (CRM_Core_Action::DELETE)) {
       $reminderName =
@@ -134,7 +135,26 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
 
     //reminder_interval
     $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
+    $title = ts('Email');
+    $isActive = ts('Send email');
+    $recordActivity = ts('Record activity for automated email');
+    if ($providersCount) {
+      $title = ts('Email or SMS');
+      $isActive = ts('Send email or SMS');
+      $recordActivity = ts('Record activity for automated email or SMS');
+      $options = CRM_Core_OptionGroup::values('msg_mode');
+      $this->add('select', 'mode', ts('Send as'), $options);
+
+      $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc');
+
+      $providerSelect = array();
+      foreach ($providers as $provider) {
+        $providerSelect[$provider['id']] = $provider['title'];
+      }
+      $this->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE);
+    }
 
+    $this->assign('title', $title);
     foreach ($this->_freqUnits as $val => $label) {
       $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
     }
@@ -152,7 +172,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
 
     $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
 
-    $this->addElement('checkbox', 'record_activity', ts('Record activity for automated email'));
+    $this->addElement('checkbox', 'record_activity', $recordActivity);
 
     $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
       NULL, array('onclick' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
@@ -221,7 +241,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
       CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
     );
 
-    $this->add('checkbox', 'is_active', ts('Send email'));
+    $this->add('checkbox', 'is_active', $isActive);
 
     $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'));
   }
@@ -268,6 +288,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
   function setDefaultValues() {
     if ($this->_action & CRM_Core_Action::ADD) {
       $defaults['is_active'] = 1;
+      $defaults['mode'] = 'Email';
       $defaults['record_activity'] = 1;
     }
     else {
@@ -342,7 +363,9 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
       'absolute_date',
       'group_id',
       'record_activity',
-      'limit_to'
+      'limit_to',
+      'mode',
+      'sms_provider_id'
     );
     foreach ($keys as $key) {
       $params[$key] = CRM_Utils_Array::value($key, $values);
index 11a5f8f8149a3e66f66d1ca648bec6264d9a0016..c2cfd25b49923ac16e91ae33aab86ece07790266 100755 (executable)
@@ -368,8 +368,9 @@ WHERE   cas.entity_value = $id AND
     return $list;
   }
 
-  static function sendReminder($contactId, $email, $scheduleID, $from, $tokenParams) {
-
+  static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams) {
+    $email = $to['email'];
+    $phoneNumber = $to['phone'];
     $schedule = new CRM_Core_DAO_ActionSchedule();
     $schedule->id = $scheduleID;
 
@@ -456,30 +457,61 @@ WHERE   cas.entity_value = $id AND
 
       $messageSubject = $smarty->fetch("string:{$messageSubject}");
 
-      // set up the parameters for CRM_Utils_Mail::send
-      $mailParams = array(
-        'groupName' => 'Scheduled Reminder Sender',
-        'from' => $from,
-        'toName' => $contact['display_name'],
-        'toEmail' => $email,
-        'subject' => $messageSubject,
-        'entity' => 'action_schedule',
-        'entity_id' => $scheduleID,
-      );
+      if ($schedule->mode == 'SMS' or $schedule->mode == 'User_Preference') {
+        $session = CRM_Core_Session::singleton();
+        $userID = $session->get('userID') ? $session->get('userID') : $contactId;
+        $smsParams = array('To' => $phoneNumber, 'provider_id' => $schedule->sms_provider_id, 'activity_subject' => $messageSubject);
+        $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type',
+          'SMS',
+          'name'
+        );
+        $details = $html ? $html : $text;
+        $activityParams = array(
+          'source_contact_id' => $userID,
+          'activity_type_id' => $activityTypeID,
+          'activity_date_time' => date('YmdHis'),
+          'subject' => $messageSubject,
+          'details' => $details,
+          'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'),
+        );
+
+        $activity = CRM_Activity_BAO_Activity::create($activityParams);
 
-      if (!$html || $contact['preferred_mail_format'] == 'Text' ||
-        $contact['preferred_mail_format'] == 'Both'
-      ) {
-        // render the &amp; entities in text mode, so that the links work
-        $mailParams['text'] = str_replace('&amp;', '&', $text);
+        CRM_Activity_BAO_Activity::sendSMSMessage($contactId,
+          $text,
+          $html,
+          $smsParams,
+          $activity->id,
+          $userID
+        );
       }
-      if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
+
+      if ($schedule->mode == 'Email' or $schedule->mode == 'User_Preference') {
+        // set up the parameters for CRM_Utils_Mail::send
+        $mailParams = array(
+          'groupName' => 'Scheduled Reminder Sender',
+          'from' => $from,
+          'toName' => $contact['display_name'],
+          'toEmail' => $email,
+          'subject' => $messageSubject,
+          'entity' => 'action_schedule',
+          'entity_id' => $scheduleID,
+        );
+
+        if (!$html || $contact['preferred_mail_format'] == 'Text' ||
           $contact['preferred_mail_format'] == 'Both'
-        )) {
-        $mailParams['html'] = $html;
+        ) {
+          // render the &amp; entities in text mode, so that the links work
+          $mailParams['text'] = str_replace('&amp;', '&', $text);
+        }
+        if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
+            $contact['preferred_mail_format'] == 'Both'
+          )
+        ) {
+          $mailParams['html'] = $html;
+        }
+        $result = CRM_Utils_Mail::send($mailParams);
       }
-
-      $result = CRM_Utils_Mail::send($mailParams);
     }
     $schedule->free();
 
@@ -703,13 +735,29 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
         }
 
         $isError  = 0;
-        $errorMsg = '';
-        $toEmail  = CRM_Contact_BAO_Contact::getPrimaryEmail($dao->contactID);
-        if ($toEmail) {
+        $errorMsg = $toEmail = $toPhoneNumber = '';
+
+        if ($actionSchedule->mode == 'SMS' or $actionSchedule->mode == 'User_Preference') {
+          $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0);
+          $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($dao->contactID, FALSE, 'Mobile', $filters);
+          //to get primary mobile ph,if not get a first mobile phONE
+          if (!empty($toPhoneNumbers)) {
+            $toPhoneNumberDetails = reset($toPhoneNumbers);
+            $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumberDetails);
+            //contact allows to send sms
+            $toDoNotSms = 0;
+          }
+        }
+        if ($actionSchedule->mode == 'Email' or $actionSchedule->mode == 'User_Preference') {
+          $toEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($dao->contactID);
+        }
+        if ($toEmail || !(empty($toPhoneNumber) or $toDoNotSms)) {
+          $to['email'] = $toEmail;
+          $to['phone'] = $toPhoneNumber;
           $result =
             CRM_Core_BAO_ActionSchedule::sendReminder(
               $dao->contactID,
-              $toEmail,
+              $to,
               $actionSchedule->id,
               $fromEmailAddress,
               $entityTokenParams
index 1faa6c99557c6618ade5ec8159e2c9b513fa2ea1..15ee30b61c658e186ff85b3006043bd61dbc9cb2 100644 (file)
@@ -97,4 +97,22 @@ SELECT @caseCompId := id FROM `civicrm_component` where `name` like 'CiviCase';
 
 UPDATE `civicrm_option_value`
 SET is_reserved = 1
-WHERE is_reserved = 0 AND option_group_id = @option_group_id_activity_type AND component_id = @caseCompId;
\ No newline at end of file
+WHERE is_reserved = 0 AND option_group_id = @option_group_id_activity_type AND component_id = @caseCompId;
+
+-- CRM-13912
+ALTER TABLE civicrm_action_schedule
+ADD COLUMN `mode` varchar(128) COLLATE utf8_unicode_ci DEFAULT 'Email' COMMENT 'Send the message as email or sms or both.';
+
+INSERT INTO
+civicrm_option_group (name, {localize field='title'}title{/localize}, is_reserved, is_active)
+VALUES
+('msg_mode', {localize}'{ts escape="sql"}Message Mode{/ts}'{/localize}, 1, 1);
+
+SELECT @option_group_id_msg_mode := max(id) from civicrm_option_group where name = 'msg_mode';
+
+INSERT INTO
+civicrm_option_value (option_group_id, label, value, name, is_default, weight, is_reserved, is_active)
+VALUES
+(@option_group_id_msg_mode, {localize}'{ts escape="sql"}Email{/ts}'{/localize}, 'Email', 'Email', 1, 1, 1, 1),
+(@option_group_id_msg_mode, {localize}'{ts escape="sql"}SMS{/ts}'{/localize},'SMS', 'SMS', 0, 2, 1, 1),
+(@option_group_id_msg_mode, {localize}'{ts escape="sql"}User Preference{/ts}'{/localize}, 'User_Preference', 'User Preference', 0, 3, 1, 1);
index 1bd573ad75298bac689b45fb32c0f72172d6c6a1..bea5d3ef368e7ae7679e3f687b295f649761d7a2 100644 (file)
         <td class="label">{$form.group_id.label}</td>
         <td>{$form.group_id.html}</td>
     </tr>
+    <tr id="msgMode" class="crm-scheduleReminder-form-block-mode">
+      <td class="label">{$form.mode.label}</td>
+      <td>{$form.mode.html}</td>
+    </tr>
+    <tr id="smsProvider" class="crm-scheduleReminder-form-block-sms_provider_id">
+      <td class="label">{$form.sms_provider_id.label}</td>
+      <td>{$form.sms_provider_id.html}</td>
+    </tr>
   </table>
   <fieldset id="compose_id"><legend>{ts}Email{/ts}</legend>
      <table id="email-field-table" class="form-layout-compressed">
         </tr>
 
   </table>
-        {include file="CRM/Contact/Form/Task/EmailCommon.tpl" upload=1 noAttach=1}
+    <div id="email">{include file="CRM/Contact/Form/Task/EmailCommon.tpl" upload=1 noAttach=1}</div>
+    <div id="sms">{include file="CRM/Contact/Form/Task/SMSCommon.tpl" upload=1 noAttach=1}</div>
   </fieldset>
 
 {include file="CRM/common/showHideByFieldValue.tpl"
          });
      });
 
-    cj(function() {
+  cj(function () {
+    loadMsgBox();
+    cj('#mode').change(function () {
+      loadMsgBox();
+    });
+  });
+
+  function loadMsgBox() {
+    if (cj('#mode').val() == 'Email' || cj('#mode').val() == 0){
+      cj('#sms').hide();
+      cj('#smsProvider').hide();
+      cj('#email').show();
+    }
+    else if (cj('#mode').val() == 'SMS'){
+      cj('#email').hide();
+      cj('#sms').show();
+      cj('#smsProvider').show();
+    }
+    else if (cj('#mode').val() == 'User_Preference'){
+        cj('#email').show();
+        cj('#sms').show();
+        cj('#smsProvider').show();
+      }
+  }
+
+  cj(function() {
        if ( cj('#is_recipient_listing').val( ) ) {
            cj('#recipientList').show();
        } else {
index 5bce795634f17fa7c0a6f497a0cab6476e514ab4..059200a6c7d119e5dd9b4020848d6020fd1b1355 100644 (file)
        <comment>Date on which the reminder be sent.</comment>
        <add>4.1</add>
   </field>
+  <field>
+    <name>mode</name>
+    <title>Message Mode</title>
+    <default>"Email"</default>
+    <type>varchar</type>
+    <length>128</length>
+    <comment>Send the message as email or sms or both.</comment>
+    <pseudoconstant>
+      <optionGroupName>msg_mode</optionGroupName>
+    </pseudoconstant>
+    <html>
+      <type>Select</type>
+    </html>
+    <add>4.5</add>
+  </field>
+  <field>
+    <name>sms_provider_id</name>
+    <type>int unsigned</type>
+    <add>4.5</add>
+    <html>
+      <type>Select</type>
+    </html>
+  </field>
+  <foreignKey>
+    <name>sms_provider_id</name>
+    <table>civicrm_sms_provider</table>
+    <key>id</key>
+    <onDelete>SET NULL</onDelete>
+    <add>4.5</add>
+  </foreignKey>
 </table>
index 560ac8681434a3b04db93225f78cc2b919c31572..ca6a66ca1e0bb945bbc1650677add3d08c1d9239 100644 (file)
@@ -206,7 +206,8 @@ VALUES
    ('financial_item_status'         , '{ts escape="sql"}Financial Item Status{/ts}'              , 1, 1),
    ('label_type'                    , '{ts escape="sql"}Label Type{/ts}'                         , 1, 1),
    ('name_badge'                    , '{ts escape="sql"}Name Badge Format{/ts}'                  , 1, 1),
-   ('communication_style'           , '{ts escape="sql"}Communication Style{/ts}'                , 1, 1);
+   ('communication_style'           , '{ts escape="sql"}Communication Style{/ts}'                , 1, 1),
+   ('msg_mode'                      , '{ts escape="sql"}Message Mode{/ts}'                       , 1, 1);
 
 SELECT @option_group_id_pcm            := max(id) from civicrm_option_group where name = 'preferred_communication_method';
 SELECT @option_group_id_act            := max(id) from civicrm_option_group where name = 'activity_type';
@@ -282,7 +283,7 @@ SELECT @option_group_id_financial_item_status := max(id) from civicrm_option_gro
 SELECT @option_group_id_label_type := max(id) from civicrm_option_group where name = 'label_type';
 SELECT @option_group_id_name_badge := max(id) from civicrm_option_group where name = 'name_badge';
 SELECT @option_group_id_communication_style := max(id) from civicrm_option_group where name = 'communication_style';
-
+SELECT @option_group_id_msg_mode := max(id) from civicrm_option_group where name = 'msg_mode';
 
 SELECT @contributeCompId := max(id) FROM civicrm_component where name = 'CiviContribute';
 SELECT @eventCompId      := max(id) FROM civicrm_component where name = 'CiviEvent';
@@ -905,7 +906,12 @@ VALUES
 
 -- Communication Styles
   (@option_group_id_communication_style, '{ts escape="sql"}Formal{/ts}'  , 1, 'formal'  , NULL, 0, 1, 1, NULL, 0, 0, 1, NULL, NULL),
-  (@option_group_id_communication_style, '{ts escape="sql"}Familiar{/ts}', 2, 'familiar', NULL, 0, 0, 2, NULL, 0, 0, 1, NULL, NULL);
+  (@option_group_id_communication_style, '{ts escape="sql"}Familiar{/ts}', 2, 'familiar', NULL, 0, 0, 2, NULL, 0, 0, 1, NULL, NULL),
+
+-- Message Mode
+(@option_group_id_msg_mode, '{ts escape="sql"}Email{/ts}', 'Email', 'Email', NULL, 0, 1, 1, NULL, 0, 1, 1, NULL, NULL),
+(@option_group_id_msg_mode, '{ts escape="sql"}SMS{/ts}', 'SMS', 'SMS', NULL, 0, 0, 2, NULL, 0, 1, 1, NULL, NULL),
+(@option_group_id_msg_mode, '{ts escape="sql"}User Preference{/ts}', 'User_Preference', 'User Preference', NULL, 0, 0, 3, NULL, 0, 1, 1, NULL, NULL);
 
 -- financial accounts
 SELECT @opval := value FROM civicrm_option_value WHERE name = 'Revenue' and option_group_id = @option_group_id_fat;