<?php
class TagLibIvears extends TagLib{
    protected $tags   =  array('Ivears'=>array('attr'=>'table,where,cid,order,limit,id,page,sql,field,key,mod,debug','level'=>3));
    public function _Ivears($attr,$content){
        $tag= $this->parseXmlAttr($attr,'select');
		if(!empty($tag['cid'])){
			$this->ActionName=$tag['table'];
			$arrid=$this->GetCategoryid($tag['cid']);
			$instr=implode(",",$arrid);
			$where2="cid in($instr) ";
		}
		$where1     =!empty($tag['where'])?$tag['where']:' 1 ';	
		if($where2){
		 	$where=$where1." and ".$where2;	
		}else{
			//$where     =!empty($tag['where'])?$tag['where']:' 1 ';	
			$where=$where1;	
		}
		//print_r($where);
         $table     =!empty($tag['table'])?$tag['table']:'';
         $order     =!empty($tag['order'])?$tag['order']:'';
         $limit     =!empty($tag['limit'])?intval($tag['limit']):'';
         $id        =!empty($tag['id'])?$tag['id']:'r';
         $key        =!empty($tag['key'])?$tag['key']:'i';
         $mod        =!empty($tag['mod'])?$tag['mod']:'2';
         $page      =!empty($tag['page'])?$tag['page']:false;
         $sql         =!empty($tag['sql'])?$tag['sql']:'';
         $field     =!empty($tag['field'])?$tag['field']:'';
         $debug     =!empty($tag['debug'])?$tag['debug']:false;
         $this->comparison['noteq']= '<>';
         $this->comparison['sqleq']= '=';
         $where     =$this->parseCondition($where);
         $sql         =$this->parseCondition($sql);
		 $m=$table.mt_rand(100,1000);
		 $ret=$table.mt_rand(100,1000).'ret';
         $parsestr.='<?php $'.$m.'=M("'.$table.'");';
         
         if($sql){
            if($page){
                $limit=$limit?$limit:10;//如果有page，没有输入limit则默认为10
                $parsestr.='import("@.ORG.Page");';
                $parsestr.='$count=count($'.$m.'->query("'.$sql.'"));';
				
				$parsestr.='$pagecount=ceil($count/'.$limit.');';
				
                $parsestr.='$p = new Page ( $count, '.$limit.' );';
                $parsestr.='$sql.="'.$sql.'";';
                $parsestr.='$sql.=" limit ".$p->firstRow.",".$p->listRows."";';
                $parsestr.='$'.$ret.'=$'.$m.'->query($sql);';
                $parsestr.='$pages=$p->show();';
                //$parsestr.='dump($count);dump($sql);';
            }else{
                $sql.=$limit?(' limit '.$limit):'';
                $parsestr.='$'.$ret.'=$'.$m.'->query("'.$sql.'");'; 
            }
         }else{
             if($page){
                 $limit=$limit?$limit:10;//如果有page，没有输入limit则默认为10
                 $parsestr.='import("@.ORG.Page");'; 
				
				if($tag['cid']){
					$parsestr.='if($cid) { ';
					$parsestr.='$this->ActionName="'.$tag["table"].'";';
					$parsestr.='D("Category")->SetActionName("'.$tag["table"].'");';
					$parsestr.='$arrid=D("Category")->GetCategoryid($cid);';
					$parsestr.='$instr=implode(",",$arrid);';
					$parsestr.='$where2="cid in($instr) ";';
					$parsestr.=' } ';
					$parsestr.='$where1     =!empty($tag["where"])?$tag["where"]:" 1 ";';	
					$parsestr.='if($where2) { ';
					$parsestr.='$where=$where1." and ".$where2;';	
					$parsestr.=' } else { ';
					$parsestr.='$where=$where1;';	
					$parsestr.=' } ';	
                 $parsestr.='$count=$'.$m.'->where($where)->count();';
				 $parsestr.='$pagecount=ceil($count/'.$limit.');';
                 $parsestr.='$p = new Page ( $count, '.$limit.' );';
                 $parsestr.='$'.$ret.'=$'.$m.'->field("'.$field.'")->where($where)->limit($p->firstRow.",".$p->listRows)->order("'.$order.'")->select();';					
				}else{
                 $parsestr.='$count=$'.$m.'->where("'.$where.'")->count();';
				 $parsestr.='$pagecount=ceil($count/'.$limit.');';
                 $parsestr.='$p = new Page ( $count, '.$limit.' );';
                 $parsestr.='$'.$ret.'=$'.$m.'->field("'.$field.'")->where("'.$where.'")->limit($p->firstRow.",".$p->listRows)->order("'.$order.'")->select();';					
				}
				 
				 

                 $parsestr.='$pages=$p->show();';
             }else{
                 $parsestr.='$'.$ret.'=$'.$m.'->field("'.$field.'")->where("'.$where.'")->order("'.$order.'")->limit("'.$limit.'")->select();'; 
             }            
         }      
         if($debug!=false){
            $parsestr.='dump($'.$ret.');dump($'.$m.'->getLastSql());';
         }
         $parsestr.= 'if ($'.$ret.'){ $'.$key.'=0;';
         $parsestr.= 'foreach($'.$ret.' as $key=>$'.$id.')';
         $parsestr.= '{ ++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>';
         $parsestr.= $this->tpl->parse($content);      
         $parsestr.= '<?php }}?>';       
         return $parsestr;
         
    }
	
	public function GetCategoryid($pid){
		$arrid[]=$pid;
		$arrid=$this->GetCategoryCid($pid,$arrid);
		return $arrid;
	}
	
	public function GetCategoryCid($cid,&$arrid){
    	$category = D("Category");
        $where = array('model'=>$this->ActionName,'pid'=>$cid);
        $ary_data = $category->where($where)->select();
		if($ary_data){
			foreach($ary_data as $key=>$one){
					$arrid[]=$one['id'];
					$this->GetCategoryCid($one['id'],$arrid);
			}	
		}
		return $arrid;
	}		
	
}
?>