<?php
class Model {

	protected $pdo;
	protected $messList;
	protected $tabName;
	protected $fieldList;
	private   $dsn;

    public function __construct() {
    	$this->dsn = "".DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME."";
    	try{
    		$this->pdo = new PDO($this->dsn,DB_USER,DB_PWD);
    		$this->pdo->exec("SET NAMES '".DB_CHARSET."'");
    	}catch (PDOexception $e){
    		echo "Êý¾Ý¿âÁ¬½ÓÊ§°Ü".$e->getMessage();
    		exit;

    	}

    }


	function add($postList) {
		$fieldList='';
		$value='';
		foreach ($postList as $k=>$v) {
			if(in_array($k, $this->fieldList)){
				$fieldList.=$k.",";
				if (!get_magic_quotes_gpc())
					$value .= "'".addslashes($v)."',";
				else
					$value .= "'".$v."',";
			}
		}

		$fieldList=rtrim($fieldList, ",");
		$value=rtrim($value, ",");

		$sql = "INSERT INTO {$this->tabName} (".$fieldList.") VALUES(".$value.")";
		$result=$this->pdo->exec($sql);

		if($result)
			return $this->pdo->lastInsertId();
		else
			return false;
	}



	function mod($postList) {
		$id=$postList["id"];
		unset($postList["id"]);
		$value='';
		foreach ($postList as $k=>$v) {
			if(in_array($k, $this->fieldList)){
				if (!get_magic_quotes_gpc())
					$value .= $k." = '".addslashes($v)."',";
				else
					$value .= $k." = '".$v."',";
			}
		}
		$value=rtrim($value, ",");
		$sql = "UPDATE {$this->tabName} SET {$value} WHERE id={$id}";
		return $this->pdo->query($sql);
	}




	function del($id) {
		if(is_array($id))
			$tmp = "IN (" . join(",", $id) . ")";
		else
			$tmp = "= $id";

		$sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;

		return $this->pdo->exec($sql);

	}

	function total($where=""){
		$sql="SELECT id FROM {$this->tabName} {$where}";
		$result=$this->pdo->query($sql);
		return $result->fetchAll();
	}
	function find($id) {
		$filed=join(",", $this->fieldList);
		$sql="select {$filed} from {$this->tabName} where id={$id}";
		$result=$this->pdo->query($sql);
		if($result){
			return $result->fetch();
		}else{
			return null;
		}

	}

	function find_next($id) {
		$filed=join(",", $this->fieldList);
		$sql="select {$filed} from {$this->tabName} where id>{$id} limit 0 ,1";
		$result=$this->pdo->query($sql);
		if($result){
			return $result->fetch();
		}else{
			return null;
		}

	}

	function find_prev($id) {
		$filed=join(",", $this->fieldList);
		$sql="select {$filed} from {$this->tabName} where id<{$id} order by id desc limit 0,1";
		$result=$this->pdo->query($sql);
		if($result){
			return $result->fetch();
		}else{
			return null;
		}

	}





	function findAll($field=array(), $offset=0, $num=0,$where="",$order="id desc"){
			if(empty($field)){
				$field=join(",", $this->fieldList);
			}else{
				$field=join(",", $field);
			}
			if(in_array('ordering',$this->fieldList) && !empty($order)){
				$order='ordering,'.$order;
			}

			if($num==0){
				$sql="select {$field} from {$this->tabName} {$where}  order by {$order}";
			}else{
				$sql="select {$field} from {$this->tabName} {$where} order by {$order} limit {$offset},{$num}";
			}
			$result=$this->pdo->query($sql);

			if($result){
				$data=array();

				while($row=$result->fetch()){
					$data[]=$row;
				}

				return $data;
			}else{
				return null;
			}
		}



	function getMessList(){
			$message="";
			if(!empty($this->messList)){
				foreach($this->messList as $value){
					$message.=$value."<br>";
				}
			}
			return "<font color=red>$message</font>";
		}


	public function close() {
			if($this->pdo)

			$this->pdo=FALSE;
		}

	public function __destruct() {
			$this->close();
		}


	function get_fields($table,$list=''){
		$this->sth = $this->pdo->query("DESCRIBE $table");
        $this->sth->setFetchMode(PDO::FETCH_ASSOC);
        $result = $this->sth->fetchAll();
        $this->sth = null;
        if(!empty($list)){

        	foreach($result as $k => $v){
        		$res.=$v[Field]."," ;
        	}
        	$result=substr($res,0,-1);
        	$result=explode(',',$result);
        }
        return $result;
	}



}

?>