You are here

function jplayer_sort_files in jPlayer 7.2

Filters an array of files into groups ready for jPlayer.

Parameters

array $raw_files: An array of files to be included.

int $player_id: A unique id for the player.

string $type: Either 'single' or 'playlist'.

string $item_type: The field type for the files.

Return value

array a settings array of data being passed to jPlayer instance

2 calls to jplayer_sort_files()
template_preprocess_jplayer in includes/jplayer.theme.inc
Preprocess function for a player.
template_preprocess_jplayer_view_playlist in includes/jplayer.theme.inc
Preprocess function for jplayer.tpl.php when displaying a view as a playlist.

File

includes/jplayer.theme.inc, line 130
Theme and preprocess functions for the jPlayer module.

Code

function jplayer_sort_files($raw_files = array(), $player_id = 0, $type = 'single', $item_type = 'file') {
  $video_extensions = array(
    'm4v',
    'mp4',
    'ogv',
    'webmv',
  );
  $audio_extensions = array(
    'mp3',
    'm4a',
    'oga',
    'webma',
    'wav',
  );
  $poster_extensions = array(
    'jpg',
    'jpeg',
    'png',
    'gif',
  );
  $poster = NULL;
  $videos = FALSE;
  $audio = FALSE;
  $media = array();
  $files = array();
  $extensions = array();
  $playlist = array();

  // Look through all the files provided and see what we have.
  foreach ($raw_files as $delta => $item) {

    // Get file URL.
    if (!isset($item['url'])) {
      switch ($item_type) {
        case 'file':
          $item['url'] = file_create_url($item['uri']);
          break;
        case 'text':
          $item['url'] = $item['safe_value'];
          break;
      }
    }

    // Get file extension from pathinfo or link_field title-attribute
    if (!isset($item['ext'])) {
      $fileinfo = pathinfo($item['url']);

      // If the path does not have an extension, no extension element will be
      // returned (see http://php.net/manual/en/function.pathinfo.php)
      if (isset($item['attributes']['title']) && !isset($fileinfo['extension'])) {
        $item['ext'] = $item['attributes']['title'];
      }
      elseif (isset($item['attributes']['title']) && !in_array($fileinfo['extension'], $audio_extensions) && !in_array($fileinfo['extension'], $video_extensions) && !in_array($fileinfo['extension'], $poster_extensions)) {
        $item['ext'] = $item['attributes']['title'];
      }
      else {
        $item['ext'] = $fileinfo['extension'];
      }
    }

    // Get file label.
    if (!isset($item['label'])) {
      if (empty($item['description'])) {
        switch ($item_type) {
          case 'file':
            $item['label'] = $item['filename'];
            break;
          case 'text':
            $fileinfo = pathinfo($item['url']);
            $item['label'] = urldecode($fileinfo['basename']);
            break;
          case 'link_field':
            $item['label'] = $item['title'];
            break;
        }
      }
      else {
        $item['label'] = $item['description'];
      }
    }

    // Add file into correct group.
    if (!isset($item['type'])) {
      $ext = strtolower($item['ext']);
      if (in_array($ext, $video_extensions)) {
        $videos = TRUE;
        $item['type'] = 'video';
      }
      elseif (in_array($ext, $audio_extensions)) {
        $audio = TRUE;
        $item['type'] = 'audio';
      }
      elseif (in_array($ext, $poster_extensions)) {
        $poster = $item['url'];
        $item['type'] = 'poster';
      }
    }
    $media[] = $item;
  }
  $num = 0;
  $player_type = '';
  foreach ($media as $file) {
    $player_type = 'audio';
    if ($videos == TRUE && $type != 'playlist') {
      if ($file['type'] == 'audio') {
        unset($file);
      }
      $player_type = 'video jp-video-360p';
    }
    elseif ($videos == TRUE && $type == 'playlist') {
      $player_type = 'video jp-video-360p';
    }
    $options = array(
      'fragment' => '',
      'attributes' => array(
        'id' => $player_id . '_item_' . $num,
        'tabindex' => 1,
        'onclick' => 'return(false);',
      ),
    );
    if (isset($file) && $file['type'] == 'audio') {
      $files[][$file['ext']] = $file['url'];
      $extensions[] = $file['ext'];
      $playlist[] = l($file['label'], $file['url'], $options);
      $num++;
    }
    elseif (isset($file) && $file['type'] == 'video') {
      $files[][$file['ext']] = $file['url'];
      if ($poster != NULL) {
        $files[]['poster'] = $poster;
      }
      $extensions[] = $file['ext'];
      $playlist[] = l($file['label'], $file['url'], $options);
      $num++;
    }
  }
  if ($type == 'single') {
    $item = array_shift($playlist);
    $playlist = array();
    $playlist[] = $item;
  }
  return array(
    'files' => $files,
    'extensions' => implode(',', $extensions),
    'playlist' => $playlist,
    'type' => $player_type,
  );
}