<?php
require($netsFileConfig['sysCoreRoot'].'Inc/m_Comm/Other/commshare.php');

class PaDefault extends CustomPage {
	public $ssetParid=1;
	
	public function PaDefault(){
		$this->CustomPage();
	}
	public function OnPInitial(){
		$this->checkUser('1,2,3,4,5,');
		$this->sTable=array(
			array('sttTableName'=>$this->sysTableHead.'commshare','sttColumnHead'=>'cost'),
			array('sttTableName'=>$this->sysTableHead.'commsharecont','sttColumnHead'=>'cosct'),);
		$this->sUiName='User/comm/share';
		
	}
	public function OnBInitial(){
		$this->sMetaTitle='分享';
		$this->sLimitCount=0;
		$this->sApp=new commshare($this->sConnString);
		//Parid
		$tmpAR0=$this->sControl->sDbInTemplate;
		$tmpAR0['Connection']=$this->sConnString;
		$tmpAR0['SQL']=' SELECT A.cosgtID,A.cosgtName,A.cosgtParid FROM '.$this->sysTableHead.'commsharegroup AS A
			WHERE 1=1 AND A.cosgtRecycle=0 AND A.cosgtChecked>1 AND A.cosgtParid={Nets.Parid}
			ORDER BY A.cosgtOrderBy DESC,A.cosgtID DESC ';
		$tmpAR0['SQL0']=' SELECT A.cosgtID,A.cosgtName,A.cosgtParid FROM '.$this->sysTableHead.'commsharegroup AS A
			WHERE 1=1 AND A.cosgtRecycle=0 AND A.cosgtChecked>1 AND A.cosgtID={Nets.Parid}
			ORDER BY A.cosgtOrderBy DESC,A.cosgtID DESC ';
		$tmpAR0['InitialString']=$this->ssetParid;
		$tmpAR0['Parid']='cosgtID';
		$tmpAR0['Text']='cosgtName';
		$tmpAR0['Value']='cosgtID';
		$tmpAR0['Type']=0;
		$tmpAR0['Style']='';
		$tmpAR0['Title']='';
		$tmpAR0['AddOne']='';
		$this->sParid1=$this->sParid2=$this->sControl->bindDropdown($tmpAR0);
		$tmpAR0=$this->sControl->sDpReturnTemplate;
		$tmpAR0['Text']='[所有分类]';
		$tmpAR0['Value']='-1';
		$tmpAR0['Style']='';
		$tmpAR0['Title']='';
		$tmpAR0['AddOne']='';
		array_push($this->sParid2,$tmpAR0);
		
	}
	public function OnAdd_Frame(){
		$tmpDbDescriptions=array();
		foreach ($this->sTable as $tableCell){
			$tmpDbDescriptions[]=$this->sDbDescriptions[$tableCell['sttTableName']];
		}
		$tmpMainid=$this->AnalyerRequest('mainID',0,1);
		$this->sMainID=$tmpMainid[0];
		$tmpMainUserid=$this->AnalyerRequest('mainUserid',0,1);
		$this->sMainUserid=$tmpMainid[0];
		$tmpMainTableid=$this->AnalyerRequest('mainTableid',0,1);
		$this->sMainTableid=$tmpMainid[0];
		//-- ------ #Add ------ --
		$this->sApp->TransStart();
		//#Get numCode
		$tmpSTR=$this->sTable[0]['sttColumnHead'].'NumCode';
		$tmpDbDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$tmpDbDescriptions[0]['Columns'][$tmpSTR]['Value']='N_'.date('YmdHis');
		$this->sApp->numCode($tmpDbDescriptions[0]);
		//#Set value
		$tmpDbDescription0=$tmpDbDescriptions;
		$Coun0=0;
		foreach ($tmpDbDescription0 as $tableCell){
			foreach ($tableCell['Columns'] as $Column){
				if($Column['stcotName']!=$this->sTable[$Coun0]['sttColumnHead'].'ID' &&
					$Column['stcotName']!=$this->sTable[$Coun0]['sttColumnHead'].'NumCode' &&
					$Column['stcotName']!=$this->sTable[$Coun0]['sttColumnHead'].'CreateBy' &&
					$Column['stcotName']!=$this->sTable[$Coun0]['sttColumnHead'].'CreateTime' &&
					$Column['stcotName']!=$this->sTable[$Coun0]['sttColumnHead'].'IndexID'){
					$tmpAR0=$this->AnalyerRequest($Column['stcotName'].'Txt',$Column['stcoctDefaultValue'],1);
					$tmpDbDescriptions[$Coun0]['Columns'][$Column['stcotName']]['Value']=$tmpAR0[0];
				}
			}
			$Coun0++;
		}
		//-- #Set conventional --
		if(isset($tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'CreateBy']['Value'])){
			$tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'CreateBy']['Value']=$tmpMainUserid[0];
		}
		if(isset($tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'CreateTime'])){
			$tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'CreateTime']['Value']=date($this->sysTimeFormat);
		}
		//DB name,Condition,User index,User column name
		$tmpFlag=0;
		foreach ($this->AnonymousUserData as $Row0){
			if (count($tmpDbDescriptions)>1) {
				if(isset($tmpDbDescriptions[1]['Columns'][$this->sTable[1]['sttColumnHead'].$Row0[0]])){
					if ($tmpDbDescriptions[1]['Columns'][$this->sTable[1]['sttColumnHead'].$Row0[0]]['Value']==$Row0[1]) {
						$tmpFlag=1;
					}
				}
			}
			if ($this->sUserInfo[0]['Columns']['uitID']['Value']==0 && $tmpFlag==1) {
				$tmpDbDescriptions[1]['Columns'][$this->sTable[1]['sttColumnHead'].$Row0[0]]['Value']=$Row0[4];
			}else if ($tmpFlag==1 && isset($tmpDbDescriptions[1]['Columns'][$this->sTable[1]['sttColumnHead'].$Row0[0]])) {
				$tmpDbDescriptions[1]['Columns'][$this->sTable[1]['sttColumnHead'].$Row0[0]]['Value']=$this->sUserInfo[$Row0[2]]['Columns'][$Row0[3]]['Value'];
			}
		}
		//Spical value array
		foreach ($this->sSpicalValue as $Cell){
			if(isset($tmpDbDescriptions[$Cell[0]]['Columns'][$Cell[1]]['Value'])){
				$tmpDbDescriptions[$Cell[0]]['Columns'][$Cell[1]]['Value']=$Cell[2];
			}
		}
		//-- #Set spical --
		$tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'Checked']['Value']=2;
		$tmpDbDescriptions[0]['Columns'][$this->sTable[0]['sttColumnHead'].'CreateBy']['Value']=$this->sUserInfo[0]['Columns']['uitID']['Value'];
		
		//#Add
		$tmpINT=$this->sApp->Check($tmpDbDescriptions,array(),array());
		if($tmpINT!=0){
			$this->sApp->TransRollback();
			$this->sJscript=$this->Message('ERROR','',$this->sApp->sysLastError,'');
			return 1;
		}
		$Coun0=0;
		while (count($tmpDbDescriptions)>$Coun0){
			if($Coun0==0){
				$tmpDbDescriptions[$Coun0]['TableDescription']['SearchColumn']=$this->sTable[$Coun0]['sttColumnHead'].'NumCode,'.$this->sTable[$Coun0]['sttColumnHead'].'ID';
			}else {
				$tmpDbDescriptions[$Coun0]['TableDescription']['SearchColumn']=$this->sTable[$Coun0]['sttColumnHead'].'IndexID';
			}
			$Coun0++;
		}
		$tmpINT=$this->sApp->add($tmpDbDescriptions);
		if($tmpINT!=0){
			$this->sApp->TransRollback();
			$this->sJscript=$this->Message('ERROR','',$this->sApp->sysLastError,'');
			return 1;
		}
		
		foreach ($this->sDb->sysDbCacheClear as $Cell){
			$this->clearCache($Cell);
		}
		foreach ($this->sysPageCacheClear as $Cell){
			$this->clearCache($Cell);
		}
		$this->sApp->TransCommit();
		/*
		$this->sJscript=$this->Message('SUCCESS','','添加成功',$this->sysPage.'?ActionType=Normal&Normal='.$tmpMainid[0].','.$tmpMainTableid[0].'');
		*/
		$this->sJscript=$this->Message_Frame('A,0',
			$this->sysFileConfig['sysCoreRoot'].$this->sysFileConfig['PathManaFrameMessage'],
			$this->sCode->URL_MakeParameter('保存成功',0),
			$this->sCode->URL_MakeParameter($this->sApp->sysLastError,0),
			'');
		return 0;
	}
	public function OnList(){
		//-- ------ #Search data ------ --
		$tmpSTR0='DList0';
		$tmpAR0=$this->getQueryString($tmpSTR0);
		$tmpSTR1='DSort0';
		$tmpAR1=$this->AnalyerRequest($tmpSTR1,'costID,2',0);
		$tmpSTR2='Search0';
		$this->sSearchData=$this->AnalyerRequest($tmpSTR2,',-1',0);
		//#Spell sql
		$tmpSQL=' SELECT A.costID,A.costName,A.costParid
			FROM '.$this->sysTableHead.'commshare AS A 
			WHERE 1=1 
			AND A.costID>'.$this->sLimitCount;
		if($this->sSearchData[0]!=''){
			$tmpSQL.=' AND A.costName LIKE \'%'.$this->sSearchData[0].'%\' ';
		}
		if($this->sSearchData[1]!='-1'){
			$tmpSQL.=' AND A.costParid='.$this->sSearchData[1].' ';
		}
		if($tmpAR1[1]==1){
			$tmpSQL.=' ORDER BY '.$tmpAR1[0].' ';
		}else if($tmpAR1[1]==2){
			$tmpSQL.=' ORDER BY '.$tmpAR1[0].' DESC ';
		}
		$this->sListData=$this->sDb->SearchP($tmpSQL,$tmpAR0);
		//#Set page button
		$this->sPButton->sPage=$this->sysPage;
		$this->sPButton->sExtendParameter='&'.$tmpSTR2.'='.$this->sSearchData[0].$this->sysSPLIT1.$this->sSearchData[1];
		$this->sPButton->setPage($this->sDb->sysPageInfo);
		$this->sPButton->setSort($tmpAR1);
		$this->sSortButton[0]=array('stcotName'=>'costID','stcoctUiName'=>'编号');
		$this->sSortButton[1]=array('stcotName'=>'costName','stcoctUiName'=>'分享名称');
		
		return 0;
	}
	public function OnExternal_Search(){
		//-- ------ #Search data ------ --
		$tmpSTR0='DList0';
		$tmpAR0=$this->getQueryString($tmpSTR0);
		$tmpSTR1='DSort0';
		$tmpAR1=$this->AnalyerRequest($tmpSTR1,'costID,2',0);
		$tmpSTR2='Search0';
		$this->sSearchData=$this->AnalyerRequest($tmpSTR2,',,-1',0);
		$this->sExternal_SearchData=$this->AnalyerRequest($this->sActionType,',-1',0);
		//#Spell sql
		$tmpSQL=' SELECT A.costID,A.costName,A.costParid
			FROM '.$this->sysTableHead.'commshare AS A 
			WHERE 1=1 
			AND A.costID>'.$this->sLimitCount;
		if($this->sSearchData[0]!=''){
			$tmpSQL.=' AND A.costName LIKE \'%'.$this->sSearchData[0].'%\' ';
		}
		if($this->sSearchData[1]!='-1'){
			$tmpSQL.=' AND A.costParid='.$this->sSearchData[1].' ';
		}
		if($tmpAR1[1]==1){
			$tmpSQL.=' ORDER BY '.$tmpAR1[0].' ';
		}else if($tmpAR1[1]==2){
			$tmpSQL.=' ORDER BY '.$tmpAR1[0].' DESC ';
		}
		$this->sListData=$this->sDb->SearchP($tmpSQL,$tmpAR0);
		//#Set page button
		$this->sExtendParameter='&ActionType='.$this->sActionType.'&'.$this->sActionType.'='.$this->sExternal_SearchData[0].','.$this->sExternal_SearchData[1];
		$this->sysPages[0]=$this->sysPage.'?'.$this->sExtendParameter;
		
		$this->sPButton->sPage=$this->sysPage;
		$this->sPButton->sExtendParameter='&'.$tmpSTR2.'='.$this->sSearchData[0].$this->sExtendParameter;
		$this->sPButton->setPage($this->sDb->sysPageInfo);
		$this->sPButton->setSort($tmpAR1);
		$this->sSortButton[0]=array('stcotName'=>'costID','stcoctUiName'=>'编号');
		$this->sSortButton[1]=array('stcotName'=>'costName','stcoctUiName'=>'分享名称');
		
		return 0;
	}
	public function OnExternal_ChildChoose(){
		//-- ------ #Search data ------ --
		$tmpSTR0='External_ChildChoose';
		$this->sExternal_ChildChooseSData=$this->AnalyerRequest($tmpSTR0,'0,,0,0',0);
		//#Spell sql
		$tmpSQL='';
		if($this->sExternal_ChildChooseSData[0]==0 || $this->sExternal_ChildChooseSData[0]==2){
			//-- Same grade --
			$tmpSQL=' SELECT A.costID,A.costName,A.costParid 
				FROM '.$this->sysTableHead.'commshare AS A
				WHERE 1=1 AND A.costRecycle=0 AND A.costChecked>1 AND 
				A.costParid=(SELECT costParid FROM commshare WHERE 1=1 AND costRecycle=0 AND costChecked>1 AND costID={Nets.ID})
				ORDER BY A.costOrderBy DESC,A.costID DESC ';
		}else if($this->sExternal_ChildChooseSData[0]==1 || $this->sExternal_ChildChooseSData[0]==3){
			//-- Child grade --
			$tmpSQL=' SELECT A.costID,A.costName,A.costParid 
				FROM '.$this->sysTableHead.'commshare AS A
				WHERE 1=1 AND A.costRecycle=0 AND A.costChecked>1 AND A.costParid={Nets.ID}
				ORDER BY A.costOrderBy DESC,A.costID DESC ';
		}
		$tmpSTR=str_replace('{Nets.ID}',$this->sExternal_ChildChooseSData[2],$tmpSQL);
		$this->sExternal_ChildChooseData=$this->sDb->Search($tmpSTR);
		
		return 0;
	}
	public function OnNormal(){
		$tmpINT=0;
		$tmpAR0=$this->AnalyerRequest('Normal','0,0',0);
		
		$tmpSQL=' SELECT * 
			FROM '.$this->sysTableHead.'commshare AS A 
			INNER JOIN '.$this->sysTableHead.'commsharecont AS B ON B.cosctIndexID=A.costID ';
		if(count($this->sTable)>2){
			$tmpSQL.=' LEFT OUTER JOIN '.$this->sTable[2]['sttTableName'].' AS C ON C.'.$this->sTable[2]['sttColumnHead'].'IndexID=A.costID ';
		}
		$tmpSQL.=' WHERE 1=1 AND A.costID='.$tmpAR0[0].' ';
		$tmpDataCollection0=$this->sDb->Search($tmpSQL);
		if(count($tmpDataCollection0)>0){
			$tmpRow=$tmpDataCollection0[0];
			$this->sMainID=$tmpRow[$this->sTable[0]['sttColumnHead'].'ID'];
			//-- #Get value --
			$Coun0=0;
			while (count($this->sUiDescription_Normal)>$Coun0) {
				if(isset($tmpRow[$this->sUiDescription_Normal[$Coun0]['stcotName']])){
					$this->sUiDescription_Normal[$Coun0]['Value']=$tmpRow[$this->sUiDescription_Normal[$Coun0]['stcotName']];
					//-- ------ #Spical set ------ --
					if($this->sUiDescription_Normal[$Coun0]['stcotName']=='costParid'){
					}else if($this->sUiDescription_Normal[$Coun0]['stcotName']=='costModelid_Detail'){
						$this->sUiDescription_Normal[$Coun0]['Value']=$this->sMainTableid;
					}
				}
				$Coun0++;
			}
		}else {
			$Coun0=0;
			foreach($this->sUiDescription_Normal as $Row0){
				if($Row0['stcotName']=='costTableid'){
					$this->sUiDescription_Normal[$Coun0]['Value']=$this->sMainTableid;
				}
				$Coun0++;
			}
		}
	}
	public function OnNormal_Frame(){
		$tmpAR0=$this->AnalyerRequest('DURL',',',0);
		//-- ------ #SET ------ --
		$Coun0=0;
		while (count($this->sUiDescription_Normal_Frame)>$Coun0) {
			if ($this->sUiDescription_Normal_Frame[$Coun0]['stcotName']=='costName') {
				$this->sUiDescription_Normal_Frame[$Coun0]['Value']=$tmpAR0[0];
			}else if ($this->sUiDescription_Normal_Frame[$Coun0]['stcotName']=='costURL') {
				$this->sUiDescription_Normal_Frame[$Coun0]['Value']=$tmpAR0[1];
			}else if ($this->sUiDescription_Normal_Frame[$Coun0]['stcotName']=='cosctContent') {
				$this->sUiDescription_Normal_Frame[$Coun0]['Value']=$tmpAR0[0];
			}
			$Coun0++;
		}
		
		return 0;
	}
	
	public function DefUiPanel(&$pColumn,$pFlag){
		parent::DefUiPanel($pColumn,$pFlag);
		if($pColumn['stcotName']=='costParid' && $pFlag=='Search'){
			$pColumn['Option']=$this->sParid2;
		}else if($pColumn['stcotName']=='costParid' && ($pFlag=='Normal' || $pFlag=='List')){
			$pColumn['Option']=$this->sParid1;
		}
		return $pColumn;
	}
	public function DefUiButton($pFlag,$Exception){
		parent::DefUiButton($pFlag,array('Normal_Frame',));
		if ($pFlag=='Normal_Frame' && !in_array($pFlag,$Exception)) { ?>
<input type="submit" value="保存" class="Control2" onclick="tmpCommon.setActionType('Normal_Frame','DAdd_Frame','','');" />

<?php }
	}
}

?>