<?php

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

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

<!--body start-->
	<?php
	loader_cache("system","table.group");
	
	//loader
	loader_setting("naver.admin");
	
	//菜单处理
	$action = GetQuery("action");
	$jump = GetQuery("jump");
	
	//下一步
	$next=true;
	
	if(!$jump) $jump="admin.list.php";
	
	$people = GetQuery("people");
	
	if($people=="me"){
		$id = $_SESSION["Manager"]["id"];
	}else{
		$id = GetNumber("id",0);
	}
	
	
	if($action){
		$account=trim(GetQuery("name"));
		$password = GetQuery("password");
		$confirm = GetQuery("confirm");
		
		$gid = GetNumber("gid",0);
		$status = GetNumber("status",1);
		$sex = GetNumber("sex",0);
		$email = GetQuery("email");
		$phone = GetQuery("phone");
		$blog = GetQuery("blog");
		$qq = GetQuery("qq");
		$birthday = GetQuery("birthday");
	}	
	
	//mysql open
	reader_mysql();
	
	//动作处理
	switch ($action){
		
		case "add":
		
			//检查权限
			$func = "sys-admin-add";
			checker_func($func);				
				
			//查询数据
			$sql="select * from ".VI_DBMANPRE."admin where account='".$account."'";
			$result=mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			if( mysql_num_rows($result)==1 ){
				
				$Project["message"]="已经存在同名用户!";
				
			}else{
			
				//插入数据
				$sql="insert into ".VI_DBMANPRE."admin(account,password,status,gid,dateline,blog,email,ip,avatar,qq,phone,sex,birthday) values('".$account."','".md5($password)."','".$status."','".$gid."',".time().",'".$blog."','".$email."','".GetIP()."','".$_SESSION["Manager"]["avatar"]."','".$qq."','".$phone."',".$sex.",'".$birthday."')";
				
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				$id = mysql_insert_id($mysql);
				
				//错误处理
				if (!$id) {
					trigger_error($sql, E_USER_ERROR);
					exit();
				}
				
				///////////////////////////
				
				//复制快捷方式
				$quick = unserialize( $VICACHE["sys.group"][$gid]["quick"] );
				
				if( $quick && is_array( $quick ) ){				
					foreach( $quick as $q ){
	
						//插入数据
						$sql = "insert into ".VI_DBMANPRE."quick(aid,appid,dateline) values(".$id.",'".$q."',".time().")";	
	
						mysql_query($sql,$mysql);
	
					}
				}
				
				//复制小工具
				$widget = unserialize( $VICACHE["sys.group"][$gid]["widget"] );

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

						foreach( $a as $w ){
	
							//插入数据
							$sql = "insert into ".VI_DBMANPRE."widget(aid,appid,widget,x,y,z,fx,fy,dateline) values(".$id.",'".$appid."','".$w."','','',0,0,0,".time().")";	
	
							mysql_query($sql,$mysql);
	
						}
	
					}
				}
				
				//////////////////////////////////////
					
				//写入日志
				event_insert($mysql,$func,time(),time(),"创建用户：".$account."，用户：".$VICACHE["sys.group"][$gid]["name"]);				
				
				//缓存系统用户
				cache_table('system',$mysql,VI_DBMANPRE."admin",'<option value=\'{table:id}\'>{table:account}</option>');
				
				reader_redirect("admin.list.php","成功添加新用户!");
				
			}
		
		break;
		
		case "update":
		
			//检查权限			
			if( $id == $_SESSION["Manager"]["id"] ){
				$func = "sys-account-mod";
				checker_func($func);				
			}else{
				$func = "sys-admin-mod";
				checker_func($func);				
			}			
				
			//更新数据
			$sql="update ".VI_DBMANPRE."admin set status='".$status."',blog='".$blog."',email='".$email."',qq='".$qq."',phone='".$phone."',sex='".$sex."',birthday='".$birthday."',modify='".time()."' where id=".$id;			
			mysql_query($sql,$mysql);
			
			//修改密码
			if($password && $password==$confirm){
				
				//检查权限			
				if( $id == $_SESSION["Manager"]["id"] ){
					$func = "sys-account-pwd";
					checker_func($func);
				}else{
					$func = "sys-admin-pwd";
					checker_func($func);				
				}				
				
				$sql="update ".VI_DBMANPRE."admin set password='".md5($password)."' where id=".$id;			
				mysql_query($sql,$mysql);
					
				//写入日志
				event_insert($mysql,$func,time(),time(),"修改用户密码：".$VICACHE["sys.admin"][$id]["name"]);				
			}
			
			//更改权限
			if( $gid && $gid != $_SESSION["Manager"]["gid"]  ){
				checker_func("sys-account-gid");
				
				$sql="update ".VI_DBMANPRE."admin set gid='".$gid."' where id=".$id;			
				mysql_query($sql,$mysql);
					
				//写入日志
				event_insert($mysql,$func,time(),time(),"修改用户：".$VICACHE["sys.admin"][$id]["name"]);				
			}
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"修改用户资料：".$VICACHE["sys.admin"][$id]["name"]);				
				
			//缓存系统用户
			cache_table('system',$mysql,VI_DBMANPRE."admin",'<option value=\'{table:id}\'>{table:account}</option>');
				
			reader_redirect(($jump?$jump:"?id=".$id."&action=edit"),"成功修改用户信息!");
		
		break;
		
		case "edit":
		
			$sql="select * from ".VI_DBMANPRE."admin where id=".$id;
			$result=mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			if(mysql_num_rows($result)==1){
				$row=mysql_fetch_assoc($result);
				
			}else{
				$Project["message"]="未找到指定用户!";
			}
		
		break;
		
	}
	
	//mysql close
	reader_mysql(true);
	
	/*
	安全问题
	<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 checker_func("sys-admin-add",true);
		break;
		
		case "edit":
			if( $id == $_SESSION["Manager"]["id"] ){
				checker_func("sys-account-mod",true);				
			}else{
				checker_func("sys-admin-mod",true);				
			}			
		break;
	}
	
	?>

	<?php   
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$Project["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"] && $Group["sys-account-pwd"]=="Y") || ($id != $_SESSION["Manager"]["id"] && $Group["sys-admin-pwd"]=="Y") ){
			echo '
	
			<tr>
				<td><strong>用户密码：</strong></td>
				<td>
					<input name="password" type="password" value="" size="35" maxlength="15" '.( !$action ? 'fix_name="用户密码" fix_password="yes"' : '' ).' 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( $Group["sys-account-gid"]=="Y" ){
            ?>
        	<tr>
            	<td><strong>用户分组：</strong></td>
                <td>
                <?php
				foreach($VICACHE["sys.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="status" value="0">
                    禁用</label>
                    <label>
                    <input type="radio" class="radio" name="status" 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=status]").value("<?php echo $row["status"];?>");
		</script>

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

<?php html_close();?>