You are here

swftools_jw5.module in SWF Tools 6.3

Enables SWF Tools support for LongTail Player 5.

File

jw5/swftools_jw5.module
View source
<?php

/**
 * @file
 * Enables SWF Tools support for LongTail Player 5.
 */

/**
 * Implementation of hook_swftools_methods().
 */
function swftools_jw5_swftools_methods() {

  // Wijering4 now comes in two versions - viral and non-viral - since we now cache the results of this function we can
  // check which is present, testing for the viral one first
  // Start with the viral player
  $player = '/player-viral.swf';

  // Get the library path
  $library = swftools_get_library('mediaplayer-5');

  // Discover if the viral player is present
  if (!file_exists($library . $player)) {

    // If not, try the non-viral version
    $player = '/player.swf';
    if (!file_exists($library . $player)) {

      // If non-viral isn't present either revert to viral for purposes of the download message (since viral is the default)
      $player = '/player-viral.swf';
    }
  }

  // Define the media player
  $jwplayer = array(
    'module' => 'swftools_jw5',
    'title' => t('JW Player 5'),
    'download' => 'http://www.longtailvideo.com/players',
    'width' => 400,
    'height' => 300,
    'library' => $library . $player,
    'profile' => array(
      'path' => 'jw5',
      'settings' => array(
        'swftools_jwplayer5',
      ),
      'file' => 'swftools_jw5.admin.inc',
      'page argument' => 'swftools_jw5_profile_form',
    ),
  );

  // Wijering support various actions with the same player and info.
  $methods['video']['jwplayer5'] = $jwplayer;
  $methods['video_list']['jwplayer5'] = $jwplayer;
  $methods['audio']['jwplayer5'] = $jwplayer;
  $methods['audio_list']['jwplayer5'] = $jwplayer;
  $methods['swftools_media_display']['jwplayer5'] = $jwplayer;
  $methods['media_list']['jwplayer5'] = $jwplayer;
  $methods['image_list']['jwplayer5'] = $jwplayer;

  // Return methods
  return $methods;
}

/**
 * Implementation of hook_menu().
 */
function swftools_jw5_menu() {
  $items['admin/settings/swftools/jw5'] = array(
    'title' => 'JW Player 5',
    'description' => 'Settings for ' . l('JW Player 5', 'http://www.longtailvideo.com/players') . '.',
    'access arguments' => array(
      'administer flash',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'swftools_jw5_admin_form',
    ),
    'file' => 'swftools_jw5.admin.inc',
  );
  $items['admin/settings/swftools/jw5/autocomplete'] = array(
    'title' => 'Autocomplete skin name',
    'access arguments' => array(
      'administer flash',
    ),
    'page callback' => 'swftools_jw5_autocomplete_skin',
    'type' => MENU_CALLBACK,
    'file' => 'swftools_jw5.admin.inc',
  );
  return $items;
}

/**
 * Returns an array of default settings for the JW Player
 *
 * Return either the full set, or the minimum set, of player settings. The full set is
 * normally request by the administration page in order to populate the form, while the
 * minimum set returns just those settings needed to successfully render a player.
 *
 * @param string $profile
 *   (optional) Name of profile to use.
 * @param int $mode
 *   (optional) Use one of the following constants:
 *   - SWFTOOLS_MINIMUM_SETTINGS: Return the minimum settings to render a player.
 *   - SWFTOOLS_FULL_SETTINGS: Return the full complement of settings.
 *
 * @return array
 *   An array of settings.
 */
