<?php

require_once APPLICATION_PATH . '/../library/phpmailer/class.phpmailer.php';

class Email{
	protected static $_smtp_servidor = '';
	protected static $_smtp_usuario = '';
	protected static $_smtp_password = '';
	protected static $_smtp_port = 25;
	protected static $_smtp_secure = '';
	protected static $_nombre_origen_por_defecto;
	protected static $_email_origen_por_defecto;
	protected static $_email_codificacion = 'UTF-8';
	
	protected $_asunto = '';
	protected $_parametros = array();
	protected $_desde_nombre;
	protected $_desde_email;
	protected $_destinatarios = array();
	protected $_destinatariosOcultos = array();
	protected $_vista = 'index';
	protected $_adjuntos = array();
	
	
	public static function setConfiguracion($configuracion){
		foreach ($configuracion as $key => $value){
			switch ($key) {
				case 'ssl':
					self::$_smtp_secure= $value;
					break;
				case 'puerto':
					self::$_smtp_port = $value;
					break;
				case 'usuario':
					self::$_smtp_usuario = $value;
					break;
				case 'password':
					self::$_smtp_password = $value;
					break;
				case 'smtpServer':
					self::$_smtp_servidor = $value;
					break;
				case 'nombre_desde':
					self::$_nombre_origen_por_defecto = $value;
					break;
				case 'codificacion':
					self::$_email_codificacion = $value;
					break;
				case 'email_desde': 
					self::$_email_origen_por_defecto = $value;
					break;
				
			
			}
		}
		
		
	}
	public static function setSMTPServidor($value){
		self::$_smtp_servidor = $value;
		
	}
	public static function setSMTPUsuario($value){
		self::$_smtp_usuario = $value;
		
	}
	public static function setSMTPPasword($value){
		self::$_smtp_password = $value;
		
	}
	public static function setSMTPPort($value){
		self::$_smtp_port = $value;
	}
	public static function setSMTPSecure($value){
		self::$_smtp_secure = $value;
	}
	public static function setOrigenPorDefecto($nombre, $email){
		self::$_nombre_origen_por_defecto = $nombre;
		self::$_email_origen_por_defecto = $email;
	}
	
	public static function setCodificacion($codificacion){
		self::$_email_codificacion = $codificacion;
	}
	
	public function setOrigen($nombre, $email){
		$this->_desde_nombre= $nombre;
		$this->_desde_email= $email;
	}

	public function getDesdeNombre(){
		return $this->_desde_nombre;
	}

	public function getDesdeEmail(){
		return $this->_desde_email;
	}
	
	public function setParametro($name, $value){
		$this->_parametros[$name] = $value;
	}
	
	public function getParametro($name){
		return $this->_parametros[$name];
	}
	
	public function __set($name, $value){
		$this->setParametro($name, $value);
		
	}
	public function __get($name){
		return $this->getParametro($name);
	}
	
	public function setVista($vista){
		$this->_vista = $vista;
	}
	public function vaciarDestinatarios(){
		$this->_destinatarios = array();
	}
	
	public function setAsunto($asunto){
		$this->_asunto = $asunto;
		
	}
	public function addAdjunto($adjunto){
		$this->_adjuntos[] = $adjunto;
	}
	public function addDestinatario($nombre, $email){
		$this->_destinatarios[] = array('nombre' => $nombre, 'email' => $email);
	}
	
	public function addDestinatarioOculto($nombre, $email){
		$this->_destinatariosOcultos[] = array('nombre' => $nombre, 'email' => $email);
	}
	
	public function renderVista(){
		ob_start();
		$normal = APPLICATION_PATH . '/emails/'.$this->_vista . '.phtml';
		$over = APPLICATION_PATH . '/templates/emails/'.$this->_vista . '.phtml';
		if(is_file($normal))
			include $normal;
		else{
			include $over;
		}
		$salida = ob_get_clean();
		return $salida;
	}
	public function enviar(){
		try {
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPDebug= 1;
			$mail->SMTPAuth= true;
			$mail->Host = self::$_smtp_servidor;
			$mail->Port = self::$_smtp_port;
			if(self::$_smtp_secure){
				$mail->SMTPSecure = self::$_smtp_secure;
			}
			
			$mail->Username = self::$_smtp_usuario;
			$mail->Password = self::$_smtp_password;
			$mail->CharSet 	= self::$_email_codificacion; 
			
			$mail->SetFrom(($this->_desde_email)?$this->_desde_email:self::$_email_origen_por_defecto, ($this->_desde_nombre)?$this->_desde_nombre:self::$_nombre_origen_por_defecto);
				
			$mail->AddReplyTo(($this->_desde_email)?$this->_desde_email:self::$_email_origen_por_defecto, ($this->_desde_nombre)?$this->_desde_nombre:self::$_nombre_origen_por_defecto);
			
			

			
			$mail->Subject = $this->_asunto;
			$mail->MsgHTML($this->renderVista());
			foreach ($this->_destinatarios as $destinatario){
				$mail->AddAddress($destinatario['email'], $destinatario['nombre']);
			}

			foreach ($this->_destinatariosOcultos as $destinatario){
				$mail->AddBCC($destinatario['email'], $destinatario['nombre']);
			}
				
			//adjuntamos ficheros
			if(count($this->_adjuntos)){
				foreach ($this->_adjuntos as $archivo){
					$mail->AddAttachment($archivo);      // attachment
			
			
				}
			}
				
			
			
			
			if ($mail->Send())
				return true;
			else {
				echo 'ERROR'.$mail->ErrorInfo;
				return false;
			}
			
		} catch (Exception $e) {
			return false;
		}
		
	}
	
}