<?php
class offer
{
	var $db;
	var $tpl;
	var $page;
	var $pic_save_path;	 
	
	function offer()
	{
		global $db;
		global $config;
		global $tpl;		
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
		$this -> cTime  = date("Y-m-d H:i");
		$this->pic_save_path = "";//图片保存目录
		if(!empty($_POST))
		{
			include_once($config['webroot'].'/includes/filter_class.php');
			$word=new Text_Filter();
			$_POST['con']=$word->wordsFilter($_POST['con'], $matche_row);
			$_POST['detail']=$word->wordsFilter($_POST['detail'], $matche_row);
			$_POST['title']=$word->wordsFilter($_POST['title'], $matche_row);
		}
	}
	
	function check_access()
	{
		global $config,$buid,$bcid;
		//-------------------------------强制工商认证
		@include($config['webroot'].'/config/module_company_config.php');
		if($module_company_config['certification']*1>=1)
		{
			$sql="select statu from ".COMPANY." where company_id='$bcid'";
			$this->db->query($sql);
			if($this->db->fetchField('statu')<1)
				msg("main.php?m=company&s=business_info");
		}
		//------------------------------
		$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
		$sql="select count(id) as num from ".INFO." WHERE userid='$buid'";
		$this->db->query($sql);
		$num=$this->db->fetchField('num');
		if($num>$config['offer_'.$ifpay])
			return false;
		else
			return true;
	}
	
	function add_info($type)
	{
		if(empty($_POST['catid'])&&empty($_POST['title'])&&empty($_POST['con']))
			return false;
		global $buid,$bcid,$config;
		//===================================过滤<a
		$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
		if($config['replace_outside_link_'.$ifpay]==1)
			$_POST["detail"]=replace_outside_link($_POST["detail"]);
		//======================================
		$cpc=$this->get_user_pv($buid);
		
		if(empty($_POST['con']))
		   $con=csubstr(strip_tags($_POST["detail"]),0,100);
		else
		   $con=htmlspecialchars($_POST["con"]);
		
		$title=htmlspecialchars($_POST["title"]);
		$catid=$_POST['catid'];
		if(!empty($_POST['tcatid']))
			$catid=$_POST['tcatid'];
		if(!empty($_POST['scatid']))
			$catid=$_POST['scatid'];
		if(!empty($_POST['sscatid']))
			$catid=$_POST['sscatid'];
		$cat_name=$this->readCatName($catid);
		$infoCheck=$config['infoCheck_'.$ifpay];
		if(!get_magic_quotes_gpc())
			$cat_name['cat']=addslashes($cat_name['cat']);
		//=====================添加图片
		$pic = implode(',',array_unique($_POST['pic']));
		
		$sql="insert into ".INFO."
		(userid,catid,cat_name,title,con,uptime,statu,price,priceDes,type,brand,valid_time,pic,user,keywords,country,province,city,company_id,company,ifpay,custom_cat)
		values
		('$buid','$catid','$cat_name[cat]','$title','$con','$this->cTime','$infoCheck','$_POST[price]','$_POST[unit]','$_POST[type]','$_POST[pinpai]','$_POST[valid_time]','$pic','$_COOKIE[USER]','$_POST[keywords]','$cpc[country]','$cpc[province]','$cpc[city]','$bcid','$cpc[company]','$ifpay','$_POST[custom_cat]')";
		$this->db->query($sql);
		$id=$this->db->lastid();
		$this->db->query("INSERT INTO ".OFFERDETAIL." (userid,offerid,detail) values ('$buid','$id','$_POST[detail]')");
		
		//--------------------------------------------------标签 
		include_once($config['webroot']."/includes/tag_inc.php");
		add_tags($_POST['keyword'],2,$id);
		//--------------------------------------------------积分
		include($config['webroot']."/includes/point_inc.php");
		if($point_config['point']=='1'&&$point_config['$offer_pic']!='0')
			renew_point('',$point_config['add_offer']);
		//--------------------------------------------------填加自定义字段
		if(isset($_POST['pinpai']))
		{
			include_once("$config[webroot]/includes/plugin_add_field_class.php");
			$addfield = new AddField('offer');
			$addfield->update_con($id,$cat_name['ext_table']);
		}
		//-------------------------------------------------
		return $id;
	}
	
