array( 'name' => t('Log only'), 'send' => 'sms_send_log', ), ); } function sms_send_log($number, $message, $options) { watchdog('sms', 'SMS message sent to %number with the text: @message', array('%number' => $number, '@message' => $message), WATCHDOG_INFO); return array('status' => TRUE); } /** * Implementation of hook_menu(). */ function sms_menu() { $items = array(); $items['admin/smsframework'] = array( 'title' => 'SMS Framework', 'description' => 'Control how your site uses SMS.', 'position' => 'right', 'page callback' => 'system_admin_menu_block_page', 'access arguments' => array('administer smsframework'), 'file' => 'system.admin.inc', 'file path' => drupal_get_path('module', 'system') ); $items['admin/smsframework/gateways'] = array( 'title' => 'Gateway configuration', 'description' => 'Configure gateways and chose the default gateway.', 'page callback' => 'drupal_get_form', 'page arguments' => array('sms_admin_default_form', NULL), 'access arguments' => array('administer smsframework'), ); $items['admin/smsframework/gateways/%'] = array( 'title callback' => 'sms_admin_gateway_title', 'title arguments' => array(3), 'page callback' => 'drupal_get_form', 'page arguments' => array('sms_admin_gateway_form', 3), 'access arguments' => array('administer smsframework'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implementation of hook_perm(). */ function sms_perm() { return array('administer smsframework'); } /** * Implementation of hook_theme(). */ function sms_theme() { return array( 'sms_admin_default_form' => array( 'arguments' => array('form' => NULL), ) ); } /** * SMS gateway menutitle callback. */ function sms_admin_gateway_title($gateway_id) { $gateway = sms_gateways('gateway', $gateway_id); return sprintf('%s gateway', $gateway['name']); } 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; } 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']); } } 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; } 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(); } /** * Validation for gateway configuration. If the gateway defines a validation * function it uses that. */ 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); } } 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'; } /** * Get a list of all gateways * * @param $op * The format in which to return the list. When set to 'gateway' or 'name', * only the specified gateway is returned. When set to 'gateways' or 'names', * all gateways are returned. * * @param $gateway * A gateway identifier string that indicates the gateway to return. Leave at default * value (NULL) to return all gateways. * * @return * Either an array of all gateways or a single gateway, in a variable format. **/ function sms_gateways($op = 'gateways', $gateway = NULL) { list($_gateways, $_names) = _gateways_build(); switch ($op) { case 'gateways': return $_gateways; case 'gateway': $return = $_gateways[$gateway]; $return['identifier'] = $gateway; return $return; case 'names': return $_names; case 'name': return $_names[$gateway]; } } function _gateways_build() { $_gateways = array(); $_names = array(); $gateway_array = module_invoke_all('gateway_info'); foreach ($gateway_array as $identifier => $info) { $info['configuration'] = variable_get('sms_'. $identifier .'_settings', ''); $_gateways[$identifier] = $info; $_names[$identifier] = $info['name']; } asort($_names); return array($_gateways, $_names); } function sms_handle_result($result, $number, $message) { if ($result['status']) { return TRUE; } else { $error_message = 'Sending SMS to %number failed.'; $variables['%number'] = $number; if ($result['message']) { $error_message .= ' The gateway said '. $result['message']; if (!empty($result['variables'])) { $variables = array_merge($variables, $result['variables']); } } watchdog('sms', $error_message, $variables, WATCHDOG_ERROR); return FALSE; } } function sms_number_format($number) { $gateway = sms_default_gateway(); if ($gateway['number_formatter'] && function_exists($gateway['number_formatter'])) { return $gateway['number_formatter']($number_formatter); } else { return $number; } } function sms_formatter($number, $format = 1) { // Remove non-number characters $number = preg_replace("/[^0-9]/", '', $number); if (strlen($number) > 16) { if ($number[0] == 1) { $number = ltrim($number, 1); } else { return FALSE; } } elseif (strlen($number) < 10) { return FALSE; } return $number; } /** * Generates a SMS sending form and adds gateway defined elements. The form * array that is returned can be merged with an existing form using * array_merge(). * * @param $required * Specify if the user is required to provide information for the fields. * * @return $form */ function sms_send_form($required = FALSE) { $gateway = sms_default_gateway(); $form['number'] = array( '#type' => 'textfield', '#title' => t('Phone number'), '#size' => 40, '#maxlength' => 255, '#required' => $required, ); // Add gateway defined fields if (function_exists($gateway['send form'])) { $form['gateway']['#tree'] = TRUE; $form['gateway'] = array_merge($gateway['send form']($required), $form['gateway']); } return $form; } function sms_send_form_validate($form, &$form_state) { if (!sms_formatter($form_state['values']['number'])) { form_set_error('number', t('You must enter a valid phone number.')); } } function sms_validate_number(&$number) { if (!strlen($number) || !$number = sms_formatter($number)) { return t('The phone number is invalid.'); } // Allow the active gateway to provide number validation $gateway = sms_default_gateway(); if (function_exists($gateway['validate number']) && $error = $gateway['validate number']($number)) { return $error; } } /** * Performs a simple send on submit. */ function sms_send_form_submit($form, &$form_state) { $form_state['values']['number'] = sms_formatter($form_state['values']['number']); sms_send($form_state['values']['number'], $form_state['values']['message'], $form_state['values']['gateway']); }