You are here

function filedepotAjaxServer_updateFileSubscription in filedepot 6

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

File

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

Code

function filedepotAjaxServer_updateFileSubscription($fid, $op = 'toggle') {
  global $user;
  $retval = array(
    'retcode' => '',
    'subscribed' => '',
  );
  if ($user->uid > 0) {
    $uid = $user->uid;
  }
  else {
    $retval['retcode'] = FALSE;
    return $retval;
  }
  if (db_result(db_query("SELECT count(fid) FROM {filedepot_files} WHERE fid=%d", $fid)) == 1) {

    // Valid file and user
    $cid = db_result(db_query("SELECT cid FROM {filedepot_files} WHERE fid=%d", $fid));

    // Check if user has an ignore file changes record or a subscribe to changes record for this file
    $direct = FALSE;
    $ignorefilechanges = FALSE;
    $query = db_query("SELECT fid,ignore_filechanges FROM {filedepot_notifications} WHERE fid=%d and uid=%d", $fid, $uid);
    if ($A = db_fetch_array($query)) {
      if ($A['ignore_filechanges'] == 1) {
        $ignorefilechanges = TRUE;
      }
      else {
        $direct = TRUE;
      }
    }
    $indirect = db_result(db_query("SELECT cid_changes FROM {filedepot_notifications} WHERE cid=%d AND uid=%d", $cid, $uid));
    if ($indirect and $direct) {

      // User may have subscribed to single file and the folder option was also set
      if ($op == 'toggle' or $op == 'remove') {
        db_query("UPDATE {filedepot_notifications} set ignore_filechanges = 1 WHERE fid=%d AND uid=%d", $fid, $uid);
        $retval['subscribed'] = FALSE;
      }
    }
    elseif (($direct or $indirect) and !$ignorefilechanges) {

      // User is subscribed - so un-subscribe
      if ($op == 'toggle' or $op == 'remove') {
        $retval['subscribed'] = FALSE;
        if ($direct > 0) {
          db_query("DELETE FROM {filedepot_notifications} WHERE fid=%d AND uid=%d", $fid, $uid);
        }
        elseif ($indirect > 0) {
          db_query("INSERT INTO {filedepot_notifications} (fid,ignore_filechanges,uid,date) VALUES (%d,1,%d,%d)", $fid, $uid, time());
        }
      }
    }
    else {

      // User is not subscribed
      if ($op == 'toggle' or $op == 'add') {
        $retval['subscribed'] = TRUE;
        if ($ignorefilechanges) {

          //delete the exception record
          db_query("UPDATE {filedepot_notifications} set ignore_filechanges = 0 WHERE fid=%d AND uid=%d", $fid, $uid);
        }
        elseif (!$direct and !$indirect) {
          db_query("INSERT INTO {filedepot_notifications} (fid,cid,uid,date) VALUES (%d,%d,%d,%d)", $fid, $cid, $uid, time());
        }
      }
    }
    $retval['retcode'] = TRUE;
  }
  else {
    $retval['retcode'] = FALSE;
  }
  return $retval;
}