############################################################## 
## MOD Title: [Birthdays 2.0.1] Topic calendar Add-on
## MOD Author: TerraFrost < terrafrost@phpbb.com > (Jim Wigginton) http://www.frostjedi.com/terra/wordpress/
## MOD Description: Adds a Birthday field to v1.0.1 of Ptirhiik's Topic calendar MOD.
## MOD Version: 1.0.0
##
## Installation Level: Easy
## Installation Time: 7 Minutes
##
## Files To Edit: 3
##      language/lang_english/lang_main.php
##      templates/subSilver/calendar_overview_profil.tpl
##      includes/functions_calendar.php
##
## Included Files: 0
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## For security purposes, please check: http://www.phpbb.com/mods/
## for the latest version of this MOD. Although MODs are checked
## before being allowed in the MODs Database there is no guarantee
## that there are no security problems within the MOD. No support
## will be given for MODs not found within the MODs Database which
## can be found at http://www.phpbb.com/mods/
##############################################################
## Author Notes:
##
## Intended for this MOD:
## http://www.phpbb.com/phpBB/viewtopic.php?t=150857
##
############################################################## 
## MOD History: 
##
##   2006-04-20 - Version 1.0.0
##      - initial release
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

#
#-----[ OPEN ]------------------------------------------
#
language/lang_english/lang_main.php
#
#-----[ FIND ]------------------------------------------
#
$lang['View_Birthdays'] = 'Happy Birthday!';
#
#-----[ BEFORE, ADD ]-----------------------------------
#
$lang['Age'] = 'Age';
#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/calendar_overview_profil.tpl
#
#-----[ FIND ]------------------------------------------
#
			<td class="row2">{AVATAR}</td>
			<td class="row1" width="100%" align="center"><span class="gen"><b>{L_AGE}:</b>&nbsp;{AGE}</span></td>
#
#-----[ REPLACE WITH ]----------------------------------
#
			<td class="row2">{AVATAR}</td>
			<td class="row1" width="100%" align="center">
				<!-- BEGIN agerow -->
				<span class="gen"><b>{L_AGE}:</b>&nbsp;{AGE}</span>
				<!-- END agerow -->
			</td>
