You are here

function twitter_actions_set_status_action in Twitter 7.5

Same name and namespace in other branches
  1. 6.5 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  2. 6.2 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  3. 6.3 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  4. 6.4 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  5. 7.6 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  6. 7.3 twitter_actions/twitter_actions.module \twitter_actions_set_status_action()
  7. 7.4 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.

File

twitter_actions/twitter_actions.module, line 151
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');

    // Setting variables array depending on action's group.
    switch ($context['group']) {
      case 'node':
        $node = $context['node'];
        if (!empty($node)) {
          $body = '';
          $summary = '';
          if (!empty($node->body)) {
            $body_data = field_get_items('node', $node, 'body');
            if (!empty($body_data[0]['value'])) {
              $body = $body_data[0]['value'];
            }
            if (!empty($body_data[0]['summary'])) {
              $summary = $body_data[0]['summary'];
            }
          }
          $variables = array_merge($variables, array(
            '%uid' => $node->uid,
            '%username' => $node->name,
            '%node_url' => url('node/' . $node->nid, array(
              'absolute' => TRUE,
            )),
            '%node_type' => node_type_get_name($node),
            '%title' => $node->title,
            '%summary' => $summary,
            '%body' => $body,
          ));
        }
        break;
      case 'comment':
        $node = node_load($context['comment']->nid);
        if (!empty($node)) {
          $body = '';
          $summary = '';
          if (!empty($node->body)) {
            $body_data = field_get_items('node', $node, 'body');
            if (!empty($body_data[0]['value'])) {
              $body = $body_data[0]['value'];
            }
            if (!empty($body_data[0]['summary'])) {
              $summary = $body_data[0]['summary'];
            }
          }
          $variables = array_merge($variables, array(
            '%uid' => $context['comment']->uid,
            '%username' => $context['comment']->name,
            '%node_url' => url('node/' . $node->nid, array(
              'absolute' => TRUE,
            )),
            '%node_type' => node_type_get_name($node),
            '%title' => $node->title,
            '%summary' => $summary,
            '%body' => $body,
          ));
        }
        break;
      case 'user':
        $variables['%username'] = $context['user']->name;
        break;
      case 'cron':
        break;
      default:

        // We are being called directly.
        $node = $object;
        if (!empty($node) && is_object($node)) {
          $body = '';
          $summary = '';
          if (!empty($node->body)) {
            $body_data = field_get_items('node', $node, 'body');
            if (!empty($body_data[0]['value'])) {
              $body = $body_data[0]['value'];
            }
            if (!empty($body_data[0]['summary'])) {
              $summary = $body_data[0]['summary'];
            }
          }
          $variables = array_merge($variables, array(
            '%uid' => $node->uid,
            '%username' => $node->name,
            '%node_url' => url('node/' . $node->nid, array(
              'absolute' => TRUE,
            )),
            '%node_type' => node_type_get_name($node),
            '%title' => $node->title,
            '%summary' => $summary,
            '%body' => $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,
        ))),
      ));
    }

    // Send the tweet.
    $message = strtr($context['message'], $variables);
    twitter_actions_post($twitter_uid, $message);
  }
}