View source
<?php
global $_birthdays_field;
define('BIRTHDAYS_ADMIN_MAIL_DISABLED', '0');
define('BIRTHDAYS_ADMIN_MAIL_DAILY', '1');
define('BIRTHDAYS_ADMIN_MAIL_WEEKLY', '2');
define('BIRTHDAYS_ADMIN_MAIL_MONTHLY', '3');
define('BIRTHDAYS_STARSIGN_OFF', '0');
define('BIRTHDAYS_STARSIGN_LINK', '1');
define('BIRTHDAYS_STARSIGN_NOLINK', '2');
define('BIRTHDAYS_HIDE_YEAR_NO', '0');
define('BIRTHDAYS_HIDE_YEAR_YES', '1');
define('BIRTHDAYS_HIDE_YEAR_USER', '2');
define('BIRTHDAYS_HIDE_YEAR_USER_NO', '0');
define('BIRTHDAYS_HIDE_YEAR_USER_YES', '1');
define('BIRTHDAYS_PAGE_FILTER_SORT_DATE', '0');
define('BIRTHDAYS_PAGE_FILTER_SORT_USER', '1');
define('BIRTHDAYS_PAGE_NOFILTER_SORT_USER', '2');
define('BIRTHDAYS_USER_MAIL_NO', '0');
define('BIRTHDAYS_USER_MAIL_YES', '1');
define('BIRTHDAYS_USER_MAIL_USER', '2');
define('BIRTHDAYS_USER_MAIL_USER_NO', '1');
define('BIRTHDAYS_USER_MAIL_USER_YES', '0');
function birthdays_help($section = '') {
$output = '';
switch ($section) {
case 'admin/help#birthdays':
$output = t("Allows users to store their birthdays and displays blocks of upcoming birthdays. Sends out e-mail on user's birthday automatically as cron job. Also sends admin reminder e-mails of upcoming user's birthdays. Most functionality is configurable in the admin settings and user settings. Requires profile.module and a present date field dedicated to the birthdays.");
break;
}
return $output;
}
function birthdays_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/birthdays',
'title' => t('Birthdays'),
'description' => t('Set user birthday mail and toggle user mail, upcoming birthdays mail and more.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'birthdays_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'admin/settings/birthdays/settings',
'title' => t('Settings'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'birthdays_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/settings/birthdays/sync',
'title' => t('Synchronize'),
'description' => t('Synchronize birthdays information of Profile module and Birthdays module. Used either when updating to a newer version of Birthdays or when integrating with an existing Profile Field.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'birthdays_sync_form',
),
'access' => user_access('administer site configuration'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'birthdays',
'title' => t('Birthdays'),
'description' => t('List the birthdays of all users.'),
'callback' => 'birthdays_view_page',
'access' => user_access('access birthdays'),
'type' => MENU_SUGGESTED_ITEM,
);
}
return $items;
}
function birthdays_init() {
global $_birthdays_field;
$_birthdays_field = _birthdays_get_field(variable_get('birthdays_field_id', NULL));
}
function birthdays_sync_form() {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
drupal_set_message(t('No date field has been selected as birthdays field. Please visit the <a href="@birthdays-settings">birthdays settings page</a>.', array(
'@birthdays-settings' => url('admin/settings/birthdays'),
)), 'warning');
return array();
}
$form['profile_fieldset'] = array(
'#type' => 'fieldset',
);
$form['profile_fieldset']['description'] = array(
'#type' => 'item',
'#title' => t('Profile to Birthdays'),
'#value' => t('Fill the Birthdays module\'s table with data from the Profile module\'s table. This is needed when you (re-)installed the Birthdays module, but already collected birthdays with the Profile module (e.g. for age verification).'),
);
$form['profile_fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => t('Copy Profile data to Birthdays module'),
);
$form['birthdays_fieldset'] = array(
'#type' => 'fieldset',
);
$form['birthdays_fieldset']['description'] = array(
'#type' => 'item',
'#title' => t('Birthdays to Profile'),
'#value' => t('Fill the Profile module\'s table with data from the Birthdays module\'s table. You can use this to copy the old data collected by a previous version of the Birthdays module after an update. It is only necessary when updating from a version of the Birthdays module which didn\'t use the Profile module.'),
);
$form['birthdays_fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => t('Copy Birthdays data to Profile module'),
);
return $form;
}
function birthdays_sync_form_submit($form_id, $values) {
global $_birthdays_field;
if ($values['op'] == t('Copy Profile data to Birthdays module')) {
$result = db_query('SELECT uid FROM {users}');
while ($uid = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $uid->uid,
));
if ($account->{$_birthdays_field->name}) {
user_save($account, array(
$_birthdays_field->name => $account->{$_birthdays_field->name},
));
}
}
}
else {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result = db_query('SELECT {dob}.uid, MONTH({dob}.birthday) AS month, YEAR({dob}.birthday) AS year, DAYOFMONTH({dob}.birthday) AS day FROM {dob}');
break;
case 'pgsql':
$result = db_query("SELECT uid, date_part('month',{dob}.birthday) AS month, date_part('year',{dob}.birthday) AS year, date_part('day',{dob}.birthday) AS day FROM {dob}");
break;
}
while ($birthday = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $birthday->uid,
));
$dob = array(
'day' => $birthday->day,
'month' => $birthday->month,
'year' => $birthday->year,
);
user_save($account, array(
$_birthdays_field->name => $dob,
));
}
}
drupal_set_message(t('Modules have been synchronized.'));
}
function birthdays_view_page() {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
return NULL;
}
drupal_add_css(drupal_get_path('module', 'birthdays') . '/birthdays.css');
if (isset($_REQUEST['birthdays_filter_month'])) {
$filter_month = (int) $_REQUEST['birthdays_filter_month'];
}
if (isset($_REQUEST['birthdays_filter_year'])) {
$filter_year = (int) $_REQUEST['birthdays_filter_year'];
}
$output = '';
$filter = '';
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$join = variable_get('birthdays_page_settings', BIRTHDAYS_PAGE_FILTER_SORT_DATE) != BIRTHDAYS_PAGE_NOFILTER_SORT_USER || !empty($filter_month) || !empty($filter_year) ? 'RIGHT JOIN' : 'LEFT JOIN';
$sort = variable_get('birthdays_page_settings', BIRTHDAYS_PAGE_FILTER_SORT_DATE) == BIRTHDAYS_PAGE_FILTER_SORT_DATE || !empty($filter_month) || !empty($filter_year) ? 'MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), ' : '';
$filter .= !empty($filter_month) ? " AND MONTH({dob}.birthday) = " . $filter_month : '';
$filter .= !empty($filter_year) ? " AND YEAR({dob}.birthday) = " . $filter_year : '';
break;
case 'pgsql':
$join = variable_get('birthdays_page_settings', BIRTHDAYS_PAGE_FILTER_SORT_DATE) != BIRTHDAYS_PAGE_NOFILTER_SORT_USER || !empty($filter_month) || !empty($filter_year) ? 'RIGHT JOIN' : 'LEFT JOIN';
$sort = variable_get('birthdays_page_settings', BIRTHDAYS_PAGE_FILTER_SORT_DATE) == BIRTHDAYS_PAGE_FILTER_SORT_DATE || !empty($filter_month) || !empty($filter_year) ? "date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), " : '';
$filter .= !empty($filter_month) ? " AND date_part('month', {dob}.birthday) = " . $filter_month : '';
$filter .= !empty($filter_year) ? " AND date_part('year', {dob}.birthday) = " . $filter_year : '';
break;
}
$result = pager_query("SELECT {users}.uid FROM {users} " . $join . " {dob} ON {users}.uid = {dob}.uid WHERE {users}.status <> 0 AND {users}.access <> 0" . $filter . " ORDER BY " . $sort . "{users}.name", variable_get('birthdays_page_list_number', 25));
while ($uid = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $uid->uid,
));
if (empty($filter_year) || !$account->birthdays_user_hide_year) {
$accounts[] = $account;
}
}
$output .= theme('birthdays_page', $accounts, $filter_month, $filter_year);
return $output;
}
function birthdays_page_filter($filter_month = NULL, $filter_year = NULL) {
$options_months[0] = '';
for ($i = 1; $i <= 12; $i++) {
$options_months[$i] = format_date(gmmktime(0, 0, 0, $i, 2, 1970), 'custom', 'F', 0);
}
$options_years[0] = '';
$options_years = $options_years + drupal_map_assoc(range(date('Y'), 1900));
$form['#prefix'] = '<div class="container-inline birthdays-filter">';
$form['#suffix'] = "</div>\n";
$form['birthdays_filter_month'] = array(
'#type' => 'select',
'#options' => $options_months,
'#default_value' => $filter_month,
'#title' => t('Filter by month'),
'#attributes' => array(
'onchange' => 'submit()',
),
);
if (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) != BIRTHDAYS_HIDE_YEAR_YES) {
$form['birthdays_filter_year'] = array(
'#type' => 'select',
'#options' => $options_years,
'#default_value' => $filter_year,
'#title' => t('Year'),
'#attributes' => array(
'onchange' => 'submit()',
),
);
}
$form['button'] = array(
'#type' => 'button',
'#prefix' => '<noscript>',
'#value' => t('Filter'),
'#suffix' => '</noscript>',
);
return $form;
}
function theme_birthdays_page($accounts = array(), $filter_month, $filter_year) {
global $_birthdays_field;
$header = array(
t('User'),
t('Birthday'),
);
if (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) != BIRTHDAYS_HIDE_YEAR_YES) {
$header[] = t('Age');
}
if (variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF)) {
$header[] = t('Starsign');
}
if (variable_get('user_pictures', false)) {
$header[] = ' ';
}
$rows = array();
$accounts = is_array($accounts) ? $accounts : array();
$i = 0;
foreach ($accounts as $account) {
$rows[$i] = array(
theme('username', $account),
_birthdays_show_date($account->{$_birthdays_field->name}, $account),
);
if (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) != BIRTHDAYS_HIDE_YEAR_YES) {
$age = _birthdays_show_age($account);
$rows[$i][] = isset($age) ? $age : '';
}
if (variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF)) {
$rows[$i][] = birthdays_get_starsign_image($account->birthdays_starsign, variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF));
}
if (variable_get('user_pictures', false)) {
$rows[$i][] = theme_user_picture($account);
}
$i++;
}
$output = '';
if (variable_get('birthdays_page_show_filters', 1)) {
$output .= drupal_get_form('birthdays_page_filter', $filter_month, $filter_year);
}
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, variable_get('birthdays_page_list_number', 25), 0);
return $output;
}
function birthdays_admin_settings() {
$options = _birthdays_get_date_fields();
if (empty($options)) {
drupal_set_message(t('No profile fields of type \'date\' were found, please <a href="@profile-settings-page">create one here</a>.', array(
'@profile-settings-page' => url('admin/user/profile/add/date'),
)), 'error');
return array();
}
$form['birthdays_general_settings'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$options = array(
'' => '',
) + $options;
$form['birthdays_general_settings']['birthdays_field_id'] = array(
'#type' => 'select',
'#title' => t('Profile field'),
'#default_value' => variable_get('birthdays_field_id', NULL),
'#description' => t('Choose the profile field of type \'date\' you want to use as date of birth.'),
'#options' => $options,
'#required' => TRUE,
);
$form['birthdays_general_settings']['birthdays_show_starsign'] = array(
'#type' => 'select',
'#title' => t('Show star signs'),
'#default_value' => variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF),
'#options' => array(
BIRTHDAYS_STARSIGN_OFF => t('No'),
BIRTHDAYS_STARSIGN_LINK => t('Yes, with link to Yahoo Astrology'),
BIRTHDAYS_STARSIGN_NOLINK => t('Yes, without link to Yahoo Astrology'),
),
'#description' => t('Select whether the star signs should be enabled.'),
);
$form['birthdays_general_settings']['birthdays_hide_year'] = array(
'#type' => 'select',
'#title' => t('Hide year and age'),
'#default_value' => variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO),
'#description' => t('Select whether the birth year and age should be shown.'),
'#options' => array(
BIRTHDAYS_HIDE_YEAR_NO => t('No'),
BIRTHDAYS_HIDE_YEAR_YES => t('Yes'),
BIRTHDAYS_HIDE_YEAR_USER => t('User optional, \'No\' by default'),
),
);
$form['birthdays_page'] = array(
'#type' => 'fieldset',
'#title' => t('Birthdays page settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['birthdays_page']['birthdays_page_settings'] = array(
'#type' => 'select',
'#title' => t('Set birthdays page settings'),
'#default_value' => variable_get('birthdays_page_settings', BIRTHDAYS_PAGE_FILTER_SORT_DATE),
'#description' => t('Select whether users that haven\'t entered their date of birth should be shown on the birthdays page, and whether the list should be ordered by birthday or by username.'),
'#options' => array(
BIRTHDAYS_PAGE_FILTER_SORT_DATE => t('Filter users, sort by date'),
BIRTHDAYS_PAGE_FILTER_SORT_USER => t('Filter users, sort by username'),
BIRTHDAYS_PAGE_NOFILTER_SORT_USER => t('Do not filter users, sort by username'),
),
);
$form['birthdays_page']['birthdays_page_list_number'] = array(
'#type' => 'textfield',
'#title' => t('Birthdays page size'),
'#default_value' => variable_get('birthdays_page_list_number', 25),
'#description' => t('How many users should be listed on each page on the Birthdays listing?'),
'#required' => TRUE,
);
$form['birthdays_page']['birthdays_page_show_filters'] = array(
'#type' => 'select',
'#title' => t('Show filter options'),
'#default_value' => variable_get('birthdays_page_show_filters', 1),
'#description' => t('Show the "Filter by month and year" selections on the birthdays page. When "Hide year" has been set to "Yes", the year filter isn\'t shown.'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['birthdays_email'] = array(
'#type' => 'fieldset',
'#title' => t('E-mail settings'),
'#description' => t('Adjust the e-mail settings for both e-mails to users and e-mails to the administrator. This requires cron jobs to be active. Please see the <a href="@cron_url">online handbook</a> for more information on how to accomplish that.', array(
'@cron_url' => 'http://drupal.org/cron',
)),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['birthdays_email']['admin'] = array(
'#type' => 'fieldset',
'#title' => t('Admin'),
'#collapsible' => TRUE,
'#collapsed' => variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED) == BIRTHDAYS_ADMIN_MAIL_DISABLED,
);
$days = array(
0 => t('sunday'),
1 => t('monday'),
2 => t('tuesday'),
3 => t('wednesday'),
4 => t('thursday'),
5 => t('friday'),
6 => t('saturday'),
);
$form['birthdays_email']['admin']['birthdays_remind'] = array(
'#type' => 'select',
'#title' => t('Send upcoming birthdays to admin'),
'#default_value' => variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED),
'#options' => array(
BIRTHDAYS_ADMIN_MAIL_DISABLED => t('Disabled'),
BIRTHDAYS_ADMIN_MAIL_DAILY => t('Daily'),
BIRTHDAYS_ADMIN_MAIL_WEEKLY => t('Weekly, on @first_weekday', array(
'@first_weekday' => $days[variable_get('date_first_day', 0)],
)),
BIRTHDAYS_ADMIN_MAIL_MONTHLY => t('Monthly'),
),
'#description' => t('Do you want a daily e-mail containing all upcoming birthdays?'),
);
$form['birthdays_email']['users'] = array(
'#type' => 'fieldset',
'#title' => t('User'),
'#collapsible' => TRUE,
'#collapsed' => variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == BIRTHDAYS_USER_MAIL_NO,
);
$form['birthdays_email']['users']['birthdays_send_user'] = array(
'#type' => 'select',
'#title' => t('Send user e-mail on day of birth'),
'#default_value' => variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO),
'#options' => array(
BIRTHDAYS_USER_MAIL_NO => t('No'),
BIRTHDAYS_USER_MAIL_YES => t('Yes'),
BIRTHDAYS_USER_MAIL_USER => t('User optional, \'Yes\' by default'),
),
'#description' => t('Should users that have their birthday today receive an e-mail?'),
);
$form['birthdays_email']['users']['birthdays_send_user_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => variable_get('birthdays_send_user_subject', t('Happy Birthday!')),
);
$form['birthdays_email']['users']['birthdays_send_user_message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => variable_get('birthdays_send_user_message', t("Hey @name,\nHappy birthday!\nHope you have a great day!")),
'#description' => t('@name will be replaced by the username.'),
);
return system_settings_form($form);
}
function birthdays_perm() {
return array(
'access birthdays',
);
}
function birthdays_cron() {
global $_birthdays_field;
if (isset($_birthdays_field) && (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) != BIRTHDAYS_USER_MAIL_NO || variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED) != BIRTHDAYS_ADMIN_MAIL_DISABLED)) {
$time = time();
if (variable_get('birthdays_last_cron', 0) <= $time - 24 * 3600) {
variable_set('birthdays_last_cron', floor($time / 100) * 100);
$remind_frequency = variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED);
_birthdays_send_user_message();
if ($remind_frequency == BIRTHDAYS_ADMIN_MAIL_DAILY) {
_birthdays_send_admin_message(1);
}
elseif ($remind_frequency == BIRTHDAYS_ADMIN_MAIL_WEEKLY && date('w', $time) == variable_get('date_first_day', 0)) {
_birthdays_send_admin_message(7);
}
elseif ($remind_frequency == BIRTHDAYS_ADMIN_MAIL_MONTHLY && date('j', $time) == 1) {
_birthdays_send_admin_message(date('t', $time));
}
}
}
}
function birthdays_block($op = 'list', $delta = 'by_days', $edit = array()) {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
return;
}
switch ($op) {
case 'list':
$blocks['by_days']['info'] = t('Birthdays Block: Next N days');
$blocks['by_birthdays']['info'] = t('Birthdays Block: N upcoming birthdays');
return $blocks;
case 'configure':
switch ($delta) {
case 'by_days':
$form["birthdays_block_settings"] = array(
'#type' => 'textfield',
'#title' => t("Number of days to show"),
'#default_value' => variable_get("birthdays_block_number_by_days", 7),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("Number of days looking forward for upcoming birthdays. Use 1 for today's birthdays only. Note: it might show more or less birthday items than the specified number of days, because not all days have birthdays, and some days have multiple birthdays."),
'#required' => true,
);
$form['birthdays_block_hide'] = array(
'#type' => 'radios',
'#title' => t('Hide block when no birthdays'),
'#default_value' => variable_get("birthdays_block_hide_empty", 0),
'#options' => array(
t('No'),
t('Yes'),
),
'#description' => t("Should the block be hidden when there are no upcoming birthdays, or should it show a message."),
);
return $form;
case 'by_birthdays':
$form["birthdays_block_settings"] = array(
'#type' => 'textfield',
'#title' => t("Number of birthdays to show"),
'#default_value' => variable_get("birthdays_block_number_by_birthdays", 6),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("Number of upcoming birthdays to list in the block. It will show exactly the specified number of birthdays, even if more people have their birthday on the same day. In that case, there will be people who will never be shown."),
'#required' => true,
);
$form['birthdays_block_hide'] = array(
'#type' => 'radios',
'#title' => t('Hide block when no birthdays'),
'#default_value' => variable_get("birthdays_block_hide_empty", 0),
'#options' => array(
t('No'),
t('Yes'),
),
'#description' => t("Should the block be hidden when there are no upcoming birthdays, or should it show a message."),
);
return $form;
}
case 'save':
variable_set('birthdays_block_number_' . $delta, $edit['birthdays_block_settings']);
variable_set('birthdays_block_hide_empty', $edit['birthdays_block_hide']);
return;
case 'view':
$block = array();
if (user_access('access birthdays')) {
switch ($delta) {
case 'by_days':
$amount = variable_get("birthdays_block_number_by_days", 7);
$birthdays = birthdays_get_birthdays_by_days($amount);
if (count($birthdays) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {
$block['subject'] = t('Upcoming Birthdays');
$block['content'] = theme('birthdays_block', $birthdays, $amount, $delta);
}
break;
case 'by_birthdays':
$amount = variable_get("birthdays_block_number_by_birthdays", 6);
$birthdays = birthdays_get_birthdays($amount);
if (count($birthdays) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {
$block['subject'] = t('Upcoming Birthdays');
$block['content'] = theme('birthdays_block', $birthdays, $amount, $delta);
}
break;
}
return $block;
}
}
}
function theme_birthdays_block($birthdays, $amount, $delta) {
global $_birthdays_field;
if (!empty($birthdays)) {
$output = '<table>';
foreach ($birthdays as $b) {
$account = user_load(array(
'uid' => $b,
));
$age = _birthdays_show_age($account);
$age = isset($age) ? '(' . ($account->age + !($account->{$_birthdays_field->name}['day'] == format_date(time(), 'custom', 'j') && $account->{$_birthdays_field->name}['month'] == format_date(time(), 'custom', 'n'))) . ')' : '';
$account->{$_birthdays_field->name}['year'] = NULL;
$output .= '<tr><td>' . theme('username', $account) . ' <small>' . $age . '</small></td><td>' . _birthdays_show_date($account->{$_birthdays_field->name}, $account) . '</td></tr>';
}
$output .= '</table>';
}
else {
$output = '<p>' . t('Nobody is having their birthday soon.') . '</p>';
}
$output .= '<div class="more-link">' . l(t('more'), 'birthdays', array(
'title' => t('Show all birthdays.'),
)) . '</div>';
return $output;
}
function birthdays_get_todays_birthdays() {
return birthdays_get_birthdays_by_days(1);
}
function birthdays_get_birthdays_by_days($amount) {
$birthdays = array();
global $user;
if ($amount < 1) {
$amount = 1;
}
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timezone = $user->timezone;
}
else {
$timezone = variable_get('date_default_timezone', 0);
}
$timezone -= date('Z');
$current_year = date('Y');
$next_year = $current_year + 1;
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result = db_query("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND (\n (\n DATE_FORMAT({dob}.birthday,'{$next_year}%%m%%d') - DATE_FORMAT(ADDDATE(ADDDATE(NOW(),INTERVAL %d SECOND),INTERVAL %d DAY),'%%Y%%m%%d') < 0\n )\n OR\n (\n DATE_FORMAT({dob}.birthday,'{$current_year}%%m%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%Y%%m%%d') >= 0\n AND\n DATE_FORMAT({dob}.birthday,'{$current_year}%%m%%d') - DATE_FORMAT(ADDDATE(ADDDATE(NOW(),INTERVAL %d SECOND),INTERVAL %d DAY),'%%Y%%m%%d') < 0\n ) )\n ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, $amount, $timezone, $timezone, $amount);
break;
case 'pgsql':
$result = db_query("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND (\n (\n cast(to_char({dob}.birthday,'{$next_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds' + INTERVAL '%d days','YYYYMMDD') as integer) < 0\n )\n OR\n (\n cast(to_char({dob}.birthday,'{$current_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','YYYYMMDD') as integer) >= 0\n AND\n cast(to_char({dob}.birthday,'{$current_year}MMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds' + INTERVAL '%d days','YYYYMMDD') as integer) < 0\n ) )\n ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, $amount, $timezone, $timezone, $amount);
break;
}
while ($account = db_fetch_object($result)) {
$birthdays[] = $account->uid;
}
return $birthdays;
}
function birthdays_get_birthdays($amount) {
$birthdays = array();
global $user;
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timezone = $user->timezone;
}
else {
$timezone = variable_get('date_default_timezone', 0);
}
if ($amount < 1) {
$amount = 1;
}
$timezone -= date('Z');
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND DATE_FORMAT({dob}.birthday,'%%c%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%c%%d') >= 0\n ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, 0, $amount);
break;
case 'pgsql':
$result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND cast(to_char({dob}.birthday,'FMMMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','FMMMDD') as integer) >= 0\n ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, 0, $amount);
break;
}
while ($account = db_fetch_object($result)) {
$birthdays[] = $account->uid;
}
if (db_num_rows($result) < $amount) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND DATE_FORMAT({dob}.birthday,'%%c%%d') - DATE_FORMAT(ADDDATE(NOW(),INTERVAL %d SECOND),'%%c%%d') < 0\n ORDER BY MONTH({dob}.birthday), DAYOFMONTH({dob}.birthday), YEAR({dob}.birthday), {users}.name", $timezone, 0, $amount - db_num_rows($result));
break;
case 'pgsql':
$result = db_query_range("SELECT {dob}.uid FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND {users}.status <> 0 AND {users}.access <> 0\n AND cast(to_char({dob}.birthday,'FMMMDD') as integer) - cast(to_char(current_timestamp + INTERVAL '%d seconds','FMMMDD') as integer) < 0\n ORDER BY date_part('month', {dob}.birthday), date_part('day', {dob}.birthday), date_part('year', {dob}.birthday), {users}.name", $timezone, 0, $amount - db_num_rows($result));
break;
}
while ($account = db_fetch_object($result)) {
$birthdays[] = $account->uid;
}
}
return $birthdays;
}
function birthdays_user($op, &$edit, &$account, $category = NULL) {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
return;
}
switch ($op) {
case 'load':
return birthdays_load_user($account);
case 'update':
case 'insert':
return birthdays_save_user($edit, $account, $category);
case 'form':
return birthdays_form_user($edit, $account, $category);
case 'register':
return birthdays_form_user($edit, $account, $category, TRUE);
case 'delete':
db_query('DELETE FROM {dob} WHERE uid = %d', $account->uid);
}
}
function birthdays_load_user(&$account) {
global $_birthdays_field;
profile_load_profile($account);
if ($account->{$_birthdays_field->name}) {
$account->age = _birthdays_get_age($account->{$_birthdays_field->name});
}
}
function birthdays_save_user(&$edit, &$account, $category) {
global $_birthdays_field;
if (isset($_birthdays_field) && array_key_exists($_birthdays_field->name, $edit)) {
if (is_array($edit[$_birthdays_field->name])) {
extract($edit[$_birthdays_field->name]);
}
db_query("DELETE FROM {dob} where uid = %d", $account->uid);
if ($day && $year && $month) {
$edit['birthdays_starsign'] = _birthdays_get_starsign($day, $month);
db_query("INSERT INTO {dob} (uid, birthday) VALUES (%d, '%d-%d-%d');", $account->uid, $year, $month, $day);
}
else {
unset($edit[$_birthdays_field->name]);
$edit['birthdays_starsign'] = '';
}
}
}
function birthdays_profile_alter(&$account, &$fields) {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
return;
}
if (array_key_exists($_birthdays_field->category, $fields) && array_key_exists($_birthdays_field->name, $fields[$_birthdays_field->category])) {
if (user_access('access birthdays')) {
$starsign = '<span class="birthdays-starsign">' . birthdays_get_starsign_image($account->birthdays_starsign, variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF)) . '</span> ';
if (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_NO || variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_USER && $account->birthdays_user_hide_year != BIRTHDAYS_HIDE_YEAR_USER_YES) {
$age = ' <span class="birthdays-age">(' . $account->age . ')</span>';
}
$fields[$_birthdays_field->category][$_birthdays_field->name]['value'] = $starsign . _birthdays_show_date($account->{$_birthdays_field->name}, $account, 'medium') . $age;
}
else {
unset($fields[$_birthdays_field->category][$_birthdays_field->name]);
}
}
}
function birthdays_form_user($edit, $account, $category, $register = FALSE) {
global $_birthdays_field;
drupal_add_css(drupal_get_path('module', 'birthdays') . '/birthdays.css', 'module');
if ($category == $_birthdays_field->category || $_birthdays_field->register && $register) {
if (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_USER) {
$form[$_birthdays_field->category][$_birthdays_field->name]['birthdays_user_hide_year'] = array(
'#type' => 'checkbox',
'#title' => t("Hide age and birth year"),
'#default_value' => (int) $account->birthdays_user_hide_year,
'#description' => t("Do not show your age and your year of birth."),
'#return_value' => BIRTHDAYS_HIDE_YEAR_USER_YES,
'#weight' => 1,
'#prefix' => '<div class="birthdays-container">',
'#suffix' => '</div>',
);
}
if (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == BIRTHDAYS_USER_MAIL_USER) {
$form[$_birthdays_field->category][$_birthdays_field->name]['birthdays_user_send_mail'] = array(
'#type' => 'checkbox',
'#title' => t("Do not send birthday mail"),
'#default_value' => (int) $account->birthdays_user_send_mail,
'#description' => t("Do not send me an e-mail or e-card when it's my birthday."),
'#return_value' => BIRTHDAYS_HIDE_YEAR_USER_NO,
'#weight' => 2,
'#prefix' => '<div class="birthdays-container">',
'#suffix' => '</div>',
);
}
}
return $form;
}
function birthdays_form_alter($form_id, &$form) {
global $_birthdays_field;
if ($form_id == 'user_edit' && isset($form[$_birthdays_field->category]) || $form_id == 'user_register' && $_birthdays_field->register) {
$form[$_birthdays_field->category][$_birthdays_field->name]['#process'] = array(
'expand_birthdays_date' => array(),
);
$form[$_birthdays_field->category][$_birthdays_field->name]['#validate'] = array(
'birthdays_date_validate' => array(),
);
}
}
function expand_birthdays_date($element) {
if (empty($element['#value'])) {
$element['#value'] = array(
'day' => '',
'month' => '',
'year' => '',
);
}
$element = expand_date($element);
$element['month']['#options'] = array(
'' => '--',
) + $element['month']['#options'];
$element['day']['#options'] = array(
'' => '--',
) + $element['day']['#options'];
$element['year']['#options'] = array(
'' => '--',
) + drupal_map_assoc(range(date('Y'), 1900));
return $element;
}
function birthdays_date_validate($element) {
extract($element['#value']);
if (empty($month) || empty($year) || empty($day)) {
if ($element['#required']) {
form_error($element, t('!name field is required.', array(
'!name' => $element['#title'],
)));
}
elseif (!(empty($month) && empty($year) && empty($day))) {
form_error($element, t('The specified date is invalid.'));
}
}
elseif (!checkdate($month, $day, $year)) {
form_error($element, t('The specified date is invalid.'));
}
}
function _birthdays_send_admin_message($days = 1) {
global $_birthdays_field;
if ($frequency = variable_get('birthdays_remind', BIRTHDAYS_ADMIN_MAIL_DISABLED)) {
switch ($frequency) {
case BIRTHDAYS_ADMIN_MAIL_DAILY:
$period = t('Today');
break;
case BIRTHDAYS_ADMIN_MAIL_WEEKLY:
$period = t('This week');
break;
case BIRTHDAYS_ADMIN_MAIL_MONTHLY:
$period = t('This month');
break;
}
$accounts = birthdays_get_birthdays_by_days($days);
$message = t("@period, the following users are having their birthday:\n", array(
'@period' => $period,
));
foreach ($accounts as $uid) {
$account = user_load(array(
'uid' => $uid,
));
$account->{$_birthdays_field->name}['year'] = NULL;
$age = $account->age + !($account->{$_birthdays_field->name}['day'] == format_date(time(), 'custom', 'j') && $account->{$_birthdays_field->name}['month'] == format_date(time(), 'custom', 'n'));
$message .= "\n" . ($frequency == BIRTHDAYS_ADMIN_MAIL_DAILY ? '' : _birthdays_show_date($account->{$_birthdays_field->name}, $account) . ': ') . $account->name . ' (' . $age . ')';
}
if (count($accounts) > 0) {
$from = variable_get('site_mail', ini_get('sendmail_from'));
$to = $from;
$subject = t("Upcoming Birthdays");
drupal_mail('birthdays_admin_mail', $to, $subject, $message, $from);
watchdog('Birthdays', t('Sent birthday overview e-mail to admin.'), WATCHDOG_NOTICE, ' ');
}
}
}
function _birthdays_send_user_message() {
if (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) > BIRTHDAYS_USER_MAIL_NO) {
$accounts = birthdays_get_todays_birthdays();
$from = variable_get('site_mail', ini_get('sendmail_from'));
$subject = variable_get('birthdays_send_user_subject', t('Happy Birthday!'));
$message = variable_get('birthdays_send_user_message', t("Hey @name,\nHappy birthday!\nHope you have a great day!"));
foreach ($accounts as $uid) {
$account = user_load(array(
'uid' => $uid,
));
if (variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == BIRTHDAYS_USER_MAIL_YES || variable_get('birthdays_send_user', BIRTHDAYS_USER_MAIL_NO) == 2 && $account->birthdays_user_send_mail != BIRTHDAYS_USER_MAIL_USER_NO) {
$parsed_message = strtr($message, array(
'@name' => check_plain($account->name),
));
drupal_mail('birthdays_user_message', $account->name . ' <' . $account->mail . '>', $subject, $parsed_message, $from);
watchdog('Birthdays', t('Sent @name a birthday e-mail.', array(
'@name' => $account->name,
)), WATCHDOG_NOTICE, ' ');
}
}
}
}
function _birthdays_get_starsign($day, $month) {
switch ($month) {
case 1:
$starsign = $day < 20 ? 'capricorn' : 'aquarius';
break;
case 2:
$starsign = $day < 19 ? 'aquarius' : 'pisces';
break;
case 3:
$starsign = $day < 21 ? 'pisces' : 'aries';
break;
case 4:
$starsign = $day < 20 ? 'aries' : 'taurus';
break;
case 5:
$starsign = $day < 21 ? 'taurus' : 'gemini';
break;
case 6:
$starsign = $day < 22 ? 'gemini' : 'cancer';
break;
case 7:
$starsign = $day < 23 ? 'cancer' : 'leo';
break;
case 8:
$starsign = $day < 23 ? 'leo' : 'virgo';
break;
case 9:
$starsign = $day < 23 ? 'virgo' : 'libra';
break;
case 10:
$starsign = $day < 23 ? 'libra' : 'scorpio';
break;
case 11:
$starsign = $day < 23 ? 'scorpio' : 'sagittarius';
break;
case 12:
$starsign = $day < 22 ? 'sagittarius' : 'capricorn';
break;
}
return $starsign;
}
function _birthdays_get_date_fields() {
$options = array();
$result = db_query("SELECT fid, name FROM {profile_fields} WHERE type = 'date'");
while ($field = db_fetch_object($result)) {
$options[$field->fid] = $field->name;
}
return $options;
}
function _birthdays_get_field($fid) {
if (isset($fid)) {
$field = db_fetch_object(db_query("SELECT * FROM {profile_fields} WHERE fid = %d", $fid));
return empty($field) ? NULL : $field;
}
else {
return NULL;
}
}
function birthdays_get_starsign_image($starsign, $show = BIRTHDAYS_STARSIGN_OFF) {
$output = '';
if ($show > BIRTHDAYS_STARSIGN_OFF && !empty($starsign)) {
$output = '<img src="' . base_path() . drupal_get_path('module', 'birthdays') . '/starsigns/' . $starsign . '.gif" alt="' . t(ucfirst($starsign)) . '" />';
if ($show == BIRTHDAYS_STARSIGN_LINK) {
$output = '<a href="http://astrology.yahoo.com/astrology/general/dailyoverview/' . $starsign . '" target="_blank" title="' . t(ucfirst($starsign)) . '">' . $output . '</a>';
}
}
return $output;
}
function _birthdays_get_age($date) {
if (is_array($date)) {
extract($date);
return _birthdays_calculate_age($day, $month, $year);
}
else {
return NULL;
}
}
function _birthdays_calculate_age($day, $month, $year) {
if ($year && $month && $day) {
return format_date(time(), 'custom', 'Y') - $year - (format_date(time(), 'custom', 'nd') < $month . str_pad($day, 2, 0, STR_PAD_LEFT));
}
else {
return NULL;
}
}
function _birthdays_show_age($account) {
$age = NULL;
if (isset($account->age) && (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_NO || variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_USER && $account->birthdays_user_hide_year != BIRTHDAYS_HIDE_YEAR_USER_YES)) {
$age = $account->age;
}
return $age;
}
function _birthdays_show_date($date, $account, $format = 'small') {
if (is_array($date)) {
extract($date);
return _birthdays_show_date_2($day, $month, $year, $account, $format);
}
else {
return NULL;
}
}
function _birthdays_show_date_2($day, $month, $year, $account, $type = 'small') {
$output = '';
switch ($type) {
case 'medium':
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
break;
case 'small':
default:
$format = variable_get('date_format_short', 'm/d/Y - H:i');
}
if ($year && (variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_YES || variable_get('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_NO) == BIRTHDAYS_HIDE_YEAR_USER && $account->birthdays_user_hide_year == BIRTHDAYS_HIDE_YEAR_USER_YES)) {
$year = NULL;
}
$replace = array(
'd' => sprintf('%02d', $day),
'D' => NULL,
'j' => $day,
'm' => sprintf('%02d', $month),
'M' => map_month($month),
'Y' => $year,
'H:i' => NULL,
'G:i' => NULL,
'g:ia' => NULL,
'F' => t(gmdate('F', mktime(0, 0, 0, $month, 15, 2000))),
);
$output .= strtr($format, $replace);
$output = trim($output, '/ ,.:-');
return $output;
}
function birthdays_potx() {
$strings = array(
t('Aquarius'),
t('Pisces'),
t('Aries'),
t('Taurus'),
t('Gemini'),
t('Cancer'),
t('Leo'),
t('Virgo'),
t('Libra'),
t('Scorpio'),
t('Sagittarius'),
t('Capricorn'),
);
}