<?php

/**
 * @author applelor
 * @version 创建日期：2011.3.8
 */

class Picture {
	var $picTable;
	var $stateField;
	var $stateArr;
	var $primaryKey;
	var $foreignKey;
	var $picAddressField;
	var $extraField;
	var $up;


    public function __construct()
    {
    	$this->picTable = 'upload';
		$this->stateField = 'flg';
		$this->primaryKey = 'id';
		$this->foreignKey = 'rowid';
		$this->extraField = 'tbl';
		$this->picAddressField = 'filename';
		$this->up = new UploadFile();
    	$this->stateArr  = array(
    		'check'=>1,
    		'uncheck'=>0,
    		'hide'=> 2,
    		'freeze'=>3
    	);
    }

	/*
     * @use 更改图片未审核
     * @param $picIds 图片ID
	 *
	 *     */
	function uncheck($picIds){
		$rt = false;
		if($picIds)
			$rt = changeState($this->picTable,$this->stateField,$this->stateArr['uncheck'],$this->primaryKey,$picIds);
		return $rt;
    }

	/*
     * @use 更改图片审核
     * @param $picIds 图片ID
     */
	function check($picIds){
		$rt = false;
		if($picIds)
			$rt = changeState($this->picTable,$this->stateField,$this->stateArr['check'],$this->primaryKey,$picIds);
		return $rt;
    }

	/*
     * @use 更改图片隐藏
     * @param $picIds 图片ID
     */
	function hide($picIds){
		$rt = false;
		if($picIds)
			$rt = changeState($this->picTable,$this->stateField,$this->stateArr['hide'],$this->primaryKey,$picIds);
		return $rt;
    }

	/*
     * @use 更改图片冻结
     * @param $picIds 图片ID
     */
	function freeze($picIds){
		$rt = false;
		if($picIds)
			$rt = changeState($this->picTable,$this->stateField,$this->stateArr['freeze'],$this->primaryKey,$picIds);
		return $rt;
    }

    /*
     * @use 删除图片
     * @param $picIds 图片ID
     */
    function delete($picIds){
    	global $db;
    	if(is_array($picIds)){
    		$picStrs = implode(',',$picIds);
    		$sql = "SELECT $this->primaryKey,$this->picAddressField FROM $this->picTable WHERE $this->primaryKey IN ($picStrs)";
    		$rs = $db->GetAll($sql);
    		if($rs){
    			foreach ($rs as $row){
		    		$this->up->remove($row[$this->picAddressField]);
		    		$db->Execute("DELETE FROM $this->picTable WHERE $this->primaryKey=".$row[$this->primaryKey]);
    			}
    		}
    	}else{
    		$sql = "SELECT $this->picAddressField FROM $this->picTable WHERE $this->primaryKey ='$picIds'";
    		$rs = $db->GetOne($sql);
    		if($rs){
		    		$this->up->remove($rs);
		    		$db->Execute("DELETE FROM $this->picTable WHERE $this->primaryKey=".$picIds);
    		}
    	}
    	return true;
    }

    /*
     * @use 获取图片状态
     * @param $picIds 图片ID
     */
    function getState($picIds){
    	global $db;
    	if(is_array($picIds)){
    		$picStrs = implode(',',$picIds);
    		$sql = "SELECT $this->primaryKey,$this->stateField FROM $this->picTable WHERE $this->primaryKey IN ($picStrs)";
    		$rs = $db->GetAll($sql);
    		if($rs){
    			return $rs;
    		}else{
    			return false;
    		}
    	}else{
    		$sql = "SELECT $this->stateField FROM $this->picTable WHERE $this->primaryKey = '$picIds'";
    		$rs = $db->GetOne($sql);
    		return $rs;
    	}
    }

    /*
     * @use 设置房源的默认图片
     * @param $picId 图片Id
     * @param $houseId 房源Id
     * @param $type sell | rent 表名
     */
    function setDefault($picId,$houseId,$type='sell'){
    	global $db;
		if(is_numeric($picId)&&is_numeric($houseId)){
			$sql = "SELECT $this->primaryKey,$this->stateField,$this->picAddressField FROM $this->picTable WHERE $this->primaryKey = '$picId'";
    		$rs = $db->GetAll($sql);
    		if($rs){
				$state = $rs[0][$this->stateField];
				$pic = $rs[0][$this->picAddressField];
    		}

			if($state&&$state!=$this->stateArr['hide']&&$state!=$this->stateArr['freeze']){
				$sql = "UPDATE $type SET pic1 = '$pic' WHERE id='$houseId'";
				$rs = $db->Execute($sql);
				if($rs){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}

		}
		return false;
    }

}

?>
