You are here

function _sort_fids in Ubercart 5

Take a list of file ids and sorts the list to where directories are list last and by order of descending depth

@return: The sorted array of file ids

Parameters

$fids: The array of file ids

1 call to _sort_fids()
_file_table_action in uc_file/uc_file.module
Perform a specified action on the uc_files table

File

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

Code

function _sort_fids($fids) {
  $dir_fids = array();
  $output = array();
  foreach ($fids as $fid) {
    $filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $fid));
    if (substr($filename, -1) == '/') {
      $dir_fids[$fid] = $filename;
    }
    else {
      $output[] = $fid;
    }
  }
  while (!empty($dir_fids)) {
    $highest = 0;
    foreach ($dir_fids as $dir_fid => $filename) {
      if (substr_count($filename, '/') > $highest) {
        $highest = substr_count($filename, '/');
        $highest_fid = $dir_fid;
      }
    }
    $output[] = $highest_fid;
    unset($dir_fids[$highest_fid]);
  }
  return $output;
}