<?php
class UzivatelController extends Zend_Controller_Action {
	public function indexAction(){		
	}
	
	public function loginAction(){	
		$registracia = $this->_request->getParam('registracia');		
		if(!empty($registracia)){
			$this->_redirect('/uzivatel/registracia');
		}
		
		$username = $this->_request->getParam('username');
		$password = $this->_request->getParam('password');
		if(empty($username) || empty($password)){
			$this->_redirect('/');	
		}
		
		$user = new Model_DbTable_Uzivatel();
		$DB = $user->getDefaultAdapter();	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$registry = Zend_Registry::getInstance();
		//$DB = $registry->get('db');		
		var_dump($this->_request->getParams());
		$auth = Zend_Auth::getInstance();
		$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
   		$authAdapter->setTableName('user')
               ->setIdentityColumn('login')
               ->setCredentialColumn('password');    
   		$authAdapter->setIdentity($username);
   		$authAdapter->setCredential(md5($password));
   		
   		$result = $auth->authenticate($authAdapter);
   		if($result->isValid()){
  			$data = $authAdapter->getResultRowObject(null,'password');  		
  			$auth->getStorage()->write($data);  		
  			$this->_redirect('/uzivatel/info');
		}else{
  			$this->_redirect('/');
		}				
	}
	
	public function infoAction(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			$this->_redirect('/uzivatel/login');
		}
		$this->view->uzivatel = $auth->getStorage()->read();
	}
	
	public function registraciaAction(){
		$odosli = $this->_request->getParam('odosli');
		if(!empty($odosli)){
			$login = $this->_request->getParam('login');
			$mail = $this->_request->getParam('mail');
			$password1 = $this->_request->getParam('password1');
			$password2 = $this->_request->getParam('password2');
		  $uzivatel = new Model_DbTable_Uzivatel();
		  $data = array(
		    'login' => $login,
		    'password' => md5($password1),
		    'mail' => $mail,
		    'date' => new Zend_Db_Expr('NOW()'),
		    'acl_id' => '0',
		    'profile_id' => '-1'		  
		  );	
		  if(($password1 == $password2) && !empty($mail) && !empty($login)){
		    $uzivatel->insert($data);
		    $this->_redirect('/');
		  }		  
		  $this->view->mail = $mail;
		  $this->view->login = $login;
		}else{
			
		}
	}
	
	public function helperAction(){	
		$this->_helper->layout()->disableLayout();			
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
		  $this->view->online = true;
		  $info = $auth->getStorage()->read();
		  $this->view->info = $info;		  
		}else{
		  $this->view->online = false;
		}
	}
	
	public function logoutAction(){
		$admin = $this->_request->getParam('admin');
		if(!empty($admin)){
			$this->_redirect('/admin');
		}
		$this->_helper->layout()->disableLayout();	
		$this->_helper->viewRenderer->setNoRender(true);
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			$auth->clearIdentity();
			$this->_redirect('/');		
		}
	}
}
?>
