<?php

class ProjectController extends Zend_Controller_Action {


	public function init() {
		$information = new Zend_Session_Namespace('information');	
		if( Lib_Permission::CheckPermission('project') != 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');
		}	

		/*
			if( $_SERVER['REMOTE_ADDR'] != '5.172.239.27' ) die('Trwa aktualizacja modulu Projekty w systemie...');
		*/
	}	
	
	
	public function indexAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$filtrproject = new Zend_Session_Namespace('filtrproject');
		
		$v = $this->_request->getParams();
		$vPost = $this->_request->getPost();
		
		
		if( $information->addProjectSuccess ){
			$this->view->addProjectSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->removeSuccess ){
			$this->view->removeSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->addFiltrCompanySuccess ){
			$this->view->addFiltrCompanySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->addFiltrCompanyRemove ){
			$this->view->addFiltrCompanyRemove = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->addFiltrStatusSuccess ){
			$this->view->addFiltrStatusSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->addFiltrStatusRemove ){
			$this->view->addFiltrStatusRemove = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		
		
		
		
		
		
		
		if( isset($v['filtrLimit']) ){
			$filtrproject->limit = $v['filtrLimit'];
			$this->_helper->redirector('index','project');	
		}
		
		
		if( isset($vPost['filtrCompany']) ){
			if( $vPost['filtrCompany'] != 'all' ){
				$filtrproject->company_id = $vPost['filtrCompany'];
				$information->addFiltrCompanySuccess = true;
			}else{
				//Zend_Session::namespaceUnset('filtrproject');
				unset($filtrproject->company_id);
				$information->addFiltrCompanyRemove = true;
			}
			$this->_helper->redirector('index','project');	
		}
		
		if( isset($vPost['filtrProjectStatus']) ){
			if( $vPost['filtrProjectStatus'] != 'all' ){
				$filtrproject->project_status = $vPost['filtrProjectStatus'];
				$information->addFiltrStatusSuccess = true;
			}else{
				//Zend_Session::namespaceUnset('filtrproject');
				unset($filtrproject->project_status);
				$information->addFiltrStatusRemove = true;
			}
			$this->_helper->redirector('index','project');	
		}
		
		
		
		// filtr nazwa klienta - poczatek
		if( isset($vPost['removeProjectName']) ){
			unset($filtrproject->filtr_project_name);
			$this->_helper->redirector('index','project');	
		}
		if( isset($vPost['filtrProjectName']) ){
			$filtrproject->filtr_project_name = $vPost['filtrProjectName'];
			$information->addFiltrClientNameSuccess = true;
			$this->_helper->redirector('index','project');	
		}		
		if( $filtrproject->filtr_project_name ){
			$this->view->filtr_project_name =  $filtrproject->filtr_project_name;
			$searchProjectName = " AND project_name LIKE '".str_replace("*","%",$filtrproject->filtr_project_name)."'";
		}
		if( !isset($searchProjectName) ){
			$searchProjectName = '';
		}
		// filtr nazwa klienta - poczatek
		// ==========================================================
		
		if( $filtrproject->company_id ){
			$this->view->filtr_company_id =  $filtrproject->company_id;
			$search = 'company_id='.$filtrproject->company_id;
		}
		
		if( $filtrproject->project_status ){
			$this->view->filtr_project_status =  $filtrproject->project_status;
			$status = $filtrproject->project_status - 1;
			if( isset($search) ) $search = $search.' AND project_status='.$status;
			else $search = 'project_status='.$status;
		}
		
		if( isset($search) ) $search = $search.' AND '.Lib_Permission::setPermissionProjectSQL().$searchProjectName;
		else $search = Lib_Permission::setPermissionProjectSQL().$searchProjectName;
		
				
		if( !$filtrproject->limit ){
			$filtrproject->limit = 10;
		}
		
		if( !isset($search) ){
			$search = null;
		}
		
		$this->view->filtrLimit =  $filtrproject->limit;
		$limit = $filtrproject->limit;
		$limitpage = 10;

		$page = $this->_request->getParams();
		if( isset($page['page']) ) $page = $page['page'];
		else $page = 1;
			
		$Project = new Project();	
		$dataCount = count($Project->fetchAll($search , 'project_ins DESC')); 
			$p = ($page-1)*$limit;		
		$data = $Project->fetchAll($search , 'project_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('project');
		$Lib_Page->setAction('index');
		$Lib_Page->setPrefix('page');
		$this->view->str = $Lib_Page->result();
		$this->view->page = ($page-1)*$limit;		
		
		$Company = new Company();
		$data = $Company->fetchAll(Lib_Permission::setPermissionCompanySQL(), " company_name ASC "); 		
		if( count($data) > 0 ) $this->view->dataTableCompany = $data;
		
		//echo Lib_Permission::setPermissionProjectSQL();
	}
	
	
	public function addAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$filtrproject = new Zend_Session_Namespace('filtrproject');
		
		$Project = new Project();
		$Company = new Company();		
		
		$v = $this->_request->getPost();
		
		if( isset($v['admin_addproject']) ){
		
		
			if( Lib_Permission::getPermissionCompany( $v['company_id'] ) != 1 ){
				$information->permissionError = true;
				$this->_helper->redirector('index','index');		
			}
		
			$data = array(
				'company_id' => $v['company_id'],
				'project_name' => $v['project_name'],
			
			/*
				'project_activ_employee' => isset($v['project_activ_employee'])?$v['project_activ_employee']:0,
				'project_activ_settlement' => isset($v['project_activ_settlement'])?$v['project_activ_settlement']:0,
				'project_status_finish' => isset($v['project_status_finish'])?$v['project_status_finish']:0,
				'project_status_calc' => isset($v['project_status_calc'])?$v['project_status_calc']:0,
			*/	
				
				'project_ins' => Lib_Date::datetime(),
				'project_edit' => Lib_Date::datetime()
			);
			$idIns = $Project->insert($data);
			
			$information->addProjectSuccess = true; 
		//	$this->_helper->redirector('index','project');	

			$this->_helper->redirector('edit','project',null,array('id' => $idIns));					
			
		}		
		
		if( $filtrproject->company_id ){
			$this->view->filtr_company_id =  $filtrproject->company_id;
		}
		
		$data = $Company->fetchAll('company_status = 0 AND '.Lib_Permission::setPermissionCompanySQL(), " company_name ASC "); 		
		if( count($data) > 0 ) $this->view->dataTableCompany = $data;

	}
	
