You are here

function _uc_file_sort_fids in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 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

$fids: The array of file ids.

Return value

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 1513

Code

function _uc_file_sort_fids($fids) {
  $dir_fids = array();
  $output = array();
  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;
}