<?php

/**
 * @file
 * Extends Service Links with dynamic buttons.
 *
 * @author Fabio Mucciante (TheCrow)
 */

/**
 * Implements hook_service_links().
 */
function widget_services_service_links() {
  $links = array();

  $links['facebook_share'] = array(
    'name' => t('Share on Facebook'),
    'link' => 'http://www.facebook.com/sharer.php',
    'description' => t('Share this post on Facebook'),
    'javascript' => 'facebook_share.js',
    'icon' => 'facebook.png',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_facebook_share_data',
  );
  $links['twitter_widget'] = array(
    'name' => 'Tweet Widget',
    'link' => 'http://twitter.com/share?url=<raw-encoded-short-url>&count=<data-count>&via=<data-via>&text=<raw-encoded-title>&counturl=<raw-encoded-url>',
    'icon' => 'twitter.png',
    'description' => t('Tweet This'),
    'attributes' => array('class' => array('twitter-share-button')),
    'javascript' => 'twitter_button.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_twitter_widget_data',
  );
  $links['facebook_like'] = array(
    'name' => 'Facebook Like',
    'link' => 'http://www.facebook.com/plugins/like.php?href=<encoded-url>&layout=<layout>&show_faces=<show_faces>&action=<action>&colorscheme=<colorscheme>&width=<width>&height=<height>&font=<font>&locale=<locale>',
    'icon' => 'facebook.png',
    'description' => t('I Like it'),
    'javascript' => 'facebook_like.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_facebook_like_data',
  );
  $links['google_plus_one'] = array(
    'name' => 'Google Plus One',
    'link' => '<url>',
    'description' => t('Plus it'),
    'javascript' => 'google_plus_one.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_google_plus_one_data',
  );
  $links['linkedin_share_button'] = array(
    'name' => 'Linkedin Share Button',
    'link' => '<url>',
    'icon' => 'linkedin.png',
    'description' => t('Share on Linkedin'),
    'javascript' => array(
      'http://platform.linkedin.com/in.js?async=true',
      'linkedin_share_button.js',
    ),
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_linkedin_share_button_data',
  );
  $links['pinterest_button'] = array(
    'name' => 'Pinterest',
    //'link' => 'http://pinterest.com/pin/create/button/?url=<raw-encoded-url>&description=<pinterest-description>&media=<pinterest-media>',
    'link' => 'http://pinterest.com/pin/create/button/?url=<raw-encoded-long-url>&description=<pinterest-description>&media=<pinterest-media>',
    'icon' => 'pinterest.png',
    'description' => t('Pin It'),
    'javascript' => array(
      'pinterest_button.js',
      //'http://assets.pinterest.com/js/pinit.js',
    ),
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'attributes' => array('class' => array('pin-it-button')),
    'callback' => 'ws_pinterest_button_data',
    'preset' => 'ws_pinterest_button_tags',
  );

  return $links;
}

/**
 * Callback function for Twitter Widget.
 */
function ws_twitter_widget_data(&$service, $subst) {
  static $params;

  if (!isset($params)) {
    $params = array(
      'count' => check_plain(variable_get('service_links_tw_data_count', 'horizontal')),
      'via' => check_plain(variable_get('service_links_tw_data_via' , '')),
    );
  }
  
  $service['url'][1] = array_merge($service['url'][1], $params);
}

/**
 * Callback function for Facebook Share.
 */
function ws_facebook_share_data(&$service, $subst) {
  static $fs_settings;

  if (!isset($fs_settings)) {
    $fs_settings = array(
      'type' => check_plain(variable_get('service_links_fs_type', 'button_count')),
      'app_id' => check_plain(variable_get('service_links_fs_app_id', '150123828484431')),
      'css' => check_plain(preg_replace('/[^0-9a-z\-:;]/', '', variable_get('service_links_fs_css', ''))),
    );

    drupal_add_js(array('ws_fs' => $fs_settings), 'setting');
  }

  $service['attributes']['rel'] = $subst['url'];
}

/**
 * Callback function for Facebook Like.
 */