function _swftools_jw5_settings($profile = '', $mode = SWFTOOLS_MINIMUM_SETTINGS) {

  // Define the defaults for the minimum and full conditions
  $defaults = array(
    SWFTOOLS_MINIMUM_SETTINGS => array(
      'basic' => array(
        'width' => '400',
        'height' => '320',
      ),
      'appearance' => array(
        'fillemptyimages' => 0,
      ),
      'accessibility' => array(
        'accessible' => 0,
      ),
      'imagecache' => array(
        'imagecache_player' => SWFTOOLS_UNDEFINED,
        'imagecache_playlist' => SWFTOOLS_UNDEFINED,
      ),
    ),
    SWFTOOLS_FULL_SETTINGS => array(
      'basic' => array(
        'playlistsize' => '',
        'width' => '400',
        'height' => '320',
      ),
      'color' => array(
        'backcolor' => '',
        'frontcolor' => '',
        'lightcolor' => '',
        'screencolor' => '',
      ),
      'appearance' => array(
        'skin' => '',
        'logo' => '',
        'overstretch' => 'default',
        'controlbar' => 'default',
        'playlist' => 'default',
        'plugins' => '',
        'fillemptyimages' => 0,
      ),
      'playback' => array(
        'autostart' => 'default',
        'bufferlength' => '',
        'displayclick' => 'default',
        'repeat' => 'default',
        'shuffle' => 'default',
        'volume' => '',
      ),
      'interaction' => array(
        'captions' => '',
        'link' => '',
        'linktarget' => 'default',
        'streamscript' => '',
        'type' => 'default',
        'fullscreen' => 'default',
      ),
      'accessibility' => array(
        'accessible' => SWFTOOLS_ACCESSIBLE_DISABLED,
      ),
      'imagecache' => array(
        'imagecache_player' => SWFTOOLS_UNDEFINED,
        'imagecache_playlist' => SWFTOOLS_UNDEFINED,
      ),
    ),
  );

  // TODO: Does this actually work? Once a form has been stored will swftools_variable_get() not
  // always retrieve a full set of variables? The minimum set will only be returned when there
  // are no settings stored in the database?
  // Retrieve current default settings
  $settings = swftools_variable_get('swftools_jwplayer4', $defaults[SWFTOOLS_MINIMUM_SETTINGS], $profile);

  // If a full set of settings are requested for the admin page then merge with full defaults
  if ($mode == SWFTOOLS_FULL_SETTINGS) {
    $settings = swftools_array_merge($defaults[SWFTOOLS_FULL_SETTINGS], $settings);
  }

  // Return resulting defaults
  return $settings;
}

/**
 * Return the default flashvar configuration for JW Player 4.
 *
 * This function is called from swftools_jw5_swftools_flashvars() which is called from swf()
 * It will return the default flashvar configuration, just prior to any overrides
 * passed into swf(). We start with the settings defined on admin/swf/wijering
 * which are returned by _swftools_jw5_settings(). Then we prepare these values for output
 * to html (eg. '1' become 'true') and we unset undefined flashvars to prevent their output.
 *
 * TODO: Could we just do this when we save the settings, and then save the result, as this doesn't
 * change until the settings are updated.
 *
 * @param string $profile
 *   (optional) Name of profile to use.
 *
 * @return array
 *   An array of flashvars as key => value pairs.
 */
function _swftools_jw5_flashvars($profile = '') {

  // Initialise cache to handle repeated calls
  static $cache = array();

  // Store name of profile
  $_profile = $profile ? $profile : SWFTOOLS_UNDEFINED;

  // If no settings stored for this profile then fetch them
  if (!isset($cache[$_profile])) {

    // Get saved settings for this method.
    $settings = _swftools_jw5_settings($profile);

    // Flatten defaults
    swftools_array_flatten($settings);

    // Iterate over boolean settings
    foreach (array(
      'autostart',
      'shuffle',
      'fullscreen',
    ) as $key) {

      // Settings are already encoded properly on the settings page, but if we called from PHP it might be 1/0 or TRUE/FALSE
      if (isset($settings[$key])) {
        $settings[$key] = _swftools_tf($settings[$key]);
      }
    }

    // Store the result
    $cache[$_profile] = $settings;
  }

  // Return the result
  return $cache[$_profile];
}

/**
 * Implementation of hook_swftools_playlist_[player]().
 */
