You are here

function _uc_file_get_dir_file_ids in Ubercart 8.4

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

Returns a list of file ids that are in the directory.

Parameters

int $fid: The file id associated with the directory.

bool $recursive: Whether or not to list recursive directories and their files.

Return value

array|false If there are files in the directory, returns an array of file ids. Else returns FALSE.

6 calls to _uc_file_get_dir_file_ids()
ActionForm::buildForm in uc_file/src/Form/ActionForm.php
Form constructor.
ActionForm::buildJsFileDisplay in uc_file/src/Form/ActionForm.php
Shows all possible files in selectable list.
FileDeleteForm::buildForm in uc_file/src/Form/FileDeleteForm.php
Form constructor.
FileDeleteForm::buildJsFileDisplay in uc_file/src/Form/FileDeleteForm.php
Shows all possible files in selectable list.
uc_file_remove_by_id in uc_file/uc_file.module
Deletes files (or directories).

... See full list

File

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

Code

function _uc_file_get_dir_file_ids($fids, $recursive = FALSE) {
  $result = [];
  $connection = \Drupal::database();

  // Handle an array or just a single.
  if (!is_array($fids)) {
    $fids = [
      $fids,
    ];
  }
  foreach ($fids as $fid) {

    // Get everything inside and below the given directory, or if it's file,
    // just the file. We'll handle recursion later.
    if (!($base = uc_file_get_by_id($fid))) {
      continue;
    }
    $base_name = $base->filename . (is_dir(uc_file_qualify_file($base->filename)) ? '%' : '');
    $files = $connection
      ->query('SELECT * FROM {uc_files} WHERE filename LIKE :name', [
      ':name' => $base_name,
    ]);

    // PHP str_replace() can't replace only N matches, so we use regex. First
    // we escape our file slashes, though, ... using str_replace().
    $base_name = str_replace("\\", "\\\\", $base_name);
    $base_name = str_replace("/", "\\/", $base_name);
    foreach ($files as $file) {

      // Make the file path relative to the given directory.
      $filename_change = preg_replace('/' . $base_name . '/', '', $file->filename, 1);

      // Remove any leading slash.
      $filename = substr($filename_change, 0, 1) == '/' ? substr($filename_change, 1) : $filename_change;

      // Recurring, or a file? Add it.
      if ($recursive || !strpos($filename, '/')) {
        $result[] = $file->fid;
      }
    }
  }
  return array_unique($result);
}