<?php
/**
 * Klasa do obsugi mf Linka
 *
 */
class MfLinkDescriptionDAL extends DefaultDAL {

	protected static $objClassName;
	protected static $objClassTable;
	protected static $objClassTablePK;
	private static $optClass;

	public static function Save($obj) {
		return self::DefaultSave(self::GetObjClassTable(), self::GetObjClassTablePK(), $obj);
	}

	public static function Insert($obj) {
		return self::DefaultInsert(self::GetObjClassTable(), $obj);
	}

	public static function Update($obj) {
		self::DefaultUpdate(self::GetObjClassTable(), self::GetObjClassTablePK(), $obj);
	}

	public static function Delete($id) {
		self::DefaultDelete(self::GetObjClassTable(), self::GetObjClassTablePK(), $id);
	}

	public static function DeleteByParentId($id) {
		$sql = "DELETE FROM `mf_link_description` WHERE `id_mf_link`=:0";

		$db = Registry::Get('db');

		$db->Prepare($sql)
		->BindParam(0, $id)
		->Execute();
	}

	public static function GetById($id) {
		return self::DefaultGetById(self::GetOptClass(), self::GetObjClassTable(), self::GetObjClassName(), self::GetObjClassTablePK(), $id);
	}

	public static function GetEmptyObj() {
		$obj = new MfLinkDescription(-1);
		return $obj;
	}

	public static function GetArrayObjAll() {

	}

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

		if(!is_array($data)){
			$data = array();
		}

		$db = Registry::Get('db');

		$queryCacheName = __CLASS__. "_" .__FUNCTION__. "_" .md5(implode($queryFields));

		if(isset(QueryCacheTemp::$cacheQuery[$queryCacheName]))
		{
			$sql = QueryCacheTemp::$cacheQuery[$queryCacheName];
		}
		else
		{
			if($count == true)
			$select = "count(*) as count";
			else
			$select =" " . SQL::ToSelect(self::GetOptClass(),$queryFields) . " ";

			$sql = " SELECT $select FROM " . self::GetObjClassTable() . " WHERE 1=1 ";
			$q = new QueryCache($queryCacheName,$sql);
		}

		foreach ($data as $key => $value)
		{
			if($key == "id")
			$key = 'id_mf_link_description' ;

			if(is_array($value))
			$sql .= ( is_numeric($value['value']) || $value ? " AND ".$key." ".$value['condition']." '". $value['value'] ."' " : "");
			else
			$sql .= ( is_numeric($value) || $value ? " AND ".$key." = '". $value ."' " : "");
		}

		$sql .= ( $sortBy ? " ORDER BY $sortBy " : "").
		( $limit ? " LIMIT " . $limit : "").
		" ";

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


		$array = $stmt->fetchAllAssoc();

		if($count == true)
		return  $array[0]['count'];

		$done = array();

		for($i=0;$i<count($array);$i++)
		{
			$className=self::GetObjClassName();
			$obj = new $className();
			$obj->FromArray($array[$i],1);
			$done[] = $obj;
		}
		return $done;

	}



	
	/**
	 * Pusty konstruktor
	 *
	 */
	public function __construct() {

	}

	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(self::$objClassTable!='') {
			$return = self::$objClassTable;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetTableName();
		}

		return $return;
	}
}
?>