You are here

function _signup_cron_send_reminders in Signup 5.2

Same name and namespace in other branches
  1. 6.2 includes/cron.inc \_signup_cron_send_reminders()
  2. 6 includes/cron.inc \_signup_cron_send_reminders()
  3. 7 includes/cron.inc \_signup_cron_send_reminders()

Helper function that sends cron-based reminder e-mails.

Invokes the method for the installed event/date backend module to get the right query fragments, and builds a query to find all nodes that need a reminder email. For each one, it loops over the users signed up for that node and send off the emails.

See also

signup_cron()

signup_reminder_sql()

_signup_build_query()

1 call to _signup_cron_send_reminders()
signup_cron in ./signup.module
Implementation of hook_cron().

File

./signup.module, line 51
The Signup module (http://drupal.org/project/signup) manages replies to nodes. In particular, it's good for event management. Signup supports sending reminder emails and automatically closing signups for nodes with a start time, via the Event…

Code

function _signup_cron_send_reminders() {
  $type_reminder_sql = array();
  foreach (signup_content_types() as $type) {
    $type_sql = signup_reminder_sql($type);
    if (!empty($type_sql)) {
      $type_reminder_sql[$type] = $type_sql;
    }
  }
  if (empty($type_reminder_sql)) {

    // No node types support reminder emails, so bail out now.
    return;
  }
  $reminder_common_sql = array(
    'primary' => '{node} n',
    'fields' => array(
      'n.title',
      'n.nid',
      'n.type',
      's.reminder_email',
      's.forwarding_email',
    ),
    'where' => array(
      's.send_reminder = 1',
      "n.type = '%s'",
    ),
    'joins' => array(
      'INNER JOIN {signup} s ON s.nid = n.nid',
    ),
  );
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  foreach ($type_reminder_sql as $type => $reminder_sql) {
    $sql = _signup_build_query($reminder_common_sql, $reminder_sql);
    $result = db_query($sql, $type);

    // Grab each node, construct the email header and subject, and query
    // the signup log to pull all users who are signed up for this node.
    while ($node = db_fetch_object($result)) {
      $subject = t('!node_type reminder: !title', array(
        '!node_type' => node_get_types('name', $type),
        '!title' => $node->title,
      ));
      $signups = db_query("SELECT u.name, u.mail, s_l.anon_mail, s_l.form_data FROM {signup_log} s_l INNER JOIN {users} u ON u.uid = s_l.uid WHERE s_l.nid = %d", $node->nid);

      // Loop through the users, composing their customized message
      // and sending the email.
      while ($signup = db_fetch_object($signups)) {

        // Get the hard-coded tokens provided by the signup module to use
        // for the reminder email.  This also gives us the email to send to.
        $signup_tokens = _signup_get_email_tokens($node, $signup);
        $user_mail = $signup_tokens['%user_mail'];
        $message = strtr($node->reminder_email, $signup_tokens);
        if (module_exists('token')) {
          $message = token_replace($message, 'node', node_load($node->nid));
        }
        drupal_mail('signup_reminder_mail', $user_mail, $subject, $message, $from);
        watchdog('signup', t('Reminder for %title sent to %user_mail.', array(
          '%title' => $node->title,
          '%user_mail' => $user_mail,
        )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
      }

      // Reminders for this node are all sent, so mark it in the
      // database so they're not sent again.
      db_query("UPDATE {signup} SET send_reminder = 0 WHERE nid = %d", $node->nid);
    }
  }
}