You are here

function swftools_prepare_playlist_data in SWF Tools 6

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

This function is a work in progress.

Sent through array of files (simple), or an If $action is missing then we try to work it out.

1 call to swftools_prepare_playlist_data()
_swftools_filter_process_text in ./swftools.module

File

./swftools.module, line 818

Code

function swftools_prepare_playlist_data($files, $title = '', $get_action = TRUE, $type_filter = array()) {
  $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'];
        $playlist_data['playlist'][$key]['filepath'] = swftools_get_media_path() . $data['filepath'];
        $playlist_data['playlist'][$key]['fileurl'] = swftools_get_media_url($playlist_data['playlist'][$key]['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 $action = FALSE.
  if (!$get_action) {

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

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

    // 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']);
      $extension = strtolower($path_parts['extension']);
      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 $file_types 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. squash 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 make up a pretend file name based on
      // the discovered file type and find out the default action for this file type.
      $action = swftools_get_action('dummy.' . $first_valid_file_type);
    }

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

    // Assign the action we've derived.
    $playlist_data['action'] = $action;
    return $playlist_data;
  }
}