<?php

class UsersController extends Zend_Controller_Action {

	public function init() {
		$information = new Zend_Session_Namespace('information');	
		if( Lib_Permission::CheckPermission('users') != true ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}
		
		$admin = new Zend_Session_Namespace('admin');
		if( $admin->admin_auth != true ){
			$this->_helper->redirector('index','login');
		}			
		
		//echo '<div class="row text-center" style="margin:10px;color:red;font-size:16px;border-bottom:3px solid red;"><b>UWAGA!</b> Trwaja prace w module Uzytkownicy.</div>';
	}	
	
	
	public function indexAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$filtruser = new Zend_Session_Namespace('filtruser');
		
		$v = $this->_request->getParams();
		
		
		if( $information->addUserSuccess ){
			$this->view->addUserSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->removeSuccess ){
			$this->view->removeSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( isset($v['filtrLimit']) ){
			$filtruser->limit = $v['filtrLimit'];
			$this->_helper->redirector('index','users');	
		}
		
		if( !$filtruser->limit ){
			$filtruser->limit = 10;
		}
		
		if( !isset($search) ){
			$search = null;
		}
		
		$this->view->filtrLimit =  $filtruser->limit;
		$limit = $filtruser->limit;
		$limitpage = 10;

		$page = $this->_request->getParams();
		if( isset($page['page']) ) $page = $page['page'];
		else $page = 1;
			
		$Admin = new Admin();	
		$dataCount = count($Admin->fetchAll(null , 'admin_ins DESC')); 
			$p = ($page-1)*$limit;		
		$data = $Admin->fetchAll(null , 'admin_ins DESC', $limit,$p); 		
		$this->view->dataTable = $data;		
	
		$Lib_Page = new Lib_Page();
		$Lib_Page->setLimit( $limit );
		$Lib_Page->setLimitPage( $limitpage );
		$Lib_Page->setMax( $dataCount );
		$Lib_Page->setActualId( $page );
		$Lib_Page->setController('users');
		$Lib_Page->setAction('index');
		$Lib_Page->setPrefix('page');
		$this->view->str = $Lib_Page->result();
		$this->view->page = ($page-1)*$limit;			
		
	}
	
	
	public function addAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		
		$Admin = new Admin();		
		$AdminPermission = new AdminPermission();
		$AdminPermissionCompany = new AdminPermissionCompany();
		$AdminPermissionProject = new AdminPermissionProject();	
		$AdminCalcPermissionCompany = new AdminCalcPermissionCompany();
		$AdminCalcPermissionProject = new AdminCalcPermissionProject();	
		
		$v = $this->_request->getPost();
		
		if( isset($v['admin_adduser']) ){
		
			$data = array(
				'admin_login' => $v['admin_login'],
				'admin_pass' => hash('sha256',$v['admin_pass']),
				'admin_firstname' => $v['admin_firstname'],
				'admin_secondname' => $v['admin_secondname'],
				'admin_email' => $v['admin_email'],
				'admin_ins' => Lib_Date::datetime(),
				'admin_edit' => Lib_Date::datetime(),
				'admin_changepass' => Lib_Date::datetime(),
				'admin_permission_all' => $v['admin_permission_all'],
				'admin_permission_company_all' => $v['admin_permission_company_all'],
				'admin_calc_permission_company_all' => $v['admin_calc_permission_company_all']
			);
			$idIns = $Admin->insert($data);
			
			if( $v['admin_permission_all'] == 0 ){
				foreach( $v['permission_id'] as $key => $value ){
					$data = array(
						'admin_id' => $idIns,
						'permission_id' => $value
					);
					$AdminPermission->insert($data);
				}
			}
			
			
			
			
			
			if( $v['admin_permission_company_all'] == 0 ){
				foreach( $v['permissionCompany_id'] as $key => $value ){
					$data = array(
						'admin_id' => $idIns,
						'company_id' => $value,
						'admin_permission_project_all' => $v['admin_permission_project_all'][$value]
					);
					$idInsPC = $AdminPermissionCompany->insert($data);
					
					if( $v['admin_permission_project_all'][$value] == 0 ){
						foreach( $v['permissionProject_id'][$value] as $keyp => $valuep ){						
							$data = array(
								'admin_permission_company_id' => $idInsPC,
								'admin_id' => $idIns,
								'project_id' => $valuep
							);
							$AdminPermissionProject->insert($data);					
						}
					}
					
				}
			}
			
			
			
			
			
			if( $v['admin_calc_permission_company_all'] == 0 ){
				foreach( $v['permissionCompanyCalc_id'] as $key => $value ){
					$data = array(
						'admin_id' => $idIns,
						'company_id' => $value,
						'admin_calc_permission_project_all' => $v['admin_calc_permission_project_all'][$value]
					);
					$idInsPC = $AdminCalcPermissionCompany->insert($data);
					
					if( $v['admin_calc_permission_project_all'][$value] == 0 ){
						foreach( $v['permissionProjectCalc_id'][$value] as $keyp => $valuep ){						
							$data = array(
								'admin_calc_permission_company_id' => $idInsPC,
								'admin_id' => $idIns,
								'project_id' => $valuep
							);
							$AdminCalcPermissionProject->insert($data);					
						}
					}
					
				}
			}
			
			
			
			
			
			$information->addUserSuccess = true; 
			$this->_helper->redirector('index','users');		
			
		}		
		
