<?php

class LoginController extends Zend_Controller_Action {

	public function init() {}	
	
	
	public function indexAction() {	

		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		$filtremployee = new Zend_Session_Namespace('filtremployee');
		$filtrproject = new Zend_Session_Namespace('filtremployee');
		$filtrdocuments = new Zend_Session_Namespace('filtrdocuments');
		$filtrcalculation = new Zend_Session_Namespace('filtrcalculation');
		if( $admin->admin_auth == true ){
			$this->_helper->redirector('index','index');
		}		
		
		$v = $this->_request->getParams();
		
		if( $information->changePasswordSuccess ){
			$this->view->changePasswordSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->successLogout ){
			$this->view->successLogout = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->errorLogin ){
			$this->view->errorLogin = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->errorLoginBlock ){
			$this->view->errorLoginBlock = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( isset($v['admin_login']) && isset($v['admin_pass']) ){
			// logowanie
			$Admin = new Admin();
			$select = $Admin->select();		
			$select->where('admin_login = ?', $v['admin_login']);	
			$select->where('admin_pass = ?', hash('sha256',$v['admin_pass']));			
			$select->where('admin_lock = ?', 0);			
			$row = $Admin->fetchRow($select);
			
			if( isset($row) ){
				// zalogowany
				if( $row['admin_lock'] == 1 ){
					$information->errorLoginBlock = true;
				}else{
				
					$HistoryLog = new HistoryLog();
					$data = array(
						'admin_id'      => $row['admin_id'],
						'history_log_date' => date("Y-m-d H:i:s"),
						'history_log_ip'      => Zend_Registry::get('users_ip'),
						'history_log_host'      => Zend_Registry::get('users_host')
					);
					$HistoryLog->insert($data);	
					
					$admin->admin_id = $row['admin_id'];
					$admin->admin_auth = true;
					$information->successLogin = true; 
					
					$filtrdocuments->filtrGot = 1;
					
					$filtremployee->employee_status = 1;
					$filtremployee->filtr_nh = 1;
					$filtrproject->limit = 100;
					$filtrproject->project_status = 1;
					
					$filtrcalculation->limit = 100;
					$filtrcalculation->project_status = 1;
				}
			}else{
				// blad logowania				
				$information->errorLogin = true; 
			}
			
			$this->_helper->redirector('index','index');			
		}
	
	}
	
	public function logoutAction() {
		Zend_Session::namespaceUnset('admin');	
		$information = new Zend_Session_Namespace('information');	
		$information->successLogout = true; 
		$this->_helper->redirector('index','index');	
	}

}

// $this->_helper->viewRenderer->setNoRender();