	function edit_info($type)
	{
		global $buid,$config;
		//===================================过滤<a
		$ifpay=empty($_SESSION["IFPAY"])?1:$_SESSION["IFPAY"];
		if($config['replace_outside_link_'.$ifpay]==1)
			$_POST["detail"]=replace_outside_link($_POST["detail"]);
		//===================================
		$infoCheck=$config['infoCheck_'.$ifpay];//是否需要审核
		
		$title=htmlspecialchars($_POST["title"]);
		
		if(empty($_POST['con']))
		   $con=csubstr(strip_tags($_POST["detail"]),0,100);
		else
		   $con=htmlspecialchars($_POST["con"]);
		
		$cpc=$this->get_user_pv($buid);
		
		$catid=$_POST['catid'];
		if(!empty($_POST['tcatid']))
			$catid=$_POST['tcatid'];
		if(!empty($_POST['scatid']))
			$catid=$_POST['scatid'];
		if(!empty($_POST['sscatid']))
			$catid=$_POST['sscatid'];		
		$cat_name=$this->readCatName($catid);
		if(!get_magic_quotes_gpc())
			$cat_name['cat']=addslashes($cat_name['cat']);
			
		if(!empty($_POST['dele_pic'])||!empty($_POST['pic']))
		{	
			$pic = implode(',',array_unique($_POST['pic']));
			$sub_sql .= ",pic='$pic'";
		}

		$sql="update ".INFO." SET 
		catid='$catid',type='$_POST[type]',cat_name='$cat_name[cat]',title='$title',con='$con',
	    price='$_POST[price]',priceDes='$_POST[unit]',brand='$_POST[pinpai]',valid_time='$_POST[valid_time]',keywords='$_POST[keywords]',country='$_POST[country]',province='$cpc[province]',city='$cpc[city]',company='$cpc[company]',ifpay='$ifpay',statu='$infoCheck',custom_cat='$_POST[custom_cat]',uptime='".$this -> cTime."'".$sub_sql." WHERE id='$_POST[editID]' and userid='$buid'";
		$re=$this->db->query($sql);
		if($re)
		{	
			$this->db->query("update ".OFFERDETAIL." set detail='$_POST[detail]' where offerid='$_POST[editID]'");
			include_once($config['webroot']."/includes/tag_inc.php");
			edit_tags($_POST['keyword'],2,$_POST['editID']);
			//----------------------------填加自定义字段
			if(isset($_POST['pinpai']))
			{
				include_once("$config[webroot]/includes/plugin_add_field_class.php");
				$addfield = new AddField('offer');
				$addfield->update_con($_POST[editID],$cat_name['ext_table']);
			}
		}
		return true;
	}
	
	function add_user_common_cat($cid)
	{
		global $buid;
		//-------------------
		$cid=explode(",",$cid);
		$id=$cid[0];
		if(!empty($cid[1]))
			$id=$cid[1];
		if(!empty($cid[2]))
			$id=$cid[2];
		if(!empty($cid[3]))
			$id=$cid[3];
		$sql="select id from ".SHOPSETTING." where userid='$buid'";
		$this->db->query($sql);
		$rec=$this->db->fetchRow();
		if($rec['id'])
			$sql="update ".SHOPSETTING." set common_cat=REPLACE(common_cat,',$id',''),common_cat=concat(common_cat,',$id') where userid='$buid'";
		else
			$sql="insert into ".SHOPSETTING." (userid,rec_pro,common_cat) values ('$buid','',',$id')";
		$re=$this->db->query($sql);
	}

	
	function get_user_pv($userid)
	{
		$sql="select * from ".ALLUSER." where userid='$userid'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		foreach($re as $key=>$v)
		{
			if(!is_array($v))
				$re[$key]=addslashes($v);
		}
		return $re;
	}
	
