You are here

function filedepot_sendNotification in filedepot 6

Same name and namespace in other branches
  1. 7 lib-common.php \filedepot_sendNotification()

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

Parameters

string $id Key used to retrieve details depending on message type:

string $type Message type ->: (1) FILEDEPOT_NOTIFY_NEWFILE, (2) FILEDEPOT_NOTIFY_APPROVED, (3) FILEDEPOT_NOTIFY_REJECT, (4) FILEDEPOT_NOTIFY_ADMIN

Return value

Boolean Returns TRUE if atleast 1 message was sent out

6 calls to filedepot_sendNotification()
filedepot::approveFileSubmission in ./filedepot.class.php
filedepot::deleteSubmission in ./filedepot.class.php
filedepot::saveFile in ./filedepot.class.php
filedepot::saveVersion in ./filedepot.class.php
filedepotAjaxServer_moveCheckedFiles in ./lib-ajaxserver.php

... See full list

File

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

Code

function filedepot_sendNotification($id, $type = 1) {
  global $user;
  $filedepot = filedepot_filedepot();

  /* If notifications have been disabled via the module admin settings - return TRUE */
  if (variable_get('filedepot_notifications_enabled', 1) == 0) {
    return TRUE;
  }
  $target_users = array();
  $message = array();
  $message['headers'] = array(
    'From' => variable_get('site_mail', ''),
  );
  $messagetext = '';
  $messagetext2ary = array();
  switch ($type) {
    case FILEDEPOT_NOTIFY_NEWFILE:

      // New File added where $id = file id. Send to all subscribed users
      $sql = "SELECT file.fid,file.fname,file.cid,file.submitter,category.name FROM " . "{filedepot_files} file, {filedepot_categories} category " . "WHERE file.cid=category.cid and file.fid=%d";
      $query = DB_query($sql, $id);
      list($fid, $fname, $cid, $submitter, $catname) = array_values(db_fetch_array($query));
      $link = url('filedepot', array(
        'query' => drupal_query_string_encode(array(
          'cid' => $cid,
          'fid' => $fid,
        )),
        'absolute' => true,
      ));
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New Document Management Update');
      $messagetext2ary = array(
        '!file' => $fname,
        '!bp' => '<p>',
        '!ep' => '</p>',
        '!folder' => $catname,
        '!link' => url($link, array(
          'absolute' => TRUE,
        )),
      );
      break;
    case FILEDEPOT_NOTIFY_APPROVED:

      // File submission being approved by admin where $id = file id. Send only to user
      $sql = "SELECT file.fid,file.fname,file.cid,file.submitter,category.name FROM {filedepot_files} file, " . "{filedepot_categories} category WHERE file.cid=category.cid and file.fid=%d";
      $query = db_query($sql, $id);
      list($fid, $fname, $cid, $submitter, $catname) = array_values(db_fetch_array($query));

      // Just need to create this SQL record for this user - to fake out logic below
      $target_users[] = $submitter;
      $link = url('filedepot', array(
        'query' => drupal_query_string_encode(array(
          'cid' => $cid,
          'fid' => $fid,
        )),
        'absolute' => true,
      ));
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission Approved');
      $messagetext = t('Site member %@name: your file in folder: !folder', array(
        '!folder' => $catname,
      )) . '<p>';
      $messagetext .= t('The file: !filename has been approved and can be accessed !link', array(
        '!filename' => $fname,
        '!link' => $link,
      )) . '</p><p>';
      $messagetext .= t('You are receiving this because you requested to be notified.') . '</p><p>';
      $messagetext .= t('Thank You') . '</p>';
      break;
    case FILEDEPOT_NOTIFY_REJECT:

      // File submission being declined by admin where $id = new submission record id. Send only to user
      $fname = db_result(db_query("SELECT fname FROM {filedepot_filesubmissions} WHERE id=%d", $id));
      $submitter = db_result(db_query("SELECT submitter FROM {filedepot_filesubmissions} WHERE id=%d", $id));

      // Just need to create this SQL record for this user - to fake out logic below
      $target_users[] = $submitter;
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission Cancelled');
      $messagetext = t('Your recent file submission: !filename, was not accepted', array(
        '!filename' => $fname,
      )) . '<p>';
      $messagetext .= t('Thank You') . '</p>';
      break;
    case FILEDEPOT_NOTIFY_ADMIN:

      // New File Submission in queue awaiting approval
      $sql = "SELECT file.fname,file.cid,file.submitter,category.name FROM {filedepot_filesubmissions} file , " . "{filedepot_categories} category WHERE file.cid=category.cid and file.id=%d";
      $query = db_query($sql, $id);
      list($fname, $cid, $submitter, $catname) = array_values(db_fetch_array($query));
      $submitter_name = db_result(db_query("SELECT name FROM {users} WHERE uid=%d", $submitter));
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission requires Approval');
      $messagetext2ary = array(
        '!filename' => $fname,
        '!bp' => '<p>',
        '!ep' => '</p>',
        '!folder' => $catname,
      );
      break;
  }
  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 = db_fetch_object($query_users)) {
        if ($filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          $personal_exception = FALSE;
          if (db_result(db_query("SELECT uid FROM {filedepot_usersettings} WHERE uid=%d AND notify_newfile=0", $A->uid) == $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_result(db_query("SELECT count(*) FROM {filedepot_notifications} WHERE cid=%d AND uid=%d AND cid_newfiles=0", $cid, $A->uid)) > 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 = db_fetch_object($query_users)) {
        if ($filedepot
          ->checkPermission($cid, 'view', $A->uid)) {
          $personal_exception = FALSE;
          if (db_result(db_query("SELECT ignore_filechanges FROM {filedepot_notifications} WHERE fid=%d and uid=%d", $id, $A->uid)) == 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;
          }
        }
      }
    }
  }
  elseif ($type == FILEDEPOT_NOTIFY_ADMIN) {
    $query_users = db_query("SELECT uid FROM {users} WHERE uid > 0 AND status = 1");
    while ($A = db_fetch_object($query_users)) {
      if ($filedepot
        ->checkPermission($cid, 'approval', $A->uid)) {
        $personal_exception = FALSE;
        if (db_result(db_query("SELECT uid FROM {filedepot_usersettings} WHERE uid=%d AND notify_newfile=0", $A->uid) == $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_result(db_query("SELECT count(*) FROM {filedepot_notifications} WHERE cid=%d AND uid=%d AND cid_newfiles=0", $cid, $A->uid)) > 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;
        }
      }
    }
  }
  $messagetext = drupal_html_to_text($messagetext);
  if (is_array($target_users) and count($target_users) > 0) {
    if ($type == FILEDEPOT_NOTIFY_APPROVED or $type == FILEDEPOT_NOTIFY_REJECT) {

      // Only send this type of notification to user that submitted the file
      $query = db_query("SELECT name,mail FROM {users} WHERE uid=%d", $submitter);
      $rec = db_fetch_object($query);
      if ($type == FILEDEPOT_NOTIFY_APPROVED) {
        $messagetext = sprintf($messagetext, $rec->name);
      }
      $message['body'] = t('Hello @username', array(
        '@username' => $rec->name,
      )) . ",\n\n";
      $message['body'] .= $messagetext;
      $message['body'] .= variable_get('site_name', '') . "\n";
      $message['to'] = $rec->mail;
      drupal_mail_send($message);
      $sql = "INSERT INTO {filedepot_notificationlog} (target_uid,submitter_uid,notification_type,fid,cid,datetime) " . "VALUES (%d,%d,%d,%d,%d,%d )";
      db_query($sql, $submitter, $submitter, $type, $id, $cid, time());
      return TRUE;
    }
    elseif ($type == FILEDEPOT_NOTIFY_NEWFILE or $type == FILEDEPOT_NOTIFY_ADMIN) {
      $name = db_result(db_query("SELECT name FROM {users} WHERE uid=%d", $submitter));
      $messagetext2ary['@@name'] = $name;
      switch ($type) {
        case FILEDEPOT_NOTIFY_NEWFILE:
          $messagetext = t('Site member @@name has submitted a new file (!file)!bp Folder: !folder !ep!bp The file can be accessed at !link !ep!bp You are receiving this because you requested to be notified of updates.!ep!bp Thank You !ep', $messagetext2ary);
          break;
        case FILEDEPOT_NOTIFY_ADMIN:
          $messagetext = t('Site member @@name has submitted a new file !filename for folder !folder that requires approval !bp Thank You !ep', $messagetext2ary);
          break;
        default:
          break;
      }
      $messagetext = drupal_html_to_text($messagetext);
      $message['body'] = $messagetext . variable_get('site_name', '') . "\n";

      // Sort the array so that we can check for duplicate user notification records
      sort($target_users);
      reset($target_users);
      $lastuser = '';
      $distribution = array();

      /* Send out Notifications to all users on distribution using Bcc - Blind copy to hide the distribution
       * To send to complete distribution as one email and not loop thru distribution sending individual emails
       */
      foreach ($target_users as $target_uid) {
        if ($target_uid != $lastuser) {
          $query = db_query("SELECT name,mail FROM {users} WHERE uid=%d", $target_uid);
          $rec = db_fetch_object($query);
          if (!empty($rec->mail)) {
            $distribution[] = $rec->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, $submitter, $type, $id, $cid, time());
          }
          $lastuser = $target_uid;
        }
      }
      if (count($distribtion >= 1)) {
        $message['to'] = 'Filedepot Distribution';
        $message['headers']['Bcc'] = implode(',', $distribution);
        drupal_mail_send($message);
        return TRUE;
      }
      else {
        return FALSE;
      }
    }
    else {
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}