-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.admin.inc
356 lines (315 loc) · 10.4 KB
/
sms.admin.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
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
/**
* @file
* The admin sms functions.
*/
/**
* Select default sms gateway form.
*/
function sms_admin_default_form() {
$gateways = sms_gateways();
foreach ($gateways as $identifier => $gateway) {
$active = ($identifier == variable_get('sms_default_gateway', 'log'));
$options[$identifier] = '';
$form[$gateway['name']]['id'] = array('#value' => $identifier);
if (function_exists($gateway['configure form'])) {
$form[$gateway['name']]['configure'] = array('#value' => l(t('configure'), 'admin/smsframework/gateways/'. $identifier));
}
else {
$form[$gateway['name']]['configure'] = array('#value' => t('No configuration options'));
}
}
$form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('sms_default_gateway', 'log'));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Set default gateway'),
);
return $form;
}
/**
* Form submission handler for sms_admin_default_form().
*
* @see sms_admin_default_form()
*/
function sms_admin_default_form_submit($form, &$form_state) {
// Process form submission to set the default gateway
if ($form_state['values']['default']) {
drupal_set_message(t('Default gateway updated.'));
variable_set('sms_default_gateway', $form_state['values']['default']);
}
}
/**
* Returns HTML for the admin default gateway form.
*
* @param $form
* An form array.
*
* @ingroup themeable
*/
function theme_sms_admin_default_form($form) {
$rows = array();
foreach ($form as $name => $element) {
if (isset($element['id']) && is_array($element['id'])) {
$rows[] = array(
drupal_render($form['default'][$element['id']['#value']]),
check_plain($name),
drupal_render($element['configure']),
);
unset($form[$name]);
}
}
$header = array(t('Default'), t('Name'), array('data' => t('Operations'), 'colspan' => 1));
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Form builder for the sms gateway configuration form.
*
* @param @gateway_id
* The ID of the sms gateway.
*
* @see sms_admin_gateway_form_validate()
* @see sms_admin_gateway_form_submit()
*/
function sms_admin_gateway_form(&$form_state, $gateway_id) {
$gateway = sms_gateways('gateway', $gateway_id);
if ($gateway && function_exists($gateway['configure form'])) {
drupal_set_title(t('@gateway configuration', array('@gateway' => $gateway['name'])));
$form = $gateway['configure form']($gateway['configuration']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['gateway'] = array('#type' => 'value', '#value' => $gateway);
return $form;
}
drupal_not_found();
exit();
}
/**
* Form validation handler for sms_admin_gateway_form().
*
* @see sms_admin_gateway_form()
* @see sms_admin_gateway_form_submit()
*/
function sms_admin_gateway_form_validate($form, &$form_state) {
// Pass validation to gateway
$function = $form_state['values']['gateway']['configure form'] .'_validate';
if (function_exists($function)) {
$function($form, $form_state);
}
}
/**
* Form submission handler for sms_admin_gateway_form().
*
* @see sms_admin_gateway_form()
* @see sms_admin_gateway_form_validate()
*/
function sms_admin_gateway_form_submit($form, &$form_state) {
$gateway = $form_state['values']['gateway'];
// Remove uneccesary values
unset($form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['gateway'], $form_state['values']['form_token'], $form_state['values']['form_id']);
variable_set('sms_'. $gateway['identifier'] .'_settings', $form_state['values']);
drupal_set_message(t('The gateway settings have been saved.'));
$form_state['redirect'] = 'admin/smsframework/gateways';
}
/**
* Form builder for the list of sms carriers.
*
* @see sms_carriers_admin_form_submit().
*/
function sms_carriers_admin_form() {
$carriers = sms_carriers();
$form = array();
foreach ($carriers as $id => $carrier) {
$actions = array();
$css_safe_id = str_replace('.', '-', $id);
switch ($carrier['type']) {
case SMS_CARRIER_DEFAULT:
$storage = t('Default');
break;
case SMS_CARRIER_OVERRIDDEN:
$storage = t('Overridden');
break;
case SMS_CARRIER_NORMAL:
$storage = t('Normal');
break;
}
$form['status']['#tree'] = TRUE;
$form['status'][$css_safe_id] = array(
'#type' => 'checkbox',
'#title' => $carrier['name'],
'#description' => $storage,
'#default_value' => $carrier['status'],
);
$form['prefix'][$css_safe_id] = array(
'#type' => 'markup',
'#value' => $carrier['prefix']
);
$form['receive'][$css_safe_id] = array(
'#type' => 'markup',
'#value' => $carrier['receive']
);
$form['domain'][$css_safe_id] = array(
'#type' => 'markup',
'#value' => $id,
);
$actions[] = l(t('Edit'), "admin/smsframework/carriers/{$id}");
if ($carrier['type'] == SMS_CARRIER_OVERRIDDEN) {
$actions[] = l(t('Revert'), "admin/smsframework/carriers/delete/{$id}");
}
else if ($carrier['type'] == SMS_CARRIER_NORMAL) {
$actions[] = l(t('Delete'), "admin/smsframework/carriers/delete/{$id}");
}
$form['actions'][$css_safe_id] = array(
'#type' => 'markup',
'#value' => implode(' | ', $actions),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
/**
* Form submission handler for sms_carriers_admin_form().
*
* @see sms_carriers_admin_form().
*/
function sms_carriers_admin_form_submit(&$form, &$form_state) {
$enabled_carriers = array();
foreach ($form_state['values']['status'] as $carrier => $status) {
if ($status) {
$enabled_carriers[] = str_replace('-', '.', $carrier);
}
}
variable_set('sms_enabled_carriers', $enabled_carriers);
drupal_set_message('The configuration options have been saved.');
}
/**
* Form builder for the carrier edit form.
*
* @param $carrier
* An associative array defining the sms carrier.
*
* @see sms_carriers_edit_form_submit().
*/
function sms_carriers_edit_form($form_state, $carrier = array()) {
$form['carrier'] = array(
'#type' => 'value',
'#value' => $carrier['domain'],
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $carrier['name'],
'#required' => TRUE,
);
$form['domain'] = array(
'#type' => 'textfield',
'#title' => t('Domain'),
'#default_value' => $carrier['domain'],
'#required' => TRUE,
);
$form['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#default_value' => $carrier['prefix']
);
$form['receive'] = array(
'#type' => 'checkbox',
'#title' => t('Can Receive?'),
'#default_value' => $carrier['receive']
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Form submission handler for sms_carriers_edit_form().
*
* @see sms_carriers_edit_form().
*/
function sms_carriers_edit_form_submit(&$form, &$form_state) {
$carrier = array();
$carrier = array(
'name' => $form_state['values']['name'],
'domain' => $form_state['values']['domain'],
'prefix' => $form_state['values']['prefix'],
'receive' => $form_state['values']['receive']
);
carrier_save($form_state['values']['carrier'], $carrier);
drupal_set_message(t('The carrier has been saved.'));
drupal_goto('admin/smsframework/carriers');
}
/**
* Form builder for the carrier deletion confirmation form.
*
* @param $carrier
* An associative array defining the sms carrier.
*
* @see sms_carriers_delete_form_submit()
*/
function sms_carriers_delete_form(&$form_state, $carrier) {
$form['domain'] = array('#type' => 'value', '#value' => $carrier['domain']);
$form['type'] = array('#type' => 'value', '#value' => $carrier['type']);
if ($carrier['type'] == SMS_CARRIER_OVERRIDDEN) {
return confirm_form($form, t('Are you sure you want revert %carrier?', array('%carrier' => $carrier['name'])), 'admin/smsframework/carriers', t('Reverting this carrier will delete it from the database. It will be replaced with the default carrier settings. This action cannot be undone.'), t('Revert'), t('Cancel'));
}
if ($carrier['type'] == SMS_CARRIER_NORMAL) {
return confirm_form($form, t('Are you sure you want delete %carrier?', array('%carrier' => $carrier['name'])), 'admin/smsframework/carriers', t('This carrier will be removed from the database. This action cannot be undone.'), t('Delete'), t('Cancel'));
}
}
/**
* Form submission handler for sms_carriers_delete_form().
*
* @see sms_carriers_delete_form()
*/
function sms_carriers_delete_form_submit($form, &$form_state) {
db_query('DELETE FROM {sms_carriers} WHERE domain = "%s"', $form_state['values']['domain']);
if ($carrier['type'] == SMS_CARRIER_OVERRIDDEN) {
drupal_set_message(t('The carrier has been reverted.'));
}
if ($carrier['type'] == SMS_CARRIER_NORMAL) {
drupal_set_message(t('The carrier has been deleted.'));
}
$form_state['redirect'] = 'admin/smsframework/carriers';
}
/**
* Returns HTML for the sms carriers admin form.
*
* @param $form
* A form array.
*
* @ingroup themeable
*/
function theme_sms_carriers_admin_form($form) {
$output = '';
$header = array('', t('Carrier'), t('Domain'), t('Prefix'), t('Can Receive?'), t('Actions'));
$rows = array();
foreach (element_children($form['status']) as $element) {
$name = "<div class='carrier'>";
$name .= "<strong>{$form['status'][$element]['#title']}</strong>";
$name .= "<div class='description'>{$form['status'][$element]['#description']}</div>";
$name .= "</div>";
unset($form['status'][$element]['#title']);
unset($form['status'][$element]['#description']);
$row = array(
'status' => drupal_render($form['status'][$element]),
'name' => $name,
'domain' => drupal_render($form['domain'][$element]),
'prefix' => drupal_render($form['prefix'][$element]),
'receive' => drupal_render($form['receive'][$element]) ? 'Yes' : 'No',
'actions' => drupal_render($form['actions'][$element]),
);
$rows[] = $row;
}
$output .= theme('table', $header, $rows, array('id' => 'sms-form-table', 'class' => 'sms'));
$output .= drupal_render($form['submit']);
$output .= drupal_render($form);
return $output;
}