1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141:
<?php
class Birthday_Notify_Background extends SMF_BackgroundTask
{
public function execute()
{
global $txt, $smcFunc, $txtBirthdayEmails, $modSettings, $sourcedir;
$greeting = isset($modSettings['birthday_email']) ? $modSettings['birthday_email'] : 'happy_birthday';
$month = date('n');
$day = date('j');
$result = $smcFunc['db_query']('', '
SELECT id_member, real_name, lngfile, email_address
FROM {db_prefix}members
WHERE is_activated < 10
AND MONTH(birthdate) = {int:month}
AND DAYOFMONTH(birthdate) = {int:day}
AND YEAR(birthdate) > {int:year}
' . ($smcFunc['db_title'] === POSTGRE_TITLE ? 'AND indexable_month_day(birthdate) = indexable_month_day({date:bdate})' : ''),
array(
'year' => 1004,
'month' => $month,
'day' => $day,
'bdate' => '1004-' . $month . '-' . $day,
)
);
$birthdays = array();
while ($row = $smcFunc['db_fetch_assoc']($result))
{
if (!isset($birthdays[$row['lngfile']]))
$birthdays[$row['lngfile']] = array();
$birthdays[$row['lngfile']][$row['id_member']] = array(
'name' => $row['real_name'],
'email' => $row['email_address']
);
}
$smcFunc['db_free_result']($result);
if (!empty($birthdays))
{
require_once($sourcedir . '/ScheduledTasks.php');
loadEssentialThemeData();
require_once($sourcedir . '/Subs-Post.php');
foreach ($birthdays as $lang => $members)
{
loadLanguage('EmailTemplates', $lang);
$txt['happy_birthday_subject'] = $txtBirthdayEmails[$greeting . '_subject'];
$txt['happy_birthday_body'] = $txtBirthdayEmails[$greeting . '_body'];
require_once($sourcedir . '/Subs-Notify.php');
$prefs = getNotifyPrefs(array_keys($members), array('birthday'), true);
foreach ($members as $member_id => $member)
{
$pref = !empty($prefs[$member_id]['birthday']) ? $prefs[$member_id]['birthday'] : 0;
$replacements = array(
'REALNAME' => $member['name'],
);
if ($pref & self::RECEIVE_NOTIFY_ALERT)
{
$alertdata = loadEmailTemplate('happy_birthday', $replacements, $lang, false);
$alertdata['body'] = preg_replace("~\s*[\r\n]+\s*~", '<br>', $alertdata['body']);
$alert_rows[] = array(
'alert_time' => time(),
'id_member' => $member_id,
'content_type' => 'birthday',
'content_id' => 0,
'content_action' => 'msg',
'is_read' => 0,
'extra' => $smcFunc['json_encode'](array('happy_birthday' => $alertdata['body'])),
);
}
if ($pref & self::RECEIVE_NOTIFY_EMAIL)
{
$emaildata = loadEmailTemplate('happy_birthday', $replacements, $lang, false);
sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'birthday', $emaildata['is_html'], 4);
}
}
}
AddMailQueue(true);
if (!empty($alert_rows))
{
$smcFunc['db_insert']('',
'{db_prefix}user_alerts',
array(
'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string',
'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string',
),
$alert_rows,
array()
);
updateMemberData(array_keys($members), array('alerts' => '+'));
}
}
return true;
}
}
?>