You are here

function filedepotAjaxServer_moveCheckedFiles in filedepot 6

Same name and namespace in other branches
  1. 7 lib-ajaxserver.php \filedepotAjaxServer_moveCheckedFiles()
1 call to filedepotAjaxServer_moveCheckedFiles()
filedepot_dispatcher in ./ajaxserver.php
@file ajaxserver.php Implementation of filedepot_ajax() - main ajax handler for the module

File

./lib-ajaxserver.php, line 983
lib-ajaxserver.php Library functions for the ajax_server

Code

function filedepotAjaxServer_moveCheckedFiles() {
  global $user;
  $filedepot = filedepot_filedepot();
  $message = '';
  $retval = array();
  $cid = intval($_POST['cid']);
  $newcid = intval($_POST['newcid']);
  $reportmode = check_plain($_POST['reportmode']);
  $fileitems = check_plain($_POST['checkeditems']);
  $files = explode(',', $fileitems);
  $filedepot->cid = $cid;
  $filedepot->activeview = $reportmode;
  $duplicates = 0;
  $movedfiles = 0;
  if ($newcid > 0 and $user->uid > 0) {
    foreach ($files as $id) {
      if ($id > 0) {
        if ($reportmode == 'incoming') {
          if ($filedepot
            ->moveIncomingFile($id, $newcid)) {
            $movedfiles++;
          }
        }
        else {
          $fname = db_result(db_query("SELECT fname FROM {filedepot_files} WHERE fid=%d", $id));
          if (db_result(db_query("SELECT fid from {filedepot_files} WHERE cid=%d AND fname='%s'", $newcid, $fname)) > 0) {
            $duplicates++;
          }
          elseif ($filedepot
            ->moveFile($id, $newcid)) {
            $movedfiles++;
          }
        }
      }
    }
  }
  if ($movedfiles > 0) {
    $message = "Successfully moved {$movedfiles} files to this folder.";
    if ($reportmode == 'incoming') {

      // Send out email notifications of new file added to all users subscribed  -  Get fileid for the new file record
      $args = array(
        $newcid,
        $user->uid,
      );
      $fid = db_result(db_query_range("SELECT fid FROM {filedepot_files} WHERE cid=%d AND submitter=%d ORDER BY fid DESC", $args, 0, 1));
      filedepot_sendNotification($fid, FILEDEPOT_NOTIFY_NEWFILE);
    }
    if ($duplicates > 0) {
      if ($duplicates == 1) {
        $message .= " File could not be moved as it is a duplicate.";
      }
      else {
        $message .= " {$duplicates} files could not be moved as they are duplicates.";
      }
    }
    $cid = $newcid;
  }
  elseif ($newcid == 0) {
    $message = 'Unable to move any files - Invalid new folder selected';
  }
  elseif ($duplicates > 0) {
    if ($duplicates == 1) {
      $message = "File could not be moved as it is a duplicate.";
    }
    else {
      $message = "{$duplicates} files could not be moved as they are duplicates.";
    }
  }
  else {
    $message = 'Unable to move any files - invalid folder or insufficient rights';
  }
  $retval['retcode'] = 200;
  $retval['cid'] = $cid;
  $retval['movedfiles'] = $movedfiles;
  $retval['message'] = $message;
  $retval['activefolder'] = theme('filedepot_activefolder');
  $retval['displayhtml'] = filedepot_displayFolderListing($cid);
  return $retval;
}