function swftools_jw5_swftools_playlist_jwplayer5($data) {

  // Get current defaults for the player
  $saved_settings = _swftools_jw5_flashvars($data['othervars']['profile']);

  // Initialise a string to contain the elements
  $xml = '';

  // Iterate over the playlist to build elements xml
  foreach ($data['othervars']['playlist_data']['playlist'] as $track => $details) {
    if (!$details['image'] && $saved_settings['fillemptyimages']) {
      $details['image'] = theme('swftools_empty_image', $data);
    }

    // Create an individual xml element
    $xml .= theme('swftools_jw5_playlist_element', $details, $saved_settings['imagecache_playlist'] == SWFTOOLS_UNDEFINED ? '' : $saved_settings['imagecache_playlist']);
  }

  // Add xml wrapper around the elements
  $xml = theme('swftools_jw5_playlist_wrapper', $data['othervars']['playlist_data']['header'], $xml);

  // Return the resulting xml
  return $xml;
}

/**
 * Implementation of hook_swftools_variable_mapping().
 *
 */
function swftools_jw5_swftools_variable_mapping() {
  return array(
    'jwplayer5' => array(
      'height' => 'flashvars',
      'width' => 'flashvars',
      'shuffle' => 'flashvars',
      'fullscreen' => 'flashvars',
      'autostart' => 'flashvars',
      'quality' => 'flashvars',
      'backcolor' => 'flashvars',
      'frontcolor' => 'flashvars',
      'lightcolor' => 'flashvars',
      'screencolor' => 'flashvars',
      'logo' => 'flashvars',
      'captions' => 'flashvars',
      'link' => 'flashvars',
      'streamscript' => 'flashvars',
      'skin' => 'flashvars',
      'playlistsize' => 'flashvars',
      'bufferlength' => 'flashvars',
      'volume' => 'flashvars',
      'type' => 'flashvars',
      'repeat' => 'flashvars',
      'linktarget' => 'flashvars',
      'stretching' => 'flashvars',
      'playlist' => 'flashvars',
      'controlbar' => 'flashvars',
      'displayclick' => 'flashvars',
    ),
  );
}

/**
 * Implementation of hook_help().
 */
function swftools_jw5_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/swftools/jw5':
      return '<p>' . t('These are the settings for Longtail Video\'s JW Player 5. For details of what each parameter does refer to the <a href="@wijering">JW Player for Flash wiki page</a>. It is possible that you do not need to change any of these settings and blank values will defer to friendly defaults. Note that the label in (<em>brackets</em>) is the actual flashvar name and corresponds to the wiki page. If content is embedded using the SWF Tools filter then each parameter can be over-ridden by specifying a new value in the filter string.', array(
        '@wijering' => 'http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12536/configuration-options',
      )) . '</p>';
  }
}

/**
 * Implementation of hook_theme().
 */
function swftools_jw5_theme() {
  return array(
    'swftools_jw5_playlist_element' => array(
      'template' => 'swftools-jw5-playlist-element',
      'arguments' => array(
        'element' => NULL,
        'imagecache' => NULL,
      ),
    ),
    'swftools_jw5_playlist_wrapper' => array(
      'template' => 'swftools-jw5-playlist-wrapper',
      'arguments' => array(
        'header' => NULL,
        'xml' => NULL,
      ),
    ),
    'swftools_jw5_accessible' => array(
      'arguments' => array(
        'id' => NULL,
        'visible' => NULL,
      ),
    ),
  );
}

/**
 * Returns markup to enable accessible controls for the Wijering 4 player.
 *
 * @param string $id
 *   id of the player to be made accessible.
 * @param int $visible
 *   Whether the controls should be visible. Options are one the following constants:
 *   - SWFTOOLS_ACCESSIBLE_DISABLED
 *   - SWFTOOLS_ACCESSIBLE_HIDDEN
 *   - SWFTOOLS_ACCESSIBLE_VISIBLE
 *
 * @return string
 *   Mark up to place accessible controls on the page.
 *
 * @ingroup themeable
 * @ingroup swftools
 */
function theme_swftools_jw5_accessible($id, $visible) {
  $actions = array(
    'play' => t('Play'),
    'pause' => t('Pause'),
    'mute' => t('Mute'),
    'unmute' => t('Unmute'),
    'stop' => t('Rewind and stop'),
  );
  return theme('swftools_accessible_controls', 'jw5', $id, $actions, $visible);
}

