<?php

class Todojsc_UsersController extends Zend_Controller_Action
{

    public function init()
    {
        $logger = Zend_Registry::get('log');
        $logger->log('Users Controller init', Zend_Log::INFO);
        $this->_model = new Todojsc_Model_Users();
        $this->_authService = new Todojsc_Service_Authentication();
        $this->view->loginForm = $this->getLoginForm();
        $this->view->registerForm = $this->getRegistrationForm();
    }

    public function getLoginForm()
    {
        $urlHelper = $this->_helper->getHelper('url');
        $this->_forms['login'] = $this->_model->getForm('login');
        $this->_forms['login']->setAction($urlHelper->url(array('controller' => 'users', 'action' => 'authenticate'), 'default'));
        return $this->_forms['login'];
    }

    public function getRegistrationForm()
    {
        $urlHelper = $this->_helper->getHelper('url');
        $this->_forms['register'] = $this->_model->getForm('register');
        $this->_forms['register']->setAction($urlHelper->url(array('controller' => 'users', 'action' => 'complete-registration'), 'default'));
        return $this->_forms['register'];
    }

    public function getEditUserForm($username)
    {
        $urlHelper = $this->_helper->getHelper('url');
        $this->_forms['user-edit'] = $this->_model->getForm('UserEdit');
        $this->_forms['user-edit']->setUser($username);
        $this->_forms['user-edit']->setAction($urlHelper->url(array('controller' => 'users', 'action' => 'index'), 'default'));
        $this->_forms['user-edit']->populate(array('uid' => $username));
        return $this->_forms['user-edit'];
    }

    public function getEditUserContactForm($username)
    {
        $urlHelper = $this->_helper->getHelper('url');
        $this->_forms['user-edit-contact'] = $this->_model->getForm('UserEditContact');
        $this->_forms['user-edit-contact']->setUser($username);
        $this->_forms['user-edit-contact']->setAction($urlHelper->url(array('controller' => 'users', 'action' => 'index'), 'default'));
        $this->_forms['user-edit-contact']->populate(array('uid' => $username));
        return $this->_forms['user-edit-contact'];
    }
    
    public function indexAction()
    {
        try
        {
            $this->view->users = $this->_model->getUsers();
            $this->view->title = "Users List";
            $this->view->headTitle($this->view->title, 'PREPEND');
        }
        catch (Exception $ex)
        {
            return $this->render('login');
        }
    }

    public function editAction()
    {
        $username = $this->_getParam('username', '');
        $this->view->editUserForm = $this->getEditUserForm($username);
        $this->view->editUserContactForm = $this->getEditUserContactForm($username);
    }

    public function deleteAction()
    {
        // action body
    }

    public function authenticateAction()
    {
        $request = $this->getRequest();
        if (!$request->isPost())
        {
            return $this->_helper->redirector('index');
        }
                
        $form = $this->_forms['login'];
        if (!$form->isValid($request->getPost()))
        {
            return $this->render('login');
        }
        if (false == $this->_authService->authenticate($form->getValues()))
        {
            $form->setDescription('Login failed, try again.');
            return $this->render('login');
        }
        return $this->_helper->redirector('index');
    }

    public function loginAction()
    {
        //No specific action to take, we just display the login form.
    }

    public function logoutAction()
    {
        $this->_authService->clear();
                return $this->_helper->redirector('index');
    }

    public function registerAction()
    {
        //No specific action to take, we just display the registration form.
    }

    public function completeRegistrationAction()
    {
        $request = $this->getRequest();
        if (!$request->isPost())
        {
            return $this->_helper->redirector('register');
        }

        $form = $this->_forms['register'];
        if (!$form->isValid($request->getPost()))
        {
            return $this->render('register');
        }
        if (false == $this->_model->registerUser($form->getValues()))
        {
            $form->setDescription('Registration failed, try again.');
            return $this->render('register');
        }
        //return $this->_helper->redirector('index');
    }


}

