	public function editAction() {	
		$this->view->projecteditmenu = 1;
		
		$v = $this->_request->getParams();
		$information = new Zend_Session_Namespace('information');
		
		
		if( Lib_Permission::getPermissionProject( $v['id'] ) != 1 ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}

		$Project = new Project();
		$Company = new Company();		
		
		if( $information->editProjectSuccess ){
			$this->view->editProjectSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		if( isset($v['admin_editproject']) ){		
		
		
			if( $v['project_h'] == null ) $h = -1;
			else $h = $v['project_h'];
			
			if( $v['project_h_add'] == null ) $h_add = -1;
			else $h_add = $v['project_h_add'];
		
			if( $v['project_m2'] == null ) $m2 = -1;
			else $m2 = $v['project_m2'];
			
			if( $v['project_h_clinker'] == null ) $hclinker = -1;
			else $hclinker = $v['project_h_clinker'];
		
			if( $v['project_m2_clinker'] == null ) $m2clinker = -1;
			else $m2clinker = $v['project_m2_clinker'];
		
			$data = array(
				'company_id' => $v['company_id'],
				'project_name' => $v['project_name'],
				
				'project_nr' => isset($v['project_nr'])?$v['project_nr']:'',
				'project_h' => $h,
				'project_h_clinker' => $hclinker,
				'project_h_add' => $h_add,
				'project_m2' => $m2,
				'project_m2_clinker' => $m2clinker,
				'project_employee_name' => isset($v['project_employee_name'])?$v['project_employee_name']:'',
				'project_employee_phone' => isset($v['project_employee_phone'])?$v['project_employee_phone']:'',
				
				'project_employee_manager_name' => isset($v['project_employee_manager_name'])?$v['project_employee_manager_name']:'',
				'project_employee_manager_phone' => isset($v['project_employee_manager_phone'])?$v['project_employee_manager_phone']:'',
				
				'project_employee_cetla_name' => isset($v['project_employee_cetla_name'])?$v['project_employee_cetla_name']:'',
				'project_employee_cetla_phone' => isset($v['project_employee_cetla_phone'])?$v['project_employee_cetla_phone']:'',
				
				'project_activ_employee' => isset($v['project_activ_employee'])?$v['project_activ_employee']:0,
				'project_activ_settlement' => isset($v['project_activ_settlement'])?$v['project_activ_settlement']:0,
				'project_activ_analysis' => isset($v['project_activ_analysis'])?$v['project_activ_analysis']:0,
				
				'project_status_finish' => isset($v['project_status_finish'])?$v['project_status_finish']:0,
				'project_status_calc' => isset($v['project_status_calc'])?$v['project_status_calc']:0,
				
				'project_date_start' => Lib_Date::convertDate($v['project_date_start'],"d-m-Y","Y-m-d"),
				'project_date_stop' => Lib_Date::convertDate($v['project_date_stop'],"d-m-Y","Y-m-d"),
				
				'project_status' => $v['project_status'],
				'project_edit' => Lib_Date::datetime()
			);
			$Project->update( $data , 'project_id='.$v['id'] );
						
			$information->editProjectSuccess = true; 
			$this->_helper->redirector('edit','project',null,array('id' => $v['id']));			
		}		
		
		
		
		$select = $Project->select();
		$select->where('project_id = ?', $v['id']);	
		$row = $Project->fetchRow($select);
		$this->view->projectData = $row;
		
		$data = $Company->fetchAll('(company_status = 0 OR company_id = '.$row->company_id.') AND '.Lib_Permission::setPermissionCompanySQL(), " company_name ASC "); 		
		if( count($data) > 0 ) $this->view->dataTableCompany = $data;
	}
	
	public function editgeoAction() {	
		$this->view->projecteditmenu = 2;
		
		$v = $this->_request->getParams();
		$information = new Zend_Session_Namespace('information');
		
		
		if( Lib_Permission::getPermissionProject( $v['id'] ) != 1 ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}

		$Project = new Project();
		$Company = new Company();		
		
		if( $information->editgeoProjectSuccess ){
			$this->view->editgeoProjectSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		if( isset($v['admin_editproject']) ){		
			
			$data = array(
				'project_geo_lon' => isset($v['project_geo_lon'])?$v['project_geo_lon']:0,
				'project_geo_lat' => isset($v['project_geo_lat'])?$v['project_geo_lat']:0				
			);
			$Project->update( $data , 'project_id='.$v['id'] );

			$information->editgeoProjectSuccess = true; 
			$this->_helper->redirector('editgeo','project',null,array('id' => $v['id']));			
		}		
		
		$select = $Project->select();
		$select->where('project_id = ?', $v['id']);	
		$row = $Project->fetchRow($select);
		$this->view->projectData = $row;
	}
	
	public function editgeoresetAction() {
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$information = new Zend_Session_Namespace('information');	
	
		$Project = new Project();
		$data = array(
			'project_geo_lon' => 0,
			'project_geo_lat' => 0				
		);
		$Project->update( $data , 'project_id='.$v['id'] );

		$information->editgeoProjectSuccess = true; 
		$this->_helper->redirector('editgeo','project',null,array('id' => $v['id']));	
	
	}
	
	public function removeAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		
		if( Lib_Permission::getPermissionProject( $v['id'] ) != 1 ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}
		
		$Project = new Project();
		$where = array(
			'project_id = ?' => $v['id']
		);
		$Project->delete($where);	
		$information->removeSuccess = true; 
		$this->_helper->redirector('index','project');		
	}


}
