You are here

public function filedepot::saveVersion in filedepot 6

File

./filedepot.class.php, line 975
filedepot.class.php Main class for the Filedepot module

Class

filedepot
@file filedepot.class.php Main class for the Filedepot module

Code

public function saveVersion($file, $validators = array()) {
  global $conf, $user;
  $nexcloud = filedepot_nexcloud();

  // Check for allowable file type.
  if (!$this
    ->checkFilter($file->name, $file->type)) {
    $message = t('The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
      '%name' => $file->name,
      '%mimetype' => $file->type,
    ));
    drupal_set_message($message, 'error');
    watchdog('filedepot', 'The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
      '%name' => $file->name,
      '%mimetype' => $file->type,
    ));
    return FALSE;
  }
  if ($file->folder > 0 and file_exists($this->tmp_storage_path) and is_writable($this->tmp_storage_path)) {

    /* Tried to use the file_save_upload but was getting a PHP error in CCK but field_file_save_upload worked
     * $nodefileObj = file_save_upload($file->tmp_name,array(), $this->tmp_storage_path);
     */
    $nodefile = field_file_save_file($file->tmp_name, array(), $this->tmp_storage_path);
    $filedepot_private_directory_path = $this->root_storage_path . $file->folder;

    // Need to trick the file API to accept the private directory or the file_move() will fail
    $conf['file_directory_path'] = $filedepot_private_directory_path;
    $dest = rtrim($filedepot_private_directory_path, '\\/') . '/' . $file->name;
    $src = $nodefile['filepath'];

    // After a successful file_move, $src will be the set to the new filename including path
    // In case of a duplicate file in the destination directory,
    // the variable $src will be updated with the resulting appended incremental number
    // Refer to the drupal file_move API
    if (file_move($src, $dest, FILE_EXISTS_RENAME)) {

      // update db with the filename and full name including directory after the successful move
      $filename = basename($src);
      db_query("UPDATE {files} SET filename = '%s', filepath = '%s' WHERE fid = %d", $filename, $src, $nodefile['fid']);
      $query = db_query("SELECT cid,fname,version,cckfid FROM {filedepot_files} WHERE fid=%d", $file->fid);
      list($cid, $fname, $curVersion, $cckfid) = array_values(db_fetch_array($query));
      $field = content_fields('field_filedepot_file', 'filedepot_folder');
      $db_info = content_database_info($field);
      db_query("UPDATE " . $db_info['table'] . " SET field_filedepot_file_fid = %d WHERE field_filedepot_file_fid = %d", $nodefile['fid'], $cckfid);
      if ($curVersion < 1) {
        $curVersion = 1;
      }
      $newVersion = $curVersion + 1;
      $sql = "INSERT INTO {filedepot_fileversions} (fid, cckfid, fname, version, notes, size, date, uid, status) " . "VALUES (%d,%d,'%s',%d,'%s',%d,%d,%d,1)";
      db_query($sql, $file->fid, $nodefile['fid'], $filename, $newVersion, $file->vernote, $file->size, time(), $user->uid);
      $sql = "UPDATE {filedepot_files} SET fname='%s',version='%s',size=%d,date=%d,cckfid=%d WHERE fid=%d";
      db_query($sql, $filename, $newVersion, $file->size, time(), $nodefile['fid'], $file->fid);

      // Update tags for this file
      if (!empty($file->tags) and $this
        ->checkPermission($file->folder, 'view', 0, FALSE)) {
        $nexcloud
          ->update_tags($file->fid, $file->tags);
      }

      // Send out email notifications of new file added to all users subscribed
      if ($_POST['notify'] == 1) {
        filedepot_sendNotification($file->fid);
      }
      return TRUE;
    }
  }
}