<?php
class user
{
	var $db;
	var $tpl;
	var $page;
	function user()
	{
		global $db;
		global $tpl;		
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
	}
	
	function get_cominfo($uid)
	{

		global $buid,$config;$right=array();
		include($config['webroot']."/config/usergroup.php");
		
		//------------------user info----------
		$sql="select a.*, b.ifpay,b.earnest,b.addr,b.fax,b.company,c.ename,b.province as p,b.statu as status,b.certification,b.ctype from ".ALLUSER." a left join ".COMPANY." b on a.company_id=b.company_id left join ".COUNTRY." c on b.country = c.id  WHERE a.userid='$uid'";
		$this->db->query($sql);
		$cominfo=$this->db->fetchRow();
		
		//-------------------user rank logo-----	
		$cominfo['grouplogo']=empty($group[$cominfo['ifpay']]['minilogo'])?$group[$cominfo['ifpay']]['name']:'<img src=\''.$group[$cominfo['ifpay']]['minilogo'].'\'>';
		//-------------------user gongshang-------
		$cominfo['business']=$cominfo['certification'];
		//----------------------------------------
		$cominfo['time_long']=date("Y")-substr($cominfo['regtime'],0,4)+1;
		return $cominfo;
	}
	
	function get_user_detail($id)
	{
		if(empty($id)) return NULL;
			
		global $config;$catname=NULL;$catv=array();

		$sql="select b.*,a.regtime,a.lastLoginTime,a.logo as plogo,a.ip,a.name
			from ".ALLUSER." a left join ".COMPANY." b on a.company_id=b.company_id WHERE a.userid='$id'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		$catinfo=explode(",",$re['catid']);
		foreach($catinfo as $v)
		{
			$re['catinfo'][]=$this->get_cat_name($v);
		}
		$_SESSION['IFPAY']=$ifpay=empty($re['ifpay'])?1:$re['ifpay'];
		include($config['webroot']."/config/usergroup.php");
		$medallog=empty($group[$ifpay]['logo'])?$config['weburl']."/image/default/icon_person.gif":$group[$ifpay]['logo'];
		$re['medal']="<img width='20' height='20' src=".$medallog." />";
		$re['rank']=$group[$ifpay]['name'];
		$re['modeu']=$group[$ifpay]['modeu'];
		//=========================
		$re['main_pro_list'] = @explode(',',$re['main_pro']);
		$re['purch_pro_list'] = @explode(',',$re['purch_pro']);
		return $re;
	}
	
	function get_cat_name($id)
	{
		if(is_numeric($id))
		{
			global $db;
			$sql="SELECT catid,cat FROM ".COMPANYCAT." WHERE catid='$id'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			return $re;
		}
	}
	
	function update_user()
	{
		global $config,$buid,$bcid;$catid=NULL;$ssql=NULL;
		$pn=time();
		if(!empty($_POST['right_category_id']))
		{
			$catid=implode(",",$_POST['right_category_id']);
			$catid=",".$catid.",";
		}
		//-------------------------------------
		$main_pro = '';//主营产品
		$main_pro_list = $_POST['main_pro'];
		if($main_pro_list!=NULL&&sizeof($main_pro_list)>0){
			foreach($main_pro_list as $pl){
				if($pl!='')
					$main_pro.= str_replace( ',','，',$pl ).',';
			}
		}

		$purch_pro = '';//采购产品
		$purch_pro_list = $_POST['purch_pro'];
		if($purch_pro_list!=NULL&&sizeof($purch_pro_list)>0)
		{
			foreach($purch_pro_list as $pl){
				if($pl!='')
					$purch_pro.= str_replace( ',','，',$pl ).',';
			}
		}
		$_POST['identity']*=1;
		$main_pro = addslashes($main_pro);
		$purch_pro = addslashes($purch_pro);
		
		//--------------------------------------
		$this->db->query("select company_id,shop_statu from ".COMPANY." where company_id='$bcid'");
		$ure=$this->db->fetchRow();
		$company_id=$ure['company_id'];
		$shop_statu=$ure['shop_statu']*1;
		if(!empty($company_id))
		{
			$sql="UPDATE ".COMPANY." SET 
			company='$_POST[company]',contact='$_POST[contact]',tel='$_POST[tel]',fax='$_POST[fax]',zip='$_POST[zip]',province='$_POST[province]',addr='$_POST[addr]',city='$_POST[city]',url='$_POST[url]',ctype='$_POST[ctype]',catid='$catid',main_pro='$main_pro',purch_pro='$purch_pro',staff_num='$_POST[staff_num]',turnover='$_POST[turnover]',registered_capital='$_POST[registered_capital]',uptime='$pn',logo='$_POST[logo]' $ssql WHERE company_id='$bcid'";
			$re=$this->db->query($sql);
			
		}
		else
		{
			$sql="select user,country from ".ALLUSER." where userid='$buid'";
			$this->db->query($sql);
			$puser=$this->db->fetchRow();
			$puser['country']*=1;
			
			$sql="insert into ".COMPANY."
			(company,contact,tel,fax,zip,province,addr,city,url,ctype,company_id,domain,country,catid,logo,registered_capital,main_pro,purch_pro,staff_num,turnover,identity)
			VALUES
			('$_POST[company]','$_POST[contact]','$_POST[tel]','$_POST[fax]','$_POST[zip]','$_POST[province]','$_POST[addr]','$_POST[city]',
			'$_POST[url]','$_POST[ctype]','$cuid','$puser[user]','$puser[country]','$catid','$_POST[logo]','$_POST[registered_capital]','$main_pro','$purch_pro','$_POST[staff_num]','$_POST[turnover]','$_POST[identity]')";
			$re=$this->db->query($sql);
			foreach($_POST['right_category_id'] as $v)
			{
				update_cat_nums($v,'add','com');
			}
		}
		if($shop_statu==0&&$config['open_shop_guide']*1==0)
		{
			$sql="UPDATE ".COMPANY." SET shop_statu=1 where company_id='$uid'";
			$this->db->query($sql);
		}
		return $re;
	}
	
	function update_user_info()
	{
		global $bcid,$config;
		$this->db->query("select company_id from ".CDETAIL." where company_id='$bcid' and type='$_POST[type]'");
		$company_id=$this->db->fetchField('company_id');
		if($company_id)
			$sql="update ".CDETAIL." set intro='$_POST[intro]',logo='$_POST[logo]' where company_id='$bcid' and type='$_POST[type]'";
		else
			$sql="insert into ".CDETAIL." (company_id,intro,type,logo) values ('$bcid','$_POST[intro]','$_POST[type]','$_POST[logo]')";
		$re=$this->db->query($sql);
	}
	
	function get_user_info($bcid,$type)
	{
		global $config;
		$this->db->query("select * from ".CDETAIL." where company_id='$bcid' and type='$type'");
		$de=$this->db->fetchRow();
		//===============================
		$de['fckintro'] = $de['intro'];
		//=================================
		return $de;
	}

}
?>