<?php

/**
 * Description of IndexController
 *
 * @author janusz
 */
class Newsletter_Controller_IndexController extends Eppe_Frontend_Controller_Action {

        protected $_post = array();
        protected $_subscriber;
        protected $_enctyptionInstance;

        public function widgetSingInAction() {
                $form = new Newsletter_Form_SingIn();
                $this->view->form = $form;
        }

        public function ajaxSignInAction() {
                $response = array();
                $form = new Newsletter_Form_SingIn();

                if (NULL !== $this->_post = $this->getRequest()->getPost()) {
                        if ($form->isValid($this->_post)) {
                                $this->_insertSubscriber();
                                $this->_sendNotificaton();

                                $response = array('success' => true,
                                    'message' => '<div class="newsletter-block newsletter-ok">
					<span class="bold">Dziękujemy za zapisanie się!</span>
					Prosimy o potwierdzenie przez
					link zawarty w e-mailu.
				    </div>');
                        } else {
                                $message = '';
				
				$msgArray = array();
                                foreach ($form->getMessages() as $msg) {
					foreach($msg as $m){
						$msgArray[] = $m;
					}
                                }
				foreach (array_unique($msgArray) as $msg){
					$message .= $msg;
				}
                                $response = array('success' => false,
                                    'message' => $message
                                );
                        }
                }
                echo Zend_Json::encode($response);
                exit;
        }

        protected function _insertSubscriber() {
                $this->_enctyptionInstance = Eppe_Encryption::getInstance();
                $this->_createSubscriberRow();
        }

        protected function _createSubscriberRow() {
                $subscribersModel = new Newsletter_Model_Nluser();

                $this->_subscriber = R::dispense($subscribersModel::NAME);
                $this->_subscriber->email = $this->_post['email'];
                $this->_subscriber->name = $this->_post['name'];
                $this->_subscriber->number = $this->_post['number'];
                $this->_subscriber->active = 0;
                $this->_subscriber->mailconfirm = 0;
                $this->_subscriber->codeconfirm = $this->_enctyptionInstance->getSalt(12);
                R::store($this->_subscriber);
        }

        protected function _sendNotificaton() {
                $view = new Zend_View();
                $mail = new Zend_Mail('utf-8');

                $url = '<a href="' . $view->serverUrl() . $view->url(array('id' => $this->_subscriber->id, 'hash' => base64_encode($this->_subscriber->codeconfirm)), 'nl-confirm') . '">Zapisuje się do newslettera</a>';
                $data = [
                    'messageBody' => "<p>Dziękujemy!</p><p>Proszę kliknąć w poniższy link aby dokończyć zapisywa się do newslettera</br>$url</p>",
                    'imagesPath' => Newsletter_Module_Module::getInstance()->getFrontImgPath()
                ];

                $mail->setBodyHtml($this->view->partial('send-notification.phtml', $data));

                $mail->setFrom(Newsletter_Sender::NEWSLETTER_EMAIL, Newsletter_Sender::NEWSLETTER_NAME);

                $mail->addTo(trim($this->_subscriber->email));

                $mail->setSubject('Newsletter - Okręgowa Izba Lekarska w Szczecinie');
                $mail->send();
        }

        public function confirmNewsletterAction() {
                if (NULL === $id = $this->_getParam('id'))
                        $this->_redirect($this->view->url(array(), 'homepage'));

                if (NULL === $hash = $this->_getParam('hash'))
                        $this->_redirect($this->view->url(array(), 'homepage'));

                $hash = base64_decode($hash);

                $subscribersModel = new Newsletter_Model_Nluser();
                $bean = R::findOne($subscribersModel::NAME, 'id = :beanId', array('beanId' => $id));

                if ($bean) {
                        if ($bean->codeconfirm == $hash) {
                                if ($bean->mailconfirm == 0) {
                                        $bean->mailconfirm = 1;
                                        $bean->active = 1;
                                        R::store($bean);
                                        R::exec("INSERT INTO nlgroupassign (nluser_id,nlgroup_id) VALUES({$bean->id},1)");
                                        $this->view->status = 0; //all ok
                                        $this->view->msg = '<span class="bold">Dziękujemy! Twoje konto zostało dodane do naszego newslettera.</span><br/>';
                                } else {
                                        $this->view->status = 3; //link nie aktywny
                                        $this->view->msg = '<span class="bold">Link aktywacyjny był użyty</span><br/>';
                                }
                        } else {
                                $this->view->status = 1; //zły hash
                                $this->view->msg = '<span class="bold">Nie prawidłowy link aktywacyjny</span><br/>';
                        }
                } else {
                        $this->view->status = 2; //brak rekordu
                        $this->view->msg = '<span class="bold">Nie prawidłowy link aktywacyjny</span><br/>';
                }
        }

        /**
         * Akcja może być wywoływana z bezpośredniego linka z e-maila
         */
        public function signOutAction() {
                if (NULL === $id = $this->_getParam('id'))
                        $this->_redirect($this->view->url(array(), 'homepage'));

                if (NULL === $hash = $this->_getParam('hash'))
                        $this->_redirect($this->view->url(array(), 'homepage'));

                $hash = base64_decode($hash);

                $subscribersModel = new Newsletter_Model_Nluser();
                $bean = R::findOne($subscribersModel::NAME, 'id = :beanId', array('beanId' => $id));

                if ($bean) {
                        R::exec("DELETE FROM `nlgroupassign` WHERE (`nluser_id` = '$id')");
                        R::trash($bean);
                        $this->view->status = 0; //all ok
                        $this->view->msg = '<span class="bold">Twój email został usunięty z naszego newslettera</span><br/>';
                } else {
                        $this->view->status = 2; //brak rekordu
                        $this->view->msg = '<span class="bold">Nie prawidłowy link</span><br/>';
                }
        }

}

?>
 