<?php
class channelModel extends Model{
	function __construct(){
		parent::__construct();
		$this->tabName = TAB_PREFIX."channel";
		$this->fieldList=$this->get_fields($this->tabName,'list');
	}



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

			if(!Validate::required($_POST['link'])){
				$this->messList[] = "链接地址不能为空.";
				$result=false;
			}
		}else{
				if(!Validate::required($_POST['cnname'])){
					$this->messList[] = "频道名称不能为空.";
					$result=false;
				}
				if(!Validate::required($_POST['template'])){
					$this->messList[] = "需指定频道模板.";
					$result=false;
				}

				if(!empty($_POST[htmldir])){
				if(!preg_match("/(.*)\/$/",$_POST[htmldir])){

					$this->messList[] = "HTML目录格式不正确";
					$result=false;
				}
				}


		}
		return $result;
	}


	function getChannelList($where=""){
		if($where==""){
		return $this->findAll("","", "","","ordering,id");
		}else{

			$newresult=array();
			$result =$this->findAll("","", "",$where,"ordering,id");
			foreach($result as $key =>$val){
				$newresult[$key]=$val;
				$newresult[$key]['menu']=file_get_contents(TEMPLATE_PATH."admin/$val[model]/menu.html");
				$newresult[$key]['menu']=str_replace('#id#',$val[id],$newresult[$key]['menu']);
			}
			return $newresult;
		}

	}

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

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

	}



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

		}
		return $result;

	}




}
?>
