Skip to content

Commit 7c656bc

Browse files
committed
refs #39637, gh-111, add sms provider for flydove to save config
1 parent 89c100e commit 7c656bc

File tree

4 files changed

+158
-21
lines changed

4 files changed

+158
-21
lines changed

Diff for: CRM/SMS/Form/Provider.php

+57-11
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public function buildQuickForm() {
8686
$providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
8787
$apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
8888

89-
$this->add('select', 'name', ts('Name'), array('' => ts('-- Select --')) + $providerNames, TRUE);
89+
$eleProviders = $this->addSelect('name', ts('Name'), array('' => ts('-- Select --')) + $providerNames, TRUE);
90+
if ($this->_action & CRM_Core_Action::UPDATE) {
91+
$eleProviders->freeze();
92+
}
9093

9194
$this->add('text', 'title', ts('Title'),
9295
$attributes['title'], TRUE
@@ -97,25 +100,51 @@ public function buildQuickForm() {
97100
$this->_id,
98101
));
99102

100-
$this->add('text', 'username', ts('Username'),
101-
$attributes['username'], TRUE
102-
);
103+
$this->add('text', 'username', ts('Username'), $attributes['username']);
103104

104-
$this->add('password', 'password', ts('Password'),
105-
$attributes['password'], TRUE
106-
);
105+
$this->add('password', 'password', ts('Password'), $attributes['password']);
107106

108107
$this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
109108

110109
$this->add('text', 'api_url', ts('API URL'), $attributes['api_url'], TRUE);
111110

112-
$this->add('textarea', 'api_params', ts('API Parameters'),
113-
"cols=50 rows=6"
114-
);
111+
$this->add('textarea', 'api_params', ts('API Parameters'), "cols=50 rows=6");
115112

116113
$this->add('checkbox', 'is_active', ts('Is this provider active?'));
117114

118115
$this->add('checkbox', 'is_default', ts('Is this a default provider?'));
116+
117+
$this->addFormRule(array('CRM_SMS_Form_Provider', 'formRule'));
118+
}
119+
120+
/**
121+
* Form rule
122+
*
123+
* @param array $fields
124+
* @return array
125+
*/
126+
public static function formRule($fields) {
127+
$errors = array();
128+
if ($fields['name'] == 'CRM_SMS_Provider_Mitake') {
129+
// check requirement of these fields
130+
$checkFields = array(
131+
'username' => ts('Username'),
132+
'password' => ts('Password'),
133+
);
134+
foreach(array_keys($checkFields) as $field) {
135+
if (empty($fields[$field])) {
136+
$errors[$field] = ts('%1 is a required field.', array(1 => $checkFields[$field]));
137+
}
138+
}
139+
}
140+
if ($fields['name'] === 'CRM_SMS_Provider_Flydove' && !empty($fields['flydove_api_token'])) {
141+
foreach($fields['flydove_api_token'] as $token) {
142+
if (!preg_match('/^[0-9a-zA-Z]*$/', $token)) {
143+
$errors['qfKey'] = ts('Invalid value for field(s)');
144+
}
145+
}
146+
}
147+
return $errors;
119148
}
120149

121150
/**
@@ -134,7 +163,11 @@ public function setDefaultValues() {
134163
}
135164

136165
if (!$this->_id) {
137-
$defaults['is_active'] = $defaults['is_default'] = 1;
166+
$defaults['is_active'] = 1;
167+
$providers = CRM_SMS_BAO_Provider::getProviders();
168+
if (empty($providers)) {
169+
$defaults['is_default'] = 1;
170+
}
138171
return $defaults;
139172
}
140173

@@ -151,6 +184,13 @@ public function setDefaultValues() {
151184

152185
CRM_Core_DAO::storeValues($dao, $defaults);
153186

187+
if ($defaults['name'] == 'CRM_SMS_Provider_Flydove' && !empty($defaults['api_params'])) {
188+
$apiParams = json_decode($defaults['api_params'], TRUE);
189+
if (!empty($apiParams) && is_array($apiParams['tokens'])) {
190+
$this->assign('flydove_api_token', $apiParams['tokens']);
191+
}
192+
}
193+
154194
return $defaults;
155195
}
156196

@@ -168,6 +208,12 @@ public function postProcess() {
168208
}
169209

170210
$recData = $values = $this->controller->exportValues($this->_name);
211+
if ($recData['name'] === 'CRM_SMS_Provider_Flydove' && !empty($this->_submitValues['flydove_api_token'])) {
212+
$apiParams = array(
213+
'tokens' => $this->_submitValues['flydove_api_token'],
214+
);
215+
$recData['api_params'] = json_encode($apiParams);
216+
}
171217
$recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
172218
$recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
173219
$domainID = CRM_Core_Config::domainID();

Diff for: CRM/SMS/Page/Provider.php

+18
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ public function browse($action = NULL) {
143143

144144
$apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
145145
$provider['api_type'] = $apiTypes[$provider['api_type']];
146+
if ($provider['name'] == 'CRM_SMS_Provider_Flydove') {
147+
$apiParams = json_decode($provider['api_params'], TRUE);
148+
if (is_array($apiParams)) {
149+
$provider['api_params'] = array();
150+
foreach($apiParams as $key => $val) {
151+
if (is_array($val)) {
152+
$provider['api_params'][] = '<strong>'.$key.'</strong>';
153+
foreach($val as $k => $v) {
154+
$provider['api_params'][] = $k.":" . CRM_Utils_String::mask($v);
155+
}
156+
}
157+
else {
158+
$provider['api_params'][] = $key.":" . CRM_Utils_String::mask($val);
159+
}
160+
}
161+
$provider['api_params'] = implode('<br>', $provider['api_params']);
162+
}
163+
}
146164

147165
$provider['action'] = CRM_Core_Action::formLink(self::links(), $action,
148166
array('id' => $provider['id']),

Diff for: CRM/SMS/Provider/Flydove.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
class CRM_SMS_Provider_Flydove extends CRM_SMS_Provider {
4+
5+
/**
6+
* We only need one instance of this object. So we use the singleton
7+
* pattern and cache the instance in this variable
8+
*
9+
* @var object
10+
* @static
11+
*/
12+
static private $_singleton = NULL;
13+
14+
public $_bulkMode = FALSE;
15+
16+
public $_bulkLimit = 500;
17+
18+
public static function &singleton($providerParams = array(), $force = FALSE) {
19+
$providerId = CRM_Utils_Array::value('provider_id', $providerParams);
20+
$providerId = CRM_Utils_Type::validate($providerId, 'Integer');
21+
if (empty($providerId)) {
22+
CRM_Core_Error::fatal('Provider not known or not provided.');
23+
}
24+
if (!isset(self::$_singleton)) {
25+
self::$_singleton = new CRM_SMS_Provider_Flydove($providerId);
26+
}
27+
return self::$_singleton;
28+
}
29+
30+
function __construct($providerId) {
31+
}
32+
33+
/**
34+
* Main function to send SMS
35+
*
36+
* The result should be mapping to activity status name for better update activity
37+
*
38+
* @param array $messages [
39+
* 'phone' => string,
40+
* 'body' => string,
41+
* 'guid' => string,
42+
* 'activityId' => int,
43+
* ]
44+
* @return array response of self::doRequest
45+
*/
46+
public function send(&$messages){
47+
$response = array();
48+
return $response;
49+
}
50+
51+
public function activityUpdate() {
52+
}
53+
}

