<?php

class productModel extends Model{

    function __construct($val=""){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."product".$val;
			$this->fieldList=$this->get_fields($this->tabName,'list');
		}

		function get_cate($id=0){
			if($id==='ALL'){
				return $this->findAll("", "", "","where channelid=$_REQUEST[channelid]","ordering,id");
			}else{

				return $this->findAll("", "", "","where channelid=$_REQUEST[channelid] and  parentid=$id","ordering,id");
			}


		}


		function cate_select($mod=0,$id=0){
			static $sp="",$html="";
			$m='';
			if(!empty($mod)){
			$val=$this->find($mod);
			$m= $val['parentid'];
			}
			if($_REQUEST[select_id]){
				$m=$_REQUEST[select_id];
			}
			if($_REQUEST[cateid]){
				$m=$_REQUEST[cateid];
			}
			$val = $this->get_cate($id);
			if($val!=null){
				foreach ($val as $k=>$v){
					if($v['id']==$m){
						$html.="<option value=".$v['id']." selected='selected'>$sp $v[catename]</option>";
					}else{
						$html.="<option value=".$v['id']." >$sp $v[catename]</option>";
					}
					$sp.="&nbsp;&nbsp;";
					$this->cate_select($mod,$v['id']);
					$sp=substr($sp,12);
				}
			}
			return $html;

		}




		function cate_list($parentid=0){	//分类列表！
		$val=$this->get_cate('ALL');
		static $space="",$html="";
			for($i=0;$i<count($val);$i++){
        		 if($val[$i][parentid]==$parentid){
         				if(empty($val[$i]['catepic'])){
         					$catpic="admin/images/cate_nopic.gif";
         				}else{
         					$catpic=$val[$i]['catepic'];
         				}
         				$size = substr_count($space,"┗");
         				$html.="<tr>
		<td><img src=".ATTCHMENT_PATH."$catpic width='30px' height='30px'/></td>
	  	<td>".$space.$val[$i]['catename']."</td>
		<td>".$val[$i]['htmldir']."</td>
		<td><input type='text' value=".$val[$i]['ordering']." size='5' name='order[".$val[$i]['id']."]'></td>
		<td>
        <a href='admin.php?m=product&a=cate_edit&id=".$val[$i]['id']."&channelid=".$_REQUEST[channelid]."'>修改</a>&nbsp;&nbsp;
        <a href='admin.php?m=product&a=cate_del&id=".$val[$i]['id']."&channelid=".$_REQUEST[channelid]."'>删除</a></td>
	</tr>";


							$space.="┗&nbsp;";

            			$this->cate_list($val[$i][0]);
            			$space=substr($space,8);
         		 }
     		}
     		return $html;

		}


		function get_htmldir($pdir){
			return $dir = !empty($_POST['htmldir']) ? $pdir.$_POST['htmldir'] : $pdir.Common::pinyin($_POST['catename'])."/";
		}

		function path_dir($pdir){
			$pdir = rtrim($pdir,"/");
			$pdir = explode("/",$pdir);
			return end($pdir)."/";
		}



		function validate(){
			$result = true;
			if(!Validate::required($_POST['catename'])){
					$this->messList[] = "分类名称不能为空.";
					$result=false;
			}

			return $result;

		}


		function ordering(){
		$result = true;
			foreach($_POST['order'] as $key=>$val){
				$result = $this->mod(array('id'=>$key,'ordering'=>$val));

			}
		return $result;

		}

		function change_cate(){
		$result = true;

		foreach($_POST['xm'] as $key=>$val){
			$result = $this->mod(array('id'=>$val,'cateid'=>$_REQUEST['change_cate']));

		}
		return $result;

		}


		function valorder(){
		$result = true;
		foreach($_POST['order'] as $key =>$val){

					if(!validate::isNumber($val)){
					$this->messList[] = "ID为".$key."的排序不为数字.";
					$result=false;
					}
		}
		return $result;

		}

		function checkcolumn(){
			if($this->get_cate($_REQUEST["id"])==null){
				return true;
			}else{
				return false;
			}
		}


		function check_parentid($id){
			global $code;
			$val =$this->get_cate($id);

				foreach($val as $v){
					$code[] =$v['id'];
					$this->check_parentid($v['id']);
				}

				return $code ;



		}



	//==================上面为分类==下面为文章=====================
		function chech_form(){
			$result = true;
			if(!Validate::noZero($_POST['cateid'])){
					$this->messList[] = "请选择产品所属的分类";
					$result = false;
			}
			if(!Validate::required($_POST['title'])){
					$this->messList[] = "产品名称不能为空";
					$result = false;
			}
			if(!Validate::required($_POST['content'])){
					$this->messList[] = "产品描述不能为空";
					$result = false;
			}
			if(!Validate::isNumber($_POST['click'])){
					$this->messList[] = "点击次数必须为数字";
					$result = false;
			}
			return $result;
		}






}
?>
