<?php
// $Id: uc_ca_sms.module,v 1.1 2010/07/08 09:53:18 shushu Exp $

/**
 * @file
 * Adds Ubercart Conditional actions for sending SMS.
 *
 * Conditional actions action and form
 */

/**
 * Implementation of hook_ca_action().
 */
function uc_ca_sms_ca_action() {
  $order_arg = array(
    '#entity' => 'uc_order',
    '#title' => t('Order'),
  );

  $actions['uc_ca_sms'] = array(
    '#title' => t('Send SMS message'),
    '#category' => t('Order'),
    '#callback' => 'uc_ca_sms_action_sms',
    '#arguments' => array(
      'order' => $order_arg,
    ),
  );

  return $actions;
}

/**
 * Send an SMS concerning an order.
 *
 * The recipients and message fields take order token replacements.
 *
 * @see uc_ca_sms_action_sms_form()
 */
function uc_ca_sms_action_sms($order, $settings) {
  $account = uc_order_user_load($order);

  // Token replacements for the subject and body
  $settings['replacements'] = array(
    'global' => NULL,
    'order' => $order,
    'user' => $account,
  );

  // Apply token replacements to from and recipient e-mail addressses.
  $uids_field = token_replace_multiple($settings['uids'], $settings['replacements']);
  $numbers_field = token_replace_multiple($settings['numbers'], $settings['replacements']);
  $message = token_replace_multiple($settings['message'], $settings['replacements']);

  // Split up our recipient user ids
  $uids = array();
  foreach (explode(",", $uids_field) as $uid) {
    $uids[] = trim($uid);
  }

  foreach ($uids as $uid) {
    $user = user_load($uid);
    $name = $user->name;
    $sent = sms_user_send($uid, $message);

    if (!$sent) {
      watchdog('ca', 'Attempt to send SMS to @name concerning order @order_id failed.', array('@name' => $name, '@order_id' => $order->order_id), WATCHDOG_ERROR);
    }
  }

  // Split up our recipient user ids
  $numbers = array();
  foreach (explode(",", $numbers_field) as $number) {
    $numbers[] = trim($number);
  }

  foreach ($numbers as $number) {
    $sent = sms_send($number, $message);

    if (!$sent) {
      watchdog('ca', 'Attempt to send SMS to @number concerning order @order_id failed.', array('@number' => $number, '@order_id' => $order->order_id), WATCHDOG_ERROR);
    }
  }
}

/**
 * @see uc_ca_sms_action_sms()
 */
function uc_ca_sms_action_sms_form($form_state, $settings = array()) {
  $form['uids'] = array(
    '#type' => 'textarea',
    '#title' => t('Recipients'),
    '#default_value' => isset($settings['uids']) ? $settings['uids'] : '',
    '#description' => t('Enter the uids, separated by comma. You may use order tokens for dynamic numbers.'),
    '#required' => FALSE,
  );
  $form['numbers'] = array(
    '#type' => 'textarea',
    '#title' => t('Numbers'),
    '#default_value' => isset($settings['numbers']) ? $settings['numbers'] : '',
    '#description' => t('Enter the phone numbers, separated by comma. You may use order tokens for dynamic numbers.'),
    '#required' => FALSE,
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => $settings['message'],
  );

  $form['token_help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Replacement patterns'),
    '#description' => t('You can make use of the replacement patterns in the SMS message, numbers and recipients fields.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  foreach (array('global', 'order') as $name) {
    $form['token_help'][$name] = array(
      '#type' => 'fieldset',
      '#title' => t('@name replacement patterns', array('@name' => drupal_ucfirst($name))),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['token_help'][$name]['content'] = array(
      '#value' => theme('token_help', $name),
    );
  }

  return $form;
}

