-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.pages.inc
218 lines (203 loc) · 6.92 KB
/
twilio.pages.inc
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
<?php
/**
* @file
* User facing pages
*/
/**
* Menu callback for adding and confirming a user's mobile number.
*/
function twilio_user_settings($account) {
return drupal_get_form('twilio_user_settings_form', $account);
}
/**
* User settings form.
*/
function twilio_user_settings_form($form, &$form_state, $account) {
if (isset($form_state['values']['account'])) {
$account = user_load($form_state['values']['account']->uid);
}
$form['#prefix'] = '<div id="twilio-user-form">';
$form['#suffix'] = '</div>';
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $account->uid,
);
if (empty($account->twilio_user['status'])) {
$form['countrycode'] = array(
'#type' => 'select',
'#title' => t('Country code'),
'#options' => twilio_country_codes(),
);
$form['number'] = array(
'#type' => 'textfield',
'#title' => t('Phone number'),
'#description' => t('A confirmation code will be sent to via SMS to the number provided'),
'#size' => 40,
'#maxlength' => 255,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Confirm number'),
'#validate' => array('twilio_user_settings_add_form_validate'),
'#submit' => array('twilio_user_settings_add_form_submit'),
'#ajax' => array(
'callback' => 'twilio_user_settings_ajax_callback',
'wrapper' => 'twilio-user-form',
'method' => 'replace',
),
);
}
elseif ($account->twilio_user['status'] == 1) {
$form['number'] = array(
'#type' => 'item',
'#title' => t('Mobile phone number'),
'#markup' => $account->twilio_user['number'],
);
$form['confirm_code'] = array(
'#type' => 'textfield',
'#title' => t('Confirmation code'),
'#description' => t('Enter the confirmation code sent by SMS to your mobile phone.'),
'#size' => 4,
'#maxlength' => 4,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Confirm number'),
'#validate' => array('twilio_user_settings_confirm_form_validate'),
'#submit' => array('twilio_user_settings_confirm_form_submit'),
'#ajax' => array(
'callback' => 'twilio_user_settings_ajax_callback',
'wrapper' => 'twilio-user-form',
'method' => 'replace',
),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Delete & start over'),
'#validate' => array('twilio_user_settings_confirm_form_validate'),
'#submit' => array('twilio_user_settings_confirm_form_submit'),
'#ajax' => array(
'callback' => 'twilio_user_settings_ajax_callback',
'wrapper' => 'twilio-user-form',
'method' => 'replace',
),
);
}
elseif ($account->twilio_user['status'] == 2) {
$form['twilio_user']['number'] = array(
'#type' => 'item',
'#title' => t('Your mobile phone number'),
'#markup' => '+' . $account->twilio_user['country'] . ' ' . $account->twilio_user['number'],
'#description' => t('Your mobile phone number has been confirmed.'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Delete & start over'),
'#validate' => array('twilio_user_settings_reset_form_validate'),
'#submit' => array('twilio_user_settings_reset_form_submit'),
'#ajax' => array(
'callback' => 'twilio_user_settings_ajax_callback',
'wrapper' => 'twilio-user-form',
'method' => 'replace',
),
);
}
return $form;
}
/**
* Ajax callback for user forms.
*/
function twilio_user_settings_ajax_callback($form, &$form_state) {
return $form;
}
/**
* Validation function for user settings form.
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_settings_add_form_validate($form, &$form_state) {
$value = $form_state['values']['number'];
if (!is_numeric($value)) {
form_set_error('number', t('You must enter a valid phone number'));
}
elseif (twilio_verify_duplicate_number($form_state['values']['number'])) {
form_set_error('number', t('This number is already in use and cannot be assigned to more than one account'));
}
}
/**
* Submit handler for user settings form.
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_settings_add_form_submit($form, &$form_state, $account = NULL) {
if (!$account) {
$account = user_load($form_state['values']['uid']);
}
$account = twilio_user_send_confirmation($account, $form_state['values']['number'], $form_state['values']['countrycode']);
$form_state['values']['account'] = $account;
$form_state['rebuild'] = TRUE;
drupal_set_message(t("A confirmation code has been sent to your mobile device"), 'status');
}
/**
* Validation handler for user settings confirmation form.
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_settings_confirm_form_validate($form, &$form_state) {
if ($form_state['clicked_button']['#value'] == t('Confirm number')) {
$account = user_load($form_state['values']['uid']);
if ($form_state['values']['confirm_code'] != $account->twilio_user['code']) {
form_set_error('confirm_code', t('The confirmation code is invalid.'));
}
}
}
/**
* Submit handler for user settings confirmation form.
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_settings_confirm_form_submit($form, &$form_state) {
$account = user_load($form_state['values']['uid']);
if ($form_state['clicked_button']['#value'] == t('Delete & start over')) {
twilio_user_delete($account);
$account = user_load($form_state['values']['uid'], TRUE);
drupal_set_message(t('Your mobile information has been removed'), 'status');
}
else {
/* $data = array(
'number' => $account->twilio_user['number'],
'status' => TWILIO_USER_CONFIRMED,
);*/
// 8/17/mcl: new hook, only one param. modify $account first
$account->twilio_user['status'] = TWILIO_USER_CONFIRMED;
//$account = user_save($account, array('twilio' => $data), 'twilio');
twilio_user_save($account);
drupal_set_message(t('Your mobile number has been confirmed'), 'status');
}
$form_state['values']['account'] = $account;
$form_state['rebuild'] = TRUE;
}
/**
* Validation handler for user settings reset form.
*/
function twilio_user_settings_reset_form_validate($form, &$form_state) {
}
/**
* Submit handler for user settings reset form.
*
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function twilio_user_settings_reset_form_submit($form, &$form_state) {
$account = user_load($form_state['values']['uid']);
twilio_user_delete($account);
$account = user_load($form_state['values']['uid'], TRUE);
drupal_set_message(t('Your mobile information has been removed'), 'status');
$form_state['values']['account'] = $account;
$form_state['rebuild'] = TRUE;
}