You are here

function filefield_source_reference_get_files in FileField Sources 7

Same name and namespace in other branches
  1. 6 sources/reference.inc \filefield_source_reference_get_files()

Get all the files used within a particular field (or all fields).

Parameters

$file_name: The partial name of the file to retrieve.

$instance: Optional. A CCK field array for which to filter returned files.

1 call to filefield_source_reference_get_files()
filefield_source_reference_autocomplete in sources/reference.inc
Menu callback; autocomplete.js callback to return a list of files.

File

sources/reference.inc, line 225
A FileField extension to allow referencing of existing files.

Code

function filefield_source_reference_get_files($filename, $instance = NULL) {
  if (isset($instance)) {

    // If we are looking at a single field, cache its settings, in case we want to search all fields.
    $setting_autocomplete = empty($instance['widget']['settings']['filefield_sources']['source_reference']['autocomplete']) ? 0 : 1;
    $setting_search_all_fields = empty($instance['widget']['settings']['filefield_sources']['source_reference']['search_all_fields']) ? 0 : 1;
  }
  $instances = array();
  if (!isset($instance) || $setting_search_all_fields) {
    foreach (field_info_fields() as $instance) {
      if ($instance['type'] == 'file' || $instance['type'] == 'image') {
        $instances[] = $instance;
      }
    }
  }
  else {
    $instances = array(
      $instance,
    );
  }
  $files = array();
  foreach ($instances as $instance) {

    // Load the field data, which contains the schema information.
    $field = field_info_field($instance['field_name']);

    // We don't support fields that are not stored with SQL.
    if (!isset($field['storage']['details']['sql']['FIELD_LOAD_CURRENT'])) {
      continue;
    }

    // If we are searching all fields, use the autocomplete settings from the source field.
    if (!empty($setting_search_all_fields)) {
      $instance['widget']['settings']['filefield_sources']['source_reference']['autocomplete'] = empty($setting_autocomplete) ? 0 : 1;
    }

    // 1 == contains, 0 == starts with.
    $like = empty($instance['widget']['settings']['filefield_sources']['source_reference']['autocomplete']) ? db_like($filename) . '%' : '%' . db_like($filename) . '%';
    $table_info = reset($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
    $table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
    $query = db_select($table, 'cf');
    $query
      ->innerJoin('file_managed', 'f', 'f.fid = cf.' . $table_info['fid']);
    $query
      ->fields('f');
    $query
      ->condition('f.status', 1);
    $query
      ->condition('f.filename', $like, 'LIKE');
    $query
      ->orderBy('f.timestamp', 'DESC');
    $query
      ->groupBy('f.fid');
    $query
      ->range(0, 30);
    $query
      ->addTag('filefield_source_reference_list');
    $result = $query
      ->execute();
    foreach ($result as $file) {
      $files[$file->fid] = $file;
    }
  }
  return $files;
}