<?php
class cmsdoc extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function cmsdoc($pConnection){
		$this->netsDb($pConnection);
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		
		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='cdtName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		return 0;
	}
	
	public function initialObject($pID){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Check cookie exist ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsDoc_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		//-- ------ #Update data ------ --
		//-- #Update Discuss --
		$tmpCountConfigs=array();
		//#Info
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'cmsdoccont';
		$tmpAR1['Parent_CountColumnName']='cdctCount_Discuss';
		$tmpAR1['Parent_Condition']=' AND cdctID='.$pID;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'cmsdocdiscuss';
		$tmpAR1['Child_A_CountColumnName']='cddtID';
		$tmpAR1['Child_A_Condition']=' AND cddtRecycle=0 AND cddtChecked>1 AND cddtParid='.$pID;
		$tmpCountConfigs[]=$tmpAR1;
		//#Group
		$tmpSQL=' SELECT A.cdtID,A.cdtParid
			FROM {Nets.TableHead}cmsdoc AS A 
			WHERE 1=1 AND A.cdtRecycle=0 AND A.cdtChecked>1 AND A.cdtID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
		$tmpData0=$this->Search($tmpSTR);
		$tmpParid=0;
		if (count($tmpData0)>0) {
			$tmpParid=$tmpData0[0]['cdtParid'];
		}
		
		$tmpAR1=$this->sCountUpdateConfig_Template;
		$tmpAR1['Parent_TableName']=$this->sysTableHead.'cmsdocgroupcont';
		$tmpAR1['Parent_CountColumnName']='cdgctCount_Info';
		$tmpAR1['Parent_Condition']=' AND cdgctID='.$tmpParid;
		$tmpAR1['Child_A_TableName']=$this->sysTableHead.'cmsdocgroup';
		$tmpAR1['Child_A_CountColumnName']='cdtID';
		$tmpAR1['Child_A_Condition']=' AND cdtRecycle=0 AND cdtChecked>1 AND cdtParid='.$tmpParid;
		$tmpCountConfigs[]=$tmpAR1;
		
		$tmpINT=$this->updateCount($tmpCountConfigs);
		//-- #Update Hit --
		if($tmpAR0[0]==''){
			$tmpSQL=' UPDATE {Nets.TableHead}cmsdoccont SET cdctCount_Views=cdctCount_Views+1 WHERE 1=1 AND cdctIndexID={Nets.ID} ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$tmpINT=$this->Execute($tmpSTR);
			if($tmpINT!=0){
				$this->sysLastError='更新文档数据时出错，请稍后再试。';
				$this->TransRollback();
				return $tmpINT;
			}
			$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		}
		
		$this->TransCommit();
		return 0;
	}
	public function onEvaluation($pID,$pIndex,$pEvaluations){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCode=new netsCode();
		$tmpHtml=new netsHtml();
		$this->TransStart();
		//-- ------ #Check cookie exist ------ --
		//Get cookie
		$tmpCookieName=$this->sysCookieHead.'Cookie_CmsDoc_Evaluation_'.$pID;
		$tmpAR0=$tmpHtml->AnalyerRequest($tmpCookieName,'',2);
		if($tmpAR0[0]!=''){
			$this->sysLastError='您已为此文档打过分数。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ #Check evaluation count ------ --
		$tmpAR0=explode($this->sysSPLIT4,$pEvaluations);
		//-- #Select by id --
		$tmpSQL=' SELECT A.cdtID,B.cdctEvaluation 
			FROM '.$this->sysTableHead.'cmsdoc AS A INNER JOIN '.$this->sysTableHead.'cmsdoccont AS B ON B.cdctIndexID=A.cdtID
			WHERE 1=1 AND A.cdtRecycle=0 AND A.cdtChecked>1 AND A.cdtID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)!=1){
			$this->sysLastError='对不起，您提交的参数不正确，请与管理员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'002';
		}
		//-- #Reset evaluation data --
		$tmpAR1=explode($this->sysSPLIT1,$tmpData0[0]['cdctEvaluation']);
		if(count($tmpAR0)!=count($tmpAR1)){
			$tmpAR1=array();
			$Coun0=0;
			foreach ($tmpAR0 as $Cell){
				$tmpAR1[]=0;
				$Coun0++;
			}
		}
		//-- #Check index out size --
		if(count($tmpAR1)<=$pIndex){
			$this->sysLastError='对不起，您提交的参数不正确，请与管理员联系。';
			$this->TransRollback();
			return $this->sysErrorFuncID.'003';
		}
		//-- #Add code --
		$tmpAR1[$pIndex]+=1;
		//-- ------ #Spell evaluation data ------ --
		$Coun0=0;
		foreach ($tmpAR1 as $Cell){
			if($Coun0==0){
				$tmpData0[0]['cdctEvaluation']=$Cell;
			}else {
				$tmpData0[0]['cdctEvaluation'].=$this->sysSPLIT1.$Cell;
			}
			$Coun0++;
		}
		//-- ------ #Update ------ --
		$tmpSQL=' UPDATE '.$this->sysTableHead.'cmsdoccont SET cdctEvaluation=\'{Nets.Evaluation}\' WHERE 1=1 AND cdctIndexID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.Evaluation}',$tmpData0[0]['cdctEvaluation'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$tmpData0[0]['cdtID'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->sysLastError='提交打分错误，请稍后再试。';
			$this->TransRollback();
			return $tmpINT;
		}
		//-- ------ #Write cookie ------ --
		$this->writeCookie($this->sysNormalCookiePath,$tmpCookieName,1);
		
		$this->TransCommit();
		return 0;
	}
	public function makeSitemap($pKeyword){
		$tmpID=0;
		//-- ------ # ------ --
		$tmpSQL=' SELECT 
			A.citID,A.citName
			FROM '.$this->sysTableHead.'cmsintroduct AS A 
			WHERE 1=1 AND A.citName LIKE \'{Nets.Name}\' LIMIT 0,1 ';
		$tmpSTR=str_replace('{Nets.Name}',$pKeyword,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			return 1;
		}
		$tmpID=$tmpData0[0]['citID'];
		//-- ------ #Analyse sitemap config ------ --
		$tmpSTR=$this->sysFileConfig['SitemapTemplate'];
		$tmpOpen_Introduct=0;
		$tmpStart_IntroductgroupID=1;
		$tmpLevel_Introductgroup=0;
		$tmpStart_DocgroupID=1;
		$tmpLevel_Docgroup=0;
		$tmpContainDoc_DocgroupID=array();
		$tmpSitemapTemplate=array();
		
		$tmpAR0=explode($this->sysSPLIT7,$this->sysFileConfig['SitemapTemplate']);
		if(count($tmpAR0)!=2){
			return 2;
		}
		$tmpAR1=explode($this->sysSPLIT4,$tmpAR0[0]);
		if(count($tmpAR1)!=7){
			return 2;
		}
		$tmpSitemapTemplate=explode($this->sysSPLIT4,$tmpAR0[1]);
		if(count($tmpSitemapTemplate)!=5){
			return 2;
		}
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[0]);
		if(count($tmpAR0)!=2) return 2;
		$tmpOpen_Introduct=$tmpAR0[1];
		
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[1]);
		if(count($tmpAR0)!=2) return 2;
		$tmpStart_IntroductgroupID=$tmpAR0[1];
		
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[2]);
		if(count($tmpAR0)!=2) return 2;
		$tmpLevel_Introductgroup=$tmpAR0[1];
		
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[3]);
		if(count($tmpAR0)!=2) return 2;
		$tmpStart_DocgroupID=$tmpAR0[1];
		
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[4]);
		if(count($tmpAR0)!=2) return 2;
		$tmpLevel_Docgroup=$tmpAR0[1];
		
		$tmpAR0=explode($this->sysSPLIT1,$tmpAR1[5]);
		if(count($tmpAR0)<1) return 2;
		$Coun0=0;
		foreach ($tmpAR0 as $Cell){
			if($Coun0>0){
				$tmpContainDoc_DocgroupID[]=$tmpAR0[$Coun0];
			}
			$Coun0++;
		}
		
		$tmpContent=$tmpSitemapTemplate[0];
		if($tmpOpen_Introduct==1){
			$tmpSQL=' SELECT
				A.cigtID,A.cigtName
				FROM '.$this->sysTableHead.'cmsintroductgroup AS A
				WHERE 1=1 AND A.cigtRecycle=0 AND A.cigtChecked>1 AND A.cigtParid={Nets.Parid}
				ORDER BY A.cigtOrderBy DESC,A.cigtID DESC ';
			$tmpSTR=str_replace('{Nets.Parid}',$tmpStart_IntroductgroupID,$tmpSQL);
			$tmpData0=$this->Search($tmpSTR);
			foreach ($tmpData0 as $Row0){
				$tmpSTR=str_replace('<Nets.IntroductgroupName>',$Row0['cigtName'],$tmpSitemapTemplate[1]);
				$tmpContent.=$tmpSTR;
				
				$tmpSQL=' SELECT
					A.citID,A.citName
					FROM '.$this->sysTableHead.'cmsintroduct AS A
					WHERE 1=1 AND A.citRecycle=0 AND A.citChecked>1 AND A.citParid={Nets.Parid}
					ORDER BY A.citOrderBy DESC,A.citID DESC ';
				$tmpSTR=str_replace('{Nets.Parid}',$Row0['cigtID'],$tmpSQL);
				$tmpData0=$this->Search($tmpSTR);
				foreach ($tmpData0 as $Row0){
					$tmpSTR=str_replace('<Nets.IntroductID>',$Row0['citID'],$tmpSitemapTemplate[2]);
					$tmpSTR=str_replace('<Nets.IntroductName>',$Row0['citName'],$tmpSTR);
					$tmpContent.=$tmpSTR;
				}
				$tmpContent.=$this->sysSPLIT8.$this->sysSPLIT8;
			}
		}
		
		$tmpSQL=' SELECT
			A.cdgtID,A.cdgtName
			FROM '.$this->sysTableHead.'cmsdocgroup AS A
			WHERE 1=1 AND A.cdgtRecycle=0 AND A.cdgtChecked>1 AND A.cdgtParid={Nets.Parid}
			ORDER BY A.cdgtOrderBy DESC,A.cdgtID DESC ';
		$tmpSTR=str_replace('{Nets.Parid}',$tmpStart_DocgroupID,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		foreach ($tmpData0 as $Row0){
			$tmpSTR=str_replace('<Nets.docgroupID>',$Row0['cdgtID'],$tmpSitemapTemplate[3]);
			$tmpSTR=str_replace('<Nets.docgroupName>',$Row0['cdgtName'],$tmpSTR);
			$tmpContent.=$tmpSTR;
			
			$tmpSTR=str_replace('{Nets.Parid}',$Row0['cdgtID'],$tmpSQL);
			$tmpData0=$this->Search($tmpSTR);
			foreach ($tmpData0 as $Row0){
				$tmpSTR=str_replace('<Nets.docgroupID>',$Row0['cdgtID'],$tmpSitemapTemplate[4]);
				$tmpSTR=str_replace('<Nets.docgroupName>',$Row0['cdgtName'],$tmpSTR);
				$tmpContent.=$tmpSTR;
			}
			$tmpContent.=$this->sysSPLIT8.$this->sysSPLIT8;
		}
		//-- ------ #Update ------ --
		$tmpSQL=' UPDATE '.$this->sysTableHead.'cmsintroductcont SET cictContent=\'{Nets.Content}\' WHERE 1=1 AND cictIndexID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.Content}',$tmpContent,$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$tmpID,$tmpSTR);
		$this->Execute($tmpSTR);
		return 0;
	}
}
?>