<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id:veryide.profile.php ,v1 10:39 2008-10-16 Lay $
*/

require '../../include/loader.php';
html_start("用户编辑 - VeryIDE");
?>

<!--body start-->
	<?php
	Cached :: loader("system","table.group");
	
	//loader
	require('include/naver.admin.php');
	
	//菜单处理
	$action = getgpc('action');
	$jump = getgpc('jump');
	
	//下一步
	$next=true;
	
	if(!$jump) $jump="admin.list.php";
	
	$people = getgpc("people");
	
	if($people=="me"){
		$id = $_SESSION['Manager']['id'];
	}else{
		$id = getnum('id',0);
	}
	
	if($action){
		$account = trim( getgpc('name') );
		$password = trim( getgpc('password') );
		$confirm = getgpc('confirm');
		$gid = getnum('gid',0);
		$state = getnum("state",1);
		$sex = getnum('sex',0);
		$email = getgpc('email');
		$phone = getgpc('phone');
		$blog = getgpc('blog');    
		$qq = getgpc('qq');
		$birthday = getgpc('birthday');
	}	
	
	//连接数据库
	System :: connect();

	//动作处理
	switch ($action){
		
		case "add":
		
			//检查权限
			$func = "sys-admin-add";
			System :: check_func( $func, false );				
				
			//查询数据
			$sql="SELECT * FROM `sys:admin` WHERE account='".$account."'";
			$row = System :: $db -> getOne( $sql );

			if( $row ){
				
				$_G['project']['message']="已经存在同名用户!";
				
			}else{
			
				//插入数据
				$sql="INSERT INTO `sys:admin`(account,password,state,gid,dateline,blog,email,ip,avatar,qq,phone,sex,birthday,config) values('".$account."','".md5($password)."','".$state."','".$gid."',".time().",'".$blog."','".$email."','".GetIP()."','".$_SESSION['Manager']["avatar"]."','".$qq."','".$phone."',".$sex.",'".$birthday."','{}')";
				
				System :: $db -> execute( $sql );
				
				$id = System :: $db -> getInsertId();
				
				///////////////////////////
				
				//复制快捷方式
				$quick = unserialize( $_CACHE['system']['group'][$gid]['quick'] );
				
				if( $quick && is_array( $quick ) ){
				
					foreach( $quick as $q ){	
						//插入数据
						$sql = "INSERT INTO `sys:quick`(aid,appid,dateline) values(".$id.",'".$q."',".time().")";	
	
						System :: $db -> execute( $sql );	
					}
					
				}
				
				//复制小工具
				$widget = unserialize( $_CACHE['system']['group'][$gid]['widget'] );

				if( $widget && is_array( $widget ) ){
					foreach( $widget as $appid => $a ){

						foreach( $a as $w ){
	
							//插入数据
							$sql = "INSERT INTO `sys:widget`(aid,appid,widget,x,y,z,fx,fy,dateline) values(".$id.",'".$appid."','".$w."','','',0,0,0,".time().")";	
	
							System :: $db -> execute( $sql );
	
						}
	
					}
				}
				
				//////////////////////////////////////
					
				//写入日志
				System :: insert_event($func,time(),time(),"创建用户：".$account."，用户：".$_CACHE['system']['group'][$gid]["name"]);				
				
				//缓存系统用户
				Cached :: table( 'system', 'sys:admin', array( 'jsonde' => array('config') ) );
				
				System :: redirect("admin.list.php","成功添加新用户!");
				
			}
		
		break;
		
		case "update":
		
			//检查权限			
			if( $id == $_SESSION['Manager']['id'] ){
				$func = "sys-account-mod";
				System :: check_func( $func, false );
			}else{
				$func = "sys-admin-mod";
				System :: check_func( $func, false );
			}			
				
			//更新数据
			$sql="UPDATE `sys:admin` SET `state`='".$state."',blog='".$blog."',email='".$email."',qq='".$qq."',phone='".$phone."',sex='".$sex."',birthday='".$birthday."',`modify`='".time()."' WHERE id=".$id;			
			
			System :: $db -> execute( $sql );
			
			//修改密码
			if($password && $password==$confirm){
				
				//检查权限			
				if( $id == $_SESSION['Manager']['id'] ){
					$func = "sys-account-pwd";
					System :: check_func( $func, false );
				}else{
					$func = "sys-admin-pwd";
					System :: check_func( $func, false );
				}				
				
				$sql="UPDATE `sys:admin` SET password='".md5($password)."' WHERE id=".$id;			
				
				System :: $db -> execute( $sql );
					
				//写入日志
				System :: insert_event($func,time(),time(),"修改用户密码：".$_CACHE['system']['admin'][$id]["name"]);				
			}
			
			//更改权限
			if( $gid ){
				
				$func = "sys-account-gid";
				System :: check_func( $func, false );
				
				$sql="UPDATE `sys:admin` SET gid='".$gid."' WHERE id=".$id;			
				
				System :: $db -> execute( $sql );
					
				//写入日志
				System :: insert_event($func,time(),time(),"变更用户组：".$_CACHE['system']['admin'][$id]["name"]);				
			}
					
			//写入日志
			System :: insert_event($func,time(),time(),"修改用户资料：".$_CACHE['system']['admin'][$id]["name"]);				
				
			//缓存系统用户
			Cached :: table( 'system', 'sys:admin', array( 'jsonde' => array('config') ) );
				
			System :: redirect(($jump?$jump:"?id=".$id."&action=edit"),"成功修改用户信息!");
		
		break;
		
		case "edit":
		
			$sql="SELECT * FROM `sys:admin` WHERE id=".$id;
			$row = System :: $db -> getOne( $sql );
		
		break;
		
	}
	
	//关闭数据库
	System :: connect();
	
	/*
	安全问题
	<select name="question" onchange="showcustomquest(this.value)" style="width:124px">
		<option value="0">无安全问题</option>
		<option value="1">我爸爸的出生地</option>
		<option value="2">我妈妈的出生地</option>
		<option value="3">我的小学校名</option>
		<option value="4">我的中学校名</option>
		<option value="5">我最喜欢的运动</option>
		<option value="6">我最喜欢的歌曲</option>
		<option value="7">我最喜欢的电影</option>
		<option value="8" >我最喜欢的颜色</option>
		<option value="-1">自定义问题</option>
	</select>
	*/	

	?>

	<?php
	
	//显示权限状态
	switch($action){
		case "":
			echo System :: check_func("sys-admin-add",true);
		break;
		
		case "edit":
			if( $id == $_SESSION['Manager']['id'] ){
				echo System :: check_func("sys-account-mod",true);				
			}else{
				echo System :: check_func("sys-admin-mod",true);				
			}			
		break;
	}
	
	?>

	<?php   
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$_G['product']['version']);    
    ?>
    
	<div id="box">
		<form action="?" method="post" mode="edit" verify="true">
                
        <table cellpadding="0" cellspacing="0" class="form">
            
            <tr><td colspan="2" class="section"><strong>基本信息</strong></td></tr>
                
            <tr>
                <td><strong>用户名称：</strong></td>
                <td><input name="name" type="text" class="text" id="name" value="<?php echo $row['account'];?>" size="35" fix_name="用户名称" fix_null="yes" <?php echo ($action?'disabled="true"':'');?> /></td>
            </tr>
        
			<?php
            
			if( $action=="add" || $action=="" || ($id == $_SESSION['Manager']['id'] && System :: check_func('sys-account-pwd')) || ($id != $_SESSION['Manager']['id'] && System :: check_func('sys-admin-pwd')) ){
			echo '
	
			<tr>
				<td><strong>用户密码：</strong></td>
				<td>
					<input name="password" type="password" value="" size="35" maxlength="15"  fix_null="'. ( !$action ? 'yes' : 'no' ) .'" fix_name="用户密码" fix_value="input[name=confirm]" onkeyup="Mo.Password( this.value, function( value, level, pos ){ Mo(\'#level\').attr( {\'class\' : \'l\'+ pos} ) }); ">
					
					<div id="level"></div>
				</td>
			</tr>
		
			<tr>
				<td><strong>确认密码：</strong></td>
				<td><input name="confirm" type="password" value="" size="35" maxlength="15" /> <span>同上</span></td>
			</tr>
			';
			}
            
            ?>
            
			<?php
            if( System :: check_func('sys-account-gid') ){
            ?>
        	<tr>
            	<td><strong>用户分组：</strong></td>
                <td>
                <?php
				foreach($_CACHE['system']['group'] as $key => $value){
					echo '<label><input type="radio" class="radio" name="gid" value="'.$key.'" '.($key==$row["gid"]?'checked="checked"':'').' fix_name="用户级别" fix_null="yes" /><img src="'.VI_BASE.'image/medal/mini_'.$value["medal"].'" />'.$value["name"].'</label>';
				}			
				?>
                </td>
            </tr>            
            <?php
            }
            ?>
        
        	<tr>
            	<td><strong>帐号状态：</strong></td>
                <td>		
                    <label>
                    <input type="radio" class="radio" name="state" value="0">
                    禁用</label>
                    <label>
                    <input type="radio" class="radio" name="state" value="1" checked> 
                    正常
                    </label>
                </td>
            </tr>
            
            <tr><td colspan="2" class="section"><strong>扩展信息</strong></td></tr>
        
        	<tr>
            	<td><strong>我的生日：</strong></td>
                <td><input name="birthday" id="birthday" type="text" class="text" value="<?php echo $row["birthday"];?>" size="35" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年-月-日">
                </td>
            </tr>
        
        	<tr>
            	<td><strong>我的性别：</strong></td>
                <td>
                <label><input type="radio" class="radio" name="sex" value="0" fix_name="用户性别" fix_null="yes" /><img src="<?php echo VI_BASE;?>image/icon/female.png" /> 女士</label>
                <label><input type="radio" class="radio" name="sex" value="1" fix_name="用户性别" fix_null="yes" /><img src="<?php echo VI_BASE;?>image/icon/male.png" /> 男士</label>
                </td>
            </tr>
                
        	<tr>
            	<td width="60"><strong>QQ/MSN：</strong></td>
                <td><input name="qq" type="text" class="text" value="<?php echo $row["qq"];?>" size="35"></td>
            </tr>
        
        	<tr>
            	<td><strong>电话号码：</strong></td>
                <td><input name="phone" type="text" class="text" value="<?php echo $row["phone"];?>" size="35"></td>
            </tr>
        
        	<tr>
            	<td><strong>电子邮箱：</strong></td>
                <td><input name="email" type="text" class="text" value="<?php echo $row["email"];?>" size="35">
                </td>
            </tr>
        
        	<tr>
            	<td><strong>个人主页：</strong></td>
                <td><input name="blog" type="text" class="text" value="<?php echo $row["blog"];?>" size="35"></td>
            </tr>
        
        	<tr class="nobr">
            	<td><strong></strong></td>
                <td>
                <?php
				
				if($action=="edit" || $action=="update" ){
					echo '
					<input type="submit" name="Submit" value="修改用户" class="submit '.($disabled?'disabled="true"':'').'" />
					<input type="hidden" name="action" id="action" value="update" />
					<input type="hidden" name="id" id="id" value="'.$id.'" />
					';
				}else{
					echo '
					<input type="submit" name="Submit" value="添加用户" class="submit" '.($disabled?'disabled="true"':'').' />
					<input type="hidden" name="action" id="action" value="add" />
					';
				}
				echo '<input type="hidden" name="jump" id="jump" value="'.$_GET["jump"].'" />';
				
				?>
                </td>
            </tr>
        
        </table>
        
		<script type="text/javascript">
			Mo("input[name=sex]").value("<?php echo $row["sex"];?>");
			Mo("input[name=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>");
		</script>

		</form>
	</div>
<!--body end-->

<?php html_close();?>