You are here

function filedepotAjaxServer_broadcastAlert in filedepot 6

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

File

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

Code

function filedepotAjaxServer_broadcastAlert($fid, $comment) {
  global $user;
  $filedepot = filedepot_filedepot();
  $retval = '';
  $target_users = array();
  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 = db_fetch_object($uquery)) {
      if ($A->uid != $user->uid) {
        if (db_result(db_query("SELECT allow_broadcasts FROM {filedepot_usersettings} WHERE uid=%d", $A->uid)) == 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
        if ($personal_setting == TRUE) {
          $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 = db_fetch_object($uquery)) {
      if ($user->uid != $B->uid) {
        $target_users[] = $B->uid;
      }
    }
  }
  if (count($target_users) > 0) {

    /* Send out Notifications to all users on distribution
     * Use the Bcc feature of COM_mail (added June/2009)
     * To send to complete distribution as one email and not loop thru distribution sending individual emails
     */
    $distribution = array();
    $lastuser = 0;
    $type = FILEDEPOT_BROADCAST_MESSAGE;
    $sql = "SELECT file.title,file.cid FROM {filedepot_files} file WHERE file.fid=%d";
    $query = db_query($sql, $fid);
    $frec = db_fetch_object($query);
    foreach ($target_users as $target_uid) {

      // Check that user has view access to this folder
      if ($target_uid != $lastuser and $filedepot
        ->checkPermission($frec->cid, 'view', $target_uid)) {
        $query = db_query("SELECT name,mail FROM {users} WHERE uid=%d", $target_uid);
        $urec = db_fetch_object($query);
        if (!empty($urec->mail)) {
          $distribution[] = $urec->mail;
          $sql = "INSERT INTO {filedepot_notificationlog} (target_uid,submitter_uid,notification_type,fid,cid,datetime) " . "VALUES (%d,%d,%d,%d,%d,%d)";
          db_query($sql, $target_uid, $user->uid, $type, $fid, $frec->cid, time());
        }
        $lastuser = $target_uid;
      }
    }
    if (count($distribution) > 0) {
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('Broadcast Notification');
      $message['body'] = $comment . "\n\n";
      $link = url('filedepot', array(
        'query' => drupal_query_string_encode(array(
          'cid' => $frec->cid,
        )),
        'absolute' => true,
      ));
      $message['body'] .= t('The file: !filename can be accessed at !link', array(
        '!filename' => $frec->title,
        '!link' => $link,
      )) . "\n\n";
      $message['body'] .= t('You are receiving this broadcast alert, because your notification setting is enabled.');
      $message['to'] = 'Filedepot Distribution';
      $message['headers']['Bcc'] = implode(',', $distribution);
      drupal_mail_send($message);
      $retval['retcode'] = 200;
      $retval['count'] = count($distribution);
    }
    else {
      $retval['retcode'] = 205;
    }
  }
  else {
    $retval['retcode'] = 205;
  }
  return $retval;
}