<?php
/**
 * ****************************************************************
 * Project:      table-booking
 * Copyright:    Copyright (c) 2007
 * Company:      Thomas Niemann
 * ****************************************************************
 * Workfile:     ReservationManager.php
 * ****************************************************************
 * Description:  
 * ***************************************************************/
class NewsletterManager
{
    private $messageInstance = null;
    private $configInstance = null;
    private $wsdl_file = null;
    private $username = null;
    private $password = null;
    private $validate = null;
    /**
     * default Constructor
     */
    public function __construct()
    {
        $this->configInstance 	= ConfigLoader::getInstance();
        $this->messageInstance 	= MessageLoader::getInstance();
        $this->validate 		= new Validate();
        $this->wsdl_file 		= (string)$this->configInstance->getConfigPath("sopaServer");
        $this->username 		= (string)$this->configInstance->getAppSettings("soapLoginUsername");
        $this->password 		= Util::encode((string)$this->configInstance->getAppSettings("soapLoginPassword"));
    }
    public function removeClientFromNewsletter($_email, $_company_id)
    {
        $soapclient = new SoapClient($this->wsdl_file, array("trace" => 1, "exceptions" => 0));
        $result = $soapclient->removeClientFromNewsletter($this->username, $this->password, $_email);
        //exceptions abfangen
        if (is_soap_fault($result)) {
            $errorMsg = "FehlerCode: " . $result->faultcode . "\n";
            $errorMsg .= "Beschreibung: " . $result->faultstring . "\n";
            $errorMsg .= "Sender: " . $result->faultactor . "\n";
            $errorMsg .= "Fehler: " . $result->faultname . "\n";
            ErrorLogLoader::getInstance()->logError($errorMsg, __CLASS__." --> ".__METHOD__." Zeile ".__LINE__, true);
            return -1;
        } else
        	return $result;
    }
    public function insertClientToNewsletter($_post, $_company_id)
    {
        //POST DATEN
        $postData = array();
        $postData['lastname'] 			= ucfirst($_post['lastname']);
        $postData['firstname'] 			= ucfirst($_post['firstname']);
        $postData['handy'] 				= $_post['handy'];
        $postData['email'] 				= $_post['email'];
        
        $soapclient = new SoapClient($this->wsdl_file, array("trace" => 1, "exceptions" => 0));
        $result = $soapclient->insertClientToNewsletter($this->username, $this->password, Util::encodeUtf8($postData));
        //exceptions abfangen
        if (is_soap_fault($result)) {
            $errorMsg = "FehlerCode: " . $result->faultcode . "\n";
            $errorMsg .= "Beschreibung: " . $result->faultstring . "\n";
            $errorMsg .= "Sender: " . $result->faultactor . "\n";
            $errorMsg .= "Fehler: " . $result->faultname . "\n";
            ErrorLogLoader::getInstance()->logError($errorMsg, __CLASS__." --> ".__METHOD__." Zeile ".__LINE__, true);
            return -1;
        } else
        	return $result;
    }
    public function checkNewsletter($_companyId, $_post) {
		$_errorMessage = array();
		foreach($_post as $key => $value){;
			switch ($key) {
						
				case 'firstname':
					if(empty($value) && !$_insert)
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('empty_firstname','form')));
					elseif(strlen($value) > 30 && !$_insert)
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('invalid_firstname','form')));
					break;
						
				case 'lastname':
					if(empty($value))
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('empty_lastname','form')));
					elseif(strlen($value) > 25)
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('invalid_lastname','form')));
					break;
					
				case 'handy':
					if(!empty($value) && !ValidateHelper::validatePhonenumber($value))
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('invalid_handy','form')));
					break;
					
				case 'email':
					if(empty($value))
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('empty_email','form')));
					elseif(false === $this->validate->email($value,true))
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('invalid_email','form')));
					break;
					
				case 'captchastring':
					include_once './extensions/_securimage/securimage.php';
					$securimage = new Securimage();
					if (empty ($value)) {
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('empty_captcha')));
					}
					elseif ($securimage->check($value) == false) {
						array_push($_errorMessage ,trim($this->messageInstance->getErrorMsg('wrong_captcha')));
					}
				break;
			}
		}
		return $_errorMessage;
	}
}
?>