You are here

function filedepotAjaxServer_updateFileSubscription in filedepot 7

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

File

./lib-ajaxserver.php, line 1384
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_query("SELECT count(fid) FROM {filedepot_files} WHERE fid=:fid", array(
    ':fid' => $fid,
  ))
    ->fetchField() == 1) {

    // Valid file and user
    $cid = db_query("SELECT cid FROM {filedepot_files} WHERE fid=:fid", array(
      ':fid' => $fid,
    ))
      ->fetchField();

    // 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=:fid and uid=:uid", array(
      ':fid' => $fid,
      ':uid' => $uid,
    ));
    if ($A = $query
      ->fetchAssoc()) {
      if ($A['ignore_filechanges'] == 1) {
        $ignorefilechanges = TRUE;
      }
      else {
        $direct = TRUE;
      }
    }
    $indirect = db_query("SELECT cid_changes FROM {filedepot_notifications} WHERE cid=:cid AND uid=:uid", array(
      ':cid' => $cid,
      ':uid' => $uid,
    ))
      ->fetchField();
    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=:fid AND uid=:uid", array(
          ':fid' => $fid,
          ':uid' => $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=:fid AND uid=:uid", array(
            ':fid' => $fid,
            ':uid' => $uid,
          ));
        }
        elseif ($indirect > 0) {
          db_query("INSERT INTO {filedepot_notifications} (fid,ignore_filechanges,uid,date) VALUES (:fid,1,:uid,:time)", array(
            ':fid' => $fid,
            ':uid' => $uid,
            ':time' => 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=:fid AND uid=:uid", array(
            ':fid' => $fid,
            ':uid' => $uid,
          ));
        }
        elseif (!$direct and !$indirect) {
          db_query("INSERT INTO {filedepot_notifications} (fid,cid,uid,date) VALUES (:fid,:cid,:uid,:time)", array(
            ':fid' => $fid,
            ':cid' => $cid,
            ':uid' => $uid,
            ':time' => time(),
          ));
        }
      }
    }
    $retval['retcode'] = TRUE;
  }
  else {
    $retval['retcode'] = FALSE;
  }
  return $retval;
}