You are here

function swftools_prepare_playlist_data in SWF Tools 6.2

Same name and namespace in other branches
  1. 5 swftools.module \swftools_prepare_playlist_data()
  2. 6 swftools.module \swftools_prepare_playlist_data()

Take an array of filenames and prepare them to be used as a playlist

Parameters

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

$title: Optional playlist title

$get_action: Optional parameter indicating if the function should determine an appropriate action. Default is TRUE.

$type_filter: Optional parameter - an array of file extensions that are permitted

$stream: Option parameter to indicate if this is going to be a streamed playlist, in which case checks for the existence of files should be skipped

Return value

unknown_type

2 calls to swftools_prepare_playlist_data()
swf in ./swftools.module
Return output, which might be embed markup, or pre-flash markup that includes the appropriate jQuery added to the <head>
_swftools_filter_process_text in ./swftools.module

File

./swftools.module, line 1021

Code

function swftools_prepare_playlist_data($files, $title = '', $get_action = TRUE, $type_filter = array(), $stream = FALSE) {

  // Initialise an array to return the results in
  $playlist_data = array();
  $playlist_data['header']['title'] = $title;

  // Run through all $files and and make the data look the same.
  $id = 0;
  foreach ($files as $key => $data) {
    while (array_key_exists($id, $files)) {
      $id++;
    }
    if (is_object($data)) {
      $files[$key] = (array) $data;
    }
    elseif (!is_array($data)) {

      // Move this file name to a new key to give it the structure of a file attachment array
      $files[$id]['filepath'] = $data;
      if (!is_numeric($key)) {
        $files[$id]['filename'] = $key;
      }
      else {
        $files[$id]['filename'] = $data;
      }
      unset($files[$key]);
    }
  }

  // Check the fileurl element and add generate it if it's missing.
  $playlist_data['playlist'] = $files;
  foreach ($files as $key => $data) {
    if (!isset($data['fileurl'])) {
      if (valid_url($data['filepath'], TRUE)) {

        // A full http:// file path has been passed.
        $playlist_data['playlist'][$key]['filepath'] = FALSE;

        // Flag that we don't know the server path.
        $playlist_data['playlist'][$key]['fileurl'] = $data['filepath'];
      }
      elseif (isset($data['fid'])) {

        // This is a classes upload module files array.
        $playlist_data['playlist'][$key]['filename'] = $data['filename'];
        $playlist_data['playlist'][$key]['fileurl'] = swftools_get_media_url($playlist_data['playlist'][$key]['filepath'], FALSE);
      }
      else {

        // Otherwise just complete url path.
        $playlist_data['playlist'][$key]['filename'] = $data['filename'];
        if (!$stream) {

          //          This code was building the wrong path when used with CCK filefield, so it may have been
          //          causing problems in other circumstances when partial paths were sent to a playlist
          //          $playlist_data['playlist'][$key]['filepath'] = swftools_get_media_path() . $data['filepath'];
          //          $playlist_data['playlist'][$key]['fileurl'] = swftools_strip_base_root(swftools_get_media_url($playlist_data['playlist'][$key]['filepath']));
          //          The code below is taken from the main swf() function, and uses file_create_path first
          // Then check if files are being sourced locally, and if they are build a file path
          if (swftools_get_media_path()) {
            $file = file_create_path($data['filepath']);
          }
          else {
            $file = $data['filepath'];
          }

          // Build a filepath and url
          $playlist_data['playlist'][$key]['filepath'] = $file;
          $playlist_data['playlist'][$key]['fileurl'] = swftools_strip_base_root(swftools_get_media_url($file));
        }
        else {
          $playlist_data['playlist'][$key]['filepath'] = $data['filepath'];
          $playlist_data['playlist'][$key]['fileurl'] = $data['filepath'];
        }
      }
    }
    if (!isset($data['filename'])) {
      $path_parts = pathinfo($playlist_data['playlist'][$key]['fileurl']);
      $playlist_data['playlist'][$key]['filename'] = $path_parts['basename'];
    }
    if (!isset($data['title'])) {
      $playlist_data['playlist'][$key]['title'] = $playlist_data['playlist'][$key]['filename'];
    }

    // Here is where you might call audio.module or video.module for more.
  }

  // Note, we want to exit quickly if the caller did not want us to
  // dynamically determine the display action by passing $get_action = FALSE.
  if (!$get_action) {

    // The caller knows what swftools action to set, so exit here.
    return $playlist_data;
  }

  // Try to work out the action from the files passed.
  $first_valid_file_type = FALSE;
  $mixed_media = FALSE;

  // Process the files attached to the node to determine the correct action.
  foreach ($playlist_data['playlist'] as $key => $data) {

    // fileurl is always set, irrespective of what we are passing, so use this to determine extension
    $path_parts = pathinfo($data['fileurl']);

    // Get the extension for the file
    $extension = strtolower($path_parts['extension']);

    // Only try to determine actions if there's an extension to work with
    if ($extension) {

      // Determine if this is an image type
      if (strpos('|jpg|jpeg|gif|png|', $extension)) {

        // Treat all types of images as the same file type
        $extension = 'img';
      }

      // Only process the file if $type_filter is empty (ie. no filter)
      // or if the extension is declared in the $type_filter array.
      if (!count($type_filter) || in_array($extension, $type_filter)) {

        // $first_valid_file_type stores the file type of the first valid file.
        // This will be compared to subsequent files and if two files
        // have different types, the action will be defined as SWFTOOLS_MEDIA_DISPLAY_LIST
        // in order to utilize a flexible media player.
        if (!$first_valid_file_type) {
          $first_valid_file_type = $extension;
        }
        else {
          if ($first_valid_file_type != $extension) {
            $mixed_media = TRUE;
          }
        }
      }
      else {

        // This file is not desired so remove it
        unset($playlist_data['playlist'][$key]);
      }
    }
  }

  // Make a decision based on analysing the file array.
  if ($first_valid_file_type == '') {

    // No files passed our test.
    return FALSE;
  }

  // Determine the required action.
  if ($mixed_media) {

    // We have files of multiple types.
    $action = SWFTOOLS_MEDIA_DISPLAY_LIST;
  }
  else {

    // We only had one file type, so discover the action for that type by
    // calling swftools_get_action() with a dummy filename
    $action = swftools_get_action('dummy.' . $first_valid_file_type);
  }

  // Pluralize the action for multiple files if not already pluralized
  if (count($playlist_data['playlist']) > 1 && substr($action, -5, 5) != '_list') {
    $action = $action . '_list';
  }

  // Assign the action to the playlist data ready for return
  $playlist_data['action'] = $action;

  // Return the resulting playlist data
  return $playlist_data;
}