You are here

function filedepotAjaxServer_updateFolder in filedepot 7

Same name and namespace in other branches
  1. 6 lib-ajaxserver.php \filedepotAjaxServer_updateFolder()
1 call to filedepotAjaxServer_updateFolder()
filedepot_dispatcher in ./ajaxserver.php

File

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

Code

function filedepotAjaxServer_updateFolder() {
  global $user;
  $filedepot = filedepot_filedepot();
  $cid = intval($_POST['cid']);
  $catpid = intval($_POST['catpid']);
  $folderorder = intval($_POST['folderorder']);
  $fileadded = intval($_POST['fileadded_notify']);
  $filechanged = intval($_POST['filechanged_notify']);
  $catname = check_plain($_POST['categoryname']);
  $catdesc = check_plain($_POST['catdesc']);
  $retval = array();
  if ($cid > 0 and $filedepot
    ->checkPermission($cid, 'admin')) {
    $retval['retcode'] = 200;
    $retval['cid'] = $cid;
    db_query("UPDATE {filedepot_categories} SET name=:catname, description=:desc WHERE cid=:cid", array(
      ':catname' => $catname,
      ':desc' => $catdesc,
      ':cid' => $cid,
    ));
    $nid = db_query("SELECT nid FROM {filedepot_categories} WHERE cid=:cid", array(
      ':cid' => $cid,
    ))
      ->fetchField();
    db_query("UPDATE {node} SET title=:catname WHERE nid=:nid", array(
      ':catname' => $catname,
      ':nid' => $nid,
    ));
    db_query("UPDATE {node_revision} SET title=:catname WHERE nid=:nid", array(
      ':catname' => $catname,
      ':nid' => $nid,
    ));
    if (db_query("SELECT folderorder FROM {filedepot_categories} WHERE cid=:cid", array(
      ':cid' => $cid,
    ))
      ->fetchField() != $folderorder) {
      db_query("UPDATE {filedepot_categories} SET folderorder=:folder WHERE cid=:cid", array(
        ':folder' => $folderorder,
        ':cid' => $cid,
      ));

      /* Re-order any folders that may have just been moved */
      $query = db_query("SELECT cid,folderorder from {filedepot_categories} WHERE pid=:pid ORDER BY folderorder", array(
        ':pid' => $catpid,
      ));
      $folderorder = 10;
      $stepnumber = 10;
      while ($A = $query
        ->fetchAssoc()) {
        if ($A['folderorder'] != $folderorder) {
          db_query("UPDATE {filedepot_categories} SET folderorder=:folder WHERE cid=:cid", array(
            ':folder' => $folderorder,
            ':cid' => $A['cid'],
          ));
        }
        $folderorder += $stepnumber;
      }
    }

    // Update the personal folder notifications for user
    if ($filechanged == 1 or $fileadded == 1) {
      if (db_query("SELECT count(*) FROM {filedepot_notifications} WHERE cid=:cid AND uid=:uid", array(
        ':cid' => $cid,
        ':uid' => $user->uid,
      ))
        ->fetchField() == 0) {
        $sql = "INSERT INTO {filedepot_notifications} (cid,cid_newfiles,cid_changes,uid,date) ";
        $sql .= "VALUES (:cid,:added,:changed,:uid,:time)";
        db_query($sql, array(
          ':cid' => $cid,
          ':added' => $fileadded,
          ':changed' => $filechanged,
          ':uid' => $user->uid,
          ':time' => time(),
        ));
      }
      else {
        $sql = "UPDATE {filedepot_notifications} set cid_newfiles=:added, ";
        $sql .= "cid_changes=:changed, date=:time ";
        $sql .= "WHERE uid=:uid and cid=:cid";
        db_query($sql, array(
          ':added' => $fileadded,
          ':changed' => $filechanged,
          ':time' => time(),
          ':uid' => $user->uid,
          ':cid' => $cid,
        ));
      }
    }
    else {
      db_query("DELETE FROM {filedepot_notifications} WHERE uid=:uid AND cid=:cid", array(
        ':uid' => $user->uid,
        ':cid' => $cid,
      ));
    }

    // Now test if user has requested to change the folder's parent and if they have permission to this folder
    $pid = db_query("SELECT pid FROM {filedepot_categories} WHERE cid=:cid", array(
      ':cid' => $cid,
    ))
      ->fetchField();
    if ($pid != $catpid && $catpid != $cid) {
      if ($filedepot
        ->checkPermission($catpid, 'admin') or user_access('administer filedepot')) {

        // Check if user is trying to set the folder's parent to a child folder - ERROR!
        $children = $filedepot
          ->getFolderChildren($cid);
        if (!in_array($catpid, $children)) {
          db_query("UPDATE {filedepot_categories} SET pid=:pid WHERE cid=:cid", array(
            ':pid' => $catpid,
            ':cid' => $cid,
          ));

          // Need to force a reset of user accessible folders in case folder has been moved under a parent with restricted access
          db_update('filedepot_usersettings')
            ->fields(array(
            'allowable_view_folders' => '',
          ))
            ->execute();

          // If the folder is now a top level folder - then remove it from the recent folders list as top level don't appear.
          if ($filedepot->ogmode_enabled and $catpid == $filedepot->ogrootfolder or $catpid == 0) {
            db_query("DELETE FROM {filedepot_recentfolders} WHERE cid=:cid ", array(
              ':cid' => $cid,
            ));
          }
        }
        else {
          watchdog('filedepot', "Attempt to set the parent folder for :foldername to a current child folder", array(
            ':foldername' => $catname,
          ));
          $retval['retcode'] = 500;
        }
      }
      else {
        $retval['retcode'] = 500;
      }
    }
  }
  else {
    $retval['retcode'] = 500;
  }
  return $retval;
}