You are here

function swftools_prepare_playlist in SWF Tools 6.3

Prepares an array of filenames, or file objects, for use in a playlist.

This function processes the supplied array and returns an array of playlist data which has two elements, header and playlist. The header array contains the playlist title, which may be an empty string if nothing was set. The playlist array contains an array of playlist elements. As a minimum each element will contain:

  • filepath : the filepath for the file, if it is a local file, or FALSE if a full url
  • title : the title for the element, set to the filename if nothing given
  • filename : the filename for the file
  • fileurl : the full url to the file

drupal_alter is called just prior to returning the playlist, so a module can implement hook_swftools_playlist_alter(&$playlist_data) to modify the playlist prior to return. This means other modules can modify, or add, elements. For example, the swftools_getid3 module implements this hook to attach ID3 data to each playlist element.

Parameters

array $files: An array of files that will be added to the playlist.

array $options: Array of data passed in from the calling swf() function.

Return value

nothing Attaches the resulting playlist data to $options['othervars']['playlist_data']

1 call to swftools_prepare_playlist()
swf in ./swftools.module
Processes a file, or an array of files, and returns the relevant mark-up to render a Flash based player.

File

./swftools.module, line 794
The primary component of SWF Tools that enables comprehensive media handling.

Code

function swftools_prepare_playlist($files, &$options) {

  // Make sure stream and action are present on options
  $options['methods'] += array(
    'player' => '',
  );

  // Initialise two flags for establishing the action to be taken on this playlist
  $action = '';
  $mixed_media = FALSE;

  // Initialise playlist data array
  $playlist_data = array(
    'header' => array(),
    'playlist' => array(),
  );

  // Iterate over the array of files to set filepath, fileurl and title keys
  foreach ($files as $key => $data) {

    // If $data is an object convert it to an array
    if (is_object($data)) {
      $data = (array) $data;
    }
    elseif (!is_array($data)) {

      // Create an array with key filepath set to the $data string
      $data = array(
        'filepath' => $data,
      );
    }

    // Attach the incoming element to the playlist
    $playlist_data['playlist'][$key] = $data;

    /**
     * Ensure the title key is present to simplify later code as we know it will be present.
     * Note we no longer set a default title. We leave it empty since all output should
     * now be handled by a theme function, so if the user wants to make a default we can
     * let them since they will know a specific value wasn't given. We don't do it for them.
     *
     * We also initialise some other empty strings to make checks in other modules easier.
     */
    $playlist_data['playlist'][$key] += array(
      'title' => '',
      'image' => '',
      'description' => '',
      'author' => '',
      'date' => '',
      'link' => '',
      'duration' => '',
      'stream' => FALSE,
    );

    // Find out if this item is a stream by checking for rtmp and exploding it
    if (strpos($playlist_data['playlist'][$key]['filepath'], 'rtmp:') === 0) {
      $stream = explode(' ', $playlist_data['playlist'][$key]['filepath']);

      // If the filepath exploded then assume we have a valid stream
      if (count($stream) == 2) {
        list($playlist_data['playlist'][$key]['stream'], $playlist_data['playlist'][$key]['filepath']) = $stream;
        $options['othervars']['stream'] = TRUE;

        // TODO: Action checking fails as streams don't have an extension - assume mixed media
        $options['methods']['action'] = 'media_list';
      }
    }

    // If this whole playlist isn't a stream, and this item isn't a stream
    if (!is_string($options['othervars']['stream']) && !$playlist_data['playlist'][$key]['stream']) {

      // Expand $file as necessary if local or remote
      $source = swftools_get_url_and_path($playlist_data['playlist'][$key]['filepath']);

      // Store results
      $playlist_data['playlist'][$key]['filepath'] = $source['filepath'];
      $playlist_data['playlist'][$key]['fileurl'] = $source['fileurl'];
    }
    else {
      $playlist_data['playlist'][$key]['fileurl'] = $playlist_data['playlist'][$key]['filepath'];
    }

    // See if we have an image we need to expand
    if ($playlist_data['playlist'][$key]['image']) {
      $source = swftools_get_url_and_path($playlist_data['playlist'][$key]['image']);
      $playlist_data['playlist'][$key]['image'] = $source['fileurl'];
    }

    // Allow other modules to modify this playlist element (e.g. getID3)
    drupal_alter('swftools_playlist_element', $playlist_data['playlist'][$key]);

    // If the caller wants us to work out the action for them then it happens in here
    if (!$options['methods']['action']) {

      // Get the extension for this item
      $extension = strtolower(pathinfo($playlist_data['playlist'][$key]['fileurl'], PATHINFO_EXTENSION));

      // Only try to determine actions if there's an extension to work with, and we didn't already work out it's mixed
      if ($extension && !$mixed_media) {

        // Work out what we'd do with this file
        $action_for_this_file = swftools_get_action('dummy.' . $extension);

        // If this is the first file we've processed we log it
        if (!$action) {
          $action = $action_for_this_file;
        }

        // Is this action the same as the first file we saw? If not we have mixed media
        if ($action != $action_for_this_file) {
          $mixed_media = TRUE;
          $action = 'media_list';
        }
      }
    }
  }

  // If we didn't get an action (happens with streams as they have no extension) then specify an action now
  $action = $action ? $action : 'media_list';

  // Pluralize the action for multiple files if not already pluralized
  $action = substr($action, -5, 5) == '_list' ? $action : $action . '_list';

  // If the called didn't specify an action then assign it now
  $options['methods']['action'] = $options['methods']['action'] ? $options['methods']['action'] : $action;

  // Attach the resulting playlist to the array (we are working by reference)
  $options['othervars']['playlist_data'] = $playlist_data;

  // Call drupal_alter to let other modules modify the entire playlist if they want
  drupal_alter('swftools_playlist', $playlist_data);
}