	function info_list($type)
	{
		global $buid;
		if(isset($_GET['statu']))
		{	
			$_GET['statu']*=1;
			if($_GET['statu']=='1')
				$sql=" and statu>=1 ";
			else
				$sql=" and statu='$_GET[statu]' ";
		}
		$sql="select id,type,title,uptime,statu,pic,valid_time from ".INFO." 
			where userid='$buid' $sql order by uptime desc";
		$this->db->query($sql);
		//=============================
	  	$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$page->totalRows =$this->db->num_rows();
		}
        $sql .= "  limit ".$page->firstRow.",20";
		//=============================
		$res=$this->db->getRows();
		foreach($res as $pl)
		{
		
			$pl['img'] = explode(',',$pl['pic']);
			if( $pl['img']=='')
				$pl['nopic']=1;
			$re["list"][]=$pl;	
		}
		$re["page"]=$page->prompt();
		return $re;
	}
	
	function space_offer_list($catid)
	{
		global $config;
		if(!empty($catid))
			$sql=" and custom_cat='$catid'";
		
		$sql="select id,`type`,title,con,uptime,userid,user,pic,price from ".INFO." a where company_id='$_GET[uid]' $sql order by id desc";
		
		//--------------------------------------------------
		include_once($config['webroot']."/includes/page_utf_class.php");
		$page = new Page;
		$page->listRows=16;
		$page->url="shop.php";
		if (!$page->__get('totalRows')){
			$this->db->query($sql);
			$page->totalRows = $this->db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.", ".$page->listRows;
		//--------------------------------------------------
		$this ->db ->query($sql);
		$infoList['list'] = null;
		while($pl=$this ->db ->fetchRow())
		{
			$pl['img'] = $this->get_cover_img( $pl['pic'] );
			$infoList['list'][] = $pl;
		}
		$infoList['page']=$page->prompt();
		return $infoList;
	}
	
	function del_info($id,$uid=NULL)
	{
		global $buid,$config;
		if(!empty($uid))
			$buid=$uid;
			
		include($config['webroot']."/includes/point_inc.php");
		if($point_config['point']=='1'&&$point_config['del_offer']!='0')
			renew_point('',$point_config['del_offer']);
		
		$this->db->query("select catid,pic from ".INFO." where id='$id' and userid='$buid'");
		$re=$this->db->fetchRow();
		
		update_cat_nums($re['catid'],'del','offer');

		//--------------------------删除扩展信息
		$cat_name=$this->readCatName($re['catid']);
		if($cat_name['ext_table'])
		{
			include_once("$config[webroot]/includes/plugin_add_field_class.php");
			$addfield = new AddField('offer');
			$addfield->delete_con($id,$cat_name['ext_table']);
		}
		//---------------------------
		
		$this->db->query("delete from  ".INFO." where id='$id' and userid='$buid'");
		$this->db->query("delete from  ".OFFERDETAIL." where offerid='$id'");
		
		include_once($config['webroot']."/includes/tag_inc.php");
		del_tag($id,2);

		//---------------------------删除图片
		if($re['pic']!='')
		{
			$pic = explode( ',',$re['pic'] );
			foreach($pic as $i)
			{
				@unlink($config['webroot'].'/'.$this->pic_save_path."$i");
				@unlink($config['webroot'].'/'.$this->pic_save_path.substr($i,0,strrpos($i,'/')+1).'big_'.substr($i,strrpos($i,'/')+1));
			}
		}
	}
	
	function update_info($id)
	{
		global $buid,$config;
		if($id=="all")
		{
			include("includes/point_inc.php");
			if($point_config['point']=='1'&&$point_config['renew_offer']!='0')
			renew_point('',$point_config['renew_offer']);
			$sql="update ".INFO." SET uptime='$this->cTime' where userid='$buid'";
		}
		else
			$sql="update ".INFO." SET uptime='$this->cTime' where id='$id'";
		$this->db->query($sql);
	}

	function readCatName($id)
	{
		$this->db->query("select * from ".OCAT." where catid='$id'");
		$re=$this->db->fetchRow();
		return $re;
	}
	
	function info_detail($id)
	{
		if($id)
		{
			global $config,$buid;
			include_once($config['webroot']."/includes/tag_inc.php");
			$sql="select a.*,b.detail from ".INFO." a left join ".OFFERDETAIL." b on a.id=b.offerid 
				where a.id='$id' and b.userid='$buid'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			
			$sql="select ext_table from ".OCAT." where catid='$re[catid]'";
			$this->db->query($sql);
			
			$re['ext_table']=$this->db->fetchField('ext_table');
			$re['ocatid']=$re['catid'];
			
			if(strlen($re['catid'])>8)
				$re['sscatid']=$re['catid'];
			
			if(strlen($re['catid'])>6)
				$re['scatid']=substr($re['catid'],0,8);
			
			if(strlen($re['catid'])>4)
				$re['tcatid']=substr($re['catid'],0,6);
			
			$re['catid']=substr($re['catid'],0,4);

			$re['custom_cat_id'] =$re['custom_cat'];
			
			$re['keyword']=get_tags($id,2);
			
			
			//=================================
			if(!empty($re['pic'])){
				$pic = $this->get_cover_img( $prod['pic'] );
				$re['cover_big'] = substr($pic,0,strrpos($pic,'/')+1).'big_'.substr($pic,strrpos($pic,'/')+1);
				$re['pic'] = explode(",",$re['pic']);		
			}
			return $re;
		}
	}
	//==================================================
	function space_offer_detail($id)
	{
		if(is_numeric($id))
		{
			global $config;
			$valid_time=explode('|',$config['valid_time']);
			$infoType=explode('|',$config['infoType']);
			
			$sql="select a.*,b.detail from ".INFO." a left join ".OFFERDETAIL." b on a.id=b.offerid 
			      where a.id='$_GET[id]'";
			$this->db->query($sql);
			$info=$this->db->fetchRow();
			$info['offertype']=$infoType[$info['type']];
			$info['valid_time']=$valid_time[$info['valid_time']];
			
			if(!empty($info['pic']))
			{
				$pic = $this->get_cover_img( $info['pic'] );
				$info['cover'] = $pic;
				$info['pic'] = explode(",",$info['pic']);		
			}
			return $info;
		}
		else
		{
			header("Location: 404.php");
		    exit();
		}
	}

	//##产品封面
	function get_cover_img( $pic='' )
	{
		global $config;
		if($pic=='' )
			return NULL;
		$pic = explode( ',',$pic );
		foreach( $pic as $v ){
		if(file_exists(str_replace($config['weburl'],$config['webroot'],$v)))
			{
			return $v;
			}
		}
		return '';
	}
	
	//================================================
	function getProTypeName($prod)
	{
		if(!empty($prod))
		{
			$sql = "select cat from ".OCAT." where catid in($prod)";
			$this->db->query($sql);
			$fieldlist="";
			$res=$this->db->getRows();
			foreach($res as $v)
			{
				if($v["cat"]!="")
				$fieldlist.=$v["cat"]."->";
			}
			$fieldlist = trim($fieldlist,"->");
		}
		return $fieldlist;
	}
	
	function offer_merge_user($array)
	{
		$old_uid=$array['old_uid'];
		$new_uid=$array['new_uid'];

		$sql = "update ".INFO." set userid='$new_uid' where userid='$old_uid'";
		$this->db->query($sql);
		$sql = "update ".OFFERDETAIL." set userid='$new_uid' where userid='$old_uid'";
		$this->db->query($sql);
	}
}
?>
