<?php
require_once "swift/swift_required.php";

if($_GET['id']){
require('../config.php');
require('../init.php');
}
class eMail extends Swift {

	var $settings;

	function eMail() {
		global $db;
		$this->db = $db;
		$this->tpl = $tpl;
		$this->path = $_SERVER['DOCUMENT_ROOT'] . "/";

		$this->Set_Vars();
		$transport = Swift_SmtpTransport::newInstance($this->settings['server'], 25);
		$this->mailer = Swift_Mailer::newInstance($transport);
	}

	function Set_Vars(){
		$this->settings = array();
		$this->settings['server'] = "localhost";
		$this->settings['from_email'] = "enovice@imos.si";
		$this->settings['from_name'] = "Imos.si - enovice";
		$this->settings['notice_email'] = "racuni@zabec.net";
	}

	function bumbar($var) {
		return $var;
	}
	function eNovice($email,$data,$export = 0){


		$this->email['to_email'] = $email;
		$this->email['subject'] = $data['naslov'];

		$this->tpl = new TemplatePower($this->path . "enovice1/enovice_template.tpl");

		$this->tpl->prepare();

		$this->tpl->assign("naslov",$this->email['subject']);
        $this->tpl->assign("datum",$data['datum']);
		$this->tpl->assign("novica1naslov",$data['novica1naslov']);
		$this->tpl->assign("novica1link",$data['novica1link']);
		$this->tpl->assign("novica1vsebina",$data['novica1vsebina']);
		
		$this->tpl->assign("novica2naslov",$data['novica2naslov']);
		$this->tpl->assign("novica2datum",$data['novica2datum']);
		$this->tpl->assign("novica2link",$data['novica2link']);
		$this->tpl->assign("novica2vsebina",$data['novica2vsebina']);

		$this->tpl->assign("novica3naslov",$data['novica3naslov']);
		$this->tpl->assign("novica3datum",$data['novica3datum']);
		$this->tpl->assign("novica3link",$data['novica3link']);
		$this->tpl->assign("novica3vsebina",$data['novica3vsebina']);
		
		$this->tpl->assign("novica4naslov",$data['novica4naslov']);
		$this->tpl->assign("novica4datum",$data['novica4datum']);
		$this->tpl->assign("novica4link",$data['novica4link']);
		$this->tpl->assign("novica4vsebina",$data['novica4vsebina']);
		
		$ste=1;
		while($data['novo'.$ste.'naslov']){
			$this->tpl->newBlock("novogradnje");
			$this->tpl->assign("novonaslov",$data['novo'.$ste.'naslov']);
			$this->tpl->assign("novolink",$data['novo'.$ste.'link']);	
			$ste++;		
		}

		$this->tpl->gotoBlock("_ROOT");
		/*$this->tpl->assign("novo2naslov",$data['novo2naslov']);
		$this->tpl->assign("novo2link",$data['novo2link']);
		$this->tpl->assign("novo3naslov",$data['novo3naslov']);
		$this->tpl->assign("novo3link",$data['novo3link']);
		$this->tpl->assign("novo4naslov",$data['novo4naslov']);
		$this->tpl->assign("novo4link",$data['novo4link']);
		$this->tpl->assign("novo5naslov",$data['novo5naslov']);
		$this->tpl->assign("novo5link",$data['novo5link']);
		$this->tpl->assign("novo6naslov",$data['novo6naslov']);
		$this->tpl->assign("novo6link",$data['novo6link']);
		$this->tpl->assign("novo7naslov",$data['novo7naslov']);
		$this->tpl->assign("novo7link",$data['novo7link']);
		$this->tpl->assign("novo8naslov",$data['novo8naslov']);
		$this->tpl->assign("novo8link",$data['novo8link']);*/
		
		$this->tpl->assign("slika", $data['slika']);

		$this->tpl->assign("email_sha", $data['email_sha']);

        $this->tpl->prepare();
		$this->email['text'] = $this->tpl->getOutputContent(); // dela
		//$this->email['text'] = "bla";      //dela

		if($export > 0){
			return $this->email['text'];
			
		}else{
			$this->Send();
		}
	}

	function eNoviceNotice($email,$data){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Hvala za prijavo na Zabec.net eNovice";

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/enovice_welcome.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov","Hvala za prijavo na Zabec.net eNovice");
		$this->tpl->assign("email",$email);
		$this->tpl->assignGlobal("to",$this->email['to_email']);
		$this->tpl->prepare();
		$this->email['text'] = $this->tpl->getOutputContent();
		$this->Send();
	}
	function SendNotice($data){

		//Create the message
		$message = Swift_Message::newInstance()
  			->setSubject("Naročilo")
  			->setFrom(array($this->settings['from_email'] => $this->settings['from_name']))
  			->setTo(array($this->settings['notice_email']))
  			->setBody(strip_tags("Oddano naročilo " . $data['order_id'] . " na ime " . $data['user']))
  		;
  		$result = $this->mailer->send($message);
	}

	function Send(){

		//Create the message
		$message = Swift_Message::newInstance()
  			->setSubject($this->email['subject'])
  			->setFrom(array($this->settings['from_email'] => $this->settings['from_name']))
  			->setTo(array($this->email['to_email']))
  			->setBody(strip_tags($this->email['text']))
  			->addPart($this->email['text'], 'text/html')
  		;
  		$result = $this->mailer->send($message);
	}

	function SendAttach(){
		//Create the message
		$message = Swift_Message::newInstance()
  			->setSubject($this->email['subject'])
  			->setFrom(array($this->settings['from_email'] => $this->settings['from_name']))
  			->setTo(array($this->email['to_email']))
  			->setBody(strip_tags($this->email['text']))
  			->addPart($this->email['text'], 'text/html')
  			 ->attach(Swift_Attachment::fromPath($this->email['attach']))
  		;
  		$result = $this->mailer->send($message);
	}

}



	function Welcome($email,$data){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Dobrodošli v spletni mlaki";

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/welcome.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$data['email']);
		$this->tpl->assign("user",$data['user']);
		$this->tpl->assign("ip",$data['ip']);
		$this->tpl->assign("date",$data['date']);
		$this->tpl->assignGlobal("to",$this->email['to_email']);

		$this->email['text'] = $this->tpl->getOutputContent();

		$this->Send();
	}

	function Order_Moneta($email,$data){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Potrditev naročila " . $data['order_id'];

		$this->tpl = new TemplatePower("templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message","templates/email/narocilo_moneta.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$data['email']);
		$this->tpl->assign("user",$data['user']);
		$this->tpl->assign("ip",$data['ip']);
		$this->tpl->assign("date",$data['date']);
		$this->tpl->assign("link",$data['link']);
		$this->tpl->assignGlobal("to",$this->email['to_email']);


		$this->email['attach'] = $_SERVER['DOCUMENT_ROOT'] . "/files/tmp/" . $data['pdf'];
		$this->email['text'] = $this->tpl->getOutputContent();

		$this->SendAttach();
		$this->SendNotice($data);
	}

	function PayConfirmed_Moneta($email,$narocilo){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Potrjeno plačilo naročila " . $narocilo;

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/payment.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$email);
		$this->tpl->assign("narocilo",$narocilo);
		$this->tpl->assignGlobal("to",$this->email['to_email']);

		$this->email['text'] = $this->tpl->getOutputContent();

		$this->Send();

	}

	function PayConfirmed_Paypal($email,$narocilo){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Potrjeno plačilo naročila " . $narocilo;

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/payment.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$email);
		$this->tpl->assign("narocilo",$narocilo);
		$this->tpl->assignGlobal("to",$this->email['to_email']);

		$this->email['text'] = $this->tpl->getOutputContent();

		$this->Send();

	}


	function Order_Paypal($email,$data){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Potrditev naročila " . $data['order_id'];

		$this->tpl = new TemplatePower("templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message","templates/email/narocilo_paypal.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$data['email']);
		$this->tpl->assign("user",$data['user']);
		$this->tpl->assign("ip",$data['ip']);
		$this->tpl->assign("date",$data['date']);
		$this->tpl->assign("pdf",$data['pdf']);
		$this->tpl->assignGlobal("to",$this->email['to_email']);


		$this->email['attach'] = $_SERVER['DOCUMENT_ROOT'] . "/files/tmp/" . $data['pdf'];
		$this->email['text'] = $this->tpl->getOutputContent();

		$this->SendAttach();
		$this->SendNotice($data);
	}

	function Order_Bill($email,$data){

		$this->email['to_email'] = $email;
		$this->email['subject'] = "Potrditev naročila " . $data['order_id'];

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/narocilo_pdf.tpl");
		$this->tpl->prepare();
		$this->tpl->assign("naslov",$this->email['subject']);
		$this->tpl->assign("email",$data['email']);
		$this->tpl->assign("user",$data['user']);
		$this->tpl->assign("ip",$data['ip']);
		$this->tpl->assign("date",$data['date']);
		$this->tpl->assign("pdf",$data['pdf']);
		$this->tpl->assignGlobal("to",$this->email['to_email']);


		$this->email['attach'] = $_SERVER['DOCUMENT_ROOT'] . "/files/tmp/" . $data['pdf'];
		$this->email['text'] = $this->tpl->getOutputContent();

		$this->SendAttach();
		$this->SendNotice($data);
	}
	
	function svetovanje($data){

		$this->email['to_email'] = $data['email'];
		$this->email['subject'] = 'Odgovor na tvoje vprašanje.';

		$this->tpl = new TemplatePower($this->path . "templates/email/email_body.tpl");
		$this->tpl->prepare();
		$this->tpl->assigninclude("message",$this->path . "templates/email/svetovanje.tpl");
		$this->tpl->prepare();
		#$this->tpl->assign("naslov",$this->email['subject']);
		#$this->tpl->assign("email",$data['email']);
		#$this->tpl->assign("user",$data['user']);
		#$this->tpl->assign("ip",$data['ip']);
		#$this->tpl->assign("date",$data['date']);
		$this->tpl->assignGlobal("to",$this->email['to_email']);
		$this->tpl->assignGlobal('povezava', $data['povezava']);

		$this->email['text'] = $this->tpl->getOutputContent();

		$this->SendAttach();
		$this->SendNotice($data);
	}

