<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &学校管理模块&
 */
 
 if (!defined('IS_RUN')) die('Forbidden');
 
 $M = G_class('user');
 $c= _get('c','sch');$d= _get('d','');$gid= _get('gid','');$bid= _get('bid',$_CO['userbid']);$uid= _get('uid','');
 $keyword=_get('keyword','');
 $T = !empty($d)?$d:'member';
 
 $sysgroupid = $M->G->get_usergroupid_str($_CO['groupid'],$c);

 $Group = !empty($gid)? $gid:$sysgroupid;
 
 $topBranch = $M->B->findbid($_CO['userbid']);
 
 $TOPBranch_1 = $M->B->read($_CO['userbid']);
 
 $_CO_bidauthcode = fields_code($_CO['userbid'],'bid');


 $Modname = '人员架构 - 学校管理';
 
switch ($d) {
		
		case "":
		
			$strbid = $M->B->findbid($bid);
			$thisBranch = $M->B->read($bid);
			$bname = $M->B->findbranch($bid);
			$arrMembers = $M->arrmember($Group,$bid,$c,'*',$keyword);
			//获取参数验证代码
			$arrMembers[1]=fields_code($arrMembers[1],'bid');
			$arrMembers[1]=fields_code($arrMembers[1],'uid');
			$authcode=$authcode?$authcode:fields_code($bid,'bid');
			if(empty($thisBranch['bname'])&&$bid) location(SITEURL.'?mod=school&do=member&c=sch');
			$Modname = $bname?str_replace('/',' - ',$bname).' - '.$Modname:$Modname;
			//每页显示数据量
			$listNum = _get('listNum','20');
			//当前页
			$pid=$_GET['p'];
			if(is_numeric($pid)&&$pid>0){
				$pid=(int)$pid;
			}else{
				$pid='1';
			}
			//链接地址
			$url = SITEURL . '?mod=school&do=member&c='.$c.'&bid='.$bid.'&gid='.$Group.'&authcode='.$authcode;
			//数据总数
			$sum = $arrMembers[0];
			$pidf = $pid-1;
			$start = $pidf*$listNum;
			$end = $listNum;
			foreach($arrMembers[1] as $key=>$value){
				$array[] = $value;
			}
			$arrMembers[1] = array();
			$arrMembers[1] = array_slice($array, $start, $end);
			$pageinfo = grtpageno($url, $sum, $listNum, $pid);
			
			$usergroupshow_html = $M->G->usergroup_show($_CO['groupid'],$Group,$c,$bid,$pid);

		break;
		
		
		case "member_add":
		
		check_access('set_member');
		$thisBranch = $M->B->read($bid);
		$gBranch = $M->B->findbranch($bid);
		$usergroup_option = G_class('usergroup')->usergroup_option();
		$Modname = '新增成员 - '.$Modname;
		
		break;
		
		case "member_show":
		$authcode = _get('authcode');
		$oneMember = $M->read($uid);
		$gBranch = $M->B->findbranch($oneMember['bid']);
		$usergroup = G_class('usergroup')->read($oneMember['groupid']);
		$bid = $oneMember['bid'];
		$Modname = '查看'.$oneMember['realname'].' - '.$Modname;
		
		break;
		
		case "member_edit":
		check_access('set_member');
	    $oneMember = $M->read($uid);
		$thisBranch = $M->B->read($oneMember['bid']);
		$gBranch = $M->B->findbranch($oneMember['bid']);
		$bid = $oneMember['bid'];
		$usergroup_option = G_class('usergroup')->usergroup_option($oneMember['groupid']);
		$Modname = '编辑'.$oneMember['realname'].' - '.$Modname;

		break;
		
		case "member_import":
			check_access('set_member');
			$bname = $M->B->findbranch($bid);
			$Modname = '批量导入 - '.$Modname;
			$M_usergroup = G_class('usergroup');
			$usergroupArr = $M_usergroup->read_all();
			foreach($usergroupArr as $key=>$value){
				$branchArr[$value['groupid']]=$value['groupname'];
			}
			$key = array_keys($branchArr, '家长');
			if($key){
				unset($branchArr[$key[0]]);
			}
			$branchArr['6'] = '学生';
			$usergroupString = implode('、', $branchArr);
			break;
		
		case "member_import_show":
			check_access('set_member');
			$bname = $M->B->findbranch($bid);
			$Modname = '批量导入 - '.$Modname;
			$M_usergroup = G_class('usergroup');
			$usergroupArr = $M_usergroup->read_all();
			foreach($usergroupArr as $key=>$value){
				$branchArr[$value['groupid']]=$value['groupname'];
			}
			$key = array_keys($branchArr, '家长');
			if($key){
				unset($branchArr[$key[0]]);
			}
			$branchArr['6'] = '学生';			
			$usergroupString = implode('、', $branchArr);
			if($_FILES != array()){
				$file = $_FILES['file'];
				$fileinfo = pathinfo($file['name']);
				$ext = $fileinfo['extension'];
				$maxsize = 10 * 1024 * 1024;
				$typearr = array(
					'xls'
				);
				$filesize = $file['size'];
				$filetype = $file['type'];
				$result = checkFile($filesize,$maxsize,$ext,$typearr);
				if($result != ''){
					Note($result);
					exit;
				}else{
					$directory = TUNTRON_PATH . "/dat/upload/excelfile/";
					$newfilename = time() . '.' . $ext;
					if(file_upload($file['tmp_name'], $directory.$newfilename)){
						$filename = $directory.$newfilename;
						if(file_exists($filename)){
							//读取EXCEL数据方法
							$arr = readExcel($filename);
							$arr = array_slice($arr, 1);
							$i=0;
							$e=0;
							$dataArrErr = array();
							$dataArr = array();
							$username_cache = array();
							$repeat = array();
							foreach($arr as $key => $value){
								//去掉字符串尾部逗号
								$str = substr($value, 0, strlen($value)-1);
								$values = explode(',', $str);
								$j=0;
								$error = '0';
								foreach($values as $key1=>$value1){
									switch($j){
										// 检查帐号
										case "3":
											if(empty($value1)){
												$error = '用户帐号不能为空';
											}else if(in_array($value1, $username_cache)){
												$repeat[] = $value1;
												$error = '该用户帐号存在重复值';
											}else{
												$error = $M -> import_check_username($value1);
											}
											$username_cache[] = $value1;
											break;
										case '4':
											if(empty($value1)){
												$values[4] = '123456';
											}else{
												$error = $M -> import_check_password($value1);
											}
											break;
										case '5':
											if(!in_array($value1, $branchArr)){
												$error = '该用户组不存在，用户组的值为下列值之一：' . $usergroupString;
											}else if(empty($value1)){
												$error = '用户组不能为空';
											}else if($value1 == '学生'){
												if(empty($values[2])){
													$error = '学生用户的学号项为必填项';
												}
											}
											break;
										default :
											break;
									}
									if($error != '0'){
										$dataArrErr[$e]['data'] = $values;
										$dataArrErr[$e]['error'] = $error;
										$e++;
										break;
									}
									$j++;
								}
								$cb = array_keys($branchArr, $values[5]);
								$values['groupid'] = $cb[0];
								if($error == '0'){
									$dataArr[$i] = $values;
								}
								$i++;
							}
							
							//整理导入失败的数据
							foreach($dataArrErr as $key=>$value){
								$import_data_error[] = implode(',', $value['data']) . ',' . $value['error'];
							}
							$import_data_error_string = implode('|', $import_data_error);
							
							// 整理导入成功的数据
							foreach($dataArr as $key=>$value){
								$import_data[] = implode(',', $value);
							}
							$import_data_string = implode('|', $import_data);
							unset($username_cache);
						}
					}else{
						Note('文件导入失败');
						exit;
					}
				}
			}
		break;
		
		case 'export_excel':
			
			check_access('export_member');
			$arrMembers = $M->arrmember($Group,$bid,$c);
			$title = array('1'=>'用户编号','2'=>'邮箱','3'=>'用户帐号','4'=>'真实姓名','5'=>'性别','6'=>'所属用户组','7'=>'所在部门','8'=>'电话号码','9'=>'登录次数','10'=>'最后登录时间');
			$i=0;
			foreach($arrMembers[1] as $key=>$value){
				$value['sex']==0 ? $value['sex']='女' : $value['sex']='男';
				if($value['logintime']!=''){
					$value['logintime'] = date('Y年m月d号 H:i:s', $value['logintime']);
				}
				
				$array[$i]['id'] = $value['uid'];
				$array[$i]['email'] = $value['email'];
				$array[$i]['username'] = $value['username'];
				$array[$i]['realname'] = $value['realname'];
				$array[$i]['sex'] = $value['sex'];
				$array[$i]['group'] = $value['usergroup']['groupname'];
				$array[$i]['bname'] = $value['branch']['bname'];
				$array[$i]['phone'] = $value['phonenum'];
				$array[$i]['logincount'] = $value['logincount'];
				$array[$i]['logintime'] = $value['logintime'];
				$i++;
			}
			$filename = $array[0]['bname'].'_全部人员Excel数据表.xls';
			export_excel($title, $array, $filename);
			exit;
			break;
		
		case 'export_excel_member':
			check_access('export_member');
			if(!$_GET['string']){
				Note("无效的导出数据");
				exit;
			}
			
			$string = $_GET['string'];
			$arr1 = explode('|', $string);
			foreach($arr1 as $key=>$value){
				$arr2 = explode(',', $value);
				$arr[] = array_slice($arr2, 0, -1);
			}
			if($_GET['type'] == 'no'){
				$filename = '无效成员列表.xls';
				$title = array('1'=>'姓名','2'=>'性别','3'=>'学号','4'=>'帐号','5'=>'密码','6'=>'用户组','7'=>'错误描述');
			}else{
				$title = array('1'=>'姓名','2'=>'性别','3'=>'学号','4'=>'帐号','5'=>'密码','6'=>'用户组');
				$filename = '有效成员列表.xls';
			}
			$filename = iconv('UTF-8', 'gb2312', $filename);
			$M_sysnote = G_class('sysnote');
			$M_sysnote->insert_sysnote('1', '0', $_CO['uid'], $_CO['userbid'], '导出数据到Excel表：'.$filename);
			export_excel2($title, $arr, $filename);
			exit;
			break;
	}

 if($bid) {

	 $strbid = $M->B->FindAllBid($bid);
	 $str_bid = $strbid;
	 $Allhtml = $M->B->get_do_branchhtml($str_bid,'null',$c);
 }

require_once Template($T);

?>