You are here

function twitter_block_block in Twitter Block 6

Implements hook_block().

File

./twitter_block.module, line 35
A module to provide simple Twitter blocks using the Twitter Search API.

Code

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;
  }
}