You are here

function filefield_get_file_reference_count in FileField 6.3

Count the number of times the file is referenced within a field.

Parameters

$file: A file object.

$field: Optional. The CCK field array or field name as a string.

Return value

An integer value.

2 calls to filefield_get_file_reference_count()
filefield_field_delete_file in ./filefield_field.inc
Check that FileField controls a file before attempting to deleting it.
filefield_file_references in ./filefield.module
Implementation of hook_file_references().

File

./filefield.module, line 1012
FileField: Defines a CCK file field type.

Code

function filefield_get_file_reference_count($file, $field = NULL) {
  $fields = filefield_get_field_list(NULL, $field);
  $file = (object) $file;
  $references = 0;
  foreach ($fields as $field) {
    $db_info = content_database_info($field);
    $references += db_result(db_query('SELECT count(' . $db_info['columns']['fid']['column'] . ')
        FROM {' . $db_info['table'] . '}
        WHERE ' . $db_info['columns']['fid']['column'] . ' = %d', $file->fid));

    // If a field_name is present in the file object, the file is being deleted
    // from this field.
    if (isset($file->field_name) && $field['field_name'] == $file->field_name) {

      // If deleting the entire node, count how many references to decrement.
      if (isset($file->delete_nid)) {
        $node_references = db_result(db_query('SELECT count(' . $db_info['columns']['fid']['column'] . ')
            FROM {' . $db_info['table'] . '}
            WHERE ' . $db_info['columns']['fid']['column'] . ' = %d AND nid = %d', $file->fid, $file->delete_nid));
        $references = $references - $node_references;
      }
      else {
        $references = $references - 1;
      }
    }
  }
  return $references;
}