<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class RegistrationController extends Zend_Controller_Action 
{
	
	public function init()
	{
		Zend_Layout::getMvcInstance()->setLayout('registration');
		$settings = new Settings();
        if (!$settings->getValue('registrace-povolena-f')) $this->_redirect('/');
	}
	
	public function successAction(){}
	public function failAction(){}
	/**
	 * The default action - show the home page
	 */
    public function indexAction() 
    {
		$this->view->success = 0;
    	
    	$form = $this->getForm();
    	
		$r = $this->getRequest();
		
		if ($r->isPost()) {
			if ($form->isValid($r->getPost())) {
				
				$values  = $form->getvalues();
				
				$pass = true;
				$errors = array();
				
				$users = new Users();
        		$row = $users->fetchRow($users->select()->where('login LIKE ?', $values['login']));
        		
        		if ($row) {
        			$pass = false;
        			$errors['prihlasovaci-jmeno-f'] = 'loginUsed';
        		}
        		
        		$row = $users->fetchRow($users->select()->where('email LIKE ?', $values['email']));
				if ($row) {
					$pass = false;
        			$errors['e-mail-f'] = 'emailUsed';
        		}
        		
        		if ($values['password'] !== $values['password_check']) {
					$pass = false;
        			$errors['heslo-f'] = 'passwordsNotMatch';
        		}
        		
        		
        		if (!$pass) {
        			$form->populate($values);
        			$this->view->errors = $errors;
        			$this->view->form = $form;
        			return;
        		} else {
        			
        			unset($values['password_check']);
        			
        			$values['user_active_request'] = 1;
        			$values['user_active'] = 0;
        			$values['super_admin'] = 0;
        			$values['send_mail'] = 1;
        			
        			$oldPassword = $values['password'];
        			$values['password'] = sha1($values['password']);
        			
        			if ($users->insert($values)) {
        				
        				$users = new Users();
        				$rows = $users->fetchAll($users->select()->where('super_admin = 1')->where('user_active = 1'));
        				
        				$messages = new Messages();
        				
        				$subject['cs'] = 'Nová registrace';
        				$subject['en'] = 'New registration';
        				
        				$body['cs'] = 'Nová registrace - aktivaci účtu nového uživatele můžete provést v sekci administrace -> uživatelé.';
        				$body['en'] = 'New registration - you can activate new user´s account in administration -> users section.';
        				
        				
    					$data['is_bin'] = 0;
    					$data['is_read'] = 0;
    					$data['datetime'] = new Zend_Db_Expr('NOW()');
    					$data['id_sender'] = 0;
        				
        				foreach ($rows as $i)
        				{
        					$lang = ($i->lang) ? $i->lang : 'cs';
        					$data['body'] = $body[$lang];
    						$data['subject'] = $subject[$lang];
        					$data['id_reciever'] = $i->id_user;
	    					$messages->insert($data);    					
        				}
        				
        				$mailer = new Mailer();
        				$values['password'] = $oldPassword;
        				$mailer->send('registration', $values['email'], $values);
        				
        				$this->_redirect('registration/success');
        			} else {
        				$this->_redirect('registration/fail');
        			}
        			
					return;        			
        			
        		}
				
			} else {
				$form->populate($form->getUnfilteredValues());
    			if ($form->isErrors()) {
    				$helper = new FormHelper();
    				$errors = $helper->errorsToString($form);
    				
    				$this->view->errors = $errors;
    			}
			}
		}
		$this->view->form = $form;
    }
    
    private function getForm()
    {
    	$form = new Zend_Form();
    	$form->clearDecorators();
//    	$form->addElementPrefixPath('Malf_Validate_', 'Malf/Validate/', 'validate');
    				
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'user_firstname',
    					'label' => 'jmeno-f',
    					'required' => true,
    					'validators' => array('notEmpty')
    				)));
    				
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'user_surname',
    					'label' => 'prijmeni-f',
    					'required' => true,
    					'validators' => array('notEmpty')
    				)));
    				
    	
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'login',
    					'label' => 'prihlasovaci-jmeno-f',
    					'required' => true,
    					'validators' => array('notEmpty')
    				)));
    				
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'email',
    					'label' => 'e-mail-f',
    					'required' => true,
    					'validators' => array('notEmpty', 'emailAddress')
    				)));
    				
    	
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'password',
    					'label' => 'heslo-f',
    					'required' => true,
    					'validators' => array('notEmpty')
    				)));
    				
    	
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'password_check',
    					'label' => 'heslo-znovu-f',
    					'required' => true,
    					'validators' => array('notEmpty')
    				)));
    				
    	return $form;
    }
}