You are here

twitter_block.module in Twitter Block 6

A module to provide simple Twitter blocks using the Twitter Search API.

File

twitter_block.module
View source
<?php

/**
 * @file
 * A module to provide simple Twitter blocks using the Twitter Search API.
 */

/**
 * Implementation of hook_perm().
 */
function twitter_block_perm() {
  return array(
    'Administer Twitter Blocks',
  );
}

/**
 * Retrieve the Twitter block configuration options from the db.
 */
function twitter_block_get_config($delta) {
  static $config;
  if (!isset($config[$delta])) {
    $result = db_query("SELECT search_type, include_rts, search_string, default_title, results_per_page, lang FROM {twitter_block} WHERE delta = '%s'", $delta);

    // @todo There can be only one?
    while ($record = db_fetch_object($result)) {
      $config[$delta] = get_object_vars($record);
    }
  }
  return isset($config[$delta]) ? $config[$delta] : FALSE;
}

/**
 * Implements hook_block().
 */
function twitter_block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks = array();
      $result = db_query("SELECT delta, default_title FROM {twitter_block}");
      while ($record = db_fetch_object($result)) {
        $blocks[$record->delta] = array(
          'info' => t($record->default_title),
          'cache' => DRUPAL_CACHE_GLOBAL,
        );
      }
      return $blocks;
      break;
    case 'configure':
      $config = twitter_block_get_config($delta);
      $form = array();
      $form['twitter_block_' . $delta] = array(
        '#type' => 'fieldset',
        '#title' => t('Twitter Block configuration'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $form['twitter_block_' . $delta]['search_type'] = array(
        '#type' => 'radios',
        '#title' => t('Search type'),
        '#options' => array(
          'searchHashtag' => t('Tweets mentioning a hashtag or search string'),
          'getTweetsFrom' => t('Tweets sent from a specific user'),
          'getReplies' => t('Replies to the a specific user'),
          'getMentions' => t('Tweets mentioning a specific user'),
        ),
        '#default_value' => $config['search_type'],
        '#description' => t('Choose the method to use when searching tweets.'),
      );
      $form['twitter_block_' . $delta]['search_string'] = array(
        '#type' => 'textfield',
        '#title' => t('Search query'),
        '#default_value' => $config['search_string'],
        '#description' => t('Enter a username, hashtag or search string depending on the currently selected search type.'),
        '#maxlength' => 140,
      );
      $form['twitter_block_' . $delta]['include_rts'] = array(
        '#type' => 'checkbox',
        '#title' => t('Include retweets'),
        '#default_value' => $config['include_rts'],
      );
      $form['twitter_block_' . $delta]['lang'] = array(
        '#type' => 'textfield',
        '#title' => t('Language'),
        '#description' => t('Enter an <a href="@language-codes">ISO 639-1 language code</a>. Only tweets written in the specified language will be displayed. Note that Twitter only supports a limited number of languages (see the Twitter <a href="@search-page">advanced search page</a> for a list of currently supported languages). Leave blank to display tweets from all languages.', array(
          '@language-codes' => 'http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes',
          '@search-page' => 'http://twitter.com/search-advanced',
        )),
        '#default_value' => $config['lang'],
        '#size' => 2,
        '#maxlength' => 2,
      );
      $form['twitter_block_' . $delta]['results_per_page'] = array(
        '#type' => 'select',
        '#title' => t('Tweets'),
        '#options' => drupal_map_assoc(array(
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10,
          20,
          30,
          40,
          50,
          60,
          70,
          80,
          90,
          100,
        )),
        '#default_value' => $config['results_per_page'],
        '#description' => t('Select the number of tweets to display.'),
      );
      return $form;
      break;
    case 'save':
      $result = db_query("UPDATE {twitter_block} SET search_type = '%s', include_rts = '%d', search_string = '%s', results_per_page = '%s', lang = '%s' WHERE delta = '%s'", $edit['search_type'], $edit['include_rts'], $edit['search_string'], $edit['results_per_page'], $edit['lang'], $delta);
      break;
    case 'view':
      module_load_include('php', 'twitter_block', 'twitter_block.class');

      // Load the configuration.
      $config = twitter_block_get_config($delta);

      // Use the MD5 of the block config as the cache cid.
      $cid = 'twitter_block_feed_' . md5(serialize($config));
      $cache_table = 'cache';

      // Build the object.
      $twitter = new TwitterBlockSearch($config);
      $response = $twitter
        ->getJSON();
      $results = array();
      if (empty($response) || !is_array($response) || !isset($response['status']) || $response['status'] !== TRUE) {
        watchdog('Twitter Block', 'Recieved an unexpected reply from Twitter. ' . 'Perhaps just a fail whale?<br/>' . 'URL: url_query<br />' . 'response', array(
          'url_query' => $twitter->url_query,
          'response' => print_r($response, TRUE),
        ), WATCHDOG_NOTICE);
      }
      else {
        $results = $response['results'];
      }

      // Create a variable to hold the tweets.
      $tweets = array();

      // Theme each of the returned tweets.
      foreach ($results as $tweet) {
        $tweets[] = theme('twitter_block_tweet', array(
          'tweet' => $tweet,
          'api' => $twitter
            ->getApi(),
        ));
      }
      $content = theme_item_list($tweets);
      drupal_add_css(drupal_get_path('module', 'twitter_block') . '/twitter_block.css');
      $block = array();
      $block['subject'] = t($config['default_title']);
      $block['content'] = $content;
      return $block;
      break;
  }
}

