<?php
/*
	[Office 515158] (C) 2009-2012 天生创想 Inc.
	$Id: config_jituan.php 1209087 2012-01-08 08:58:28Z baiwei.jiang $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
if(!is_superadmin()){
	echo '非系统管理员，不能对此模块进行操作！';
	exit;
}
if ($do == 'list') {
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = $_CONFIG->config_data('pagenum');
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'';
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."union order by id asc");
     $sql = "SELECT * FROM ".DB_TABLEPRE."union  ORDER BY id ASC LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);
	include_once('template/jituan.php');
} elseif ($do == 'update') {
	$idarr = getGP('id','P','array');
	$namearr = getGP('name','P','array');
	$numberarr = getGP('number','P','array');
	foreach ($idarr as $id) {
		$db->query("UPDATE ".DB_TABLEPRE."union set name='".$namearr[$id]."',number='".$numberarr[$id]."' WHERE id = '$id'");
	}
	
	show_msg('下属机构信息批量更新成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');

}elseif($do=='add'){
if($_POST['view']!=''){
		$id = getGP('id','P','int');
		if($id!=''){
			$name = getGP('name','P');
			$type2 = getGP('type2','P');
			$useradmin = getGP('useradmin','P');
			$jituan = array(
				'name' => $name,
				'type2' => $type2,
				'useradmin' => $useradmin
			);
			update_db('union',$jituan, array('id' => $id));
		}else{
			$name = getGP('name','P');
			$type2 = getGP('type2','P');
			$type1 = 2;
			$useradmin = getGP('useradmin','P');
			$username = getGP('username','P');
			$password = md5(getGP('password','P'));
			$jituan = array(
				'name' => $name,
				'type1' => $type1,
				'type2' => $type2,
				'useradmin' => $useradmin,
				'username' => $username,
				'password' => getGP('password','P')
			);
			insert_db('union',$jituan);
			$id=$db->insert_id();
			$user = array(
				'username' => $username,
				'password' => $password,
				'ischeck' => 1,
				'flag' => 1,
				'date' => get_date('Y-m-d H:i:s',PHP_TIME),
				'jituanid' => $id,
				'groupid' => 1
				
			);
			insert_db('user',$user);
			$ids=$db->insert_id();
			$user_view = array(
				'name' => '分公司('.$id.')系统管理员',
				'uid' => $ids,
				'jituanid' => $id
			);
			insert_db('user_view',$user_view);
		}
		show_msg('企业信息操作成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');
	}else{
		$id = getGP('id','G','int');
		if($id!=''){
			$user = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."union where id='".$id."'");
			$_title['name']='编辑';
		}else{
			$_title['name']='发布';
			$user['type2']=1;
		}
		include_once('template/jituanadd.php');
		
	}
		
} elseif ($do == 'table') {
	$id = getGP('id','G','int');
	$data = file_get_contents('mana/data/toa.sql');
	$data=str_replace(' toa_', ' toa_'.$id.'_', $data);
	$data = str_replace("\r", "\n", $data);
	$regular = "/;\n/";
	$data = preg_split($regular,trim($data));
	//循环导入数据
	foreach($data as $val){
		mysql_query($val) or die('导入数据失败！' . mysql_error());
	}
	show_msg('数据表创建成功！', 'admin.php?ac=jituan&fileurl=mana');
} elseif ($do == 'config') {
	$id = getGP('id','G','int');
	$data = file_get_contents('mana/data/config.sql');
	$data=str_replace(' toa_', ' toa_'.$id.'_', $data);
	$data=str_replace('_jituanid_', $id, $data);
	$data=str_replace('toa_'.$id.'_usergroup', 'toa_usergroup', $data);
	$data = str_replace("\r", "\n", $data);
	$regular = "/;\n/";
	$data = preg_split($regular,trim($data));
	//循环导入数据
	foreach($data as $val){
		mysql_query($val) or die('导入数据失败！' . mysql_error());
	}
	show_msg('配置文件生成成功！', 'admin.php?ac=jituan&fileurl=mana');

} elseif ($do == 'uncache') {
	$id = getGP('id','G','int');
	oa_mana_recache($id.'_menu','menuid','menunum');
	oa_mana_recache($id.'_config','name','id');
	oa_mana_recache($id.'_keytable','id','number');
	usergroup_recache();
	show_msg('系统缓存文件更新成功，该分支机构可以正常登录使用！', 'admin.php?ac=jituan&fileurl=mana');
}
?>