You are here

function filedepot_build_notification_distribution in filedepot 7

2 calls to filedepot_build_notification_distribution()
filedepotAjaxServer_broadcastAlert in ./lib-ajaxserver.php
filedepot_sendNotification in ./lib-common.php
Send out notifications to all users that have subscribed to this file or file category Will check user preferences for notification if Messenger Plugin is installed

File

./lib-common.php, line 412
lib-common.php Common library of functions for the applications

Code

function filedepot_build_notification_distribution($id, $type = 1) {
  global $user;
  $filedepot = filedepot_filedepot();
  $target_users = array();
  if ($type == FILEDEPOT_NOTIFY_NEWFILE or $type == FILEDEPOT_NOTIFY_APPROVED or $type == FILEDEPOT_BROADCAST_MESSAGE) {
    $sql = "SELECT file.cid,file.submitter,category.name FROM {filedepot_files} file, " . "{filedepot_categories} category WHERE file.cid=category.cid and file.fid=:fid";
    $query = db_query($sql, array(
      ':fid' => $id,
    ));
    list($cid, $submitter) = array_values($query
      ->fetchAssoc());
  }
  elseif ($type == FILEDEPOT_NOTIFY_ADMIN) {
    $sql = "SELECT file.cid,file.submitter FROM {filedepot_filesubmissions} file , " . "{filedepot_categories} category WHERE file.cid=category.cid and file.id=:fid";
    $query = db_query($sql, array(
      ':fid' => $id,
    ));
    list($cid, $submitter) = array_values($query
      ->fetchAssoc());
  }
  elseif ($type == FILEDEPOT_NOTIFY_REJECT) {
    $submitter = db_query("SELECT submitter FROM {filedepot_filesubmissions} WHERE id=:fid", array(
      ':fid' => $id,
    ))
      ->fetchField();
  }

  // Generate the distribution
  if ($type == FILEDEPOT_NOTIFY_NEWFILE) {
    if (variable_get('filedepot_default_notify_newfile', 0) == 1) {

      // Site default to notify all users on new files
      $query_users = db_query("SELECT uid FROM {users} WHERE uid > 0 AND status = 1");
      while ($A = $query_users
        ->fetchObject()) {
        if ($filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          $personal_exception = FALSE;
          if (db_query("SELECT uid FROM {filedepot_usersettings} WHERE uid=:uid AND notify_newfile=0", array(
            ':uid' => $A->uid,
          ))
            ->fetchField() == $A->uid) {
            $personal_setting = FALSE;

            // User preference record exists and set to not be notified
          }
          else {
            $personal_setting = TRUE;

            // Either record does not exist or user preference is to be notified
          }

          // Check if user has any notification exceptions set for this folder
          if (db_query("SELECT count(*) FROM {filedepot_notifications} WHERE cid=:cid AND uid=:uid AND cid_newfiles=0", array(
            ':cid' => $cid,
            ':uid' => $A->uid,
          ))
            ->fetchField() > 0) {
            $personal_exception = TRUE;
          }

          // Only want to notify users that don't have setting disabled or exception record
          if ($personal_setting == TRUE and $personal_exception == FALSE and $A->uid != $submitter) {
            $target_users[] = $A->uid;
          }
        }
      }
    }
    else {
      $sql = "SELECT a.uid FROM {filedepot_usersettings} a LEFT JOIN {users} b on b.uid=a.uid WHERE a.notify_newfile = 1 and b.status=1";
      $query_users = db_query($sql);
      while ($A = $query_users
        ->fetchObject()) {
        if ($filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          $personal_exception = FALSE;
          if (db_query("SELECT ignore_filechanges FROM {filedepot_notifications} WHERE fid=:fid and uid=:uid", array(
            ':fid' => $id,
            ':uid' => $A->uid,
          ))
            ->fetchField() == 1) {
            $personal_exception = TRUE;
          }

          // Only want to notify users that have notifications enabled but don't have an exception record
          if ($personal_exception === FALSE) {
            $target_users[] = $A->uid;
          }
        }
      }

      // Check the folder specific notification options as well
      $sql = "SELECT a.uid FROM {filedepot_notifications} a LEFT JOIN {users} b on b.uid=a.uid WHERE a.cid=:cid AND a.cid_newfiles = 1 and b.status=1";
      $query_users = db_query($sql, array(
        ':cid' => $cid,
      ));
      while ($A = $query_users
        ->fetchObject()) {
        if (!in_array($A->uid, $target_users) and $filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          $target_users[] = $A->uid;
        }
      }
    }
  }
  elseif ($type == FILEDEPOT_NOTIFY_APPROVED) {

    // File submission being approved by admin where $id = file id. Send only to user
    $target_users[] = $submitter;
  }
  elseif ($type == FILEDEPOT_NOTIFY_REJECT) {

    // File submission being approved by admin where $id = file id. Send only to user
    $target_users[] = $submitter;
  }
  elseif ($type == FILEDEPOT_NOTIFY_ADMIN) {
    $query_users = db_query("SELECT uid FROM {users} WHERE uid > 0 AND status = 1");
    while ($A = $query_users
      ->fetchObject()) {
      if ($filedepot
        ->checkPermission($cid, 'approval', $A->uid)) {
        $personal_exception = FALSE;
        if (db_query("SELECT uid FROM {filedepot_usersettings} WHERE uid=:uid AND notify_newfile=0", array(
          ':uid' => $A->uid,
        ))
          ->fetchField() == $A->uid) {
          $personal_setting = FALSE;

          // User preference record exists and set to not be notified
        }
        else {
          $personal_setting = TRUE;

          // Either record does not exist or user preference is to be notified
        }

        // Check if user has any notification exceptions set for this folder
        if (db_query("SELECT count(*) FROM {filedepot_notifications} WHERE cid=:cid AND uid=:uid AND cid_newfiles=0", array(
          ':cid' => $cid,
          ':uid' => $A->uid,
        ))
          ->fetchField() > 0) {
          $personal_exception = TRUE;
        }

        // Only want to notify users that don't have setting disabled or exception record
        if ($personal_setting == TRUE and $personal_exception == FALSE and $A->uid != $submitter) {
          $target_users[] = $A->uid;
        }
      }
    }
  }
  elseif ($type == FILEDEPOT_BROADCAST_MESSAGE) {

    // Nov 2014: Added check that user also has view access to the folder from where broadcast was issued from
    if (variable_get('filedepot_default_allow_broadcasts', 1) == 1) {

      // Site default set to allow broadcast enabled
      $uquery = db_query("SELECT uid FROM {users} WHERE uid > 0 AND status = 1");
      while ($A = $uquery
        ->fetchObject()) {
        if ($A->uid != $user->uid && $filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          if (db_query("SELECT allow_broadcasts FROM {filedepot_usersettings} WHERE uid=:uid", array(
            ':uid' => $A->uid,
          ))
            ->fetchField() == 0) {
            $personal_setting = FALSE;

            // Found user setting to not be notified
          }
          else {
            $personal_setting = TRUE;
          }

          // Only want to notify users that don't have setting disabled or exception record and folder view access
          if ($personal_setting == TRUE and $filedepot
            ->checkPermission($cid, 'view', $A->uid)) {
            $target_users[] = $A->uid;
          }
        }
      }
    }
    else {
      $sql = "SELECT a.uid FROM {filedepot_usersettings} a LEFT JOIN {users} b on b.uid=a.uid " . "WHERE a.allow_broadcasts=1 and b.status=1";
      $uquery = db_query($sql);
      while ($B = $uquery
        ->fetchObject()) {
        if ($user->uid != $B->uid && $filedepot
          ->checkPermission($cid, 'view', $B->uid)) {
          $target_users[] = $B->uid;
        }
      }
    }
  }
  if (count($target_users) > 0) {

    // Sort the array so that we can check for duplicate user notification records
    sort($target_users);
    reset($target_users);
  }
  return $target_users;
}