You are here

function _uc_file_sort_fids in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_file/uc_file.module \_uc_file_sort_fids()
  2. 7.3 uc_file/uc_file.module \_uc_file_sort_fids()

Takes a list of file ids and sort the list in descending order.

Parameters

array $fids: The array of file ids.

Return value

array The sorted array of file ids.

1 call to _uc_file_sort_fids()
uc_file_remove_by_id in uc_file/uc_file.module
Deletes files (or directories).

File

uc_file/uc_file.module, line 649
Allows products to be associated with downloadable files.

Code

function _uc_file_sort_fids(array $fids) {
  $dir_fids = [];
  $output = [];
  foreach ($fids as $fid) {
    $file = uc_file_get_by_id($fid);
    $filename = $file->filename;

    // Store the files first.
    if (substr($filename, -1) != '/') {
      $output[] = $fid;
    }
    else {
      $dir_fids[$fid] = $filename;
    }
  }

  // Order the directories using a count of the slashes in each path name.
  while (!empty($dir_fids)) {
    $highest = 0;
    foreach ($dir_fids as $dir_fid => $filename) {

      // Find the most slashes. (Furthest down.)
      if (substr_count($filename, '/') > $highest) {
        $highest = substr_count($filename, '/');
        $highest_fid = $dir_fid;
      }
    }

    // Output the dir and remove it from candidates.
    $output[] = $highest_fid;
    unset($dir_fids[$highest_fid]);
  }
  return $output;
}