From: yashodha Date: Wed, 8 Jan 2014 11:13:15 +0000 (+0530) Subject: CRM-13912: added sms support for scheduled reminders X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a3e3eea10de221eede5ea8517b511223c206bd24;p=civicrm-core.git CRM-13912: added sms support for scheduled reminders --- diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 07a41c8773..ccda529b21 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -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); diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 11a5f8f814..c2cfd25b49 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -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 & entities in text mode, so that the links work - $mailParams['text'] = str_replace('&', '&', $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 & entities in text mode, so that the links work + $mailParams['text'] = str_replace('&', '&', $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 diff --git a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl index 1faa6c9955..15ee30b61c 100644 --- a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl @@ -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); diff --git a/templates/CRM/Admin/Form/ScheduleReminders.tpl b/templates/CRM/Admin/Form/ScheduleReminders.tpl index 1bd573ad75..bea5d3ef36 100644 --- a/templates/CRM/Admin/Form/ScheduleReminders.tpl +++ b/templates/CRM/Admin/Form/ScheduleReminders.tpl @@ -122,6 +122,14 @@ {$form.group_id.label} {$form.group_id.html} + + {$form.mode.label} + {$form.mode.html} + + + {$form.sms_provider_id.label} + {$form.sms_provider_id.html} +
{ts}Email{/ts} @@ -139,7 +147,8 @@
- {include file="CRM/Contact/Form/Task/EmailCommon.tpl" upload=1 noAttach=1} +
{include file="CRM/Contact/Form/Task/EmailCommon.tpl" upload=1 noAttach=1}
+
{include file="CRM/Contact/Form/Task/SMSCommon.tpl" upload=1 noAttach=1}
{include file="CRM/common/showHideByFieldValue.tpl" @@ -207,7 +216,32 @@ }); }); - 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 { diff --git a/xml/schema/Core/ActionSchedule.xml b/xml/schema/Core/ActionSchedule.xml index 5bce795634..059200a6c7 100644 --- a/xml/schema/Core/ActionSchedule.xml +++ b/xml/schema/Core/ActionSchedule.xml @@ -241,4 +241,34 @@ Date on which the reminder be sent. 4.1 + + mode + Message Mode + "Email" + varchar + 128 + Send the message as email or sms or both. + + msg_mode + + + Select + + 4.5 + + + sms_provider_id + int unsigned + 4.5 + + Select + + + + sms_provider_id + civicrm_sms_provider
+ id + SET NULL + 4.5 +
diff --git a/xml/templates/civicrm_data.tpl b/xml/templates/civicrm_data.tpl index 560ac86814..ca6a66ca1e 100644 --- a/xml/templates/civicrm_data.tpl +++ b/xml/templates/civicrm_data.tpl @@ -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;