<?php
class SetupDAL {
	/**
	 * Zapisuje warto�� zmiennej w tabeli Setup
	 *
	 * @param String $zmienna
	 * @param String $wartosc
	 * @param String $opis
	 */
	public static function SaveVariable($zmienna, $wartosc, $opis) {
		$db = Registry::Get('db');
		$sql = "REPLACE INTO wp_setup (variable, value, description) values ( :1, :2, :3)";
		$stmt=$db->prepare($sql)
		->bindParam(1, $zmienna)
		->bindParam(2, $wartosc)
		->bindParam(3, $opis)		
		->execute();
		
//		return $stmt->GetInsertionId();
	}
	
	/**
	 * Pobiera z tabeli setup zmienne oraz rejestruje je w Registry
	 *
	 * @param boolean $set_registry
	 * @return Array
	 */
	public static function GetAllVariables($set_registry=true) {
		$db = Registry::Get('db');
		$sql = "select variable,value,description from wp_setup order by variable,value";
		//$cache = new DbCache($sql);
		
		//if(!$cache->CacheCheck()) {
		$stmt=$db->execute($sql);
		$stmt->CacheStart(600);
		$array = $stmt->FetchAllAssoc();
		//$cache->CacheWrite($array);
		//} else {
		//	$array = $cache->CacheRead();
		//}
        //print_r($array);
		$returnArray = array();
		foreach ($array as $row) {
			
			$row["value_str"] = $row["value"];
			if ($row["value"] == "false") {
				$row["value"] = false;
			}
			if ($row["value"] == "true") {
				$row["value"] = true;
			}
			
			if ($set_registry) {
				Registry::Set($row["variable"], $row["value"]);
			}
			
			$tmpArray["variable"] = $row["variable"];
			$tmpArray["value"] = $row["value_str"];
			$tmpArray["description"] = $row["description"];
			
			$returnArray[] = $tmpArray;
		}
		return $returnArray;
				
	}
	
	
	/**
	 * ODczytuje z tabeli wp_setup zmienn� o danej nazwie
	 *
	 * @param String $variable
	 * @return String
	 */
	public static function GetVariableByName($variable) {
		$db = Registry::Get('db');
		$sql = "select variable,value,description from wp_setup where variable=:1";
		$stmt=$db->prepare($sql)
		->bindParam(1, $variable)
		->execute();		
		$array = $stmt->fetchAllAssoc();
		
		return $array[0];
	}
}
?>