'birthdays', 'title' => t('Birthdays'), 'access' => user_access('access birthdays'), 'callback' => 'birthdays_page', 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'admin/settings/birthdays', 'title' => t('Birthdays'), 'description' => t('Set user birthday mail and toggle user mail, upcomming birthdays mail and more.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('birthdays_admin_settings'), 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * Implementation of hook_form(). */ function birthdays_admin_settings() { // Most date-formats that are useful $dates = array('Y-m-d', 'm/d/Y', 'd/m/Y', 'Y/m/d', 'M j Y', 'j M Y', 'Y M j', 'F j, Y', 'j F, Y', 'Y, F j', 'F j, Y', 'j F Y', 'Y, F j', 'j. F Y'); // Date settings: construct choices for user foreach ($dates as $f) { $datechoices[$f] = format_date(time(), 'custom', $f); } $form['birthdays_use_starsign'] = array( '#type' => 'radios', '#title' => t('Show star signs'), '#default_value' => variable_get('birthdays_use_starsign', 1), '#options' => array(1 => t('Yes'), 0 => t('No')), '#description' => t('Select whether the star signs should be enabled') ); $form['birthdays_date_format'] = array( '#type' => 'select', '#title' => t('Birthday date format'), '#default_value' => variable_get('birthdays_date_format', $dates[1]), '#options' => $datechoices, '#description' => t('The format of the birthday date display.') ); $form['birthdays_profile_category'] = array( '#type' => 'textfield', '#title' => t('Profile category'), '#default_value' => variable_get('birthdays_profile_category', t('My profile')), '#description' => t('In what profile category should the date of birth be placed? Default is on the main profile page, do not change if not sure..') ); $form['birthdays_required'] = array( '#type' => 'radios', '#title' => t('Input required'), '#default_value' => variable_get('birthdays_required', 0), '#options' => array(1 => t('Yes'), 0 => t('No')), '#description' => t('Are users required to fill in their birthday?') ); $form['birthdays_email'] = array( '#type'=>'fieldset', '#title' => t('E-mail user'), '#collapsible' => TRUE, ); $form['birthdays_email']['birthdays_send_user'] = array( '#type' => 'radios', '#title' => t('Send user e-mail on day of birth'), '#default_value' => variable_get('birthdays_send_user', 0), '#options' => array(1 => t('Yes'), 0 => t('No')), '#description' => t('Should users that have their birthday today receive an e-mail?') ); $form['birthdays_email']['birthdays_send_user_subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => variable_get('birthdays_send_user_subject', t('Happy Birthday!')), ); $form['birthdays_email']['birthdays_send_user_message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => variable_get('birthdays_send_user_message', t("Hey %name%,\n\n" . "Happy birthday!\n" . "Hope you have a great day!\n")), '#description' => t('%name% will be replaced for the firstname of the user.') ); $form['birthdays_remind'] = array( '#type' => 'radios', '#title' => t('Send upcoming birthdays'), '#default_value' => variable_get('birthdays_remind', 0), '#options' => array(1 => t('Yes'), 0 => t('No')), '#description' => t('Do you want a dayly e-mail containing all upcoming birthdays (for the next week)?') ); return system_settings_form($form); } /** * Implementation of hook_perm(). */ function birthdays_perm() { return array('access birthdays', 'edit DOB'); } /** * Implementation of hook_cron(). */ function birthdays_cron() { // Perform these actions just once per day if (variable_get('birthdays_last_cron', 0) < (time() - 3600*24)) { _birthdays_message(); variable_set('birthdays_last_cron', time()); } } /** * Implementation of hook_block * - Block lists N upcoming birthdays * - Block lists birthdays in N days * * @param $op the operation that is being requested. This defaults to 'list', which indicates that the method should * return which blocks are available. * @param $delta the specific block to display. This is actually the offset into an array. * @return one of two possibilities. The first is an array of available blocks. The other is an array containing a * */ function birthdays_block($op = 'list', $delta = 0) { $block = array(); if($op == 'list') { $block[0]['info'] = t('Birthdays Block: N birthdays'); $block[1]['info'] = t('Birthdays Block: N days'); } elseif($op == 'configure') { // Configuration settings for block if( $delta == 0 ) { $form["birthday_past"] = array( '#type' => 'textfield', '#title' => t("Number of birthdays"), '#default_value' => variable_get("birthdays_list_number", 6), '#size' => 2, '#maxlength' => 2, '#description' => t("Number of upcoming birthdays to list in block"),); return $form; } if( $delta == 1 ) { $form["birthday_past"] = array( '#type' => 'textfield', '#title' => t("Number of days"), '#default_value' => variable_get("birthdays_days_number", 7), '#size' => 2, '#maxlength' => 2, '#description' => t("Number of days to list the birthdays of in block"),); return $form; } } else { // Render block content if (user_access('access birthdays')) { // Get birthdays based on block-type; if( $delta == 0 ) { $birthdays = _get_upcoming_birthdays(variable_get("birthdays_list_number", 6)); } else if( $delta == 1 ) { $birthdays = _get_upcoming_birthdays_days(variable_get("birthdays_days_number", 7)); } $blockContent = ''; foreach ($birthdays as $user) { $blockContent .= ''; } $blockContent .= '
' . l($user['name'], 'user/' . $user['uid']) . ' ' . $user['day'] . ' ' . $user['month'] . '
'; $blockContent .= ''; $block['subject'] = t('Upcoming Birthdays'); $block['content'] = $blockContent; } } return $block; } /** * Implementation of hook_user(). */ function birthdays_user($op, &$edit, &$user, $category = null) { switch($op) { case 'update': if($category != 'account') { break; } $year = $edit['DOB']['year']; $month = $edit['DOB']['month']; $day = $edit['DOB']['day']; $result = db_query("SELECT uid FROM {dob} WHERE uid = '%s' LIMIT 1", $user->uid); if (db_num_rows($result) == 0) { db_query("INSERT INTO {dob} (uid, birthday) VALUES ('%s', '%s-%s-%s');", $user->uid,$year,$month,$day); } else { db_query("UPDATE {dob} SET birthday = '%s-%s-%s' WHERE uid = '%s' LIMIT 1", $year,$month,$day,$user->uid); } break; case 'view': $queryResult = db_query("SELECT day(birthday) as d, month(birthday) as m, year(birthday) as y, DATE_FORMAT( NOW( ) , '%%Y' ) - DATE_FORMAT( birthday, '%%Y' ) - ( DATE_FORMAT( NOW( ) , '%%m%%d' ) < DATE_FORMAT( birthday, '%%m%%d' ) ) AS age FROM {dob} WHERE uid = '%s' LIMIT 1", $user->uid); if (db_num_rows($queryResult)>0) { $birthday = db_fetch_object($queryResult); $content = ''; if( variable_get('birthdays_use_starsign', true) ) { $content = _get_starsign($birthday->d, $birthday->m); } $content .= format_date(mktime(0,0,0,$birthday->m,$birthday->d,$birthday->y),'custom', variable_get('birthday_date_format','j F, Y')); $content .= " ({$birthday->age})"; $items[variable_get('birthdays_profile_category', t('My profile'))][] = array('title' => t('Birthday'),'value' => $content); return $items; } case 'form': if($category != 'account') { break; } if (user_access('edit DOB') || user_access('administer users')) { $queryResult = db_query("SELECT day(birthday) as d, month(birthday) as m, year(birthday) as y FROM {dob} WHERE uid = '%s' LIMIT 1", $user->uid); $birthday = db_fetch_object($queryResult); $dob = array('day' => $birthday->d, 'month' => $birthday->m, 'year' => $birthday->y); $cat = variable_get('birthdays_profile_category', t('My profile')); $form[$cat] = array('#type' => 'fieldset', '#title' => $cat); $form[$cat]['DOB'] = array('#type' => 'date', '#title' => t('Date of birth'), '#default_value' => $dob, '#description' => t('Enter your date of birth'), '#required' => variable_get('birthdays_required', false)); return $form; } } } /****************************************************************************** * Pages */ /** * VIEW PAGE */ function birthdays_page() { if (user_access('access birthdays')) { drupal_set_title(t('User Birthdays')); if( variable_get('birthdays_use_starsign', true) && arg(1) && $dates = _get_starsign_dates(arg(1)) ){ $result = db_query("SELECT u.uid, u.name, u.picture, month(birthday) as m, day(birthday) as d, year(birthday) as y, DATE_FORMAT( NOW( ) , '%%Y' ) - DATE_FORMAT( birthday, '%%Y' ) - ( DATE_FORMAT( NOW( ) , '%%m%%d' ) < DATE_FORMAT( birthday, '%%m%%d' ) ) AS age FROM {dob}, {users} u WHERE {dob}.uid=u.uid AND (( month(birthday) = %d AND day(birthday) >= %d) OR (month(birthday) = %d AND day(birthday) < %d)) ORDER BY m, d", $dates[0],$dates[1],$dates[2],$dates[3]); } else { $result = db_query("SELECT u.uid, u.name, u.picture, month(birthday) as m, day(birthday) as d, year(birthday) as y, DATE_FORMAT( NOW( ) , '%%Y' ) - DATE_FORMAT( birthday, '%%Y' ) - ( DATE_FORMAT( NOW( ) , '%%m%%d' ) < DATE_FORMAT( birthday, '%%m%%d' ) ) AS age FROM {dob}, {users} u WHERE {dob}.uid=u.uid ORDER BY m, d"); } $header = array(t('User'), t('Birthday'), t('Age')); if( variable_get('birthdays_use_starsign', true) ) { $header[] = t('Starsign'); } if( variable_get('user_pictures', false) ) { $header[] = ''; } $rows = array(); $i = 0; while ($birthday = db_fetch_object($result)) { $i++; $DOB = format_date(mktime(0,0,0,$birthday->m,$birthday->d,$birthday->y),'custom', variable_get('birthday_date_format','j F, Y')); $age = $birthday->age; if( variable_get('birthdays_use_starsign', true) ) { $starsign = _get_starsign($birthday->d, $birthday->m); } $account = user_load(array('uid' => $birthday->uid)); $picture = theme_user_picture($account); $rows[$i] = array(l($birthday->name, 'user/'.$birthday->uid), $DOB, $age); if( variable_get('birthdays_use_starsign', true) ) { $rows[$i][] = $starsign; } if( variable_get('user_pictures', false) ) { $rows[$i][] = $picture; } } $content .= theme("table", $header, $rows); print theme('page', $content); } else { drupal_access_denied(); } } /****************************************************************************** * Functions */ /** * _get_upcoming_birthdays() * * @param $limit Number of users to fetch from database * @return Returns an array of users for displaying upcoming birthdays * * - Returns an array of users that have birthdays upcoming up to a limit */ function _get_upcoming_birthdays($limit = 6) { $result = db_query("SELECT {dob}.uid, {users}.name, month({dob}.birthday) as m, day({dob}.birthday) as d FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND DAYOFYEAR({dob}.birthday) >= DAYOFYEAR(curdate()) order by m, d limit 0,%s", $limit); $birthdays = array(); while ($user = db_fetch_object($result)) { $month = strftime('%b',mktime(0,0,0,$user->m,1,2000)); $birthdays[] = array('name' => $user->name, 'uid' => $user->uid, 'day' => $user->d, 'month' => $month); } if (db_num_rows($result) < $limit) { // If less than $limit results returned, look at next year (but ignore current year) $result = db_query("SELECT {dob}.uid, {users}.name, month({dob}.birthday) as m, day({dob}.birthday) as d FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND DAYOFYEAR({dob}.birthday) >= 0 AND DAYOFYEAR({dob}.birthday) < DAYOFYEAR(curdate()) ORDER BY m, d limit 0,%s", ($limit - db_num_rows($result))); while ($user = db_fetch_object($result)) { $month = strftime('%b',mktime(0,0,0,$user->m,1,2000)); $birthdays[] = array('name' => $user->name, 'uid' => $user->uid, 'day' => $user->d, 'month' => $month); } } return $birthdays; } function _get_upcoming_birthdays_days($days = 7) { $days--; $result = db_query("SELECT {dob}.uid, {users}.name, month({dob}.birthday) as m, day({dob}.birthday) as d FROM {dob}, {users} WHERE {users}.uid = {dob}.uid AND ( (MONTH(birthday) = MONTH(CURDATE()) AND DAY(birthday) >= DAYOFMONTH(CURDATE()) AND DAY(birthday) <= (DAY(CURDATE())+%s)) OR (MONTH(birthday) = MONTH(ADDDATE(CURDATE(),%s)) AND DAY(birthday) <= DAY(ADDDATE(CURDATE(),%s))) ) order by m, d", $days, $days, $days); $birthdays = array(); while ($user = db_fetch_object($result)) { $month = strftime('%b',mktime(0,0,0,$user->m,1,2000)); $birthdays[] = array('name' => $user->name, 'uid' => $user->uid, 'day' => $user->d, 'month' => $month); } return $birthdays; } /** * Returns starsign of user * * @param $dob_day The day. * @param $dob_month The month. * @return A picture link to the list of the users of the same sign */ function _get_starsign($dob_day, $dob_month) { switch($dob_month) { case 1: if ($dob_day < 20) {$starsign = 'capricorn';} else {$starsign = 'aquarius';} break; case 2: if ($dob_day < 20) {$starsign = 'aquarius';} else {$starsign = 'pisces';} break; case 3: if ($dob_day < 21) {$starsign = 'pisces';} else {$starsign = 'aries';} break; case 4: if ($dob_day < 20) {$starsign = 'aries';} else {$starsign = 'taurus';} break; case 5: if ($dob_day < 21) {$starsign = 'taurus';} else {$starsign = 'gemini';} break; case 6: if ($dob_day < 22) {$starsign = 'gemini';} else {$starsign = 'cancer';} break; case 7: if ($dob_day < 23) {$starsign = 'cancer';} else {$starsign = 'leo';} break; case 8: if ($dob_day < 23) {$starsign = 'leo';} else {$starsign = 'virgo';} break; case 9: if ($dob_day < 23) {$starsign = 'virgo';} else {$starsign = 'libra';} break; case 10: if ($dob_day < 23) {$starsign = 'libra';} else {$starsign = 'scorpio';} break; case 11: if ($dob_day < 22) {$starsign = 'scorpio';} else {$starsign = 'sagittarius';} break; case 12: if ($dob_day < 22) {$starsign = 'sagittarius';} else {$starsign = 'capricorn';} break; } $link = ''; $link .= ''; $link .= ''; return $link; } function _get_starsign_dates($starsign) { switch ($starsign ) { case t('capricorn'): $ret = array(12,22,1,20); break; case t('aquarius'): $ret = array(1,20,2,20); break; case t('pisces'): $ret = array(2,20,3,21); break; case t('aries'): $ret = array(3,21,4,20); break; case t('taurus'): $ret = array(4,20,5,21); break; case t('gemini'): $ret = array(5,21,6,22); break; case t('cancer'): $ret = array(6,22,7,23); break; case t('leo'): $ret = array(7,23,8,23); break; case t('virgo'): $ret = array(8,23,9,23); break; case t('libra'): $ret = array(9,23,10,23); break; case t('scorpio'): $ret = array(10,23,11,22); break; case t('sagittarius'): $ret = array(11,22,12,22); break; default: $ret = false; } return $ret; } /** * Sends e-mail to administrator as reminder and to users on birthday and e-mail users who's birthdays it is */ function _birthdays_message() { if (variable_get('birthdays_remind', true)) { $users = db_query("SELECT uid, MONTH(birthday) as m, DAY(birthday) as d FROM {dob} WHERE ( (MONTH(birthday) = MONTH(CURDATE()) AND DAY(birthday) >= DAYOFMONTH(CURDATE()) AND DAY(birthday) <= (DAY(CURDATE())+%s)) OR (MONTH(birthday) = MONTH(ADDDATE(CURDATE(),%s)) AND DAY(birthday) <= DAY(ADDDATE(CURDATE(),%s))) ) order by m, d",6,6,6); $message = t("The following users birthdays are coming up in 7 days:"); while ($user = db_fetch_object($users)) { $account = user_load(array('uid' => $user->uid)); $message .= "\n" . $account->name; } if (db_num_rows($users) != 0) { // Get site e-mail to send reminder to and from $from = variable_get('site_mail', ini_get('sendmail_from')); $to = $from; $subject = t("Upcoming Birthdays"); drupal_mail('birthdays_upcoming_mail', $to, $subject, $message, $from ); watchdog('Birthdays', 'sent birthday overview e-mail', WATCHDOG_NOTICE, ' '); } } if (variable_get('birthdays_send_user', true)) { $users = db_query("SELECT uid FROM {dob} WHERE DATE_FORMAT(birthday, '%%m%%d') = DATE_FORMAT(NOW(), '%%m%%d')"); while ($user = db_fetch_object($users)) { $account = user_load(array('uid' => $user->uid)); $message = variable_get('birthdays_send_user_message', "Hey %name%, Happy birthday! Hope you have a great day! Take care,"); $message = str_replace('%name%',$account->name, $message); $subject = variable_get('birthdays_send_user_subject', 'Happy Birthday!'); $from = variable_get('site_mail', ini_get('sendmail_from')); if (module_exists('postcard')) { // If postcard module installed, send postcard $postcard = array('field_sndr' => $from, 'field_from' => $from, 'field_nid' => 49, 'field_rcpt' => $account->name, 'field_to' => $account->mail, 'field_body' => $message); $postid = postcard_send($postcard); watchdog('Birthdays', $account->name . ' sent birthday postcard', WATCHDOG_NOTICE, l('postcard', 'postcard/45/'. $postid)); } else { // Send e-mail drupal_mail('birthdays_user_mail', $account->name . '<' . $account->mail . '>', $subject, $message, $from ); watchdog('Birthdays', $account->name . ' sent birthday e-mail', WATCHDOG_NOTICE, ' '); } } } } ?>