<?php

/**
 * @file
 *  Adds the Sharing plugin to JW player.
 *  More info at http://www.longtailvideo.com/addons/plugins/110/Sharing
 *
 * @see http://drupal.org/node/1338964 - implementing plugins
 */

/**
 * Implements hook_jw_player_plugin_info().
 *
 * Register a jw_player preset plugin.
 */
function jw_player_sharing_jw_player_plugin_info() {
  $plugins['sharing-3'] = array(
    'name' => t('Sharing'),
    'description' => t('Sharing plugin for HTML5 JW player'),
    'config options' => array(
      'link' => array(
        '#type' => 'textfield',
        '#required' => FALSE,
        '#size' => 128,
        '#default_value' => '',
        '#description' => t('Set this flashvar to the url of a webpage where videos are shown, or set this to true to automatically pull in the URL of the current page of the video. When set, the share button will appear.')
        ),
      'code' => array(
        '#type' => 'textfield',
        '#required' => FALSE,
        '#size' => 128,
        '#default_value' => '',
        '#description' => t('Set this flashvar to an embed code of a video, or set this to true to automatically pull in the code of the current video. When set, the embed button is shown. Be warned that this embed code must be URI escaped or the code will break the other flashvars of the player.')
        ),
    ),
  );

  return $plugins;
}

/**
 * Implements template_preprocess_jw_player().
 *
 * If no embed link is set, set default
 */
function jw_player_sharing_preprocess_jw_player(&$variables) {
  // Check the plugin is enabled
  if (isset($variables['plugins']['sharing-3']['enable']) && $variables['plugins']['sharing-3']['enable'] == 1) {
    // Check link field for a value of 'true' to see if we need to manually add the link url
    if (isset($variables['plugins']['sharing-3']['link']) && trim(strtolower($variables['plugins']['sharing-3']['link'])) == 'true') {
      // Set link to current page trimming the beginning slash from the request uri
      $link = ltrim(request_uri(), '/');
      // If the video is set to inline, escape the quotes
      if (variable_get('jw_player_inline_js', FALSE)) {
        $embed_code = addslashes($link);
      }
      $variables['plugins']['sharing-3']['link'] = url($link, array('absolute' => TRUE));
      $variables['config']['plugins']['sharing-3']['link'] = url($link, array('absolute' => TRUE));
    }
    // Check code field for a value of 'true' to see if we need to manually add the embed code
    if (isset($variables['plugins']['sharing-3']['code']) && trim(strtolower($variables['plugins']['sharing-3']['code'])) == 'true') {
      // Set the embed code to the current video
      $embed_code = '<embed src="' . $variables['sources'][0]['file_path'] . '" width="' . $variables['width'] . '" height="' . $variables['height'] . '" allowfullscreen="true" />';
      // If the video is set to inline, escape the quotes
      if (variable_get('jw_player_inline_js', FALSE)) {
        $embed_code = addslashes($embed_code);
      }
      $variables['plugins']['sharing-3']['code'] = $embed_code;
      $variables['config']['plugins']['sharing-3']['code'] = $embed_code;
    }
  }
}
