You are here

function webfm_dbinsert_file in Web File Manager 5

Same name and namespace in other branches
  1. 5.2 webfm.module \webfm_dbinsert_file()

webfm_dbinsert_file - inserts a file object into the webfm_file table

Parameters

object $file - complete file object:

string &$error - reference to error string:

array $metadata - an array of key => value pairs, where key matches a field in the webfm_file table:

Return value

bool - TRUE if query executed successfully, otherwise FALSE

4 calls to webfm_dbinsert_file()
webfm_insert_file in ./webfm_file.inc
webfm_insert_file - inserts a file into the webfm_file table
webfm_upload in ./webfm.module
Called by upload form submit
webfm_upload_new in ./webfm.module
webfm_version_upload in ./webfm.module
Version uploaded file that overwrites existing file NOTE: uploaded file is already stored in $_SESSION['temp_upload'] via iframe. Feedback in ajax since drupal_set_message points back into main page (not refreshed).

File

./webfm.module, line 2561

Code

function webfm_dbinsert_file($file, &$error, $metadata = array()) {

  //we need our user
  global $user;
  if (!isset($file->filepath)) {
    $error = "No file path supplied";
    return FALSE;
  }
  else {
    $metadata['fpath'] = $file->filepath;
  }

  // always a new record
  unset($metadata['fid']);

  // fill $metadata values not supplied in 3rd arg
  if (!isset($metadata['uid'])) {
    $metadata['uid'] = $user->uid;
  }
  if (!isset($metadata['fsize'])) {
    $metadata['fsize'] = @filesize($file->filepath);
  }
  if (!isset($metadata['fcreatedate'])) {
    $metadata['fcreatedate'] = @filemtime($file->filepath);
  }
  if (!isset($metadata['perm'])) {
    $metadata['perm'] = webfm_default_file_perm();
  }
  if (!isset($metadata['fmime'])) {
    $metadata['fmime'] = webfm_mime_type($file->filepath);
  }
  if (!isset($metadata['fdesc'])) {
    $metadata['fdesc'] = '';
  }

  //create a string of fields for the query
  $fields = implode(', ', array_keys($metadata));

  //build printf style list of values
  foreach ($metadata as $key => $value) {
    if (is_numeric($value)) {
      $printfvalues[] = '%d';
    }
    else {
      $printfvalues[] = "'%s'";
    }
  }

  //create a srting of printf style values
  $printfvalues = implode(', ', $printfvalues);

  //create an array of just the values for the db_query
  $values = array_values($metadata);

  //make a db_query friendly query with prinf stuff
  $query = "INSERT INTO {webfm_file} ({$fields}) VALUES ({$printfvalues})";
  $result = db_query($query, $values);
  if ($result === FALSE) {
    $error = $file->filepath . ' could not be inserted into db';
    drupal_set_message(t('Could not insert %file into the database', array(
      '%file' => $file->filepath,
    )), error);
    return FALSE;
  }
  else {

    // return fid to calling routine
    $query = "SELECT fid FROM {webfm_file} WHERE fpath = '%s'";
    $result = db_query($query, $file->filepath);
    if ($result !== FALSE && ($row = db_fetch_object($result))) {
      return $row->fid;
    }
    else {
      return FALSE;
    }
  }
}