You are here

twitter_actions.module in Twitter 6.4

Exposes Drupal actions for sending Twitter messages.

File

twitter_actions/twitter_actions.module
View source
<?php

/**
 * @file
 * Exposes Drupal actions for sending Twitter messages.
 */

/**
 * Implementation of hook_action_info().
 */
function twitter_actions_action_info() {
  return array(
    'twitter_actions_set_status_action' => array(
      'type' => 'system',
      'description' => t('Post a message to Twitter'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array(
          'view',
          'insert',
          'update',
          'delete',
        ),
        'comment' => array(
          'view',
          'insert',
          'update',
          'delete',
        ),
        'user' => array(
          'view',
          'insert',
          'update',
          'delete',
          'login',
        ),
        'cron' => array(
          'run',
        ),
      ),
    ),
  );
}

/**
 * Return a form definition so the Send email action can be configured.
 *
 * @param $context
 *   Default values (if we are editing an existing action instance).
 * @return
 *   Form definition.
 */
function twitter_actions_set_status_action_form($context = array()) {

  // Set default values for form.
  $context += array(
    'account_id' => -1,
    'screen_name' => '',
    'message' => '',
  );
  $options = array();
  $results = db_query("SELECT screen_name FROM {twitter_account}");
  while ($row = db_fetch_array($results)) {
    $options[$row['screen_name']] = $row['screen_name'];
  }
  $form['screen_name'] = array(
    '#type' => 'select',
    '#title' => t('Twitter account name'),
    '#options' => $options,
    '#default_value' => $context['screen_name'],
    '#description' => t('The Twitter account which will be used to post to Twitter. It can be ' . 'added by editing a user account.'),
    '#required' => TRUE,
  );
  if (!count($options)) {
    $form['screen_name']['#description'] = t('You first need to add a Twitter account to one of ' . 'your users with rights for posting to Twitter.');
  }
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => $context['message'],
    '#cols' => '80',
    '#rows' => '3',
    '#description' => t('The message that should be sent. The following tokens are available: ' . '%site_name, %username, %node_url, %node_type, %title, %teaser, %body, ' . '%tinyurl. Note that not all variables will be available in all contexts.'),
    '#required' => TRUE,
  );
  if (module_exists('token')) {
    $form['token_help'] = array(
      '#title' => t('Extra replacement tokens'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
    );
    $form['token_help']['help'] = array(
      '#value' => theme('token_help', 'node'),
    );
  }
  return $form;
}

/**
 * Implements hook_actions_set_status_action_submit()
 */
function twitter_actions_set_status_action_submit($form, $form_state) {
  $form_values = $form_state['values'];
  $twitter_uid = db_result(db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = '%s'", $form_values['screen_name']));

  // Process the HTML form to store configuration. The keyed array that
  // we return will be serialized to the database.
  $params = array(
    'twitter_uid' => $twitter_uid,
    'screen_name' => $form_values['screen_name'],
    'message' => $form_values['message'],
  );
  return $params;
}

/**
 * Implementation of a configurable Drupal action.
 * Sends an email.
 */
function twitter_actions_set_status_action($object, $context) {
  global $user;
  $variables['%site_name'] = variable_get('site_name', 'Drupal');
  switch ($context['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($context['hook'], $context_list, TRUE)) {
      $message = token_replace($message, $context['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);
    }
  }
  module_load_include('inc', 'twitter');
  $twitter_account = twitter_account_load($context['twitter_uid']);
  twitter_set_status($twitter_account, $message);
}

Functions

Namesort descending Description
twitter_actions_action_info Implementation of hook_action_info().
twitter_actions_set_status_action Implementation of a configurable Drupal action. Sends an email.
twitter_actions_set_status_action_form Return a form definition so the Send email action can be configured.
twitter_actions_set_status_action_submit Implements hook_actions_set_status_action_submit()