		$Permission = new Permission();
		$data = $Permission->fetchAll(null, 'permission_sort ASC'); 		
		$this->view->dataTablePermission = $data;
		
		$Company = new Company();
		$data = $Company->fetchAll(null , 'company_name ASC'); 		
		$this->view->dataTableCompany = $data;

		$dataProject[] = array();		
		$Project = new Project();
		foreach($data as $key => $value){
			$dataProject[$value['company_id']] = $Project->fetchAll('company_id = '.$value['company_id'], " project_name ASC ");		
		}
		$this->view->dataTableProject = $dataProject;

		$dataProjectCalc[] = array();		
		$Project = new Project();
		foreach($data as $key => $value){
			$dataProjectCalc[$value['company_id']] = $Project->fetchAll('project_activ_settlement = 1 AND company_id = '.$value['company_id'], " project_name ASC ");		
		}
		$this->view->dataTableProjectCalc = $dataProjectCalc;
		
	}
	
	public function editAction() {	
		$v = $this->_request->getParams();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		
		$Admin = new Admin();		
		$AdminPermission = new AdminPermission();	
		$AdminPermissionCompany = new AdminPermissionCompany();
		$AdminPermissionProject = new AdminPermissionProject();	
		$AdminCalcPermissionCompany = new AdminCalcPermissionCompany();
		$AdminCalcPermissionProject = new AdminCalcPermissionProject();	
		
		
		
		if( $information->editUserChangeStatus ){
			$this->view->editUserChangeStatus = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->editUserChangePass ){
			$this->view->editUserChangePass = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->editUserSuccess ){
			$this->view->editUserSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		
		if( isset($v['admin_changestatususer']) ){
			$data = array(
				'admin_lock' => $v['admin_lock'],
				'admin_edit' => Lib_Date::datetime()
			);
			$Admin->update( $data , 'admin_id='.$v['id'].' AND admin_noedit = 0' );
			$information->editUserChangeStatus = true; 
			$this->_helper->redirector('edit','users',null,array('id' => $v['id']));
		}
		
		
		
		if( isset($v['admin_resetpassuser']) ){
			$data = array(
				'admin_pass' => hash('sha256',$v['admin_pass']),
				'admin_edit' => Lib_Date::datetime(),
				'admin_changepass' => Lib_Date::datetime()
			);
			$Admin->update( $data , 'admin_id='.$v['id'].' AND admin_noedit = 0' );
			$information->editUserChangePass = true; 
			$this->_helper->redirector('edit','users',null,array('id' => $v['id']));
		}
		
		
		if( isset($v['admin_edituser']) ){		
			$data = array(
				'admin_firstname' => $v['admin_firstname'],
				'admin_secondname' => $v['admin_secondname'],
				'admin_email' => $v['admin_email'],
				'admin_edit' => Lib_Date::datetime(),
				'admin_permission_all' => $v['admin_permission_all'],
				'admin_permission_company_all' => $v['admin_permission_company_all'],
				'admin_calc_permission_company_all' => $v['admin_calc_permission_company_all']
			);
			$Admin->update( $data , 'admin_id='.$v['id'].' AND admin_noedit = 0' );
			
			
			$where = array(
				'admin_id = ?' => $v['id']
			);
			$AdminPermission->delete($where);	
			$AdminPermissionCompany->delete($where);	
			$AdminCalcPermissionCompany->delete($where);	
			
			if( $v['admin_permission_all'] == 0 ){
				foreach( $v['permission_id'] as $key => $value ){
					$data = array(
						'admin_id' => $v['id'],
						'permission_id' => $value
					);
					$AdminPermission->insert($data);
				}
			}
			
			
			
			
			
			
			if( $v['admin_permission_company_all'] == 0 ){
				foreach( $v['permissionCompany_id'] as $key => $value ){
					$data = array(
						'admin_id' => $v['id'],
						'company_id' => $value,
						'admin_permission_project_all' => $v['admin_permission_project_all'][$value]
					);
					$idInsPC = $AdminPermissionCompany->insert($data);
					
					if( $v['admin_permission_project_all'][$value] == 0 ){
						foreach( $v['permissionProject_id'][$value] as $keyp => $valuep ){						
							$data = array(
								'admin_permission_company_id' => $idInsPC,
								'admin_id' => $v['id'],
								'project_id' => $valuep
							);
							$AdminPermissionProject->insert($data);					
						}
					}
					
				}
			}
			
			
			
			
			
			if( $v['admin_calc_permission_company_all'] == 0 ){
				foreach( $v['permissionCompanyCalc_id'] as $key => $value ){
					$data = array(
						'admin_id' => $v['id'],
						'company_id' => $value,
						'admin_calc_permission_project_all' => $v['admin_calc_permission_project_all'][$value]
					);
					$idInsPC = $AdminCalcPermissionCompany->insert($data);
					
					if( $v['admin_calc_permission_project_all'][$value] == 0 ){
						foreach( $v['permissionProjectCalc_id'][$value] as $keyp => $valuep ){						
							$data = array(
								'admin_calc_permission_company_id' => $idInsPC,
								'admin_id' => $v['id'],
								'project_id' => $valuep
							);
							$AdminCalcPermissionProject->insert($data);					
						}
					}
					
				}
			}
			
			
			
			
			
			
			$information->editUserSuccess = true; 
			$this->_helper->redirector('edit','users',null,array('id' => $v['id']));			
		}		
		
		
		
		$select = $Admin->select();
		$select->where('admin_id = ?', $v['id']);	
		$row = $Admin->fetchRow($select);
		$this->view->userData = $row;
				
		$allAdminPermission = $AdminPermission->fetchAll('admin_id = '.$v['id']);
		
		
		foreach( $allAdminPermission as $key => $value ){
			$dataAdminPermission[$value['permission_id']] = 1;
		}
		
		if( isset($dataAdminPermission) ){$this->view->dataAdminPermission = $dataAdminPermission;}
		
		$Permission = new Permission();
		$data = $Permission->fetchAll(null, 'permission_sort ASC'); 	
		$this->view->dataTablePermission = $data;
		
		$Company = new Company();
		$data = $Company->fetchAll(null , 'company_name ASC'); 		
		$this->view->dataTableCompany = $data;

		$dataProject[] = array();		
		$Project = new Project();
		foreach($data as $key => $value){
			$dataProject[$value['company_id']] = $Project->fetchAll('company_id = '.$value['company_id'], " project_name ASC ");		
		}
		$this->view->dataTableProject = $dataProject;

		$dataProjectCalc[] = array();		
		$Project = new Project();
		foreach($data as $key => $value){
			$dataProjectCalc[$value['company_id']] = $Project->fetchAll('project_activ_settlement = 1 AND company_id = '.$value['company_id'], " project_name ASC ");		
		}
		$this->view->dataTableProjectCalc = $dataProjectCalc;

	}
	
	public function removeAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		
		$Admin = new Admin();
		$where = array(
			'admin_id = ?' => $v['id'],
			'admin_noedit = ?' => 0
		);
		$Admin->delete($where);	
		$information->removeSuccess = true; 
		$this->_helper->redirector('index','users');		
	}

	
	
	public function historyAction() {	
		$admin = new Zend_Session_Namespace('admin');
		
		$v = $this->_request->getParams();
		
		$limit = 10;
		$limitpage = 10;

		$page = $this->_request->getParams();
		if( isset($page['page']) ) $page = $page['page'];
		else $page = 1;
			
		$HistoryLog = new HistoryLog();	
		$dataCount = count($HistoryLog->fetchAll('admin_id = '.$v['id'] , 'history_log_date DESC')); 
			$p = ($page-1)*$limit;		
		$data = $HistoryLog->fetchAll('admin_id = '.$v['id'] , 'history_log_date DESC', $limit,$p); 		
		$this->view->dataTable = $data;		
	
		$Lib_Page = new Lib_Page();
		$Lib_Page->setLimit( $limit );
		$Lib_Page->setLimitPage( $limitpage );
		$Lib_Page->setMax( $dataCount );
		$Lib_Page->setActualId( $page );
		$Lib_Page->setController('users');
		$Lib_Page->setAction('history');
		$Lib_Page->setPrefix('page');
		$Lib_Page->setGet('/id/'.$v['id']);
		$this->view->str = $Lib_Page->result();
		$this->view->page = ($page-1)*$limit;
		
		
		$Admin = new Admin();
		$select = $Admin->select();
		$select->where('admin_id = ?', $v['id']);	
		$row = $Admin->fetchRow($select);
		$this->view->userData = $row;
		
	}
	
	public function checkloginAction() {
		header("Content-Type:text/plain");
		$this->_helper->viewRenderer->setNoRender();
		$v = $this->_request->getParams();
		$valid = 0;
		// $valid = 0 - nie istnieje w bazie
		// $valid = 1 - istnieje w bazie
		
		$Admin = new Admin();	
		$select = $Admin->select();		
		$select->where('admin_login = ?', $v['admin_login']);					
		$row = $Admin->fetchRow($select);
		unset($Admin);		

		if( isset($row) ) $valid = 1;
		else $valid = 0;
			
		echo $valid;
	}
	
}
