<?php



use Website\Controller\Action;

use \Website\Model\Auth;



class AuthController extends Action {



    public function inscriptionAction()

    {

        $this->enableLayout()->setLayout('full');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/full.css');



        $form = new \Website\Tool\RegisterForm();



        if ($_POST['submit'] != '' && $form->isValid($_POST)){



            try{

                Website\Model\Auth::create(array(

                    "name"      => $form->getValue('name'),

                    "surname"   => $form->getValue('surname'),

                    "email"     => $form->getValue('email'),

                    "password"  => $form->getValue('password')

                ));

                $this->redirect($this->document->getFullPath().'/success');

                return;

            }catch (\Exception $e){

                $this->view->form = $form;

                $this->view->duplicateEmail = true;

            }

        }

        $this->view->form = $form;





    }



    public function loginAction(){

        $this->enableLayout()->setLayout('full');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/full.css');



        $form = new \Website\Tool\LoginForm();



        if ($_POST['submit'] != '' && $form->isValid($_POST)){



          if(Auth::login($form->getValue('email'), $form->getValue('password'), $form->getValue('rememberme')) == true){

              $this->redirect($this->document->getParent()->getFullPath());

              return;

          }

        }



        $this->view->form = $form;

    }



    public function logoutAction(){

        Auth::logout();

        $this->redirect($this->document->getParent()->getFullPath());

    }



    public function forgetPasswordAction(){

        $this->enableLayout()->setLayout('full');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/full.css');



        $form = new \Website\Tool\PasswordForgetForm();



        if($this->_request->isPost() && $form->isValid($_POST)){



            try{

                $a = Auth::getByEmail($form->getValue('email'));

                $newPassword = $a->resetPassword();



                $mail = new \Pimcore\Mail();

                $mail->addTo($a->getEmail());

                $mail->setDocument('/' . $this->view->lang . '/mail/password-forget');

                $mail->setParams(array("newPassword" => $newPassword));

                $mail->send();



            }catch (\Exception $e){}



            $this->redirect($this->document->getFullPath().'/success');

        }

        $this->view->form = $form;

    }



    public function changePasswordAction(){

        $this->enableLayout()->setLayout('full');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/full.css');



        $form = new \Website\Tool\UpdatePasswordForm();



        if($this->_request->isPost() && $form->isValid($_POST)){



            try{

                $a = Auth::getCurrentUser();





                if ($a instanceof Auth){





                    $a->updatePassword($form->getValue('password'));



                }



            }catch (\Exception $e){

                var_dump($e->getMessage());

            }



            $this->redirect($this->document->getFullPath().'/success');

        }

        $this->view->form = $form;



    }





}