public function filedepot::moveFile in filedepot 6
Same name and namespace in other branches
- 7 filedepot.class.php \filedepot::moveFile()
File
- ./
filedepot.class.php, line 756 - filedepot.class.php Main class for the Filedepot module
Class
- filedepot
- @file filedepot.class.php Main class for the Filedepot module
Code
public function moveFile($fid, $newcid) {
global $user;
$filemoved = FALSE;
if ($newcid > 0) {
$query = db_query("SELECT fname,cid,cckfid,version,submitter FROM {filedepot_files} WHERE fid=%d", $fid);
list($fname, $orginalCid, $cckfid, $curVersion, $submitter) = array_values(db_fetch_array($query));
if ($submitter == $user->uid or $this
->checkPermission($newcid, 'admin')) {
if ($newcid !== intval($orginalCid)) {
// Check if there is more then 1 reference to this file in this category
if (db_result(db_query("SELECT fid from {filedepot_files} WHERE cid=%d AND fname='%s'", $originalCid, $fname)) > 1) {
watchdog('filedepot', 'Checking for duplicate file - @folder, @name > Yes', array(
'@folder' => $orginalCid,
'@name' => $fname,
));
$dupfile_inuse = TRUE;
}
else {
watchdog('filedepot', 'Checking for duplicate file - @folder, @name > No', array(
'@folder' => $orginalCid,
'@name' => $fname,
));
$dupfile_inuse = FALSE;
}
/* Need to move the file */
$query2 = db_query("SELECT fname FROM {filedepot_fileversions} WHERE fid=%d", $fid);
while ($A = db_fetch_array($query2)) {
$fname = stripslashes($A['fname']);
$sourcefile = $this->root_storage_path . "{$orginalCid}/{$fname}";
if (!is_dir($sourcefile) and file_exists($sourcefile)) {
watchdog('filedepot', 'Checking if file @file exists - TRUE', array(
'@file' => $sourcefile,
));
$targetfile = $this->root_storage_path . "{$newcid}/{$fname}";
// If there is more then 1 reference to this file in this category
if ($dupfile_inuse) {
@copy($sourcefile, $targetfile);
}
else {
if (file_exists($targetfile)) {
@unlink($sourcefile);
}
else {
@rename($sourcefile, $targetfile);
}
}
// Test that file has actually been moved now
if (!is_dir($targetfile) and file_exists($targetfile)) {
$filemoved = TRUE;
db_query("UPDATE {files} SET filepath='%s' WHERE fid=%d", $targetfile, $cckfid);
}
}
else {
watchdog('filedepot', 'Checking if file @file exists - FALSE', array(
'@file' => $sourcefile,
), WATCHDOG_ERROR);
}
}
if ($filemoved) {
// At least one file moved - so now update record
db_query("UPDATE {filedepot_files} SET cid=%d WHERE fid=%d", $newcid, $fid);
/* We are moving attachments between nodes and although we have updated the filedepot records,
the native drupal cck module table still has the file linked to the original node (folder)
Trying to manually rebuild the node and do a node_save did not work
Have had to resort to manually updating the cck table
*/
$q1 = db_query("SELECT nid,vid FROM {filedepot_categories} WHERE cid=%d", $newcid);
$newrec = db_fetch_array($q1);
// Get the current file (attachment) offset for the target folder node
$q2 = db_query("SELECT delta FROM {content_field_filedepot_file} WHERE nid=%d AND vid=%d ORDER BY delta DESC LIMIT 1", $newrec['nid'], $newrec['vid']);
$delta = db_result($q2);
if ($delta !== FALSE) {
$delta++;
}
else {
$delta = 0;
}
// Retrieve the current record data -- we will need to change the nid and update the delta (offset) so it will be correct for the new folder
$q3 = db_query("SELECT * FROM {content_field_filedepot_file} WHERE field_filedepot_file_fid=%d", $cckfid);
$sfile = db_fetch_object($q3);
db_query("DELETE FROM {content_field_filedepot_file} WHERE field_filedepot_file_fid=%d", $cckfid);
// Insert new record for the moved file - for the target node
$sql = "INSERT INTO {content_field_filedepot_file} " . "(vid, nid, delta, field_filedepot_file_fid, field_filedepot_file_list, field_filedepot_file_data) " . "VALUES (%d, %d, %d, %d, 1, '%s')";
db_query($sql, $newrec['vid'], $newrec['nid'], $delta, $sfile->field_filedepot_file_fid, $sfile->field_filedepot_file_data);
}
}
else {
$filemoved = TRUE;
// No move requested but no errors or warnings so return true
}
}
else {
watchdog('filedepot', 'User (@user) does not have access to move file(@fid): @name to category: @newcid', array(
'@user' => $user->name,
'@fid' => $fid,
'@name' => $fname,
'@newcid' => $newcid,
));
}
}
return $filemoved;
}