<?php
 class ReservierungenController extends BasicController{
 	private $validate 				= null;
 	private $emailHelper			= null;
 	private $companyId				= null;
 	private $companyEmail			= null;

 /********************************************************************
 * default constructor
 *******************************************************************/
	public function __construct($_postVars, $_getVars) {
		parent :: __construct($_postVars, $_getVars);
		$this->manager 						= new ReservationManager();
		$this->emailHelper 					= new EmailHelper();
		$this->validate 					= new Validate();
		$this->companyId					= (int)$this->configInstance->getConstant('companyid');
		$this->companyEmail 				= (string)$this->configInstance->getEmail('info');
	}
	public function btnInsertReservation_onClick() {
		//clean post daten
		$post = $this->cleanPost($this->postVars);
		$error = $this->manager->checkReservation($this->companyId,$post);
		if(empty($error)){
            if(isset($post['hours_of_visit']) && $post['hours_of_visit'] >= 0 && $post['hours_of_visit'] <= 8){
                $dauer = $post['hours_of_visit'] != 0 ? $post['hours_of_visit']." Stunden" : 'open end';
                $post['info'] .= ". Dauer meines Besuches: ".$dauer;
            }
			$reservationId = $this->manager->insertReservation($post,$this->companyId);
			if(is_numeric($reservationId) && $reservationId > 0){
				 //E-Mail template
                $this->smartyInstance->assign('SEX', ucfirst($post['sex']));
                $this->smartyInstance->assign('LASTNAME', ucfirst($post['lastname']));
                $this->smartyInstance->assign('FIRSTNAME', ucfirst($post['firstname']));
                $this->smartyInstance->assign('RESERVATIONTIME', $post['reservationHour'] . ":" . $post['reservationMinute']);
                $this->smartyInstance->assign('RESERVATIONDATE', $post['reservationdate']);
                $this->smartyInstance->assign('ORDERDATE', date('d.m.Y', time()));
                $this->smartyInstance->assign('ORDERTIME', date('H:i:s', time()));
                $this->smartyInstance->assign('EMAIL', $post['email']);
                $this->smartyInstance->assign('NUMBERS', $post['numberofperson']);
                $this->smartyInstance->assign('COMMENT', $post['info']);
                $this->smartyInstance->assign('EAT', $post['eat']);
				$this->smartyInstance->assign('WATELISTE', isset($post['watelist'])?  1 : 0);
				$this->smartyInstance->assign('MAILINGLIST', $this->companyEmail);
                $this->smartyInstance->assign('RESERVATIONID',Util::encode($reservationId));

                $emailContent = $this->smartyInstance->fetch('email/reservationConfirm.tpl');
                /* End E-Mail template*/
                $sendEmail = $this->emailHelper->sendEmail("Reservierungsbestätigung", $emailContent, $post['email'],true);
                if (!$sendEmail)
               {
                    $this->errorInstance->logError("E-Mail konnte nicht gesendet werden", __CLASS__."->".__METHOD__." Zeile ".__LINE__,true);
                }
				$this->success = $this->messageInstance->getSuccessMsg('save');
			}else{
				$this->error = $this->messageInstance->getErrorMsg('save');
			}
		}else{
			$this->error = $error;
			$day 	= $this->postVars['birthday']['Date_Day'];
			$month 	= $this->postVars['birthday']['Date_Month'];
			$year 	= $this->postVars['birthday']['Date_Year'];
			$birthday = $year."-".$month."-".$day;
			$this->smartyInstance->assign('BIRTHDAY', $birthday);
		}
		$this->show();
	}
	public function show () {
		$_SESSION['bloombar_reservation'] = "first";//variable anhand derer die iframe hoehe anpasst wird
        if(empty($this->error) || !$smarty.post.btnInsertReservation){
			$this->smartyInstance->assign('RESERVATIONPLACEAREA',$this->manager->getReservationPlaceArea());
			$this->smartyInstance->assign('RESERVATIONREASON',$this->manager->getReseravtionReasons());
            $_SESSION['bloombar_reservation'] = "second";

		}
		$this->smartyInstance->assign('TPL_INCLUDE', $this->templatePath.$this->template);
		$this->smartyInstance->load_filter('output','trimwhitespace');
		$this->smartyInstance->display('page_body.tpl');
	}
	public function isValid () {
		return true;
	}
	public function showPrintable () {
		$this->show();
	}
 }
?>