<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

require_once('config/Logger.php');
require_once('config/Date.php');

require_once('db/DBConnector.php');

class ConfigDao {

	Const table = 'config';

	public static function getConfigs (){
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$select = $sql->select();
		$select->from(DATABASE_DB_TABLEPREFIX . self::table);
		$select->columns(array('name', 'value'));
	
		$selectString = $sql->getSqlStringForSqlObject($select);
		// Logger::debug('Execute query: ' . $selectString);
		$data = array();
		try {
			$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
			// $resCount = $results->count();
			foreach($results->toArray() as $row) {
				$data[$row['name']] = $row['value'];
			}
			$shortlng = explode('_',$data['language']);
			$data['language_short'] = $shortlng[0];
			$data['date_format_js'] = dateformat_PHP_to_jQueryUI($data['date_format']);
			$data['date_format_js_moment'] = dateformat_PHP_to_jsMoment($data['date_format']);
		} catch(Exception $e){
		}
		return $data;
	}
	public static function saveConfig ($name, $val) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$update = $sql->update();
		$update->table(DATABASE_DB_TABLEPREFIX . self::table);

		// $data = array();
		// $data['modifieddate'] = new Zend\Db\Sql\Expression('NOW()');
		// if (!empty($config['date_format'])) $data['date_format'] = $config['date_format'];

		$update->set(array('value' => $val));
		$update->where(array('name' => $name));

		$selectString = $sql->getSqlStringForSqlObject($update);
		Logger::debug('Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		$affectedRows = $results->getAffectedRows();
	}
}

?>
