<?php
class admin{

	var $cTime;
	var $db;
	var $tpl;
	var $page;
	
	function admin()
	{
		global $db;
		global $config;
		global $tpl;
		
		$this -> cTime  = date("Y-m-d H:i:s");
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
	}
	//=====================================
	function is_login($action)
	{
		global $buid,$bcid,$config;
		if(!$buid)
		{
			header("Location: $config[weburl]/login.php");
			exit();
		}
		
		if(empty($_SESSION["STATU"]))
		{
			$this->db->query("select statu from ".ALLUSER." WHERE userid='$buid'");
			$re=$this->db->fetchRow();
			$_SESSION["STATU"]=$re['statu'];
		}
		
		if($_SESSION["STATU"]<=-1&&$_GET['type']!='access_dine'&&$action!='logout')
		{
			header("Location: $config[weburl]/main.php?action=msg&type=access_dine");
			exit();
		}
		
		if(!empty($_SESSION["STATU"])&&$_SESSION["STATU"]==1&&$_GET['s']!="myshop"&&$_GET['s']!="cominfo"&&$action!="msg"&&$action!="logout"&&$action!="admin_personal")
		{
			header("Location: $config[weburl]/main.php?action=msg&type=active");
			exit();
		}
	}
	function msg($url,$str=NULL,$time=NULL)
	{
		$str=urlencode($str);
		$url=urlencode($url);
		header("Location:?action=msg&str=$str&url=$url&time=$time");
	}
	function get_user_common_cat($id)
	{
		$cats=array();
		$sql="select id,common_cat from ".SHOPSETTING." where company_id='$id'";
		$this->db->query($sql);
		$rec=$this->db->fetchRow();
		if(!empty($rec['id']))
		{
			$cat=explode(",",$rec['common_cat']);
			foreach($cat as $v)
			{
				$vc=array();
				if(!empty($v))
				{
					$vc[]=substr($v,0,4);
					if(strlen($v)>4)
						$vc[]=substr($v,0,6);
					if(strlen($v)>6)
						$vc[]=substr($v,0,8);
					if(strlen($v)>8)
						$vc[]=$v;
					$key=implode(",",$vc);
					$cats[$key]=$this->getProTypeName($key);
				}
			}
			return $cats;
		}
		else
			return array();
	}
	function getProTypeName($prod)
	{	
		global $db,$cat_table;
		if(!empty($prod))
		{
			$sql = "select cat from $cat_table where catid in($prod)";
			$db->query($sql);
			$fieldlist="";
			$res=$db->getRows();
			foreach($res as $v)
			{
				if($v["cat"]!="")
				$fieldlist.=$v["cat"]."->";
			}
			$fieldlist = trim($fieldlist,"->");
		}
		return $fieldlist;
	}
	function check_shop_statu()
	{
		/***0,资料不完整，没有开通。1.正常开通。2.管理员强制关闭**/
		global $config,$buid,$bcid;
		include($config['webroot'].'/config/module_company_config.php');
		if($module_company_config['open_shop_guide']==1)
		{
			//---------------企业资料完整性
			$sql="SELECT * FROM ".COMPANY." WHERE company_id='$bcid'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			if($re['company'])
				$shop['user']=1;
			else
				$shop['user']==0;
			//---------------联系人信息完整性
			$sql="SELECT * FROM ".ALLUSER." WHERE userid='$buid'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			if(!$re["name"]||!$re["position"]||!$re["tel"]||!$re["email"]||!$re["mobile"]||!$re["city"]||!$re["province"])
				$shop['alluser']=0;
			else
				$shop['alluser']=1;
			//----------------工商信息完整性
			include_once("module/company/includes/bizinfo_class.php");
			$bizinfo=new bizinfo();
			$de=$bizinfo->get_detail($bcid);
			$shop['biz']=$de['statu'];
			//----------------商情条数
			$sql="select count(id) as num from ".INFO." WHERE userid='$buid'";
			$this->db->query($sql);
			$shop['offernum']=$this->db->fetchField('num');
			
			//-----------------
			if($shop['user']>=2&&$shop['alluser']&&$shop['biz']&&$shop['offernum']>=5&&$shop['newnum']>=5)
			{
				$sql="update ".COMPANY." set shop_statu=1 where company_id='$buid';";
				$this->db->query($sql);
			}
			//-----------------
			return $shop;
		}
		else
			return 'close';	
	}
	function clear_user_shop_cache()
	{
		global $config,$buid;
		$dir=$config['webroot'].'/cache/shop/'.get_userdir($buid).'/';
		$handle = opendir($dir); 
		while ($filename = readdir($handle))
		{
			if(!is_dir($dir.$filename))
				@unlink($dir.$filename);
		}
	}
	function get_personal_detail($id)
	{
		global $buid,$config;
		if(empty($id))
			$id=$buid;
		$sql="select * from ".ALLUSER." a left join ".COUNTRY." b on a.country=b.id WHERE a.userid='$id'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($config['language']=='cn')
			$re['country']=$re['cname'];
		else
			$re['country']=$re['ename'];
		return $re;
	}
	function delete_personal($id)
	{
		global $bcid;
		$sql="delete from ".ALLUSER." WHERE company_id='$bcid' and userid='$id'";
		$this->db->query($sql);
	}
	function get_personal_list()
	{
		global $bcid;
		$sql="select * from ".ALLUSER." WHERE company_id='$bcid' and is_admin is NULL order by userid desc";
		$this->db->query($sql);
		$re=$this->db->getRows();
		return $re;
	}
	function add_personal()
	{
		global $config,$bcid;
		$p=$_POST;
		$p['pas']=md5($p['pass']);
		$time=time();
		$ip=getip();
		$p['sex']=empty($p['sex'])?1:$p['sex'];
		
		$sql="select user from ".ALLUSER." where user='$p[user]'";
		$this->db->query($sql);
		$user=$this->db->fetchField('user');
		if(empty($user))
		{
			$sql="insert into ".ALLUSER." 
			(company_id,user,password,name,email,tel,qq,province,city,sex,msn,skype,position,mobile,lastLoginTime,ip)
			value
			('$bcid','$p[user]','$p[pas]','$p[name]','$p[email]','$p[tel]','$p[qq]','$p[province]','$p[city]',
			'$p[sex]','$p[msn]','$p[skype]','$p[position]','$p[mobile]','$time','$ip')";
			$re=$this->db->query($sql);
			return $re;
		}
	}
	function update_personal($uid)
	{
		global $config,$buid;$logo=NULL;$ssql=NULL;
		if(empty($uid))
			$uid=$buid;
		$_POST['province']=!empty($_POST['province1'])?$_POST['province1']:$_POST['province'];
		$_POST['city']=!empty($_POST['city1'])?$_POST['city1']:$_POST['city'];
		$_POST['sex']=empty($_POST['sex'])?1:$_POST['sex'];
		
		$sql="UPDATE ".ALLUSER." SET
		name='$_POST[name]',email='$_POST[email]',email2='$_POST[email2]',tel='$_POST[tel]',qq='$_POST[qq]',province='$_POST[province]',city='$_POST[city]',sex='$_POST[sex]',skype='$_POST[skype]',position='$_POST[position]',mobile='$_POST[mobile]',logo='$_POST[logo]'
		WHERE userid='$uid'";
		
		$re=$this->db->query($sql);
		return $re;
	}
	function resetpass($buid)
	{
		$sql="SELECT password FROM ".ALLUSER." WHERE userid='$buid'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($re['password']!=md5($_POST["oldpass"]))
			msg("main.php?action=setpassword&info=1");
		elseif($_POST["newpass"]!=$_POST["renewpass"])
			msg("main.php?action=setpassword&info=2");
		else
		{
			$sql="UPDATE ".ALLUSER." SET password='".md5($_POST['newpass'])."' WHERE userid='$buid'";
			$re=$this->db->query($sql);
			if($re)
				msg("main.php?action=setpassword&info=3");
		}
	}
	function getComCat()
	{
		$this->db->query("SELECT catid,cat FROM ".COMPANYCAT." WHERE catid<=9999 order by nums asc");
		$re=$this->db->getRows();
		foreach($re as $key=>$v)
		{
			$s=$v["catid"]."00";
			$b=$v["catid"]."99";
			$this->db->query("SELECT * FROM ".COMPANYCAT." WHERE catid>=$s and catid<=$b ORDER BY nums ASC");
			$re[$key]['sub']=$this->db->getRows();
		}
		return $re;
	}
	/**
	 * $type 1产品，2求购,3相册
	 */
	//======Brad=======================
	function update_custom_cat($type='')
	{
		global $buid,$bcid;
		foreach($_POST['name'] as $key=>$v)
		{
			$_POST['nums'][$key]=$_POST['nums'][$key]?$_POST['nums'][$key]:"0";
			if(!empty($_POST['cid'][$key]))
			{
				$sql="update ".CUSTOM_CAT." set name='$v',nums='".$_POST['nums'][$key]."',company_id='$bcid' where
				id='".$_POST['cid'][$key]."'";
				$re=$this->db->query($sql);
			}
			elseif(!empty($v))
			{
				$sql="insert into ".CUSTOM_CAT." (userid,company_id,name,type,nums) values 
				('$buid','$bcid','$v','$type','".$_POST['nums'][$key]."')";
				$re=$this->db->query($sql);
			}
		}
		if(is_array($_POST['sname']))
		{
			foreach($_POST['sname'] as $key=>$v)
			{
				$_POST['scid'][$key]=$_POST['scid'][$key]?$_POST['scid'][$key]:"0";
				if(!empty($_POST['scid'][$key])&&!empty($_POST['sname'][$key]))
				{
					$sql="update ".CUSTOM_CAT." set name='".$_POST['sname'][$key]."',nums='".$_POST['snums'][$key]."' where
					id='".$_POST['scid'][$key]."'";
					$re=$this->db->query($sql);
				}
			}
		}
		if(is_array($_POST['pid']))
		{
			foreach($_POST['pid'] as $key=>$v)
			{
				if(!empty($v)&&!empty($_POST['addsname'][$key]))
				{
					$sql="insert into ".CUSTOM_CAT." (userid,company_id,pid,name,type,nums) values 
					('$buid','$bcid','".$_POST['pid'][$key]."','".$_POST['addsname'][$key]."','$type','".$_POST['addsnums'][$key]."')";
					$re=$this->db->query($sql);
				}
			}
		}
	}
	function get_custom_cat_list($type="", $single="")
	{
		global $config,$buid;
		if($single>0)
		{
			$sql="select * from ".CUSTOM_CAT." where id='$single' and type='$type' order by nums asc";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
		}
		else
		{
			$sql="select * from ".CUSTOM_CAT." where userid='$buid' and type='$type' and pid=0 order by nums asc";
			$this->db->query($sql);
			$re=$this->db->getRows();
			for($i=0;$i<count($re);$i++)
			{
				$sql="select * from ".CUSTOM_CAT." where pid='".$re[$i]['id']."' and pid!=0 order by nums asc";
				$this->db->query($sql);
				$re[$i]['subcat']=$this->db->getRows();
			}
		}
		return $re;
	}
	function del_custom_cat($deid="")
	{
		global $config,$buid;
		$sql="delete from ".CUSTOM_CAT." where id='$deid' and userid='$buid'";
		$re=$this->db->query($sql);
	}
}
?>