<?php
/**
 * Klasa obsĹ‚ugi tablicy slownika
 *
 */
class MfDictionaryDAL 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->setObj($obj);
		$id = self::DefaultInsert($dalData);

		return $id;
	}
	/**
     * @param Text $obj
     *
     */
	public static function Update($obj) {
		//Utils::ArrayDisplay($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);

		//MfFileDescriptionDAL::DeleteByParentId($obj->GetId());

		return self::DefaultDelete($dalData);
	}
/**
     *

     *
     */
	public static function DeleteByKey($keyword) {
		
		$db = Registry::Get('db');
		
		
		$sql = "DELETE FROM mf_dictionary WHERE mf_dictionary.keyword = :01 ";
		
		$stmt = $db->prepare($sql)
		->bindParam("01", $keyword)
		->execute();
		
	}

	/**
     *
     * @param  $id
     */
	public static function GetById($id) {
		$dalData = self::GetDalDataObj();
		$dalData->setCondition(array(self::GetObjClassTablePK()=>$id));
		$dalData->setLimit(1);
		$result = self::GetResult($dalData);
		if ((count($result) > 0) && (is_object($result[0]))) {
			return $result[0];
		} else {
			throw new Exception('Brak rekordu w tablicy '.self::GetObjClassTable().' o id <b>'.$id.'</b>!');
		}
	}

	public static function GetArrayObjAll($data, $queryFields = array(), $limit = 0, $sortBy = null,$count = null) {
		$dalData = self::GetDalDataObj();
		$dalData->setCondition($data);
		$dalData->setLimit($limit);
		$dalData->setSortBy($sortBy);
		$dalData->setCount($count);
		$result = self::DefaultGetResult($dalData);
		return $result;
	}


	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) . 
			", (SELECT en.replacement FROM mf_dictionary en  WHERE 1=1 AND MfDictionary_keyword = en.keyword AND lang = 'en') as MfDictionary_replacementEn" .
			", (SELECT ru.replacement FROM mf_dictionary ru  WHERE 1=1 AND MfDictionary_keyword = ru.keyword AND lang = 'ru') as MfDictionary_replacementRu" .
			", (SELECT de.replacement FROM mf_dictionary de  WHERE 1=1 AND MfDictionary_keyword = de.keyword AND lang = 'de') as MfDictionary_replacementDe" ;
			//", (SELECT fr.replacement FROM mf_dictionary fr  WHERE 1=1 AND MfDictionary_keyword = fr.keyword AND lang = 'fr') as MfDictionary_replacementFr" .
			//", (SELECT ua.replacement FROM mf_dictionary ua  WHERE 1=1 AND MfDictionary_keyword = ua.keyword AND lang = 'ua') as MfDictionary_replacementUa " ;
			//", (SELECT no.replacement FROM mf_dictionary no  WHERE 1=1 AND MfDictionary_keyword = no.keyword AND lang = 'no') as MfDictionary_replacementNo " ;

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

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

			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();

		//Utils::ArrayDisplay($array);
		for($i=0;$i<count($array);$i++)
		{
			$className=self::GetObjClassName();
			$obj = new $className();
			$obj->FromArray($array[$i],1);
			$obj->SetReplacementEn($array[$i]['MfDictionary_replacementEn']);
			$obj->SetReplacementDe($array[$i]['MfDictionary_replacementDe']);
			$obj->SetReplacementRu($array[$i]['MfDictionary_replacementRu']);
//			$obj->SetReplacementFr($array[$i]['MfDictionary_replacementFr']);
//			$obj->SetReplacementUa($array[$i]['MfDictionary_replacementUa']);
			$done[] = $obj;
		}
		return $done;

	}

	/**
	 * Pobiera z tabeli liste slowek i cacheuje
	 *
	 * @param integer $cacheTime
	 * @return Array
	 */
	public static function GetAllVariables($lang, $cacheTime) {
		$db = Registry::Get('db');
		$sql = "SELECT keyword, replacement FROM ".self::GetObjClassTable()." WHERE lang='".$lang."'";

		$stmt=$db->execute($sql);
		$stmt->CacheStart($cacheTime);
		$array = $stmt->FetchAllAssoc();
		

		$returnArray = array();
		foreach ($array as $keyword => $value) {
			$returnArray[$value['keyword']] = $value['replacement'];
		}
		//Utils::ArrayDisplay($returnArray);
		return $returnArray;

	}

	/**
	 * Pobiera z tabeli liste slowek i cacheuje
	 *
	 * @param integer $cacheTime
	 * @return Array
	 */
	public static function IsVariables($keyword, $lang) {
		$db = Registry::Get('db');
		$sql = "SELECT keyword, replacement FROM ".self::GetObjClassTable()." WHERE lang='".$lang."' AND keyword='".$keyword."'";
		//Utils::ArrayDisplay($sql);
		$stmt=$db->execute($sql);
		$array = $stmt->FetchAllAssoc();
		if (count($array) > 0) {
			return true;
		} else {
			return false;
		}


	}



	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;
	}

	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;
	}

}
?>