Diff for: templates/CRM/SMS/Form/Provider.tpl

+30-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{ts}Are you sure you would like to execute this job?{/ts}
3939
</div>
4040
{else}
41-
<table class="form-layout-compressed">
41+
<table class="form-layout-compressed sms-provider-table">
4242
<tr class="crm-job-form-block-name">
4343
<td class="label">{$form.name.label}</td><td>{$form.name.html}</td>
4444
</tr>
@@ -60,10 +60,18 @@
6060
<tr class="crm-job-form-block-api_params">
6161
<td class="label">{$form.api_params.label}</td><td>{$form.api_params.html}</td>
6262
</tr>
63+
<tr class="crm-job-form-block-flydove hide-row">
64+
<td class="label"><label>{ts}Flydove Settings{/ts}</label></td>
65+
<td>
66+
<div><label>{ts}Flydove File Import API Token{/ts} <input type="text" name="flydove_api_token[import]" class="huge" value="{$flydove_api_token.import}" /></label></div>
67+
<div><label>{ts}Flydove Subscribe API Token{/ts} <input type="text" name="flydove_api_token[subscribe]" class="huge" value="{$flydove_api_token.subscribe}"/></label></div>
68+
<div><label>{ts}Flydove Group Management API Token{/ts} <input type="text" name="flydove_api_token[group]" class="huge" value="{$flydove_api_token.group}"/></label></div>
69+
</td>
70+
</tr>
6371
<tr class="crm-job-form-block-is_active">
6472
<td></td><td>{$form.is_active.html}&nbsp;{$form.is_active.label}</td>
6573
</tr>
66-
<tr class="crm-job-form-block-is_active">
74+
<tr class="crm-job-form-block-is_default">
6775
<td></td><td>{$form.is_default.html}&nbsp;{$form.is_default.label}</td>
6876
</tr>
6977
</table>
@@ -73,16 +81,28 @@
7381
</fieldset>
7482
</div>
7583

76-
{if $action eq 1 or $action eq 2}
7784
<script type="text/javascript" >
7885
{literal}
79-
CRM.$(function($) {
80-
var $form = $("form.{/literal}{$form.formClass}{literal}");
81-
$('select[name=name]', $form).change(function() {
82-
var url = {/literal}"{$refreshURL}"{literal} + "&key=" + this.value;
83-
$(this).closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet({url: url}).crmSnippet('refresh');
84-
});
86+
cj(document).ready(function($){
87+
var showHideProviderFields = function(providerName) {
88+
if (providerName == 'Flydove') {
89+
$('.crm-job-form-block-username, .crm-job-form-block-password, .crm-job-form-block-api_params, .crm-job-form-block-is_default').hide();
90+
91+
$('.crm-job-form-block-flydove').removeClass('hide-row');
92+
}
93+
else if(providerName == 'Mitake') {
94+
$('.crm-job-form-block-username, .crm-job-form-block-password, .crm-job-form-block-api_params, .crm-job-form-block-is_default').show();
95+
$('.crm-job-form-block-flydove').addClass('hide-row');
96+
}
97+
}
98+
$('#name').change(function(){
99+
let className = $(this).val();
100+
let providerName = className.replace('CRM_SMS_Provider_', '');
101+
showHideProviderFields(providerName);
85102
});
103+
let className = $('#name').val();
104+
let providerName = className.replace('CRM_SMS_Provider_', '');
105+
showHideProviderFields(providerName);
106+
});
86107
{/literal}
87108
</script>
88-
{/if}

0 commit comments

Comments
 (0)