You are here

twitter_profile_widget.module in Twitter Profile Widget 7

File

twitter_profile_widget.module
View source
<?php

/**
 * Implements hook_help().
 */
function twitter_profile_widget_help($path, $arg) {
  switch ($path) {
    case 'admin/config/services/twitter_profile_widget':
      return t("Twitter profile widget provides configurable block with user's latest twits.");
    case 'admin/help#twitter_profile_widget':
      return '<p>' . t("Twitter profile widget provides configurable block with user's latest twits.") . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function twitter_profile_widget_menu() {
  $items = array();
  $items['admin/config/services/twitter_profile_widget'] = array(
    'title' => 'Twitter Profile Widget',
    'description' => 'Configure twitter profile block',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'twitter_profile_widget_admin',
    ),
    'access arguments' => array(
      'administer twitter profile widget',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'twitter_profile_widget.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function twitter_profile_widget_permission() {
  return array(
    'administer twitter profile widget' => array(
      'title' => t('Administer twitter profile widget module'),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function twitter_profile_widget_block_info() {
  $block[] = array(
    'info' => t('Twitter Profile Widget block'),
    'weight' => 0,
  );
  return $block;
}

/**
 * Implements hook_block_view().
 */
function twitter_profile_widget_block_view($delta = '') {
  $block = array(
    'subject' => '<ins class = "tpw_title">' . t('Twitter Updates') . '</ins>',
    'content' => twitter_profile_widget_start(),
  );
  return $block;
}

/**
 * Implements hook_init().
 */
function twitter_profile_widget_init() {
  drupal_add_js('http://widgets.twimg.com/j/2/widget.js', 'external');
  drupal_add_css(drupal_get_path('module', 'twitter_profile_widget') . '/twitter_profile_widget.css');
}

/**
 * Implements hook_theme().
 */
function twitter_profile_widget_theme($existing, $type, $theme, $path) {
  return array(
    'twitter_profile_block' => array(
      'variables' => array(
        'twitter_script' => NULL,
        'follow_us_enabled' => NULL,
        'follow_us_link' => NULL,
      ),
      'template' => 'twitter-profile-widget',
    ),
  );
}

/**
 * Function returns block content for twitter profile widget
 */
function twitter_profile_widget_start() {

  // Main settings
  $username = trim(variable_get('twitter_profile_widget_username', 'Twitter'));

  // Preferences
  $post_amount = variable_get('twitter_profile_widget_post_amount', '5');
  $widget_behavior = variable_get('twitter_profile_widget_behavior', 'load_all_tweets') == 'load_all_tweets' ? 'all' : 'default';
  $loop_results = $widget_behavior == 'all' ? 'false' : variable_get('twitter_profile_widget_loop_results', FALSE);
  $loop_results = $loop_results == 'false' ? 'false' : 'true';
  $tweet_intervals = trim(variable_get('twitter_profile_widget_tweet_intervals', 5)) * 1000;
  $poll_for_new_results = variable_get('twitter_profile_widget_poll_for_new_results', FALSE) ? 'true' : 'false';
  $include_scrollbar = variable_get('twitter_profile_widget_include_scrollbar', FALSE) ? 'true' : 'false';
  $show_avatars = variable_get('twitter_profile_widget_show_avatars', FALSE) ? 'true' : 'false';
  $show_timastamp = variable_get('twitter_profile_widget_show_timestamp', TRUE) ? 'true' : 'false';
  $show_hashtags = variable_get('twitter_profile_widget_show_hashtags', TRUE) ? 'true' : 'false';

  // Apprearance
  $shell_bg_color_type = variable_get('twitter_profile_widget_shell_bg_color_type', 'color');
  $shell_bg_color = $shell_bg_color_type == 'transparent' ? 'none' : '#' . trim(variable_get('twitter_profile_widget_shell_bg_color', 'FFFFFF'));
  $shell_text_color = '#' . trim(variable_get('twitter_profile_widget_shell_text_color', '425367'));
  $tweet_bg_color_type = variable_get('twitter_profile_widget_tweet_bg_color_type', 'color');
  $tweet_bg_color = $tweet_bg_color_type == 'transparent' ? 'none' : '#' . trim(variable_get('twitter_profile_widget_tweet_bg_color', 'FFFFFF'));
  $tweet_text_color = '#' . trim(variable_get('twitter_profile_widget_tweet_text_color', '3E465B'));
  $tweet_links_color = '#' . trim(variable_get('twitter_profile_widget_tweet_links_color', '0178B4'));

  // Dimensions
  $width = variable_get('twitter_profile_widget_autowidth', FALSE) ? "'auto'" : trim(variable_get('twitter_profile_widget_width', '180'));
  $height = trim(variable_get('twitter_profile_widget_height', '300'));

  // Follow us button
  $follow_us_enabled = variable_get('twitter_profile_widget_follow_us_enable', TRUE);
  $follow_us_text = trim(variable_get('twitter_profile_widget_follow_us_text', 'Follow us on twitter'));
  $follow_us_url = trim(variable_get('twitter_profile_widget_follow_us_link', 'http://twitter.com/twitter'));
  $follow_us_link = l($follow_us_text, $follow_us_url, array(
    'absolute' => TRUE,
  ));

  // Widget type
  $widget_type = variable_get('twitter_profile_widget_type', 'profile');
  $twitter_list = trim(variable_get('twitter_profile_widget_twitterlist', ''));
  $captions = '';
  $set = '';
  if ($widget_type == 'list') {
    $set = "setList('{$username}', '{$twitter_list}')";
    $title = variable_get('twitter_profile_widget_twitterlist_title', t('Everything we do at'));
    $subject = variable_get('twitter_profile_widget_twitterlist_subject', t('the twoffice'));
    $captions = "\n      title: '{$title}',\n      subject: '{$subject}',\n    ";
  }
  elseif ($widget_type == 'faves') {
    $set = "setUser('{$username}')";
    $title = variable_get('twitter_profile_widget_faves_title', t('Best twitts according to'));
    $subject = variable_get('twitter_profile_widget_faves_subject', t('Me'));
    $captions = "\n      title: '{$title}',\n      subject: '{$subject}',\n    ";
  }
  elseif ($widget_type == 'profile') {
    $set = "setUser('{$username}')";
  }

  // Load twitter widget with custom settings
  $twitter_script = "\n    new TWTR.Widget({\n      version: 2,\n      type: '{$widget_type}',\n      rpp:  {$post_amount},\n      interval: {$tweet_intervals},\n      {$captions}\n      width: {$width},\n      height: {$height},\n      theme: {\n        shell: {\n          background: '{$shell_bg_color}',\n          color: '{$shell_text_color}'\n        },\n        tweets: {\n          background: '{$tweet_bg_color}',\n          color: '{$tweet_text_color}',\n          links: '{$tweet_links_color}'\n        }\n      },\n      features: {\n        scrollbar: {$include_scrollbar},\n        loop: {$loop_results},\n        live: {$poll_for_new_results},\n        hashtags: {$show_hashtags},\n        timestamp: {$show_timastamp},\n        avatars: {$show_avatars},\n        behavior: '{$widget_behavior}'\n      }\n    }).render().{$set}.start();";
  return theme('twitter_profile_block', array(
    'twitter_script' => $twitter_script,
    'follow_us_enabled' => $follow_us_enabled,
    'follow_us_link' => $follow_us_link,
  ));
}