function ws_facebook_like_data(&$service, $subst) {
  static $params;

  if (!isset($params)) {
    $params = array(
      'layout' => check_plain(variable_get('service_links_fl_layout', 'button_count')),
      'show_faces' => check_plain(variable_get('service_links_fl_show_faces', 'false')),
      'action' => check_plain(variable_get('service_links_fl_action', 'like')),
      'colorscheme' => check_plain(variable_get('service_links_fl_colorscheme', 'light')),
      'font' => check_plain(variable_get('service_links_fl_font', '')),
      'width' => (int) variable_get('service_links_fl_width', 100),
      'height' => (int) variable_get('service_links_fl_height', 21),
      'locale' => check_plain(variable_get('service_links_fl_locale', '')),
    );

    $fl_settings = array(
      'width' => $params['width'],
      'height' => $params['height'],
    );

    drupal_add_js(array('ws_fl' => $fl_settings), 'setting');
  }

  $service['url'][1] = array_merge($service['url'][1], $params);
}

/**
 * Callback function for Google Plus One.
 */
function ws_google_plus_one_data(&$service, $subst) {
  static $gpo_settings;

  if (!isset($gpo_settings)) {
    $gpo_settings = array(
      'size' => check_plain(variable_get('service_links_gpo_size', '')),
      'annotation' => check_plain(variable_get('service_links_gpo_annotation', '')),
      'lang' => check_plain(variable_get('service_links_gpo_lang', '')),
      'callback' => check_plain(variable_get('service_links_gpo_callback', '')),
      'width' => (int) variable_get('service_links_gpo_width', 300),
    );

    drupal_add_js(array('ws_gpo' => $gpo_settings), 'setting');
  }
}

/**
 * Callback function for Linkedin Share Button.
 */
function ws_linkedin_share_button_data(&$service, $subst) {
  static $lsb_settings;

  if (!isset($lsb_settings)) {
    $lsb_settings = array(
      'countmode' => check_plain(variable_get('service_links_lsb_countmode', 'right')),
    );

    drupal_add_js(array('ws_lsb' => $lsb_settings), 'setting');
  }
}

/**
 * Preset function for Pinterest Button, which fill the media tag.
 */
function ws_pinterest_button_tags(&$service, &$settings, $node = NULL) {
  static $tokens;

  if (!isset($tokens)) {
    $tokens['media'] = variable_get('service_links_pb_mediatoken', '');
    $tokens['description'] = variable_get('service_links_pb_descriptiontoken', '');
  }
 
  $settings['tag']['pinterest-media'] = '<pinterest-media>';

  if (module_exists('token')) {
    $settings['subst']['pinterest-media'] = check_plain(@token_replace($tokens['media'], array('node' => $node)));
    if (empty($tokens['description'])) {
      $service['link'] = str_replace('pinterest-description', 'raw-encoded-teaser', $service['link']);
    }
    else {
      $settings['tag']['pinterest-description'] = '<pinterest-description>';
      $settings['subst']['pinterest-description'] = check_plain(@token_replace($tokens['description'], array('node' => $node)));
    }
  }
  else {
    $settings['subst']['pinterest-media'] = '';
    $service['link'] = str_replace('pinterest-description', 'raw-encoded-teaser', $service['link']);
  }
}

/**
 * Callback function for Pinterest Button.
 */
function ws_pinterest_button_data(&$service, $subst) {
  static $pb_settings;

  if (!isset($pb_settings)) {
    $pb_settings = array(
      'countlayout' => check_plain(variable_get('service_links_pb_countlayout', 'horizontal')),
    );

    drupal_add_js(array('ws_pb' => $pb_settings), 'setting');
  }
}

/**
 * Implements hook_menu().
 */
function widget_services_menu() {
  $items = array();

  $items['admin/config/services/service-links/widgets'] = array(
    'title' => 'Widgets',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('widget_services_admin'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_LOCAL_TASK,
    //'parent' => 'admin/config/services/service-links',
    'file' => 'widget_services.admin.inc',
  );

  return $items;
}
