<?php

class ClientController extends Zend_Controller_Action {

	public function init() {
		$information = new Zend_Session_Namespace('information');	
		if( Lib_Permission::CheckPermission('client') != 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');
		}			
	}	
	
	
	public function indexAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$filtrclient = new Zend_Session_Namespace('filtrclient');
		
		$v = $this->_request->getParams();
		$vPost = $this->_request->getPost();
		
		
		if( $information->addCompanySuccess ){
			$this->view->addCompanySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->removeSuccess ){
			$this->view->removeSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		// filtr nazwa klienta - poczatek
		if( isset($vPost['removeClientName']) ){
			unset($filtrclient->client_name);
			$this->_helper->redirector('index','client');	
		}
		if( isset($vPost['filtrClientName']) ){
			$filtrclient->client_name = $vPost['filtrClientName'];
			$information->addFiltrClientNameSuccess = true;
			$this->_helper->redirector('index','client');	
		}		
		if( $filtrclient->client_name ){
			$this->view->filtr_name_client =  $filtrclient->client_name;
			$searchClientName = "client_name LIKE '".str_replace("*","%",$filtrclient->client_name)."'";
		}
		if( !isset($searchClientName) ){
			$searchClientName = null;
		}
		// filtr nazwa klienta - poczatek
		// ==========================================================
		
		$limit = 10;
		$limitpage = 10;

		$page = $this->_request->getParams();
		if( isset($page['page']) ) $page = $page['page'];
		else $page = 1;
			
		$Client = new Client();	
		$dataCount = count($Client->fetchAll($searchClientName , 'client_name ASC')); 
			$p = ($page-1)*$limit;		
		$data = $Client->fetchAll($searchClientName , 'client_name ASC', $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('client');
		$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');
		
		$Client = new Client();		
		
		$v = $this->_request->getPost();
		
		if( isset($v['admin_addclient']) ){
		
			$data = array(
				'client_name' => $v['client_name'],				
				'client_activ_product' => isset($v['client_activ_product'])?$v['client_activ_product']:0,				
				'client_activ_main_project' => isset($v['client_activ_main_project'])?$v['client_activ_main_project']:0,				
				'client_activ_main_order' => isset($v['client_activ_main_order'])?$v['client_activ_main_order']:0,				
				'client_activ_prepared_documents' => isset($v['client_activ_prepared_documents'])?$v['client_activ_prepared_documents']:0,
				'client_activ_validity_documents' => isset($v['client_activ_validity_documents'])?$v['client_activ_validity_documents']:0
			);
			$idIns = $Client->insert($data);
			
			//var_dump($data);die();
			
			$information->addCompanySuccess = true; 
			$this->_helper->redirector('index','client');		
			
		}		

	}
	
	public function editAction() {	
		$v = $this->_request->getParams();
		$information = new Zend_Session_Namespace('information');
		
		$Client = new Client();		
		
		if( $information->editclientSuccess ){
			$this->view->editclientSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		if( isset($v['admin_editclient']) ){		
			$data = array(
				'client_name' => $v['client_name'],				
				'client_activ_product' => isset($v['client_activ_product'])?$v['client_activ_product']:0,				
				'client_activ_main_project' => isset($v['client_activ_main_project'])?$v['client_activ_main_project']:0,				
				'client_activ_main_order' => isset($v['client_activ_main_order'])?$v['client_activ_main_order']:0,				
				'client_activ_prepared_documents' => isset($v['client_activ_prepared_documents'])?$v['client_activ_prepared_documents']:0,
				'client_activ_validity_documents' => isset($v['client_activ_validity_documents'])?$v['client_activ_validity_documents']:0
			);
			$Client->update( $data , 'client_id='.$v['id'] );
						
			$information->editclientSuccess = true; 
			$this->_helper->redirector('edit','client',null,array('id' => $v['id']));			
		}		
		
		
		
		$select = $Client->select();
		$select->where('client_id = ?', $v['id']);	
		$row = $Client->fetchRow($select);
		$this->view->clientData = $row;
	}
	
	public function removeAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		
		$Client = new Client();
		$where = array(
			'client_id = ?' => $v['id']
		);
		$Client->delete($where);	
		$information->removeSuccess = true; 
		$this->_helper->redirector('index','client');		
	}
	
	
	public function docAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$information = new Zend_Session_Namespace('information');
		$filtrdocuments = new Zend_Session_Namespace('filtrdocuments');
		
		$Client = new Client();	
		
		$select = $Client->select();
		$select->where('client_id = ?', $v['id']);	
		$rowClient = $Client->fetchRow($select);

		$filtrdocuments->documents_client = $rowClient->client_name;
		$this->_helper->redirector('index','documents');
	}
}
