<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class prolistModel{
	function Run(&$atts, &$refObj, &$fields){
		global $product_class,$predata_class;
		
		$attlist = "cid=,pid=,row=100,flag=true,options_id=,range=,options_values_id=,titlelen=,desclen=,orderby=,saleid=,imgsizer=,bgcolor=,type=,separator=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE);
		
		$orderby=array_filter(explode(",",$orderby));
		$orderby_case=$orderby[0];
		$orderby_field="products_date_added";
		if(isset($orderby[1])){
			$orderby_field=$orderby[1];
		}
		
		
		switch($orderby_case){
			case "desc":
				$orderby=array("p.",$orderby_field,"desc",TABLE_PRODUCTS);
			break;
			case "asc":
				$orderby=array("p.",$orderby_field,"asc",TABLE_PRODUCTS);
			break;
			case "rand":
				$orderby=array("","","rand()",TABLE_PRODUCTS);
			break;
			default:
				$orderby=array("p.",$orderby_field,"desc",TABLE_PRODUCTS);
			break;
		}
		$categories=$predata_class->getPredata(TABLE_CATEGORIES);
	
		$line = empty($row) ? 100 : $row;
		$cateloop=false;
		if(!$cid && isset($fields['categories_id'])){
			$cid=$fields['categories_id'];
			$cateloop=true;
		}
		if(!$cid){$cid="all";}
		if($cid!="all"){
			if($cid=="current" && array_key_exists('cPath',$_GET)){
				$cid=openzc_get_current_cpath($_GET['cPath']);
			}
			$cid=array_filter(explode(",",$cid));
			
			foreach($cid as $k => $v){
				@$ids.=openzc_get_son_ids($categories,$v,$v).",";
			}
			$cid=join("','",array_filter(explode(",",$ids)));
			
		}
		if($type=="compare" || $type=="wishlist"){
			if(isset($_SESSION[$type])){
				if($pid=="current"){$pid=$_SESSION[$type]['current'];}
				else{$pid=$_SESSION[$type]['list'];}
				if(!$_SESSION[$type]['list']){return false;}
			}else{
				return false;
			}
		}
		if(isset($GLOBALS['ajaxResult']['products_id']) && isAjax()){$pid=$GLOBALS['ajaxResult']['products_id'];}

		if($type=="preview"){
			if(isset($_SESSION['products_preview_id'])){
				$pid=$_SESSION['products_preview_id'];
				unset($_SESSION['products_preview_id']);
			}else{
				$pid="-1";
			}
		}
		
		$parameter=array('cid'=>$cid,'pid'=>$pid,'row'=>$line,'flag'=>$flag,'titlelen'=>$titlelen,"desclen"=>$desclen,'orderby'=>$orderby,'saleid'=>$saleid,'imgsizer'=>$imgsizer,'bgcolor'=>$bgcolor,"type"=>$type,'range'=>$range,'options_id'=>$options_id,'options_values_id'=>$options_values_id,'fields'=>$fields,"separator"=>$separator);
		if($cateloop==true){
			$parameter['cateloop']=$cateloop;
			$parameter['allcids']=$fields['allcids'];
			$parameter['current_loop_cid']=$fields['categories_id'];
		}
		$products=$product_class->get_product_list($_GET,$parameter);
		$atts="";
		if($flag=="relate"){
			//print_r($products);exit;
		}
		//print_r($products);exit;
		return $products;
	}
	
	
}
  
?>