##############################################################
## MOD Title: Avatar Select on Register
## MOD Author: Smartor < smartor_xp@hotmail.com > (Hoang Ngoc Tu) http://smartor.is-root.com
## MOD Description:	This will allow user to select a avatar in the gallery when register
##					Already compatible with Default Avatar MOD by Shannado
## MOD Version: 1.0.2
##
## Installation Level: easy
## Installation Time: 10 Minutes
## Files To Edit: 3
##		includes/usercp_register.php
##		language/lang_english/lang_main.php
##		templates/subSilver/profile_add_body.tpl
##
## Included Files: N/A
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/downloads/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/downloads/ 
##############################################################
## Author Notes: 
## 
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
############################################################## 

#
#-----[ OPEN ]--------------------------------------------
#
templates/subSilver/profile_add_body.tpl

#
#-----[ FIND ]--------------------------------------------
#
	<!-- END switch_avatar_block -->

#
#-----[ AFTER, ADD ]--------------------------------------
#
	<!-- BEGIN switch_user_logged_out -->
	<tr>
	  <td class="catSides" colspan="2" height="28">&nbsp;</td>
	</tr>
	<tr>
	  <th class="thSides" colspan="2" height="12" valign="middle">{L_AVATAR_PANEL}</th>
	</tr>
	<tr>
		<td class="row1"><span class="gen">{L_AVATAR_SELECT}<br /></span><span class="gensmall">{L_AVATAR_SELECT_EXPLAIN}</span></td>
		<td class="row2">
			<table><tr>
				<td><select name="avatar_select" onChange="if (this.value!='') document.images['avatar_select'].src = '{AVATAR_GALLERY_PATH}/' + this.value; else document.images['avatar_select'].src = '{AVATAR_SELECT_START}';" ><option value="">{L_NO_AVATAR}</option>{AVATAR_SELECT_OPTIONS}</select>&nbsp; &nbsp;&nbsp;&nbsp;</td>
				<td><img src="{AVATAR_SELECT_START}" name="avatar_select" /></td>
			</tr></table>
		</td>
	</tr>
	<!-- END switch_user_logged_out -->

#
#-----[ OPEN ]--------------------------------------------
#
language/lang_english/lang_main.php

#
#-----[ FIND ]--------------------------------------------
#
//
// That's all Folks!

#
#-----[ BEFORE, ADD ]-------------------------------------
#
// Avatar Select on Register MOD
$lang['NO_AVATAR'] = 'NO AVATAR';
$lang['Avatar_register_explain'] = 'Depending on the board setting, you can upload or customize your custom avatar after registered successfully';


#
#-----[ OPEN ]--------------------------------------------
#
includes/usercp_register.php

#
#-----[ FIND ]--------------------------------------------
#
	if ( !$error )
	{
		if ( $avatar_sql == '' )
		{
			$avatar_sql = ( $mode == 'editprofile' ) ? '' : "'', " . USER_AVATAR_NONE;
		}

#
#-----[ REPLACE WITH ]-------------------------------------
#
	if ( !$error )
	{
		if ( $avatar_sql == '' )
		{
			if ($mode == 'editprofile')
			{
				$avatar_sql = '';
			}
			else
			{
				$avatar_register = isset($HTTP_POST_VARS['avatar_select']) ? str_replace("\'", "''", htmlspecialchars(trim($HTTP_POST_VARS['avatar_select']))) : '';
				$avatar_sql = ( $avatar_register != '' ) ? "'$avatar_register', " . USER_AVATAR_GALLERY : "'', " . USER_AVATAR_NONE;
			}
		}

#
#-----[ FIND ]--------------------------------------------
#
	$template->assign_vars(array(
		'USERNAME' => $username, 
		'CUR_PASSWORD' => $cur_password,
		'NEW_PASSWORD' => $new_password,

#
#-----[ BEFORE, ADD ]-------------------------------------
#
	//
	// Avatar Select on Register MOD
	//
	// we must do a brutal work, scanning all the avatar gallery directories
	// some codes below i borrowed from usercp_avatar.php
	if ($mode == 'register')
	{
		$dir = @opendir($board_config['avatar_gallery_path']);

		$avatar_images = array();
		$avatar_names = array();
		$avatar_count = 0;
		while( $avatar_category = @readdir($dir) )
		{
			if( $avatar_category != '.' && $avatar_category != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $avatar_category) && !is_link($board_config['avatar_gallery_path'] . '/' . $avatar_category) )
			{
				$sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $avatar_category);

				while( $avatar_file = @readdir($sub_dir) )
				{
					if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $avatar_file) )
					{
						$avatar_images[$avatar_count] = $avatar_category . '/' . $avatar_file; 
						$avatar_names[$avatar_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $avatar_category))) . ' - ' . ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $avatar_file)));
						$avatar_count++;
					}
				}
			}
		}

		@closedir($dir);

		@ksort($avatar_images);
		@reset($avatar_images);

		// Here we dump arrays above to a long string :)
		$avatar_select_options = '';
		for ($i = 0; $i < $avatar_count; $i++)
		{
			$avatar_select_options .= '<option value="' . $avatar_images[$i] . '">' . $avatar_names[$i] . '</option>';
		}
	}
	// END Avatar Select on Register MOD ----------------------------
	//

#
#-----[ FIND ]--------------------------------------------
#
		'L_LINK_REMOTE_AVATAR_EXPLAIN' => $lang['Link_remote_Avatar_explain'],
		'L_DELETE_AVATAR' => $lang['Delete_Image'],
		'L_CURRENT_IMAGE' => $lang['Current_Image'],

#
#-----[ AFTER, ADD ]--------------------------------------
#
		// Avatar Select on Register
		'L_AVATAR_SELECT' => $lang['Select_avatar'],
		'L_AVATAR_SELECT_EXPLAIN' => $lang['Avatar_register_explain'],
		'AVATAR_SELECT_START' => isset($images['default_avatar']) ? $images['default_avatar'] : 'images/spacer.gif', // hehe... Default Avatar MOD compatible ;)
		'L_NO_AVATAR' => $lang['NO_AVATAR'],
		'AVATAR_GALLERY_PATH' => $board_config['avatar_gallery_path'],
		'AVATAR_SELECT_OPTIONS' => $avatar_select_options,

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM