<?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('LeaseContentVo.php');

class LeaseContentDao {
	
	Const table = 'leasecontent';

	public static function getLease ($id) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$select = $sql->select();
		$select->from(DATABASE_DB_TABLEPREFIX . self::table);
		$select->where(array('tenancy_id' => $id));
		$select->order('position ASC');
		
		$selectString = $sql->getSqlStringForSqlObject($select);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);

		$data = array();
		foreach ($results->toArray() as $row) {
		// print_r($row);
			$data[] = new LeaseContentVo($row);
		}
		return $data;
	}
	public static function deleteLeaseByTenancy($id) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$delete = $sql->delete();
		$delete->from(DATABASE_DB_TABLEPREFIX . self::table);
		$delete->where(array('tenancy_id' => $id));

		$selectString = $sql->getSqlStringForSqlObject($delete);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		return $results->getAffectedRows();
	}
	public static function deleteAllLeaseExcept($id) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$delete = $sql->delete();
		$delete->from(DATABASE_DB_TABLEPREFIX . self::table);
		// $delete->where(array('tenancy_id' => $id));
		$delete->where('tenancy_id != ' . $id);

		$selectString = $sql->getSqlStringForSqlObject($delete);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		return $results->getAffectedRows();
	}
	
	public static function addBlock($id, $index, $head, $desc){
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$insert = $sql->insert();
		$insert->into(DATABASE_DB_TABLEPREFIX . self::table);

		$data = array();
		$data['recorddate'] = new Zend\Db\Sql\Expression('NOW()');
		$data['tenancy_id'] = $id;
		$data['position'] = $index;
		$data['header'] = $head;
		$data['content'] = $desc;

		$insert->values($data);

		$selectString = $sql->getSqlStringForSqlObject($insert);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		return $db->getDriver()->getLastGeneratedValue();
	}
	public static function getPageBreak($id) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$select = $sql->select();
		$select->columns(array('count' => new \Zend\Db\Sql\Expression('COUNT(*)')));
		$select->from(DATABASE_DB_TABLEPREFIX . self::table);
		$select->where(array('tenancy_id' => $id));
		$select->where(array('header' => "<SAUT_PAGE>"));
		
		$selectString = $sql->getSqlStringForSqlObject($select);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);

		$res = $results->toArray();
		return $res[0]['count'];
	}
}

?>
