You are here

function _ed_classified_notify_advertisers_periodic in Classified Ads 5

Same name and namespace in other branches
  1. 5.2 ed_classified_notifications.inc \_ed_classified_notify_advertisers_periodic()
  2. 6.2 ed_classified_notifications.inc \_ed_classified_notify_advertisers_periodic()
  3. 7.2 ed_classified_notifications.inc \_ed_classified_notify_advertisers_periodic()

Process "periodic" notifications Create a notification if a user has ads nearing expiration

1 call to _ed_classified_notify_advertisers_periodic()
_ed_classified_process_notification_emails in ./ed_classified_notifications.inc
Process notification email handling on cron run.

File

./ed_classified_notifications.inc, line 30
user notifications for imple text-based classified ads module Michael Curry, Exodus Development, Inc. exodusdev@gmail.com for more information, please visit http://exodusdev.com Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights Reserved.…

Code

function _ed_classified_notify_advertisers_periodic($time) {

  /*
   * Process notifications periodically but not every cron run
   * But, don't slam server -- only do (n) messages per cron run? (admin-defined?) (later)
   * When done, record ending timestamp of last completed notification run
   * So: if the time since the last completed notification run is > admin-defined limit
   *   - get list of users needing reminder mails
   *   - process (format and send) a batch of emails
   *   - if done (no more users remaining) record completion time
   */
  if (_ed_classified_periodic_notification_time($time)) {

    // get list of users having published ads nearing expiration
    $target_time = $time + _ed_classified_days_to_seconds(_ed_classified_variable_get('ad_expiration_email_warning_days', EDI_CLASSIFIED_VAR_DEF_AD_EXPIRATION_EMAIL_WARNING_DAYS));
    _edi_wd(t('Processing notification emails for ads expiring soon (between now (!now) and !date)', array(
      '!now' => format_date($time),
      '!date' => format_date($target_time),
    )));
    $result = db_query('SELECT DISTINCT({node}.uid) FROM {node} INNER JOIN {edi_classified_nodes} ON {edi_classified_nodes}.vid = {node}.vid WHERE ({node}.status = 1) AND ({edi_classified_nodes}.expires_on < %d)', $target_time);
    if ($result) {
      while ($uid = db_result($result)) {

        // todo: need bailout based on # of users emailed, total time spent
        $user = user_load(array(
          'uid' => $uid,
        ));

        // todo: need to send mails only to those with user_access('reset classified ad expiration') && user_access('edit own classified ads') permissions
        if ($user) {
          if (!_ed_classified_send_user_notification_email($user, 'expiring')) {
            _edi_wd(t('Unable to send ad expiration reminder email to user #!uid', array(
              '!uid' => $uid,
            )), WATCHDOG_ERROR);
          }
        }
        else {
          _edi_wd(t('Unable to load user !uid', array(
            '!uid' => $uid,
          )), WATCHDOG_ERROR);
        }
      }
    }

    //
    // now record the fact that we completed processing notifications, and when
    _ed_classified_record_periodic_notifications(time());
  }

  // time to notify
}