You are here

function twitter_actions_set_status_action in Twitter 6.5

Same name and namespace in other branches
  1. 6.2 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  2. 6.3 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  3. 6.4 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  4. 7.6 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  5. 7.3 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  6. 7.4 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  7. 7.5 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()

Implementation of a configurable Twitter action.

@todo Implementation for language negotiation for the body and sumary. Also need implementation for bodies with multiple values. Right now it is hard coded and it will only get body and summary for 'und' language and only the first value of the body field. If the final message is over 140 chars, there is no feedback to the user.

1 string reference to 'twitter_actions_set_status_action'
twitter_actions_rules_action_info_alter in twitter_actions/twitter_actions.rules.inc
Implementation of hook_rules_action_info_alter().

File

twitter_actions/twitter_actions.module, line 165
Exposes Drupal actions for sending Twitter messages.

Code

function twitter_actions_set_status_action($object, $context) {
  $twitter_uid = _twitter_actions_get_twitter_id($context['screen_name']);
  if ($twitter_uid) {
    global $user;
    $variables['%site_name'] = variable_get('site_name', 'Drupal');
    $hook = isset($context['hook']) ? $context['hook'] : '';
    switch ($hook) {
      case 'nodeapi':
      case 'workflow':

        // Because this is not an action of type 'node' the node
        // will not be passed as $object, but it will still be available
        // in $context.
        $node = $context['node'];
        break;

      // The comment hook provides nid, in $context.
      case 'comment':
        $comment = $context['comment'];
        $node = node_load($comment->nid);
      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 (isset($context['recipient']) && $context['recipient'] == '%author') {

          // If we don't have a node, we don't have a node author.
          watchdog('error', 'Cannot use %author token in this context.');
          return;
        }
        break;
      case 'taxonomy':
        $account = $user;
        $vocabulary = taxonomy_vocabulary_load($object->vid);
        $variables = array_merge($variables, array(
          '%term_name' => $object->name,
          '%term_description' => $object->description,
          '%term_id' => $object->tid,
          '%vocabulary_name' => $vocabulary->name,
          '%vocabulary_description' => $vocabulary->description,
          '%vocabulary_id' => $vocabulary->vid,
        ));
        break;
      default:

        // We are being called directly.
        $node = $object;
    }
    if (isset($node)) {
      if (!isset($account)) {
        $account = user_load(array(
          'uid' => $node->uid,
        ));
      }
      if (isset($context['recipient']) && $context['recipient'] == '%author') {
        $recipient = $account->mail;
      }
    }
    $variables['%username'] = $account->name;

    // Node-based variable translation is only available if we have a node.
    if (isset($node) && is_object($node)) {
      $variables = array_merge($variables, array(
        '%uid' => $node->uid,
        '%node_url' => url('node/' . $node->nid, array(
          'absolute' => TRUE,
        )),
        '%node_type' => node_get_types('name', $node),
        '%title' => $node->title,
        '%teaser' => $node->teaser,
        '%body' => $node->body,
      ));
    }

    // Only make a tinyurl if it was asked for.
    if (strstr($context['message'], '%tinyurl') !== FALSE) {
      $variables = array_merge($variables, array(
        '%tinyurl' => twitter_shorten_url(url('node/' . $node->nid, array(
          'absolute' => TRUE,
        ))),
      ));
    }
    $message = strtr($context['message'], $variables);

    // If token module is available, process status to do the token replacement
    if (module_exists('token')) {

      // Token replacement for the main context hooks other than node
      $context_list = array(
        'workflow',
        'comment',
        'user',
        'taxonomy',
      );
      if (in_array($hook, $context_list, TRUE)) {
        $message = token_replace($message, $hook, $object);
      }

      // then if we have a node object, also do it for the node
      if (isset($node) && is_object($node)) {
        $message = token_replace($message, 'node', $node);
      }
    }

    // Send the tweet.
    module_load_include('inc', 'twitter');
    try {
      $twitter_account = twitter_account_load($twitter_uid);
      twitter_set_status($twitter_account, $message);
      drupal_set_message(t('Successfully posted to Twitter'));
    } catch (TwitterException $e) {
      drupal_set_message(t('An error occurred when posting to Twitter: @message', array(
        '@message' => $e
          ->getMessage(),
      )), 'warning');
    }
  }
}