/**
 * Implementation of hook_init().
 */
function swftools_jw5_init() {

  // Add JavaScript to enable auto-close behavior and accessibility
  if (variable_get('swftools_always_add_js', SWFTOOLS_ALWAYS_ADD_JS)) {
    swftools_jw5_add_js();
  }
}

/**
 * Adds Wijering4 JavaScript to the page.
 */
function swftools_jw5_add_js() {

  // Add JavaScript to enable auto-close behavior and accessibility
  drupal_add_js(drupal_get_path('module', 'swftools_jw5') . '/swftools_jw5.js');
}

/**
 * Implementation of hook_swftools_preprocess_[player]().
 */
function swftools_jw5_swftools_preprocess_jwplayer5(&$data) {

  // Get current defaults for the player
  $saved_settings = _swftools_jw5_flashvars($data['othervars']['profile']);

  // Prepare an array of flashvars by merging defaults and user values
  $data['flashvars'] = array_merge($saved_settings, $data['flashvars']);

  // If a skin has been set it may need to be expanded to a path if not already a full url
  if (isset($data['flashvars']['skin'])) {
    if (!valid_url($data['flashvars']['skin'], TRUE)) {
      $data['flashvars']['skin'] = base_path() . swftools_get_library('mediaplayer-5') . '/skins/' . $data['flashvars']['skin'];
    }
  }

  // If an image has been set then use it
  if ($data['othervars']['image']) {

    // Get source path to the image file
    $source = swftools_get_url_and_path($data['othervars']['image']);

    // If $source succeeded add image to the playlist
    if ($source) {

      // See if we need to apply an imagecache preset
      if ($saved_settings['imagecache_player'] != SWFTOOLS_UNDEFINED) {
        $source['fileurl'] = swftools_imagecache_create_path($saved_settings['imagecache_player'], $source['fileurl']);
      }

      // Store result in flashvars
      $data['flashvars']['image'] = $source['fileurl'];
    }
  }

  // Don't output imagecache_variables
  unset($data['flashvars']['imagecache_player'], $data['flashvars']['imagecache_playlist']);

  // Is this a single streamed file, or an entirely stream playlist?
  if ($data['othervars']['stream'] && $data['othervars']['stream'] !== TRUE) {

    // Set the streamer flashvar
    $data['flashvars']['streamer'] = $data['othervars']['stream'];
  }

  // Add the accessible controls?
  if ($data['flashvars']['accessible']) {

    // Add accessible controls
    $data['othervars']['#suffix'] = theme('swftools_jw5_accessible', $data['othervars']['id'], $data['flashvars']['accessible']);
  }

  // Unset the accessible parameters from flashvars so they are not output
  unset($data['flashvars']['accessible']);

  // Attach file url of the content to display to flashvars
  $data['flashvars']['file'] = $data['othervars']['file_url'];

  // Add JavaScript to enable auto-close behavior and accessibility
  swftools_jw5_add_js();
}

Functions

Namesort descending Description
swftools_jw5_add_js Adds Wijering4 JavaScript to the page.
swftools_jw5_help Implementation of hook_help().
swftools_jw5_init Implementation of hook_init().
swftools_jw5_menu Implementation of hook_menu().
swftools_jw5_swftools_methods Implementation of hook_swftools_methods().
swftools_jw5_swftools_playlist_jwplayer5 Implementation of hook_swftools_playlist_[player]().
swftools_jw5_swftools_preprocess_jwplayer5 Implementation of hook_swftools_preprocess_[player]().
swftools_jw5_swftools_variable_mapping Implementation of hook_swftools_variable_mapping().
swftools_jw5_theme Implementation of hook_theme().
theme_swftools_jw5_accessible Returns markup to enable accessible controls for the Wijering 4 player.
_swftools_jw5_flashvars Return the default flashvar configuration for JW Player 4.
_swftools_jw5_settings Returns an array of default settings for the JW Player