You are here

function sms_actions_send_action in SMS Framework 6

Same name and namespace in other branches
  1. 6.2 modules/sms_actions/sms_actions.module \sms_actions_send_action()
  2. 7 modules/sms_actions/sms_actions.module \sms_actions_send_action()

Implementation of a Drupal action.

File

modules/sms_actions/sms_actions.module, line 172
Provides a "Send SMS" action and the ability to define custom triggers for incoming messages.

Code

function sms_actions_send_action($object, $context) {
  global $user;
  switch ($context['hook']) {
    case 'nodeapi':
      $node = $context['node'];
      break;
    case 'comment':
      $comment = $context['comment'];
      $node = node_load($comment->nid);
      break;
    case 'user':

      // Because this is not an action of type 'user' the user
      // object is not passed as $object, but it will still be available
      // in $context.
      $account = $context['account'];
      if (isset($context['node'])) {
        $node = $context['node'];
      }
      elseif ($context['recipient'] == '%author') {

        // If we don't have a node, we don't have a node author.
        watchdog('error', t("Cannot use '%author' token in this context."));
        return;
      }
      break;
    default:

      // Check context for node.
      if (!isset($object) && isset($context['node'])) {
        $node = $context['node'];
      }
      else {

        // We are being called directly.
        $node = $object;
      }
      break;
  }
  $number = $context['number'];
  $variables = array(
    '%site_name' => variable_get('site_name', 'Drupal'),
    '%username' => $account->name,
  );
  if (isset($node)) {
    if (!isset($account)) {
      $account = user_load(array(
        'uid' => $node->uid,
      ));
    }
    if ($context['author'] && $account->sms_user[0]['status'] == 2) {
      $number = $account->sms_user[0]['number'];
    }
    $variables = array_merge($variables, array(
      '%uid' => $node->uid,
      '%node_url' => url('node/' . $node->nid, array(
        'absolute' => TRUE,
      )),
      '%node_type' => check_plain(node_get_types('name', $node)),
      '%title' => filter_xss($node->title),
      '%teaser' => filter_xss($node->teaser),
      '%body' => filter_xss($node->body),
    ));
  }
  $message = strtr($context['message'], $variables);
  if (isset($node) && $context['author']) {
    sms_user_send($account->uid, $message);
  }
  else {
    sms_send($number, $message);
  }
}