<?php

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

require '../../include/loader.php';
html_start("用户组管理 - VeryIDE");
?>

<!--body start-->
	<?php
	
	//loader
	require('include/naver.admin.php');
	
	//菜单处理
	$action = getgpc('action');
	$jump = getgpc('jump');
	
	//连接数据库
	System :: connect();
	
	$id = getnum('id',0);
	$state = getnum('state',0);
	
	if($action){
	
		//动作处理
		switch ($action){
			
			case "state":
			
				//检查权限
				$func = "sys-group-mod";
				System :: check_func( $func, false );
					
				//更新数据
				$sql="UPDATE `sys:group` SET `state`='".$state."' WHERE id=".$id;
				
				System :: $db -> execute( $sql );
	
				if( System :: $db -> getAffectedRows() ){
					
					$_G['project']['message']="成功更改用户组状态!";					
					
					//写入日志
					System :: insert_event($func,time(),time(),"更改用户组状态：".$_CACHE['system']['group'][$id]["name"]);					
					
				}else{
					$_G['project']['message']="未找到指定用户组！";
				}
				
			break;
			
			case "delete":
			
				//检查权限
				$func = "sys-group-del";
				System :: check_func( $func, false );
					
				//删除数据
				$sql="DELETE FROM `sys:group` WHERE id=".$id;
				
				System :: $db -> execute( $sql );
	
				if( System :: $db -> getAffectedRows() ){
					
					$_G['project']['message']="成功删除用户组!";
					
					//写入日志
					System :: insert_event($func,time(),time(),"删除用户组：".$_CACHE['system']['group'][$id]["name"]);
					
				}else{
					$_G['project']['message']="未找到指定用户组!";
				}
				
			break;
			
		}	
			
		//缓存系统用户组
		Cached :: table( 'system', 'sys:group', array( 'jsonde' => array('config'), 'serialize' => array('quick','widget') ) );
	
	}

	//search
	$s = getgpc("s");
	$q = getgpc('q');
	?>		
	
    <div id="search">
        <form name="form1" method="get" action="">
            <span class="action">
            	<?php
				if( System :: check_func('sys-group-add') ){
				?>
                	<button type="button" class="button" onclick="location.href='group.edit.php';">添加分组</button>
                <?php
				}
				?>
                <input type="button" value="更新缓存" onclick="if(confirm('确定现在要更新缓存吗？')){location.href='?action=create';}" class='button'>
            </span>
            
             <select name="s" id="s" size="1">
               <option value="name">组名称</option>
               <option value="account">用户名</option>
               <option value="id">ID</option>
            </select>
            <input name="q" type="text" class="text" title="请输入关键字" id="q" value="<?php echo $q;?>">
            <button class="go" type="submit"></button>
        </form>
        
        <script type="text/javascript">
         Mo("#s").value("<?php echo $s;?>");
        </script>
    </div>

    <?php
    
    $sql="SELECT * FROM `sys:group` WHERE 1=1 ";
    if ($s && $q){
        $sql.=" and  $s like '%".$q."%'";
    }
    
    $sql.=" order BY id desc";
    
    //查询数据库_总记录数
    $row_count = System :: $db -> getCount( $sql );

    //分页参数
    $page=getpage("page");
    $page_start=$_G['setting']['global']['pagesize']*($page-1);
    $sql=$sql." limit $page_start,".$_G['setting']['global']['pagesize'];    
    
    //分页链接
    $url="?s=".$s."&q=$q&page=";	
    
    ?>
    
    <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table" id="table">
      <tr class="title" id="thead">
        <td></td>
        <td>ID</td>
        <td>组名</td>
        <td>用户数</td>
        <td>部分用户 <script>Mo.Tools("Tip","仅显示最近加入的 3 位用户");</script></td>
        <td>创建者</td>
        <td>权限</td>
        <td>创建时间</td>
        <td>修改时间</td>
        <td>状态</td>
        <td width="70">操作</td>
      </tr>
    
    <?php
    
    $result = System :: $db -> getAll( $sql );

	foreach( $result as $row ){
		
		$users = System :: $db -> getAll("SELECT account from `sys:admin` WHERE gid=".$row['id']." ORDER BY id desc",'account');
		$users = array_keys( $users );
		$count = count( $users );
		$output = array_slice($users, 0, 3);   
        
    ?>
        <tr class="<?php echo zebra( $i, array( "line" , "band" ) );?>" edit="group.edit.php?action=edit&id=<?php echo $row['id'];?>&jump={jump}">
            <td width="30"><img src="<?php echo VI_BASE;?>image/medal/<?php echo "mini_".$row["medal"];?>" /></td>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['name'];?></td>
            <td><a href="admin.list.php?s=gid&q=<?php echo $row['id'];?>"><?php echo $count;?></a></td>
            <td class="text-yes"><?php echo implode("，",$output); echo $count>3 ? '等……' : '';?></td>
            <td><a href="admin.edit.php?action=edit&id=<?php echo $row["aid"];?>"><?php echo $row['account'];?></a></td>
            <td><a href="group.power.php?gid=<?php echo $row['id'];?>" onclick="new parent.Mo.Dialog('dialog','用户组配置 - <?php echo $row['name'];?>' , '<iframe src='+this.href+' frameborder=0></iframe>' ,'950','65%',{ 'remove' : true, 'draged' : true, 'create' : true }); return false;">查看</a></td>
            <td title="<?php echo date("Y-m-t H:i:s",$row['dateline']);?>"><?php echo date("Y-m-t",$row['dateline']);?></td>
            <td title="<?php echo date("Y-m-t H:i:s",$row['modify']);?>"><?php echo date("Y-m-t",$row['modify']);?></td>
            <td><?php echo $_G['project']['state'][$row['state']];?></td>
            <td>
            <script type="text/javascript">
				Mo.Tools("RsEdit","group.edit.php?action=edit&id=<?php echo $row['id'];?>","");
				Mo.Tools("RsDelete","?action=delete&id=<?php echo $row['id'];?>","");
            </script>
            </td>
        </tr>
        
    <?php
        
    }
    
    ?>
    </table>
    <?php
  
	//关闭数据库
	System :: connect();
    
    ?>
    
    <div id="saving">
		<?php
            echo multipage($page,$row_count,$_G['setting']['global']['pagesize'],$url,"page");
        ?>
    </div>
    
<!--body end-->

<?php html_close();?>