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

class MailDao {
	
	Const table = 'mail';

	public static function getMail ($id) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$select = $sql->select();
		$select->from(DATABASE_DB_TABLEPREFIX . self::table);
		$select->where(array('tabid' => $id), Zend\Db\Sql\Predicate\PredicateSet::OP_OR);

		$selectString = $sql->getSqlStringForSqlObject($select);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		$resCount = $results->count();

		$data = array();
		foreach ($results->toArray() as $row) {
			$data[] = new MailVo($row);
		}
		return $data;
	}
	public static function saveMail ($mail) {
		$db = DBConnector::getZendDbConnection();
		$sql = new Zend\Db\Sql\Sql($db);
		$update = $sql->update();
		$update->table(DATABASE_DB_TABLEPREFIX . self::table);

		$data = array();
		$data['enable'] = !empty($mail['enable']) ? 1 : 0;
		$data['description'] = $mail['description'];
		$data['to'] = $mail['to'];
		$data['cc'] = $mail['cc'];
		$data['bcc'] = $mail['bcc'];
		$data['subject'] = $mail['subject'];
		$data['content'] = $mail['content'];

		$update->set($data);
		$update->where(array('tabid' => $mail['tabid']));

		$selectString = $sql->getSqlStringForSqlObject($update);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		$affectedRows = $results->getAffectedRows();
		return $affectedRows;
	}
	public static function addMail($mail) {
		$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['enable'] = !empty($mail['enable']) ? 1 : 0;
		if (!empty($mail['tabid'])) $data['tabid'] = $mail['tabid'];
		$data['description'] = $mail['description'];
		$data['to'] = $mail['to'];
		$data['cc'] = $mail['cc'];
		$data['bcc'] = $mail['bcc'];
		$data['subject'] = $mail['subject'];
		$data['content'] = $mail['content'];

		$insert->values($data);

		$selectString = $sql->getSqlStringForSqlObject($insert);
		Logger::debug('		Execute query: ' . $selectString);
		$results = $db->query($selectString, $db::QUERY_MODE_EXECUTE);
		return $db->getDriver()->getLastGeneratedValue();
	
	}
}

?>
