You are here

public function filedepot::moveIncomingFile in filedepot 6

File

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

Class

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

Code

public function moveIncomingFile($id, $newcid) {
  global $user;
  $filemoved = FALSE;
  $nid = db_result(db_query("SELECT nid FROM {filedepot_categories} WHERE cid=%d", $newcid));
  if ($newcid > 0 and $nid > 0) {
    $sql = "SELECT a.orig_filename,a.queue_filename,a.timestamp,a.uid,a.cckfid,a.size,a.mimetype,a.description,a.version_note,b.filename,b.filepath " . "FROM {filedepot_import_queue} a  LEFT JOIN {files} b on b.fid=a.cckfid WHERE id=%d";
    $query = db_query($sql, $id);
    $file = db_fetch_object($query);
    $sourcefile = $this->tmp_incoming_path . $file->filename;
    $targetfile = $this->root_storage_path . "{$newcid}/{$file->orig_filename}";
    if (!empty($file->queue_filename) and !empty($file->orig_filename) and file_exists($sourcefile)) {
      if ($submitter == $user->uid or $this
        ->checkPermission($newcid, 'admin')) {

        // Need to populate the CCK fields for the filefield field - so node_save will update the CCK Data and HOOK_nodeapi will move the file
        $nodefile['filepath'] = $file->filepath;
        $nodefile['fid'] = (int) $file->cckfid;
        $nodefile['status'] = 1;
        $nodefile['list'] = 1;
        $nodefile['data'] = serialize(array(
          'description' => $file->description,
        ));
        $nodefile['realname'] = $file->orig_filename;
        $nodefile['moderated'] = FALSE;
        $nodefile['incoming'] = TRUE;
        $node = node_load(array(
          'nid' => $nid,
        ));
        $content_type = content_types($node->type);
        $nodefileObj = new stdClass();
        $nodefileObj->fid = $file->cckfid;

        // file_set_status API expects an object but just needs fid
        file_set_status($nodefileObj, 1);
        $node->field_filedepot_file[] = $nodefile;
        node_save($node);

        // After file has been saved and moved to the private filedepot folder via the HOOK_node_api function
        // Check and see what the final filename and use that to update the filedepot tables
        $rec = db_fetch_object(db_query("SELECT filename,filepath from {files} WHERE fid=%d", $file->cckfid));
        $file->filename = $rec->filename;
        $dest = $rec->filepath;
        $ext = end(explode(".", $file->name));

        // Create filedepot record for file and set status of file to 1 - online
        $sql = "INSERT INTO {filedepot_files} (cid,fname,title,description,version,cckfid,size,mimetype,extension,submitter,status,date) " . "VALUES (%d,'%s','%s','%s',1,%d,%d,'%s','%s',%d,1,%d)";
        db_query($sql, $newcid, $file->filename, $file->orig_filename, $file->description, $file->cckfid, $file->size, $file->mimetype, $ext, $user->uid, time());

        // Get fileid for the new file record
        $args = array(
          $newcid,
          $user->uid,
        );
        $fid = db_result(db_query("SELECT fid FROM {filedepot_files} WHERE cid=%d AND submitter=%d ORDER BY fid DESC", $args, 0, 1));
        db_query("INSERT INTO {filedepot_fileversions} (fid,cckfid,fname,version,notes,size,date,uid,status)\n          VALUES (%d,%d,'%s','1','%s',%d,%d,%d,1)", $fid, $file->cckfid, $file->filename, $file->version_note, $file->size, time(), $user->uid);
        db_query("DELETE FROM {filedepot_import_queue} WHERE id = %d", $id);

        // Update related folders last_modified_date
        $workspaceParentFolder = filedepot_getTopLevelParent($newcid);
        filedepot_updateFolderLastModified($workspaceParentFolder);
        content_clear_type_cache();
        $filemoved = TRUE;
      }
      else {
        watchdog('filedepot', 'User @user does not have access to move file(@fid): @fname to category: @newcid', array(
          '@user' => $user->name,
          '@fid' => $fid,
          '@fname' => $fname,
          '@newcid' => $newcid,
        ));
      }
    }
    else {
      $GLOBALS['filedepot_errmsg'] = "Error moving file - source file {$gname} missing";
      watchdog('filedepot', 'Filedepot: @errmsg', array(
        '@errmsg' => $GLOBALS['filedepot_errmsg'],
      ));
    }
  }
  else {
    $GLOBALS['filedepot_errmsg'] = "Invalid Destination Folder";
    watchdog('filedepot', 'Filedepot: @errmsg', array(
      '@errmsg' => $GLOBALS['filedepot_errmsg'],
    ));
  }
  return $filemoved;
}