<?php

class NewsletterController extends Zend_Controller_Action {

	public function init() {			
	}
	
	public function saveAction() {
		$v = $this->_request->getParams();	

		if( isset($v['newsletter_email']) ){
		
			$Newsletter = new Newsletter();	
			$select = $Newsletter->select();		
			$select->where('newsletter_email = ?', $v['newsletter_email']);	
			$row = $Newsletter->fetchRow($select);
			
			if( !$row ){
				// zapisz i wyslij mail
			
				$hashgen = hash('sha256',time().rand(10000,99999).$v['newsletter_email']);
				$data = array(
					'newsletter_code' => $hashgen,
					'newsletter_email' => $v['newsletter_email']
				);
				$idIns = $Newsletter->insert($data);
				
				$body = Lib_Lang::translate(mail_newsletter_body);				
				$link = WWW_PATH."/newsletter/activation/code/".$hashgen;				
				$body = str_replace('%LINK%',$link,$body);
				$s= "=?utf-8?b?".base64_encode(Lib_Lang::translate(mail_newsletter_subject))."?=";
				$headers = "MIME-Version: 1.0\r\n";
				$headers.= "From: =?utf-8?b?".base64_encode(MAIL_LOGIN)."?= <".MAIL_LOGIN.">\r\n";
				$headers.= "Content-Type: text/html;charset=utf-8\r\n";
				$headers.= "Reply-To: ".MAIL_LOGIN."\r\n";  
				$headers.= "X-Mailer: PHP/" . phpversion();
				mail($v['newsletter_email'], $s, $body, $headers);	
				$this->view->sendTrue = true;	
			
			}else{
			
				if( $row->newsletter_activ == 1 ){	
					$this->view->sendTrue = false;					
				}
			
				if( $row->newsletter_activ == 0 OR $row->newsletter_activ == 2 ){				
					$hashgen = $row->newsletter_code;
					$body = Lib_Lang::translate(mail_newsletter_body);				
					$link = WWW_PATH."/newsletter/activation/code/".$hashgen;
				
					$body = str_replace('%LINK%',$link,$body);
					$s= "=?utf-8?b?".base64_encode(Lib_Lang::translate(mail_newsletter_subject))."?=";
					$headers = "MIME-Version: 1.0\r\n";
					$headers.= "From: =?utf-8?b?".base64_encode(MAIL_LOGIN)."?= <".MAIL_LOGIN.">\r\n";
					$headers.= "Content-Type: text/html;charset=utf-8\r\n";
					$headers.= "Reply-To: ".MAIL_LOGIN."\r\n";  
					$headers.= "X-Mailer: PHP/" . phpversion();
					mail($v['newsletter_email'], $s, $body, $headers);		
					$this->view->sendTrue = true;		
				}
			
			}
		
		
		
		
		
		
		
		
		}
		
	}
	
	public function remAction() {
		$v = $this->_request->getParams();

		if( isset($v['newsletter_email']) ){
		
			$Newsletter = new Newsletter();	
			$select = $Newsletter->select();		
			$select->where('newsletter_email = ?', $v['newsletter_email']);	
			$row = $Newsletter->fetchRow($select);
			
			if( $row ){			
				
				$hashgen = $row->newsletter_code;
				$body = Lib_Lang::translate(mail_newsletter_debody);				
				$link = WWW_PATH."/newsletter/deactivation/code/".$hashgen;				
				$body = str_replace('%LINK%',$link,$body);
				$s= "=?utf-8?b?".base64_encode(Lib_Lang::translate(mail_newsletter_desubject))."?=";
				$headers = "MIME-Version: 1.0\r\n";
				$headers.= "From: =?utf-8?b?".base64_encode(MAIL_LOGIN)."?= <".MAIL_LOGIN.">\r\n";
				$headers.= "Content-Type: text/html;charset=utf-8\r\n";
				$headers.= "Reply-To: ".MAIL_LOGIN."\r\n";  
				$headers.= "X-Mailer: PHP/" . phpversion();
				mail($v['newsletter_email'], $s, $body, $headers);	
				$this->view->emailSend = true;	
			
			}else{
			
				$this->view->emailFalse = true;			
			
			}
		
		
		
		
		
		
		
		
		}
	}
	
	
	public function activationAction() {
		$v = $this->_request->getParams();				
			
		$Newsletter = new Newsletter();
		$data = array(
			'newsletter_activ' => 1
		);
		$Newsletter->update( $data, "newsletter_code LIKE '".$v['code']."'" );
	}
	
	
	public function deactivationAction() {
		$v = $this->_request->getParams();				
			
		$Newsletter = new Newsletter();
			
		$where = array(
			'newsletter_code = ?' => $v['code']
		);
		$Newsletter->delete($where);
	}
	
}
