<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &个人中心-学校管理模块&
 */
 $c=_get('c','index');
 
 $Modname = '个人中心 -管理员设置';
 
 $M_user = G_class('user');
 
 $T=$c=='index'?$do:$do.'_'.$c;
 
 //左侧菜单开始 --后面考虑转到模块入口文件
  $left_menu_html = G_class('menu') ->get_left_menu_html($arrgroup['menuid'],$do,$c);
 
  //左侧菜单开始
		  $left_arr = array(
		 	'个人信息'=>'index,edit',
			'站内信'=>'message,sendmsg,replymsg',
			//'充值记录'=>'logpay',
			//'短信记录'=>'logsms',
			//'登录记录'=>'loglogin',
		  );
		  
		  $left_url = SITEURL.'?mod=school&do=setinfo&';
		  $left_html = left_html($left_arr,'c',$c,$left_url);
  
  $userinfo = $M_user->read($_CO['uid']);
		
  $gBranch = $M_user->B->findbranch($userinfo['bid']);
  $usergroup = G_class('usergroup')->read($userinfo['groupid']);
  $bid = $userinfo['bid'];
  $userinfo['uidauthcode']=fields_code($userinfo['uid'],'uid');
  
  switch ($c){
		
		case "index":
		
		$Modname =  '个人信息 - '.$Modname;
		break;
		
		case "edit":
		
		$Modname = '编辑个人信息 - '.$Modname;
		
		
		

		break;
		
		case "avatar":
		
		$Modname =  '编辑头像 - '.$Modname;
		
		function create_password($pw_length = 8)
		{
			$randpwd = '';
			for ($i = 0; $i < $pw_length; $i++) 
			{
				$randpwd .= chr(mt_rand(33, 126));
			}
			return $randpwd;
		} 
		
		$create_password = create_password(6);
		
		

		break;
		
		
		case "message":
		
		$Modname =  '站内信 - '.$Modname;
		
		$M_message = G_class('message');
		
		//$Mymessage = $M_message->find($_CO['uid'],'inbox','posttime DESC');
		$Mymessage = $M_message->getMessageByUid($_CO['uid']);
		
		
		$i=0;
		$m=0;
		$uidarr = array();
		foreach($Mymessage as $key => $value){
			if($_CO['uid'] == $value['senduid']){
				$uniqid = $value['senduid'] . '.' . $value['recvuid'];
			}else{
				$uniqid = $value['recvuid'] . '.' . $value['senduid'];
			}
			if(in_array($uniqid, $uidarr)){
				$i++;
			}else{
				$i=0;
			}
			if($value['senduid']!=$_CO['uid'] && $value['new'] == 1){
				if(isset($msg[$uniqid]['newmsg'])){
					$msg[$uniqid]['newmsg'] = $msg[$uniqid]['newmsg'] + 1;
				}else{
					$msg[$uniqid]['newmsg'] = 1;
				}
			}
			if(isset($msg[$uniqid]['num'])){
					$msg[$uniqid]['num'] = $msg[$uniqid]['num'] + 1;
				}else{
					$msg[$uniqid]['num'] = 1;
				}
			$msg[$uniqid]['msg'][$i]['pmid'] = $value['pmid'];
			$msg[$uniqid]['senduid'] = $value['senduid'];
			$msg[$uniqid]['recvuid'] = $value['recvuid'];
			$msg[$uniqid]['msg'][$i]['content'] = $value['content'];
			$msg[$uniqid]['msg'][$i]['subject'] = $value['subject'];
			$msg[$uniqid]['msg'][$i]['posttime'] = $value['posttime'];
			$msg[$uniqid]['msg'][$i]['new'] = $value['new'];
			$msg[$uniqid]['username'] = $value['username'];
			$duixiang_id = $value['senduid']==$_CO['uid']?$value['recvuid']:$value['senduid'];
			$duixiang = $M_message->getUserName($duixiang_id);
			$msg[$uniqid]['realname'] = $duixiang['realname'];
			$uidarr[] = $uniqid;
		}
		$msg = fields_code($msg,'recvuid');
		unset($uidarr);
		break;
				
		// Edit by zhaobin 11:39 2012/11/16 START
		case "send":
			//
			break;
		// Edit by zhaobin 11:39 2012/11/16 END
		
		case 'sendByReply':
			if($_POST){
				$content = $_POST['content'];
				if(empty($content)){
					Note('别忘了填写回复内容');
					exit;
				}
				$recvuid = $_POST['recvuid'];
				$uid = $_POST['uid'];
				$subject = $_POST['subject'];
				$location = $_POST['location'];
			}
			$M_message = G_class(message);
			$result = $M_message->replymsg($uid, $recvuid, $subject, $content);
			if($result > 0){
				header("location:{$location}");exit;
			}else{
				Note('回复失败');
			}
			break;
		case 'sendmsg':
			if($_GET['recvuid']){
				$M_user = G_class('user');
				$usernameArr = $M_user->get_username($_GET['recvuid']);
				foreach($usernameArr as $key=>$value){
					$usernameArr1[] = $value['username'];
				}
				$users = implode(',', $usernameArr1);
			}else{
				$users='请输入收信人，多个收信人请用英文状态下逗号隔开，如：校长;班主任';
			}
			if($_POST){
				
				$M_message = G_class('message');
			$Modname =  '发送站内信 - '.$Modname;
			$recipients = $_POST['recipients'];
			$content = $_POST['content'];
			if(empty($recipients) || $recipients == '请输入收信人，多个收信人请用英文状态下逗号隔开，如：校长;班主任'){
				Note('请填写收信人');
				exit;
			}
			if(empty($content)){
				Note('别忘了填写站内信内容');
				exit;
			}
			$title = utf_substr($content, '40');
			$reciuser = explode(',', $recipients);
			foreach($reciuser as $key => $user){
				$result = $M_user->check_isuser($user);
				if(count($result) > 0){
					$reciuid[] = $result[0]['uid'];
				}else{
					$msg = '用户： ' . $user . ' 不存在，请核实后再发送';
					Note($msg);
					exit;
				}
			}
			$uid = $_CO['uid'];
			$result = $M_message->sendmsg($uid, $reciuid, $title, $content);
			$backurl = SITEURL .'?mod=school&do=setinfo&c=message';
			Note('信息发送成功', '>>点击返回<<', $backurl);
				
				
			}
			break;
		case 'sendsms':
			$num_users=0;
			if($_GET['recvuid']){
				$M_user = G_class('user');
				$usernameArr = $M_user->get_username($_GET['recvuid']);
				foreach($usernameArr as $key=>$value){
					$usernameArr1[] = $value['phonenum'];
				}
				$users = implode(',', $usernameArr1);
				$num_users = count($usernameArr);
			}else{
				$users='请输入收信人，多个收信人请用英文状态下逗号隔开，如：校长;班主任';
			}
			if($_POST){
				
				G_class('sendsms')->M_sendSMS($_POST);
				
				
			}
			break;
			
		case 'logsms':
			$Modname =  '短信发送记录 - '.$Modname;
			$sysnote=G_class('sendsms')->read_all('',$_CO['uid']);//短信发送
			break;
		
		case "replymsg":
			$uid = $_GET['userid'];
			$senduid = $_GET['senduid'];
			$authcode = $_GET['authcode'];
			$M_message = G_class('message');
			$Modname =  '站内信息 - '.$Modname;
			$msg_get_info = array();
			$msg_post_info = array();
			$msg_get_info = $M_message->getSubjectInfo($senduid, $uid);
			$msg_post_info = $M_message->getSubjectInfo($uid, $senduid);
			$replayuser = $M_message->getUserName($uid);
			if($msg_get_info == array()){
				$msgarr = $msg_post_info;
				$reply_uid = $msg_post_info[0]['senduid'];
				$subject = $msg_post_info[0]['subject'];
			}else if($msg_post_info == array()){
				$msgarr = $msg_get_info;
				$reply_uid = $msg_get_info[0]['senduid'];
				$subject = $msg_get_info[0]['subject'];
			}else{
				$msgarr = array_merge($msg_get_info, $msg_post_info);
				$reply_uid = $msg_get_info[0]['senduid'];
				$subject = $msg_get_info[0]['subject'];
			}
			foreach($msgarr as $key => $value){
				foreach($value as $key2 => $value2){
					$msgsdata[$value['posttime']][$key2] = $value2;
				}
			}
			sort($msgsdata);
			rsort($msgsdata);
			$M_message->updateMsgState($senduid, $uid);	//更新信息阅读状态
			break;
		
		case 'smsassign':
		 	//获取该部门所有下属部门
		 	$M_user = G_class('user');
			$adminSub = $M_user->B->findbid_by_sub_admin($_CO['userbid']);
			foreach($adminSub as $key=>$value){
				$adminSub[$key]['bname'] = $M_user->B->findbranch($value['bid']);
			}
			$my_balance_info = $M_user->get_my_balance($_CO['uid']);
			//当前用户总可用短信条数
			$my_balance = $my_balance_info['sms_balance'];
			$usable=$my_balance;
			break;
  }
 
 if($no_temp<0) require_once Template($T);


?>