You are here

function filedepot_mail in filedepot 7

Implements hook_mail().

File

./filedepot.module, line 1719
filedepot.module Filedepot: File Management Module developed by Nextide www.nextide.ca Full featured document managment module with a desktop application feel. Integrated Organic Group, Role and User permissions to secure folders, automated…

Code

function filedepot_mail($key, &$message, $params) {
  global $user;
  $filedepot = filedepot_filedepot();
  $target_users = $params['target_users'];

  // Array of user id's the email will be sent to
  $domain = preg_replace('`^www.`', '', $_SERVER['HTTP_HOST']);
  $default_to_email = "noreply@{$domain}";
  $message['to'] = variable_get('filedepot_email_to', $default_to_email);
  $id = $params['fid'];
  if (intval($id) == 0 or count($target_users) == 0) {
    watchdog('filedepot', "Error in filedepot_mail: invalid item id or empty distribution list");
    return FALSE;
  }
  switch ($key) {
    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=:fid";
      $query = db_query($sql, array(
        ':fid' => $id,
      ));
      list($fid, $fname, $cid, $submitter, $catname) = array_values($query
        ->fetchAssoc());
      $submitter_name = db_query("SELECT name FROM {users} WHERE uid=:uid", array(
        ':uid' => $submitter,
      ))
        ->fetchField();
      $link = url('filedepot', array(
        'query' => array(
          'cid' => $cid,
          'fid' => $fid,
        ),
        'absolute' => TRUE,
      ));
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New Document Management Update');
      $message_args = array(
        '@@name' => $submitter_name,
        '!file' => $fname,
        '!folder' => $catname,
        '!link' => url($link, array(
          'absolute' => TRUE,
        )),
      );
      $message_body = t('Site member @@name has submitted a new file (!file) Folder: !folder. The file can be accessed at !link', $message_args);
      $message_body .= "\n\n" . t('You are receiving this because you requested to be notified') . "\n\n" . t('Thank You') . "\n";
      $message['body'][] = $message_body;
      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=:fid";
      $query = db_query($sql, array(
        ':fid' => $id,
      ));
      list($fid, $fname, $cid, $submitter, $catname) = array_values($query
        ->fetchAssoc());
      $submitter_name = db_query("SELECT name FROM {users} WHERE uid=:uid", array(
        ':uid' => $submitter,
      ))
        ->fetchField();
      $link = url('filedepot', array(
        'query' => array(
          'cid' => $cid,
          'fid' => $fid,
        ),
        'absolute' => TRUE,
      ));
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission Approved');
      $message_args = array(
        '@@name' => $submitter_name,
        '!file' => $fname,
        '!folder' => $catname,
        '!link' => url($link, array(
          'absolute' => TRUE,
        )),
      );
      $message_body = t('Site member @@name: your file (!file) in folder: !folder has been approved and can be accessed !link', $message_args);
      $message_body .= "\n\n" . t('You are receiving this because you requested to be notified') . "\n\n" . t('Thank You') . "\n";
      $message['body'][] = $message_body;
      break;
    case FILEDEPOT_NOTIFY_REJECT:

      // File submission being declined by admin where $id = new submission record id. Send only to user
      $query = db_query("SELECT fname, cid, submitter FROM {filedepot_filesubmissions} WHERE id=:fid", array(
        ':fid' => $id,
      ));
      list($fname, $cid, $submitter) = array_values($query
        ->fetchAssoc());
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission Cancelled');
      $message_body = t('Your recent file submission: !file, was not accepted', array(
        '!file' => $fname,
      )) . "\n\n";
      $message_body .= t('Thank You') . "\n";
      $message['body'][] = $message_body;
      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=:fid";
      $query = db_query($sql, array(
        ':fid' => $id,
      ));
      list($fname, $cid, $submitter, $catname) = array_values($query
        ->fetchAssoc());
      $submitter_name = db_query("SELECT name FROM {users} WHERE uid=:uid", array(
        ':uid' => $submitter,
      ))
        ->fetchField();
      $message_args = array(
        '@@name' => $submitter_name,
        '!file' => $fname,
        '!folder' => $catname,
      );
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('New File Submission requires Approval');
      $message_body = t('Site member @@name has submitted a new file (!file) for folder !folder that requires approval', $message_args);
      $message_body .= "\n\n" . t('Thank You') . "\n";
      $message['body'][] = $message_body;
      break;
    case FILEDEPOT_BROADCAST_MESSAGE:
      $submitter = $user->uid;
      $sql = "SELECT file.title,file.cid FROM {filedepot_files} file WHERE file.fid=:fid";
      $query = db_query($sql, array(
        ':fid' => $id,
      ));
      list($fname, $cid) = array_values($query
        ->fetchAssoc());
      $message['subject'] = variable_get('site_name', '') . ' - ' . t('Broadcast Notification');
      $message_body = $params['comment'] . "\n\n";
      $link = url('filedepot', array(
        'query' => array(
          'cid' => $cid,
        ),
        'absolute' => TRUE,
      ));
      $message_body .= t('The file: !filename can be accessed at !link', array(
        '!filename' => $fname,
        '!link' => $link,
      )) . "\n\n";
      $message_body .= t('You are receiving this broadcast alert, because your notification setting is enabled.');
      $message['body'][] = $message_body;
      break;
  }
  $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=:uid", array(
        ':uid' => $target_uid,
      ));
      $rec = $query
        ->fetchObject();
      if (!empty($rec->mail)) {
        $distribution[] = $rec->mail;
        $sql = "INSERT INTO {filedepot_notificationlog} (target_uid,submitter_uid,notification_type,fid,cid,datetime) " . "VALUES (:tuid,:uid,:type,:id,:cid,:time )";
        db_query($sql, array(
          ':tuid' => $target_uid,
          ':uid' => $submitter,
          ':type' => $key,
          ':id' => $id,
          ':cid' => $cid,
          ':time' => time(),
        ));
      }
      $lastuser = $target_uid;
    }
  }
  if (count($distribution >= 1)) {
    $message['headers']['Bcc'] = implode(',', $distribution);
    $filedepot->message = count($distribution);
    return TRUE;
  }
  else {
    return FALSE;
  }
}