/
tropo.module
356 lines (324 loc) · 10.8 KB
/
tropo.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
function tropo_menu() {
$items = array();
$items['admin/settings/tropo'] = array(
'title' => 'Tropo',
'description' => 'Configure site-wide settings and defaults for Tropo',
'page callback' => 'drupal_get_form',
'page arguments' => array('tropo_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['tropo/countries'] = array(
'page callback' => 'tropo_countries',
'access arguments' => array('access content'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
$items['tropo/exchanges'] = array(
'page callback' => 'tropo_exchanges',
'access arguments' => array('access content'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
$items['tropo/create'] = array(
'page callback' => 'tropo_create',
'access arguments' => array('access content'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
return $items;
}
function tropo_theme() {
return array(
'tropo_form_exchanges' => array(
'arguments' => array('exchanges' => NULL),
),
'tropo_form_countries' => array(
'arguments' => array('exchanges' => NULL),
),
);
}
/**
* Get a themed list of available exchanges from Tropo
*/
function tropo_countries() {
// TODO: make this configurable per-user
$user = variable_get('tropo_user', "");
$pass = variable_get('tropo_pass', "");
$exchanges = tropo_get_exchanges($user,$pass);
return theme('tropo_form_countries', $exchanges);
}
/**
* Get a themed list of available exchanges from Tropo
*/
function tropo_exchanges() {
// TODO: make this configurable per-user
$user = variable_get('tropo_user', "");
$pass = variable_get('tropo_pass', "");
$exchanges = tropo_get_exchanges($user,$pass);
return theme('tropo_form_exchanges', $exchanges);
}
/*
* Use hook_form_alter to set a redirect location to the URL
* of the next form. Use this example to set up redirects in
* your own modules.
*/
function tropo_form_alter(&$form, $form_state, $form_id) {
switch ($form_id){
case 'tropo_exchanges_form':
$form['#next'] = 'tropo/create';
break;
case 'tropo_countries_form':
$form['#next'] = 'tropo/exchanges';
break;
}
}
function tropo_api_call($function, $args = array(), $user,$pass) {
include_once 'lib/tropo/tropo-rest.class.php';
try {
$tropoProv = new ProvisioningAPI($user,$pass);
$resp = json_decode(call_user_func_array(array($tropoProv, $function), $args));
} catch (Exception $e) {
drupal_set_message($e->getMessage());
return;
}
}
/**
* Get a list of available exchanges from the Tropo api
*/
function tropo_get_exchanges($user,$pass) {
include_once 'lib/tropo/tropo-rest.class.php';
try {
$tropoProv = new ProvisioningAPI($user,$pass);
$resp = json_decode($tropoProv->viewExchanges());
} catch (Exception $e) {
drupal_set_message($e->getMessage());
return;
}
$exchanges = array();
foreach ($resp as $exchange) {
$arrDetails = array(
'prefix' => $exchange->prefix,
'city' => $exchange->city,
'state' => $exchange->state,
'description' => $exchange->description,
);
$exchanges[$exchange->country]['prefixes'][$exchange->prefix] = $arrDetails;
$exchanges[$exchange->country]['states'][$exchange->state] = $arrDetails;
ksort($exchanges[$exchange->country]['prefixes']);
ksort($exchanges[$exchange->country]['states']);
}
ksort($exchanges);
return $exchanges;
}
function theme_tropo_form_countries($exchanges) {
$out .= drupal_get_form('tropo_countries_form', $exchanges);
return $out;
}
function tropo_countries_form(&$form_state, $exchanges) {
foreach ($exchanges as $country => $exchange) {
$options = array('0' => 'Select...');
foreach($exchange['states'] as $details) {
$options[$details['country']] = "$country ({$details['prefix']})";
}
$form['international']['tropo_exchanges_region'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#options' => $options
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
function tropo_countries_form_validate($form, &$form_state) {
$completed = FALSE;
$values = $form_state['clicked_button']['#post'];
foreach ($values as $name => $item) {
if (is_array($item) && array_key_exists('tropo_exchanges_region', $item)) {
$completed = TRUE;
}
}
if (!$completed) {
form_set_error('', t('Please choose a country.'));
}
}
function tropo_countries_form_submit($form, &$form_state) {
$completed = FALSE;
$values = $form_state['clicked_button']['#post'];
//drupal_set_message(print_r($form_state,true));
$prefixes = array();
foreach ($values as $name => $item) {
if (is_array($item) && array_key_exists('tropo_exchanges_region', $item)) {
if ($item['tropo_exchanges_region'] > 0) {
$_SESSION['tropo_country'] = $item['tropo_exchanges_region'];
}
}
}
if (!empty($form['#next'])) {
drupal_goto($form['#next']);
}
}
function theme_tropo_form_exchanges($exchanges) {
$out .= drupal_get_form('tropo_exchanges_form', $exchanges);
return $out;
}
function tropo_exchanges_form(&$form_state, $exchanges, $filter_country = null) {
foreach ($exchanges as $country => $exchange) {
if (!empty($filter_country) && $country != $filter_country) {
// We're only showing one country, and this isn't it.
continue;
}
if (count($exchange['prefixes']) > 1) {
// There's more than one location available in this country. Group by country
$form[$country] = array(
'#type' => 'fieldset',
'#title' => t($country . ' numbers'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -10,
'#tree' => TRUE,
);
$prefix_title = t('By prefix');
$region_title = t('By region');
// Blank country out because we're already displaying the name in the fieldset
$country = '';
} else {
if (!array_key_exists('international', $form)) {
// Fieldset to group international numbers in
$form['international'] = array(
'#type' => 'fieldset',
'#title' => t('International numbers'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -9,
'#tree' => TRUE,
);
}
$prefix_title = t('By country code');
$region_title = t('By country name');
}
$options = array('0' => 'Select...');
foreach($exchange['prefixes'] as $details) {
$options[$details['prefix']] = "{$details['prefix']}: $country {$details['city']} {$details['state']}";
if ($details['description'] == 'Toll Free Phone Number') {
$options[$details['prefix']] = "{$details['prefix']} Toll Free";
}
}
$form['international']['tropo_exchanges_prefix'] = array(
'#type' => 'select',
'#title' => $prefix_title,
'#options' => $options
);
$options = array('0' => 'Select...');
foreach($exchange['states'] as $details) {
$options[$details['prefix']] = "$country{$details['state']}: {$details['city']} ({$details['prefix']})";
if ($details['description'] == 'Toll Free Phone Number') {
$options[$details['prefix']] = "Toll Free ({$details['prefix']})";
}
}
$form['international']['tropo_exchanges_region'] = array(
'#type' => 'select',
'#title' => $region_title,
'#options' => $options
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
function tropo_exchanges_form_validate($form, &$form_state) {
$completed = FALSE;
$values = $form_state['clicked_button']['#post'];
foreach ($values as $name => $item) {
if (is_array($item) && array_key_exists('tropo_exchanges_prefix', $item)) {
if ($item['tropo_exchanges_prefix'] > 0) {
$completed = TRUE;
}
if ($item['tropo_exchanges_region'] > 0) {
$completed = TRUE;
}
}
}
if (!$completed) {
form_set_error('', t('You must pick at least one location to get a number in.'));
}
}
function tropo_exchanges_form_submit($form, &$form_state) {
$completed = FALSE;
$values = $form_state['clicked_button']['#post'];
//drupal_set_message(print_r($form_state,true));
$prefixes = array();
foreach ($values as $name => $item) {
if (is_array($item) && array_key_exists('tropo_exchanges_prefix', $item)) {
if ($item['tropo_exchanges_prefix'] > 0) {
$prefixes[] = $item['tropo_exchanges_prefix'];
}
if ($item['tropo_exchanges_region'] > 0) {
$prefixes[] = $item['tropo_exchanges_region'];
}
}
}
$_SESSION['tropo_exchanges'] = $prefixes;
if (!empty($form['#next'])) {
drupal_goto($form['#next']);
}
}
function tropo_create() {
}
function tropo_create_form(&$form_state) {
$form['app_name'] = array(
'#type' => 'textfield',
'#title' => t('What would you like to call your application?')
);
$form['app_url'] = array(
'#type' => 'textfield',
'#title' => t('Where can your application code be found?'),
'#description' => t('Provide an HTTP URL where we can fetch your code.')
);
}
function tropo_create_form_validate($form, &$form_state) {
$completed = FALSE;
$values = $form_state['clicked_button']['#post'];
if (empty($form_state['values']['app_name'])) {
form_set_error('', t('We need a name for your app.'));
}
if (empty($form_state['values']['app_url'])) {
form_set_error('', t('What is the URL where your code can be found?'));
}
if (strpos($form_state['values']['app_url'], 'http://') != 0 && strpos($form_state['values']['app_url'], 'https://') != 0) {
form_set_error('', t('That doesn\'t look like a URL. Please start with "http"'));
}
}
function tropo_create_form_submit($form, &$form_state) {
createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition);
$completed = FALSE;
drupal_set_message('Created your application.');
$prefixes = array();
foreach ($values as $name => $item) {
if (is_array($item) && array_key_exists('tropo_exchanges_prefix', $item)) {
if ($item['tropo_exchanges_prefix'] > 0) {
$prefixes[] = $item['tropo_exchanges_prefix'];
}
if ($item['tropo_exchanges_region'] > 0) {
$prefixes[] = $item['tropo_exchanges_region'];
}
}
}
$_SESSION['tropo_exchanges'] = $prefixes;
if (!empty($form['#next'])) {
drupal_goto($form['#next']);
}
}
function tropo_settings() {
$form['tropo_user'] = array(
'#type' => 'textfield',
'#title' => t('Your Tropo user name'),
'#default_value' => variable_get('tropo_user', ""),
);
$form['tropo_pass'] = array(
'#type' => 'textfield',
'#title' => t('Your password'),
'#default_value' => variable_get('tropo_pass', ""),
);
return system_settings_form($form);
}