#
#-----[ OPEN ]------------------------------------------
#
includes/functions_calendar.php
#
#-----[ FIND ]------------------------------------------
#
function get_event_PCP_birthday(&$events, &$number, $start_date, $end_date, $limit=false, $start=0, $max_limit=-1)
{
	global $template, $lang, $images, $userdata, $board_config, $db, $phpbb_root_path, $phpEx;
	global $tree;

	// init results
	$number = 0;
	if ($max_limit < 0)
	{
		$max_limit = $board_config['topics_per_page'];
	}

	// add birthday events (only with Profile Control Panel) for logged people eyes
	if ($board_config['calendar_birthday'] && isset($lang['Happy_birthday']) && isset($userdata['user_birthday']) && ($userdata['user_id'] != ANONYMOUS))
	{
		// get start month
		$sql_where = '';
		$work_date = $start_date;
		while ( intval(date('Ym', $work_date)) <= intval(date('Ym', $end_date)) )
		{
			$start_month = date('md', $work_date );
			$end_month = date('m', $work_date) . '99';
			if ( intval(date('Ym', $work_date)) == intval(date('Ym', $end_date)) )
			{
				$end_month = date('md', $end_date);
			}
			$sql_where .= !empty($sql_where) ? ' OR' : '';
			$sql_where .= " ( RIGHT(u.user_birthday, 4) >= $start_month AND RIGHT(u.user_birthday, 4) < $end_month )";

			// go to next month
			$work_year = intval(date('Y', $work_date));
			$work_month = intval(date('m', $work_date));
			$work_month++;
			if ($work_month > 12)
			{
				$work_month = 1;
				$work_year++;
			}
			$work_date = mktime( 0,0,0, $work_month, 01, $work_year );
		}

		// select now profiles
		if (!empty($sql_where))
		{
			$user_id = $userdata['user_id'];
			$sql = "SELECT u.*, 
							(CASE WHEN b.buddy_ignore = 0 THEN 1 ELSE 0 END) as user_friend,
							i.buddy_ignore AS user_ignore,
							b.buddy_visible AS user_visible
						FROM ((" . USERS_TABLE . " AS u 
						LEFT JOIN " . BUDDYS_TABLE . " AS b	ON b.user_id=u.user_id AND b.buddy_id=$user_id)
						LEFT JOIN " . BUDDYS_TABLE . " AS i ON i.user_id=$user_id AND i.buddy_id=u.user_id)
						WHERE u.user_id <> " . ANONYMOUS . " 
							AND u.user_birthday <> 0 
							AND u.user_birthday <> '' 
							AND ( $sql_where )
						ORDER BY username";
			if ( !$result = $db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not read user table to get birthday today info', '', __LINE__, __FILE__, $sql);
			}

			// get the number of occurences
			$number = $db->sql_numrows($result);

			// if limit per page asked, limit the number of results
			if ($limit)
			{
				$sql .= " LIMIT $start, $max_limit";
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Could not obtain topics information', '', __LINE__, __FILE__, $sql);
				}
			}

			// read users
			while ($row = $db->sql_fetchrow($result))
			{
				// user info
				$user_id		= $row['user_id'];
				$username		= $row['username'];
				$user_birthday	= $row['user_birthday'];

				// get user relational status
				$ignore			= $row['user_ignore'];
				$friend			= $row['user_friend'];
				$always_visible = $row['user_visible'];

				// get the status of each info
				$real_display	= ( !$ignore && $userdata['user_allow_real'] && $row['user_allow_real'] && ( ($row['user_viewreal'] == YES) || ( ($row['user_viewreal'] == FRIEND_ONLY) && $friend ) ) );

				// take care of admin status
				if ( is_admin($userdata) || ($user_id == $userdata['user_id']) )
				{
					$real_display = true;
				}

				if ($real_display)
				{
					$txt_class = get_user_level_class($row['user_level'], 'genmed', $row);
					if ($row['user_allow_viewonline'] != YES)
					{
						$username = '<i>' . $username . '</i>';
					}
					$username_link = append_sid($phpbb_root_path . "./profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$user_id");

					$event_month	= intval(substr($user_birthday, 4, 2));
					$event_day		= intval(substr($user_birthday, 6, 2));
					$start_month	= intval(date('m', $start_date));
					$event_year		= intval(date('Y', $start_date));
					if ($event_month < $start_month) $event_year++;
					$event_time = mktime( 0,0,0, $event_month, $event_day, $event_year );

					// build the overview
					$sav_tpl = $template->_tpldata;
					$det_handler = '_overview_profil_' . $user_id;
					$template->set_filenames(array(
						$det_handler => 'calendar_overview_profil.tpl')
					);

					$age = $event_year - intval(substr($user_birthday, 0, 4));
					if ( intval(substr($user_birthday, 4, 4)) > intval(date('md', $event_time)) ) $age--;
					if ($age <= 0) $age = '';

					// avatar
					$avatar_display = ( $userdata['user_viewavatar'] && $row['user_allowavatar'] );
					if ( is_admin($userdata)|| ($view_user_id == $user_id) )
					{
						$avatar_display = true;
					}
					$avatar = '';
					if ( $avatar_display && $row['user_avatar_type'] )
					{
						switch( $row['user_avatar_type'] )
						{
							case USER_AVATAR_UPLOAD:
								$avatar = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $row['user_avatar'] . '" alt="" border="0" />' : '';
								break;
							case USER_AVATAR_REMOTE:
								$avatar = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $row['user_avatar'] . '" alt="" border="0" />' : '';
								break;
							case USER_AVATAR_GALLERY:
								$avatar = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $row['user_avatar'] . '" alt="" border="0" />' : '';
								break;
						}
					}

					$template->assign_vars(array(
						'L_TITLE'		=> $lang['Happy_birthday'],
						'L_USERNAME'	=> $username,
						'TXT_CLASS'		=> $txt_class,
						'L_AGE'			=> $lang['Age'],
						'AVATAR'		=> $avatar,
						'AGE'			=> $age,
						)
					);

					$template->assign_var_from_handle('_calendar_overview', $det_handler);
					$message = $template->_tpldata['.'][0]['_calendar_overview'];
					$template->_tpldata = $sav_tpl;

					// remove \n remaining from the template
					$message = preg_replace("/[\n\r]{1,2}/", '', $message);

					// store only the new values
					$new_row = array();
					$new_row['event_id']				= POST_USERS_URL . $user_id;

					$new_row['event_author_id']			= $user_id;
					$new_row['event_author']			= $username;
					$new_row['event_time']				= $event_time;

					$new_row['event_last_author_id']	= '';
					$new_row['event_last_author']		= '';
					$new_row['event_last_time']			= '';

					$new_row['event_replies']			= '';
					$new_row['event_views']				= '';
					$new_row['event_type']				= POST_BIRTHDAY;
					$new_row['event_vote']				= '';
					$new_row['event_status']			= '';
					$new_row['event_moved_id']			= '';
					$new_row['event_last_id']			= '';
					$new_row['event_forum_id']			= '';
					$new_row['event_forum_name']		= '';

					$new_row['event_icon']				= '';
					$new_row['event_title']				= $username;
					$new_row['event_short_title']		= $username;
					$new_row['event_message']			= $message;
					$new_row['event_calendar_time']		= $event_time;
					$new_row['event_calendar_duration']	= '';
					$new_row['event_link']				= $username_link;
					$new_row['event_txt_class']			= $txt_class;
					$new_row['event_type_icon']			= '<img src="' . $images['icon_tiny_profile'] . '" border="0" align="absbottom" hspace="2" />';

					$events[] = $new_row;
				}
			}
		}
	}
}
#
#-----[ REPLACE WITH ]----------------------------------
#
function get_event_PCP_birthday(&$events, &$number, $start_date, $end_date, $limit=false, $start=0, $max_limit=-1)
{
	global $template, $lang, $images, $userdata, $board_config, $db, $phpbb_root_path, $phpEx;
	global $tree;

	// init results
	$number = 0;
	if ($max_limit < 0)
	{
		$max_limit = $board_config['topics_per_page'];
	}

	$sql = "SELECT user_id, username, user_birthday, user_avatar, user_avatar_type 
		FROM " . USERS_TABLE . " 
		WHERE user_birthday >= " . gmdate('md0000',$start_date) . " 
			AND user_birthday <= " . gmdate('md9999',$end_date);
	if ( !$result = $db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, 'Could not read user table to get birthday today info', '', __LINE__, __FILE__, $sql);
	}

	// get the number of occurences
	$number = $db->sql_numrows($result);

	// if limit per page asked, limit the number of results
	if ($limit)
	{
		$sql .= " LIMIT $start, $max_limit";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain topics information', '', __LINE__, __FILE__, $sql);
		}
	}

	// read users
	while ($row = $db->sql_fetchrow($result))
	{
		// user info
		$user_id		= $row['user_id'];
		$username	= $row['username'];
		$user_birthday	= $row['user_birthday'];

		//$txt_class = get_user_level_class($row['user_level'], 'genmed', $row);
		if ($row['user_allow_viewonline'] != YES)
		{
				$username = '<i>' . $username . '</i>';
		}
		$username_link = append_sid($phpbb_root_path . "./profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$user_id");

		preg_match('/(..)(..)(....)/', sprintf('%08d',$userdata['user_birthday']), $bday_parts);
		$event_month = $bday_parts[1];
		$event_day = $bday_parts[2];
		//$event_year = $bday_parts[3];
		$start_month	= intval(date('m', $start_date));
		$event_year	= intval(date('Y', $start_date));
		if ($event_month < $start_month) $event_year++;
		$event_time = mktime( 0,0,0, $event_month, $event_day, $event_year );

		// build the overview
		$sav_tpl = $template->_tpldata;
		$det_handler = '_overview_profil_' . $user_id;
		$template->set_filenames(array(
			$det_handler => 'calendar_overview_profil.tpl')
		);

		$txt_class = 'genmed';

		$bday_month_day = $event_month.$event_day;
		$bday_year = $user_birthday - 10000*$bday_month_day;
		$age = '';
		if ( $bday_year )
		{
			$template->assign_block_vars('agerow',array());
			$age = gmdate('Y')-$bday_year;
		}

		// avatar
		$avatar_display = ( $userdata['user_viewavatar'] && $row['user_allowavatar'] );
		if ( $userdata['user_level'] == ADMIN || ($view_user_id == $user_id) )
		{
			$avatar_display = true;
		}
		$avatar = '';
		if ( $avatar_display && $row['user_avatar_type'] )
		{
			switch( $row['user_avatar_type'] )
			{
				case USER_AVATAR_UPLOAD:
					$avatar = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $row['user_avatar'] . '" alt="" border="0" />' : '';
					break;
				case USER_AVATAR_REMOTE:
					$avatar = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $row['user_avatar'] . '" alt="" border="0" />' : '';
					break;
				case USER_AVATAR_GALLERY:
					$avatar = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $row['user_avatar'] . '" alt="" border="0" />' : '';
					break;
			}
		}

		$template->assign_vars(array(
			'L_TITLE'		=> $lang['View_Birthdays'],
			'L_USERNAME'	=> $username,
			'TXT_CLASS'	=> $txt_class,
			'AVATAR'		=> $avatar,
			'L_AGE'		=> $lang['Age'],
			'AGE'		=> $age,
			)
		);

		$template->assign_var_from_handle('_calendar_overview', $det_handler);
		$message = $template->_tpldata['.'][0]['_calendar_overview'];
		$template->_tpldata = $sav_tpl;

		// remove \n remaining from the template
		$message = preg_replace("/[\n\r]{1,2}/", '', $message);

		// store only the new values
		$new_row = array();
		$new_row['event_id']				= POST_USERS_URL . $user_id;

		$new_row['event_author_id']			= $user_id;
		$new_row['event_author']			= $username;
		$new_row['event_time']				= $event_time;

		$new_row['event_last_author_id']	= '';
		$new_row['event_last_author']		= '';
		$new_row['event_last_time']			= '';

		$new_row['event_replies']			= '';
		$new_row['event_views']				= '';
		$new_row['event_type']				= POST_BIRTHDAY;
		$new_row['event_vote']				= '';
		$new_row['event_status']			= '';
		$new_row['event_moved_id']			= '';
		$new_row['event_last_id']			= '';
		$new_row['event_forum_id']			= '';
		$new_row['event_forum_name']		= '';

		$new_row['event_icon']				= '';
		$new_row['event_title']				= $username;
		$new_row['event_short_title']		= $username;
		$new_row['event_calendar_time']		= $event_time;
		$new_row['event_calendar_duration']	= '';
		$new_row['event_message']			= $message;
		$new_row['event_link']				= $username_link;
		$new_row['event_txt_class']			= $txt_class;
		$new_row['event_type_icon']			= '<img src="' . $images['icon_tiny_profile'] . '" border="0" align="absbottom" hspace="2" />';

		$events[] = $new_row;
	}
}
#
#-----[ SAVE/CLOSE ALL FILES ]--------------------------
#
# EoM