Skip to content

Commit

Permalink
Toward 3sd#7: allow selection of SMS provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
twomice committed Aug 10, 2017
1 parent 7ba2be3 commit 1f00da4
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 19 deletions.
9 changes: 8 additions & 1 deletion CRM/SmsConversation/BAO/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ static function ask($questionId, $contactId, $convContact) {
}
$convContact = $convContactUpdated['values'][$convContactUpdated['id']];

if (CRM_SmsConversation_Processor::sendSMS($contactId, $question['text'], $convContact['source_contact_id'])) {
$params = array(
'contact_id' => $contactId,
'source_contact_id' => CRM_Utils_Array::value('source_contact_id', $convContact),
'text' => CRM_Utils_Array::value('text', $question),
'sms_provider_id' => CRM_Utils_Array::value('sms_provider_id', $convContact),
);

if (CRM_SmsConversation_Processor::sendSMS($params)) {

// If there are no actions for the question, we end the conversation
$convActions = CRM_SmsConversation_BAO_Action::getAction($questionId);
Expand Down
4 changes: 2 additions & 2 deletions CRM/SmsConversation/DAO/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* Generated from xml/schema/CRM/SmsConversation/Action.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:e5468bcef2843e5a26d16dffcdd4cdd1)
* (GenCodeChecksum:a390c55a232823276d1e393f38661e92)
*/
require_once 'CRM/Core/DAO.php';
require_once 'CRM/Utils/Type.php';
Expand Down Expand Up @@ -122,7 +122,7 @@ static function &fields() {
'question_id' => array(
'name' => 'question_id',
'type' => CRM_Utils_Type::T_INT,
'title' => 'Question ID',
'title' => ts('Question ID') ,
'description' => 'FK to sms_conversation_question.id',
'table_name' => 'civicrm_sms_conversation_action',
'entity' => 'Action',
Expand Down
19 changes: 18 additions & 1 deletion CRM/SmsConversation/DAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* Generated from xml/schema/CRM/SmsConversation/Contact.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:900b55f7a9ae47b8962ab3a9278006ec)
* (GenCodeChecksum:92803d3f9c3bb58d0ae2be61e5109563)
*/
require_once 'CRM/Core/DAO.php';
require_once 'CRM/Utils/Type.php';
Expand Down Expand Up @@ -86,6 +86,12 @@ class CRM_SmsConversation_DAO_Contact extends CRM_Core_DAO {
* @var int unsigned
*/
public $source_contact_id;
/**
* FK to civicrm_sms_provider.id
*
* @var int unsigned
*/
public $sms_provider_id;
/**
* Record of all questions, answers
*
Expand Down Expand Up @@ -118,6 +124,7 @@ static function getReferenceColumns() {
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName() , 'conversation_id', 'civicrm_sms_conversation', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName() , 'current_question_id', 'civicrm_sms_conversation_question', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName() , 'source_contact_id', 'civicrm_contact', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName() , 'sms_provider_id', 'civicrm_sms_provider', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
Expand Down Expand Up @@ -196,6 +203,16 @@ static function &fields() {
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
) ,
'sms_provider_id' => array(
'name' => 'sms_provider_id',
'type' => CRM_Utils_Type::T_INT,
'description' => 'FK to civicrm_sms_provider.id',
'table_name' => 'civicrm_sms_conversation_contact',
'entity' => 'Contact',
'bao' => 'CRM_SmsConversation_DAO_Contact',
'localizable' => 0,
'FKClassName' => 'CRM_SMS_DAO_Provider',
) ,
'conversation_record' => array(
'name' => 'conversation_record',
'type' => CRM_Utils_Type::T_LONGTEXT,
Expand Down
9 changes: 9 additions & 0 deletions CRM/SmsConversation/Form/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public function buildQuickForm() {
),
));

// Add "From" field to indicate SMS provider.
$providers = CRM_SMS_BAO_Provider::getProviders(NULL, array("is_active" => 1), 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);

// export form elements
parent::buildQuickForm();
}
Expand All @@ -60,6 +68,7 @@ public function postProcess() {
$params['scheduled_date'] = CRM_Utils_Date::processDate($values['scheduled_date'], $values['scheduled_date_time']);
$params['source_contact_id'] = $session->get('userID');
$params['process_now'] = TRUE; // Start the conversation immediately
$params['sms_provider_id'] = $values['sms_provider_id'];
// Create new conversation for contact
try {
$status = civicrm_api3('SmsConversationContact', 'schedule', $params);
Expand Down
9 changes: 9 additions & 0 deletions CRM/SmsConversation/Form/ScheduleMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public function buildQuickForm() {
CRM_Core_Session::setStatus('No selected contacts had valid mobile phones.');
}

// Add "From" field to indicate SMS provider.
$providers = CRM_SMS_BAO_Provider::getProviders(NULL, array("is_active" => 1), 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);

}


Expand All @@ -76,6 +84,7 @@ public function postProcess() {
$params['conversation_id'] = $values['conversation_id'];
$params['scheduled_date'] = CRM_Utils_Date::processDate($values['scheduled_date'], $values['scheduled_date_time']);
$params['source_contact_id'] = $session->get('userID');
$params['sms_provider_id'] = $values['sms_provider_id'];
$result = civicrm_api3('SmsConversationContact', 'create', $params);
}

Expand Down
8 changes: 2 additions & 6 deletions CRM/SmsConversation/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ function inbound() {
*
* @return bool
*/
static function sendSMS($contactId, $text, $sourceContactId = NULL) {
$result = civicrm_api3('Contact', 'sms', array(
'contact_id' => $contactId,
'source_contact_id' => $sourceContactId,
'text' => $text,
));
static function sendSMS($params) {
$result = civicrm_api3('Contact', 'sms', $params);

if (!empty($result['is_error'])) {
return FALSE;
Expand Down
13 changes: 13 additions & 0 deletions CRM/SmsConversation/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@ public function upgrade_4700() {
return TRUE;
}

/**
* Add column civicrm_sms_conversation_contact.sms_provider_id
*/
public function upgrade_4701() {
// Set is_active to required, default 1
$this->ctx->log->info('Applying update 4701');
CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_sms_conversation_contact` ADD `sms_provider_id` INT( 10 ) UNSIGNED NULL COMMENT 'FK to civicrm_sms_provider.id' AFTER `source_contact_id`");
CRM_Core_DAO::executeQuery("SELECT @default_sms_provider_id := id FROM civicrm_sms_provider WHERE is_active AND is_default order by ID limit 1");
CRM_Core_DAO::executeQuery("UPDATE civicrm_sms_conversation_contact SET sms_provider_id = @default_sms_provider_id");
CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_sms_conversation_contact` ADD FOREIGN KEY (`sms_provider_id`) REFERENCES `civicrm_sms_provider`(`id`) ON DELETE SET NULL ON UPDATE RESTRICT");
return TRUE;
}

}
18 changes: 13 additions & 5 deletions api/v3/Contact/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ function civicrm_api3_contact_sms($params) {
return civicrm_api3_create_error('You should include either a contact_id or group_id in your params');
}

// use the default SMS provider
$providers=CRM_SMS_BAO_Provider::getProviders(NULL, array('is_default' => 1));
if (empty($providers)) {
throw new CRM_Core_Exception('No SMS providers found - Cannot send SMS. Please enable at least one!');
// Use the specified SMS provider if it's active, otherwise use the default.
if ($smsProviderId = CRM_Utils_Array::value('sms_provider_id', $params)) {
$providers = CRM_SMS_BAO_Provider::getProviders(NULL, array('is_active' => 1, 'id' => $smsProviderId));
if (empty($providers)) {
$provider = $providers[0];
}
}
if (empty($provider)) {
$providers=CRM_SMS_BAO_Provider::getProviders(NULL, array('is_default' => 1));
if (empty($providers)) {
throw new CRM_Core_Exception('No SMS providers found - Cannot send SMS. Please enable at least one!');
}
$provider = $providers[0];
}
$provider = $providers[0];
$provider['provider_id'] = $provider['id'];

//this should be set somehow when not set (or maybe we need to change the underlying BAO to not require it?)
Expand Down
4 changes: 1 addition & 3 deletions api/v3/Job/ProcessSmsConversations.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

function civicrm_api3_job_process_sms_conversations($params) {
if (isset($params['contact_id'])) {
$contactId = $params['contact_id'];
}
$contactId = CRM_Utils_Array::value('contact_id', $params);
$result = CRM_SmsConversation_BAO_Contact::scheduleConversations($contactId);
return civicrm_api3_create_success($result, $params,'SmsConversation','schedule');
}
Expand Down
3 changes: 2 additions & 1 deletion sql/auto_install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ CREATE TABLE `civicrm_sms_conversation_contact` (
`status_id` int unsigned NOT NULL COMMENT 'Conversation Status ID',
`current_question_id` int unsigned COMMENT 'FK to sms_conversation_question.id',
`source_contact_id` int unsigned COMMENT 'Id of contact that started the conversation',
`sms_provider_id` int(10) unsigned DEFAULT NULL COMMENT 'FK to civicrm_sms_provider.id',
`conversation_record` longtext COMMENT 'Record of all questions, answers',
`scheduled_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date and time this SMS Conversation was scheduled.'
,
PRIMARY KEY (`id`)


, CONSTRAINT FK_civicrm_sms_conversation_contact_contact_id FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_conversation_id FOREIGN KEY (`conversation_id`) REFERENCES `civicrm_sms_conversation`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_current_question_id FOREIGN KEY (`current_question_id`) REFERENCES `civicrm_sms_conversation_question`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_source_contact_id FOREIGN KEY (`source_contact_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE CASCADE
, CONSTRAINT FK_civicrm_sms_conversation_contact_contact_id FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_conversation_id FOREIGN KEY (`conversation_id`) REFERENCES `civicrm_sms_conversation`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_current_question_id FOREIGN KEY (`current_question_id`) REFERENCES `civicrm_sms_conversation_question`(`id`) ON DELETE CASCADE, CONSTRAINT FK_civicrm_sms_conversation_contact_source_contact_id FOREIGN KEY (`source_contact_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE CASCADE, CONSTRAINT `civicrm_sms_conversation_contact_sms_provider_id` FOREIGN KEY (`sms_provider_id`) REFERENCES `civicrm_sms_provider` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;

SET foreign_key_checks = 1;
4 changes: 4 additions & 0 deletions templates/CRM/SmsConversation/Form/Schedule.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div class="crm-form-block">
<table class="form-layout">
<tr>
<td>{$form.sms_provider_id.label}</td>
<td>{$form.sms_provider_id.html}</td>
</tr>
<tr>
<td>{$form.conversation_id.label}</td>
<td>{$form.conversation_id.html}</td>
Expand Down
4 changes: 4 additions & 0 deletions templates/CRM/SmsConversation/Form/ScheduleMultiple.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div class="crm-form-block">
<table class="form-layout">
<tr>
<td>{$form.sms_provider_id.label}</td>
<td>{$form.sms_provider_id.html}</td>
</tr>
<tr>
<td>{$form.conversation_id.label}</td>
<td>{$form.conversation_id.html}</td>
Expand Down
14 changes: 14 additions & 0 deletions xml/schema/SmsConversation/Contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@
<onDelete>CASCADE</onDelete>
</foreignKey>

<field>
<name>sms_provider_id</name>
<type>int unsigned</type>
<comment>FK to civicrm_sms_provider.id</comment>
<add>4.7</add>
</field>
<foreignKey>
<name>sms_provider_id</name>
<table>civicrm_sms_provider</table>
<key>id</key>
<add>4.7</add>
<onDelete>SET NULL</onDelete>
</foreignKey>

<field>
<name>conversation_record</name>
<title>Record of conversation</title>
Expand Down

0 comments on commit 1f00da4

Please sign in to comment.