You are here

function theme_swftools_flowplayer3 in SWF Tools 6.3

Embeds media on the page using the Flowplayer 3 embedding JavaScript.

$data An array of SWF Tools data.

Return value

A string of markup that includes both HTML and JavaScript necessary to embed the player.

File

flowplayer3/swftools_flowplayer3.module, line 516
Enables SWF Tools support for Flowplayer 3.

Code

function theme_swftools_flowplayer3($file, $data, $script_location = SWFTOOLS_JAVASCRIPT_INLINE) {

  // Get saved settings ready to decide if we want a playlist
  $saved_settings = _swftools_flowplayer3_settings($data['othervars']['profile']);

  // Ensure Flowplayer 3 embedding script is added to the page
  swftools_flowplayer3_add_js();

  // Convert config array to JSON
  $config = drupal_to_js($data['othervars']['flowplayer3']['config']);

  // Remove quotes from around true and false
  $config = str_replace(array(
    '"false"',
    '"true"',
  ), array(
    "false",
    "true",
  ), $config);

  // Reverse escaped quotes - temporary code while working up template based JSON
  $config = str_replace('\\"', '"', $config);

  // Reverse quoting around [ and ] - temporary code while working up template based JSON
  $config = str_replace(array(
    '"[',
    ']"',
  ), array(
    "[",
    "]",
  ), $config);

  // Clear wmode parameter as if this is not window then Flowplayer will not display
  unset($data['params']['wmode']);

  // Clear the version parameters as having it seems to upset IE6/7
  unset($data['params']['version']);

  // Put the src in to the params ready to be output
  $data['params']['src'] = $data['othervars']['fileurl'];

  // Convert parameters to JSON
  $parameters = drupal_to_js($data['params']);

  // Remove quotes from around true and false
  $parameters = str_replace(array(
    '"false"',
    '"true"',
  ), array(
    "false",
    "true",
  ), $parameters);

  // If we have a playlist then use this code, based on http://flowplayer.org/plugins/javascript/playlist.html
  if (isset($data['othervars']['playlist_data']) && $saved_settings['playlists']['playlist']) {

    // Generate mark up for the combined player and playlist using the flowplayer3_playlist template
    // Using a template lets a website completely over-ride the appearance of the player
    $html = theme('swftools_flowplayer3_playlist', $data, $parameters, $config, $saved_settings, variable_get('swftools_flowplayer3_load', TRUE));

    // Return placeholder and JavaScript ready to be cached
    return "\n" . $html . "\n";
  }

  // We have a single file, so use this code
  // Build a simple HTML string to act as the container - this version excludes alternate text so the player appears
  $html = t('<div id="!id">!alt</div>', array(
    '!id' => $data['othervars']['id'],
    '!alt' => variable_get('swftools_flowplayer3_load', TRUE) ? '' : $html_alt,
  ));

  // Start adding scripts to the page
  // Rather than add each script separately with drupal_add_js build an array and then collapse it and add once
  $script = array();

  // Provision ready for later to add some JavaScript immediately after Flowplayer embedding
  $post_js = '';

  // Script to add player instance
  $script[] = t('flowplayer("!id", !url, !config);
             !post_js
             ', array(
    '!id' => $data['othervars']['id'],
    '!url' => $parameters,
    '!config' => $config,
    '!post_js' => $post_js,
  ));

  // Script to set height and width of container
  // TODO: We could make this a behavior and use Drupal.settings to pass the details
  $script[] = t('$("#!id").height(!height).width(!width);', array(
    '!id' => $data['othervars']['id'],
    '!height' => $data['othervars']['height'],
    '!width' => $data['othervars']['width'],
  ));

  // Test code to add some JavaScript after the player - hard coded for now, but will be exposed
  //  $script[] = 'alert($f("'. $id . '").getVersion());';
  // Implode the array to a single string ready for inclusion
  $script = implode("\n", $script);

  // Generate inline script, or place script in the footer
  if ($script_location == SWFTOOLS_JAVASCRIPT_INLINE) {

    // Add script using container id as context and then retrieve it so we get it formatted and wrapped in CDATA
    drupal_add_js($script, 'inline', $data['othervars']['id']);
    $script = drupal_get_js($data['othervars']['id']);
  }
  else {

    // Add script to the footer, but we also put a copy inline, using id as context, so SWF Tools can cache it
    // Note - Flowplayer3 embedding doesn't work if the script is in the header, so always place in the footer
    drupal_add_js($script, 'inline', 'footer');
    drupal_add_js($script, 'inline', $data['othervars']['id']);
    $script = '';
  }

  //  // Add script using container id as context to retrieve only scripts relevant to this player
  //  drupal_add_js($script, 'inline', $data['othervars']['id']);
  //
  //  // Retrieve formatted string containg scripts for this player
  //  $script = drupal_get_js($data['othervars']['id']);
  // Return placeholder and JavaScript ready to be cached
  return $html . "\n" . $script;
}