/**
 * Implementation of hook_theme().
 */
function twitter_block_theme($existing, $type, $theme, $path) {
  return array(
    'twitter_block_tweet' => array(
      'arguments' => array(
        'tweet' => NULL,
        'api' => NULL,
      ),
      'path' => drupal_get_path('module', 'twitter_block'),
      'template' => 'twitter-block-tweet',
    ),
  );
}

/**
 * Implementation of hook_preprocess().
 */
function twitter_block_preprocess_twitter_block_tweet(&$variables) {
  $variables['text'] = twitter_block_linkify($variables['tweet']->text);
  $variables['date'] = format_date(strtotime($variables['tweet']->created_at));
  $variables['user_image'] = $variables['api'] == 'rest' ? $variables['tweet']->user->profile_image_url : $variables['tweet']->profile_image_url;
  $variables['name'] = $variables['api'] == 'rest' ? $variables['tweet']->user->name : $variables['tweet']->from_user_name;
  $variables['screen_name'] = $variables['api'] == 'rest' ? $variables['tweet']->user->screen_name : $variables['tweet']->from_user;
}

/**
 * Convert nested URLs, account names and hash tags into links.
 */
function twitter_block_linkify($status_text) {

  // Linkify URLs.
  $status_text = preg_replace('/(https?:\\/\\/\\S+)/', '<a href="\\1">\\1</a>', $status_text);

  // Linkify twitter users.
  $status_text = preg_replace('/(^|\\s)@(\\w+)/', '\\1@<a href="http://twitter.com/\\2">\\2</a>', $status_text);

  // Linkify tags.
  $status_text = preg_replace('/(^|\\s)#([\\wåäöÅÄÖ]+)/', '\\1#<a href="http://search.twitter.com/search?q=%23\\2">\\2</a>', $status_text);
  return $status_text;
}

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function twitter_block_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools') {
    return 'plugins/' . $plugin;
  }
}

Functions

Namesort descending Description
twitter_block_block Implements hook_block().
twitter_block_ctools_plugin_directory Implementation of hook_ctools_plugin_directory().
twitter_block_get_config Retrieve the Twitter block configuration options from the db.
twitter_block_linkify Convert nested URLs, account names and hash tags into links.
twitter_block_perm Implementation of hook_perm().
twitter_block_preprocess_twitter_block_tweet Implementation of hook_preprocess().
twitter_block_theme Implementation of hook_theme().