You are here

public function filedepot::moveFile in filedepot 7

Same name and namespace in other branches
  1. 6 filedepot.class.php \filedepot::moveFile()

File

./filedepot.class.php, line 1238
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, &$out_oldcid = NULL) {
  global $user;
  $filemoved = FALSE;
  if ($newcid > 0) {
    $query = db_query("SELECT fname,cid,drupal_fid,version,submitter FROM {filedepot_files} WHERE fid=:fid", array(
      'fid' => $fid,
    ));
    list($fname, $orginalCid, $dfid, $curVersion, $submitter) = array_values($query
      ->fetchAssoc());
    if ($out_oldcid !== NULL) {
      $out_oldcid = $orginalCid;
    }
    if ($submitter == $user->uid or $this
      ->checkPermission($newcid, 'admin')) {
      if ($newcid !== intval($orginalCid)) {

        /* Need to move the file */
        $query2 = db_query("SELECT fname, drupal_fid, version FROM {filedepot_fileversions} WHERE fid=:fid", array(
          'fid' => $fid,
        ));
        while ($A = $query2
          ->fetchAssoc()) {
          $fname = stripslashes($A['fname']);
          $sourcefile = $this->root_storage_path . "{$orginalCid}/{$fname}";
          $private_destination = "private://filedepot/{$newcid}/";

          // Best to call file_prepare_directory() - even if you believe directory exists
          file_prepare_directory($private_destination, FILE_CREATE_DIRECTORY);
          $file = file_load($A['drupal_fid']);
          $private_uri = $private_destination . $fname;
          $file = file_move($file, $private_uri, FILE_EXISTS_RENAME);
          $file->display = 1;
          list($scheme, $target) = explode('://', $file->uri, 2);
          $moved_filename = str_replace("filedepot/{$newcid}/", '', $target);
          if ($moved_filename != $fname) {
            db_update('filedepot_fileversions')
              ->fields(array(
              'fname' => $moved_filename,
            ))
              ->condition('fid', $fid)
              ->condition('version', $A['version'])
              ->execute();
          }

          // Remove the attached file from the original folder
          $source_folder_nid = db_query("SELECT nid FROM {filedepot_categories} WHERE cid=:cid", array(
            ':cid' => $orginalCid,
          ))
            ->fetchField();
          $node = node_load($source_folder_nid);

          // Remove the moved file now from the source folder
          foreach ($node->filedepot_folder_file[LANGUAGE_NONE] as $delta => $attachment) {
            if ($attachment['fid'] == $file->fid) {
              unset($node->filedepot_folder_file[LANGUAGE_NONE][$delta]);
              node_save($node);
              break;
            }
          }

          // Add the moved file to the target folder
          // Doing node_save changes the file status to permanent in the file_managed table
          $target_folder_nid = db_query("SELECT nid FROM {filedepot_categories} WHERE cid=:cid", array(
            ':cid' => $newcid,
          ))
            ->fetchField();
          $node = node_load($target_folder_nid);
          $node->filedepot_folder_file[LANGUAGE_NONE][] = (array) $file;

          //the name of the field that requires the files
          node_save($node);

          // Need to clear the cache as the node will still have the original file name
          field_cache_clear();
          db_update('filedepot_files')
            ->fields(array(
            'cid' => $newcid,
          ))
            ->condition('fid', $fid)
            ->execute();
        }
        $filemoved = 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;
}