| 1 |
<?php
|
| 2 |
// $Id: birthdays.mail.inc,v 1.3 2008/10/05 09:18:37 maartenvg Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* This file contains the files necessary to e-mail the administrator his
|
| 6 |
* periodical listings and/or the users on their birthday.
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_mail().
|
| 12 |
*/
|
| 13 |
function birthdays_mail($key, &$message, $params) {
|
| 14 |
global $_birthdays_field;
|
| 15 |
|
| 16 |
$language = $message['language'];
|
| 17 |
|
| 18 |
switch ($key) {
|
| 19 |
// Send message to administrator.
|
| 20 |
case 'admin_message':
|
| 21 |
$message['subject'] = t('Upcoming Birthdays');
|
| 22 |
switch ($params['period']) {
|
| 23 |
case BIRTHDAYS_ADMIN_MAIL_DAILY:
|
| 24 |
$period = t('Today');
|
| 25 |
break;
|
| 26 |
case BIRTHDAYS_ADMIN_MAIL_WEEKLY:
|
| 27 |
$period = t('This week');
|
| 28 |
break;
|
| 29 |
case BIRTHDAYS_ADMIN_MAIL_MONTHLY:
|
| 30 |
$period = t('This month');
|
| 31 |
break;
|
| 32 |
}
|
| 33 |
$message['body'][] = t('@period, the following users are having their birthday:', array('@period' => $period), $language->language);
|
| 34 |
|
| 35 |
// Build list of users
|
| 36 |
foreach ($params['accounts'] as $uid) {
|
| 37 |
$account = user_load(array('uid' => $uid));
|
| 38 |
|
| 39 |
// Don't show the year
|
| 40 |
$account->{$_birthdays_field->name}['year'] = NULL;
|
| 41 |
|
| 42 |
// Calculate the age to be.
|
| 43 |
$age = $account->age + !($account->{$_birthdays_field->name}['day'] == format_date(time(), 'custom', 'j') && $account->{$_birthdays_field->name}['month'] == format_date(time(), 'custom', 'n'));
|
| 44 |
|
| 45 |
// Set the message. In the case of daily messages, don't add the date.
|
| 46 |
$message['body'][] = ($params['period'] == BIRTHDAYS_ADMIN_MAIL_DAILY ? '' : _birthdays_show_date($account->{$_birthdays_field->name}, $account) . ': ') . $account->name . ' (' . $age. ')';
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
// Send message to user.
|
| 50 |
case 'user_message':
|
| 51 |
// Get the content for the placeholders.
|
| 52 |
$variables = user_mail_tokens($params['account'], $language);
|
| 53 |
|
| 54 |
$message['subject'] = _birthdays_mail_text('subject', $language, $variables);
|
| 55 |
$message['body'] = _birthdays_mail_text('message', $language, $variables);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Sends an e-mail to administrator for the upcoming day(s).
|
| 61 |
*/
|
| 62 |
function _birthdays_send_admin_message($days = 1) {
|
| 63 |
global $user;
|
| 64 |
// Only proceed when admin messages are enabled
|
| 65 |
if (variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED) != BIRTHDAYS_ADMIN_MAIL_DISABLED) {
|
| 66 |
// Get birthdays
|
| 67 |
$accounts = birthdays_get_birthdays_by_days($days);
|
| 68 |
|
| 69 |
// If there were any users:
|
| 70 |
if (count($accounts) > 0) {
|
| 71 |
// Get site e-mail to send reminder to and from
|
| 72 |
$to = variable_get('site_mail', ini_get('sendmail_from'));
|
| 73 |
// Load the accounts into the parameters;
|
| 74 |
$params['accounts'] = $accounts;
|
| 75 |
$params['period'] = variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED);
|
| 76 |
// Send the mail
|
| 77 |
drupal_mail('birthdays', 'admin_message', $to, user_preferred_language($user), $params);
|
| 78 |
// Log action
|
| 79 |
watchdog('Birthdays', 'Sent birthday overview e-mail to admin.', array(), WATCHDOG_NOTICE, ' ');
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Send all birthdays on this day a message
|
| 87 |
*
|
| 88 |
* @todo re-introduce the postcard module to make it fun
|
| 89 |
*/
|
| 90 |
function _birthdays_send_user_message() {
|
| 91 |
// If messaging is enabled
|
| 92 |
if (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) != BIRTHDAYS_USER_MAIL_NO) {
|
| 93 |
// Get all users having their birthday today
|
| 94 |
$accounts = birthdays_get_todays_birthdays();
|
| 95 |
|
| 96 |
foreach ($accounts as $uid) {
|
| 97 |
// Load user
|
| 98 |
$account = user_load(array('uid' => $uid));
|
| 99 |
// If user and/or administrator allows sending messages
|
| 100 |
if (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == BIRTHDAYS_USER_MAIL_YES || (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == BIRTHDAYS_USER_MAIL_USER && $account->birthdays_user_send_mail == BIRTHDAYS_USER_MAIL_USER_YES)) {
|
| 101 |
$to = $account->name .'<'. $account->mail .'>';
|
| 102 |
$params['account'] = $account;
|
| 103 |
|
| 104 |
// Send mail
|
| 105 |
drupal_mail('birthdays', 'user_message', $to, user_preferred_language($account), $params);
|
| 106 |
// Log actions
|
| 107 |
watchdog('Birthdays', 'Sent @name a birthday e-mail.', array('@name' => $account->name), WATCHDOG_NOTICE, ' ');
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
}
|