| 1 |
<?php
|
| 2 |
// $Id: ed_classified_notifications.inc,v 1.2 2009/09/03 02:58:11 milesgillham Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* user notifications for imple text-based classified ads module
|
| 6 |
* Michael Curry, Exodus Development, Inc.
|
| 7 |
* exodusdev@gmail.com
|
| 8 |
* for more information, please visit http://exodusdev.com
|
| 9 |
* Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights Reserved.
|
| 10 |
* Licensed under the terms of the GNU Public License (GPL) version 2. Please see LICENSE.txt for
|
| 11 |
* license terms. Posession and use of this code signifies acceptance of license
|
| 12 |
* terms.
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Process notification email handling on cron run.
|
| 17 |
* @param $time The timestamp to use for email processing.
|
| 18 |
* This allows us to use a consistent timestamp value,
|
| 19 |
* and push in fake values for testing and diagnostic
|
| 20 |
* purposes.
|
| 21 |
*/
|
| 22 |
|
| 23 |
function _ed_classified_process_notification_emails($time) {
|
| 24 |
_ed_classified_notify_advertisers_periodic($time);
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Process "periodic" notifications
|
| 29 |
* Create a notification if a user has ads nearing expiration
|
| 30 |
*/
|
| 31 |
function _ed_classified_notify_advertisers_periodic($time) {
|
| 32 |
/*
|
| 33 |
* Process notifications periodically but not every cron run
|
| 34 |
* But, don't slam server -- only do (n) messages per cron run? (admin-defined?) (later)
|
| 35 |
* When done, record ending timestamp of last completed notification run
|
| 36 |
* So: if the time since the last completed notification run is > admin-defined limit
|
| 37 |
* - get list of users needing reminder mails
|
| 38 |
* - process (format and send) a batch of emails
|
| 39 |
* - if done (no more users remaining) record completion time
|
| 40 |
*/
|
| 41 |
if (_ed_classified_periodic_notification_time($time)) {
|
| 42 |
// get list of users having published ads nearing expiration
|
| 43 |
$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));
|
| 44 |
_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))));
|
| 45 |
$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);
|
| 46 |
if ($result) {
|
| 47 |
while ($uid = db_result($result)) {
|
| 48 |
// todo: need bailout based on # of users emailed, total time spent
|
| 49 |
$user = user_load(array('uid' => $uid));
|
| 50 |
// todo: need to send mails only to those with user_access('reset classified ad expiration') && user_access('edit own classified ads') permissions
|
| 51 |
if ($user) {
|
| 52 |
if (!_ed_classified_send_user_notification_email($user, 'expiring')) {
|
| 53 |
_edi_wd(t('Unable to send ad expiration reminder email to user #!uid', array('!uid' => $uid)), WATCHDOG_ERROR);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
else {
|
| 57 |
_edi_wd(t('Unable to load user !uid', array('!uid' => $uid)), WATCHDOG_ERROR);
|
| 58 |
}
|
| 59 |
|
| 60 |
}
|
| 61 |
}
|
| 62 |
//
|
| 63 |
// now record the fact that we completed processing notifications, and when
|
| 64 |
_ed_classified_record_periodic_notifications(REQUEST_TIME);
|
| 65 |
} // time to notify
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Notify a user that their ad has expired.
|
| 70 |
* @param $node The ad node.
|
| 71 |
* This function sends a note to the user regarding their expired ad.
|
| 72 |
*/
|
| 73 |
function _ed_classified_notify_user_of_ad_expiration($node) {
|
| 74 |
$ad_author = user_load(array('uid' => $node->uid));
|
| 75 |
_ed_classified_send_user_notification_email($ad_author, 'expired');
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Send an email notification to the specified user
|
| 80 |
*/
|
| 81 |
|
| 82 |
function _ed_classified_send_user_notification_email(&$user, $key='') {
|
| 83 |
$parms = _ed_classified_displayname_parms();
|
| 84 |
$params['account'] = $user;
|
| 85 |
$params['context'] = array('!sitename' => variable_get('site_name', ''),
|
| 86 |
'!user_ads_url' => url('user/'. $user->uid .'/'. EDI_CLASSIFIED_PATH_NAME, array('absolute' => TRUE)),
|
| 87 |
'!siteurl' => url('', array('absolute' => TRUE)));
|
| 88 |
$params['context'] = array_merge($params['context'], $parms);
|
| 89 |
$from = variable_get('site_mail', ini_get('sendmail_from')); // http://drupal.org/node/77689
|
| 90 |
return drupal_mail(EDI_CLASSIFIED_MODULE_NAME, $key, $user->mail, user_preferred_language($user), $params, $from);
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Record last periodic notification processing time
|
| 95 |
*/
|
| 96 |
function _ed_classified_record_periodic_notifications($time) {
|
| 97 |
_ed_classified_variable_set('email_reminders_last_sent', $time);
|
| 98 |
}
|
| 99 |
/**
|
| 100 |
* Return TRUE if we need to send periodic notifications according to time and configuration options
|
| 101 |
* Only send notifications if sufficient time has passed since last notification run completed.
|
| 102 |
*/
|
| 103 |
function _ed_classified_periodic_notification_time($time) {
|
| 104 |
$last_notify_time = _ed_classified_variable_get('email_reminders_last_sent', 0);
|
| 105 |
// only update notifications if haven't been notified in over 24 hours
|
| 106 |
$next_notify_time = $last_notify_time + _ed_classified_variable_get('email_reminder_period_secs', 86400);
|
| 107 |
return ($time > $next_notify_time);
|
| 108 |
}
|
| 109 |
|