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_init() {
global $_birthdays_field;
$_birthdays_field = _birthdays_get_field(variable_get('birthdays_field_id', NULL));
}
function birthdays_help($path, $arg) {
global $_birthdays_field;
switch ($path) {
case 'admin/help#birthdays':
$output = '<p>' . t("The Birthdays module allows a user to put in their birthday and display it in their profile, on a separate Birthdays page and in a block. It also has the option to send out e-mails on users's birthdays, and to notify the administrator periodically about upcoming birthdays via e-mail.") . '</p>';
$output .= '<p>' . t('Some basic information about this module can be found below. For more information, support questions, feature requests and bug reports please visit the <a href="@project_url">Birthdays project page</a> and the <a href="@documentation_url">online documentation</a>.', array(
'@project_url' => 'http://drupal.org/project/birthdays',
'@documentation_url' => 'http://drupal.org/node/315658',
)) . '</p>';
$output .= '<h2>' . t('Configuring the module') . '</h2>';
$output .= '<p>' . t('Configuring the module is done on a number of pages.') . '</p>';
$output .= '<h3>' . t('Birthdays administration pages') . '</h3>';
$output .= '<p>' . t('The <a href="@birthdays_admin">Birthdays administration pages</a> are where the actual features of the birthdays module are set. Some of the options are described below.', array(
'@birthdays_admin' => url('admin/settings/birthdays'),
)) . '</p>';
$output .= '<ul><li><strong>' . t('Profile field') . '</strong>: ' . t('here you set which "date" field of the Profile module you want the Birthdays module to use.') . '</li>';
$output .= '<li><strong>' . t('Show star signs') . '</strong>: ' . t('select whether you want to display the star sign icons on the profile and on the Birthdays page, and, if so, whether it should link to Yahoo Horoscopes.') . '</li>';
$output .= '<li><strong>' . t('Hide year and age') . '</strong>: ' . t("some sites might want to protect the user's privacy by hiding their age and year of birth. The options are 'No', 'Yes' and 'User optional, defaults to No', where the latter gives the user the option to select whether he or she wants this information to be hidden. If enabled it still requires them to put the full date of birth in, but only the day and month will be displayed.") . '</li>';
$output .= '<li><strong>' . t('Set Birthdays page settings') . '</strong>: ' . t('this influences how the listing of the Birthdays page is shown. Sorted by user name of date of birth (year not included) and users without a birthday shown or not.') . '</li>';
$output .= '<li><strong>' . t('Show filter options') . '</strong>: ' . t("determine whether the buttons to filter by specific month and/or year should be displayed. When 'Hide year and age' is set to 'Yes', filtering by year is not possible.") . '</li>';
$output .= '<li><strong>' . t('Send upcoming birthdays to admin') . '</strong>: ' . t("this one has 4 values 'Disabled', 'Dayly', 'Weekly, on the first day of the week' and 'Monthly'. This will send an e-mail to your site e-mail address at the given intervals, listing users that will have their birthday in that period. The first day of the week is controlled by the <a href=\"@date_settings\">date and time settings</a>.", array(
'@date_settings' => url('admin/settings/date-time'),
)) . '</li>';
$output .= '<li><strong>' . t('Send user e-mail on day of birth') . '</strong>: ' . t("set whether users should receive an e-mail when they are having the birthday. Either 'No', 'Yes' or 'User optional, 'Yes' by default', where the latter leaves the decision up to the user.") . '</li></ul>';
$output .= '<h3>' . t('Permissions') . '</h3>';
$output .= '<p>' . t('To allow users to view birthdays of other people, the "access birthdays" <a href="@permissions_page">permissions</a> needs to be set for the appropriate role. This allows them to access the <a href="@birthdays_page">Birthdays page</a>, see activated blocks with birthdays and view the birthdays of users in the profiles.', array(
'@birthdays_page' => url('birthdays'),
'@permissions_page' => url('admin/user/permissions', array(
'fragment' => 'module-birthdays',
)),
)) . '</p>';
$output .= '<h3>' . t('Profile field settings') . '</h3>';
$output .= '<p>' . t("Several things can be adjusted to the assigned profile field. To do so please visit the !profile_admin, and edit the field in question or add one if you haven't done so already. The following settings require extra attention.", array(
'!profile_admin' => l('Profile settings', 'admin/user/profile'),
)) . '</p>';
$output .= '<ul><li><strong>' . t('The user must enter a value') . '</strong>: ' . t('force the user to fill in their date of birth. A once set birthday can not be deleted.') . '</li>';
$output .= '<li><strong>' . t('Visible in user registration form') . '</strong>: ' . t('give new users the option to add their date of birth when registering to your website. If combined with the option above, they can not register without putting their date of birth.') . '</li>';
$output .= '<li><strong>' . t('Visibility') . '</strong>: ' . t("this will influence the visibility of the field as described there, but the behavior might become erratic if not set to 'Public field, content shown on profile page but not used on member list pages'. So it is recommended to leave this as is.") . '</li></ul>';
$output .= '<p>' . t('The other options on this page behave as described on the settings page.') . '</p>';
$output .= '<h3>' . t('Date format') . '</h3>';
$output .= '<p>' . t('The way the birthdays are formatted (e.g. <em>11 may 1934</em> or <em>5/11/1934</em>) is controlled by the <a href="@date_settings_page">date and time settings</a>. The Birthdays module uses the short date format as basis for displaying days and months in the blocks and the medium date format is used in the profile and on the Birthdays page.', array(
'!date_settings_page' => url('admin/settings/date-time'),
)) . '</p>';
$output .= '<h2>' . t('Synchronizing') . '</h2>';
$output .= '<p>' . t('The birthdays are saved to two different database tables. This is because the profile module saves the dates in a format which limits the ability to perform calculations on the dates. These two tables need to be in sync with each other.') . '</p>';
$output .= '<p>' . t('Normally this is the case, but sometimes not. For example when you already collected birthdays with the Profile module, but later decided to switch to the birthdays module. Or when you accidentally (on intentionally) completely uninstalled the profile module, but left birthdays module merely disabled. To perform the synchronization you can use the <a href="@sync_pages">synchronization form</a>.', array(
'@sync_page' => url('admin/settings/birthdays/sync'),
)) . '</p>';
$output .= '<p>' . t('In the first case you need to copy the Profile data to the (most likely empty) Birthdays table, which is done with the top button. In the latter you need the copy the birthdays in the Birthdays table back to the Profile module with the bottom button.') . '</p>';
$output .= '<p>' . t('Please note that with a large amount of users this might take some time.') . '</p>';
return $output;
case 'admin/settings/birthdays':
return t('Use this page to alter the settings of the Birthdays module.');
}
}
function birthdays_menu() {
$items['admin/settings/birthdays'] = array(
'title' => 'Birthdays',
'description' => 'Set user birthday mail and toggle user mail, upcoming birthdays mail and more.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'birthdays_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'birthdays.admin.inc',
);
$items['admin/settings/birthdays/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/birthdays/sync'] = array(
'title' => 'Synchronize',
'description' => '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.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'birthdays_sync_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'birthdays.sync.inc',
);
$items['birthdays'] = array(
'title' => 'Birthdays',
'description' => 'List the birthdays of all users.',
'page callback' => 'birthdays_view_page',
'access arguments' => array(
'access birthdays',
),
'type' => MENU_SUGGESTED_ITEM,
'file' => 'birthdays.page.inc',
);
return $items;
}
function birthdays_theme() {
return array(
'birthdays_block' => array(
'arguments' => array(
'uids' => array(),
'amount' => 0,
'block_type' => NULL,
),
'template' => 'birthdays-block',
),
'birthdays_page' => array(
'arguments' => array(
'accounts' => array(),
'filter_month' => NULL,
'filter_year' => NULL,
),
'file' => 'birthdays.page.inc',
'template' => 'birthdays-page',
),
);
}
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) {
module_load_include('inc', 'birthdays', 'birthdays.mail');
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)) {
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':
$form = array();
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,
);
break;
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,
);
break;
}
$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);
$uids = birthdays_get_birthdays_by_days($amount);
if (count($uids) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {
$block['subject'] = t('Upcoming Birthdays');
$block['content'] = theme('birthdays_block', $uids, $amount, $delta);
}
break;
case 'by_birthdays':
$amount = variable_get("birthdays_block_number_by_birthdays", 6);
$uids = birthdays_get_birthdays($amount);
if (count($uids) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {
$block['subject'] = t('Upcoming Birthdays');
$block['content'] = theme('birthdays_block', $uids, $amount, $delta);
}
break;
}
return $block;
}
}
}
}
function template_preprocess_birthdays_block(&$variables) {
global $_birthdays_field;
$variables['no_birthdays'] = TRUE;
$variables['show_starsigns'] = (bool) variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF);
if (!empty($variables['uids'])) {
$row = 0;
$variables['no_birthdays'] = FALSE;
foreach ($variables['uids'] as $uid) {
$account = user_load(array(
'uid' => $uid,
));
$birthdays[$row]['account'] = $account;
$birthdays[$row]['username'] = theme('username', $account);
$account->age = $account->age + !($account->{$_birthdays_field->name}['day'] == format_date(time(), 'custom', 'j') && $account->{$_birthdays_field->name}['month'] == format_date(time(), 'custom', 'n'));
$birthdays[$row]['age'] = _birthdays_show_age($account);
$birthdays[$row]['show_age'] = isset($birthdays[$row]['age']);
$birthdays[$row]['starsign'] = birthdays_get_starsign_image($account->birthdays_starsign, variable_get('birthdays_show_starsign', BIRTHDAYS_STARSIGN_OFF));
$account->{$_birthdays_field->name}['year'] = NULL;
$birthdays[$row]['date'] = _birthdays_show_date($account->{$_birthdays_field->name}, $account);
$row++;
}
$variables['birthdays'] = $birthdays;
}
$variables['more'] = '<div class="more-link">' . l(t('more'), 'birthdays', array(
'attributes' => array(
'title' => t('Show all birthdays.'),
),
)) . '</div>';
}
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 && drupal_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 && drupal_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;
}
$count_rows = 0;
while ($account = db_fetch_object($result)) {
$birthdays[] = $account->uid;
$count_rows++;
}
if ($count_rows < $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 - $count_rows);
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 - $count_rows);
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);
break;
}
}
function birthdays_load_user(&$account) {
global $_birthdays_field;
profile_load_profile($account);
if (!empty($account->{$_birthdays_field->name})) {
$account->age = _birthdays_get_age($account->{$_birthdays_field->name});
}
}
function birthdays_save_user(&$edit, &$account, $category) {
global $_birthdays_field;
if (!empty($_birthdays_field->name) && 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 {
$edit['birthdays_starsign'] = '';
unset($edit[$_birthdays_field->name]);
}
}
}
function birthdays_profile_alter(&$account) {
global $_birthdays_field;
if (!isset($_birthdays_field)) {
return;
}
if (isset($_birthdays_field) && array_key_exists($_birthdays_field->category, $account->content) && array_key_exists($_birthdays_field->name, $account->content[$_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>';
}
$account->content[$_birthdays_field->category][$_birthdays_field->name]['#value'] = $starsign . _birthdays_show_date($account->{$_birthdays_field->name}, $account, 'medium') . $age;
}
else {
unset($account->content[$_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 || module_exists('onepageprofile') && $category == 'account') {
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_USER_MAIL_USER_NO,
'#weight' => 2,
'#prefix' => '<div class="birthdays-container">',
'#suffix' => '</div>',
);
}
}
return $form;
}
function birthdays_form_alter(&$form, &$form_state, $form_id) {
global $_birthdays_field;
if ($form_id == 'user_profile_form' && isset($form[$_birthdays_field->category]) || $form_id == 'user_register' && $_birthdays_field->register) {
$form[$_birthdays_field->category][$_birthdays_field->name]['#process'] = array(
'expand_birthdays_date',
);
$form[$_birthdays_field->category][$_birthdays_field->name]['#element_validate'] = array(
'birthdays_date_validate',
);
}
}
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) {
if (empty($element['#value'])) {
return;
}
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_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($starsign) . '" />';
if ($show == BIRTHDAYS_STARSIGN_LINK) {
$output = '<a href="http://astrology.yahoo.com/astrology/general/dailyoverview/' . $starsign . '" target="_blank" title="' . t($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_YES)) {
$year = NULL;
}
$replace = array(
'd' => sprintf('%02d', $day),
'D' => NULL,
'j' => $day,
'm' => sprintf('%02d', $month),
'M' => map_month($month),
'Y' => $year,
'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_mail_text($key, $language = NULL, $variables = array()) {
$langcode = isset($language) ? $language->language : NULL;
if ($text = variable_get('birthdays_send_user_' . $key, FALSE)) {
return strtr($text, $variables);
}
else {
switch ($key) {
case 'subject':
return t('Happy Birthday, !username!', $variables, $langcode);
case 'message':
return t("Hey !username,\n\nHappy birthday!\nWe hope you have a great day\n\nThe !site-team\n!uri_brief", $variables, $langcode);
}
}
}