<?php

/**
 *
 *
 */

class ImageGroupDAL extends DefaultDAL {
	protected static $objClassName;
    protected static $objClassTable;
    protected static $objClassTablePK;
    private static $optClass;

    public static function Save($obj) {
		$id = null;
		if($obj->GetId()==-1) {
			$id = self::Insert($obj);
		} else {
			self::Update($obj);
			$id = $obj->GetId();
		}

		return $id;
	}


	/**
     * @param Feed $obj
     *
     */
	public static function Insert($obj) {
		$dalData = new DalData();
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setObj($obj);
		$id = self::DefaultInsert($dalData);
		return $id;
	}
	/**
     * @param Text $obj
     *
     */
	public static function Update($obj) {
		$dalData = new DalData();
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setObj($obj);
		return self::DefaultUpdate($dalData);
	}

	/**
     *
     * @param integer $id
     *
     */
	public static function Delete($obj) {
		$dalData = new DalData();
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setObj($obj);
		return self::DefaultDelete($dalData);
	}

    public static function UpdateById($obj,$id) {
		$db = Registry::Get('db');

		$sql = "UPDATE mf_gallery SET ". SQL::ToUpdateSet($obj) ." WHERE id_mf_gallery=$id ";

		$stmt=$db->prepare($sql);
		$stmt->execute();

		return $obj->GetId();
    }

    /**
     *
     * @param <type> $id
     * @return ForumThread
     */
    public static function GetById($id) {
        return self::DefaultGetById(self::GetOptClass(), self::GetObjClassTable(), self::GetObjClassName(), self::GetObjClassTablePK(), $id);
    }


    public static function GetArrayObjAll() {
        return self::GetResult(array());
    }

    public static function GetResult($data,$queryFields = array(),$limit = 0, $sortBy = null,$count = null, $cache = true) {

        return self::DefaultGetResult(self::GetOptClass(), self::GetObjClassTable(), self::GetObjClassName(), $data, $queryFields, $limit, $sortBy, $count, $cache);

    }

    public static function GetObjClassName() {
        if(self::$objClassName != '') {
            $class = self::$objClassName;
        } else {
            $class = str_replace('DAL', '', __CLASS__);
        }

        return $class;
    }

    public static function GetOptClass() {
        if(self::$optClass!=null) {
            return self::$optClass;
        } else {
            return self::GetObjClassName();
        }
    }

    public static function GetObjClassTablePK() {
        if(self::$objClassTablePK != '') {
            $return = self::$objClassTablePK;
        } else {
            $class = self::GetObjClassName();
            $classObj = new $class();
            $return = $classObj->GetClassTablePK();
        }
        return $return;
    }

    public static function GetObjClassTable() {
        if(!is_null(self::$objClassTable)) {
            $return = self::$objClassTable;
        } else {
            $class = self::GetObjClassName();
            $classObj = new $class();
            eval('$return = '.$class.'::GetTableName();');
        }
        return $return;
    }
    /**
     * 
     * @return DalData
     */
    public static function GetDalDataObj() {

		$dalData = new DalData();
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());
		//$dalData->setDatabaseType('dbTemp');

		return $dalData;
    }
}
?>