<?php

class ValidityController extends Zend_Controller_Action {
	
	// $this->_helper->viewRenderer->setNoRender();



	public function init() {
		$information = new Zend_Session_Namespace('information');	
		if( Lib_Permission::CheckPermission('validity') != 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');	
		$v = $this->_request->getParams();		
		$db = Zend_Registry::get('db');
		
		if( $information->addValiditySuccess ){
			$this->view->addValiditySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		$v = $this->_request->getParams();
		$vPost = $this->_request->getPost();
	
		$ask = "
			SELECT			
				*
			FROM 
				client c
			WHERE 
				c.client_activ_validity_documents = 1
			ORDER BY
				c.client_name
		";	
		$data = $db->query($ask)->fetchAll();

		$this->view->dataTable = $data;	
	}
	
	public function viewAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();		
		$db = Zend_Registry::get('db');
		
		if( $information->addValiditySuccess ){
			$this->view->addValiditySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->editValiditySuccess ){
			$this->view->editValiditySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->removeSuccess ){
			$this->view->removeSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		$Client = new Client();
		$select = $Client->select();
		$select->where('client_id = ?', $v['id']);	
		$select->where('client_activ_validity_documents = ?', 1);	
		$rowClient = $Client->fetchRow($select);
		
		if ( !isset($rowClient->client_id) ) { 
			$this->_helper->redirector('index','validity');	
		}
		$this->view->rowClient = $rowClient;	
		
		$v = $this->_request->getParams();
		$vPost = $this->_request->getPost();
	
		$ask = "
			SELECT			
				*
			FROM 
				validity_document_file c,
				validity_document_type vdt,
				admin a
			WHERE 
				c.validity_document_type_id = vdt.validity_document_type_id
			AND
				a.admin_id = c.admin_id
			AND
				c.client_id = ".$v['id']."
			ORDER BY
				c.validity_document_file_date DESC,
				c.validity_document_file_datev DESC,
				c.validity_document_file_ins DESC,
				c.validity_document_file_id DESC
		";	
	//	var_dump($ask);die();
		$data = $db->query($ask)->fetchAll();

		$this->view->dataTable = $data;	
	}
	
	public function removeAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
				
		$ValidityDocumentFile = new ValidityDocumentFile();
		$select = $ValidityDocumentFile->select();
		$select->where('validity_document_file_id = ?', $v['id']);	
		$row = $ValidityDocumentFile->fetchRow($select);
		
		$where = array(
			'validity_document_file_id = ?' => $v['id']
		);	
		$ValidityDocumentFile->delete($where);	
		
		$information->removeSuccess = true; 
		$this->_helper->redirector('view','validity',null,array('id' => $row->client_id));		
	}
	
	public function addAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$v = $this->_request->getParams();			
		$db = Zend_Registry::get('db');
		
		if( isset($v['id']) ){	
			$Client = new Client();
			$select = $Client->select();
			$select->where('client_id = ?', $v['id']);	
			$select->where('client_activ_validity_documents = ?', 1);	
			$rowClient = $Client->fetchRow($select);
			
			if ( !isset($rowClient->client_id) ) { 
				$this->_helper->redirector('index','validity');	
			}			
		}
		
		if( isset($v['admin_addvalidity']) ){		
			$upload_path = "validitydocuments/";
			$filename_name = $admin->admin_id.'_'.$v['client_id'].'_'.$v['validity_document_type_id'].'_'.hash('sha256',$admin->admin_id.time().rand(0,100000));
			move_uploaded_file($_FILES["validity_document_file_file"]["tmp_name"], $upload_path.$filename_name);
			
			$ValidityDocumentFile = new ValidityDocumentFile();
			$data = array(
				'client_id' => $v['client_id'],
				'validity_document_type_id' => $v['validity_document_type_id'],
				'validity_document_file_date_issue' => Lib_Date::convertDate($v['validity_document_file_date_issue'],"d-m-Y","Y-m-d"),
				'validity_document_file_date' => Lib_Date::convertDate($v['validity_document_file_date'],"d-m-Y","Y-m-d"),
				'validity_document_file_datev' => Lib_Date::convertDate($v['validity_document_file_datev'],"d-m-Y","Y-m-d"),
				'validity_document_file_filename' => $_FILES["validity_document_file_file"]["name"],
				'validity_document_file_filesize' => $_FILES["validity_document_file_file"]["size"],
				'validity_document_file_filetype' => $_FILES["validity_document_file_file"]["type"],
				'validity_document_file_file' => $filename_name,			
				'validity_document_file_note' => $v['validity_document_file_note'],
				'validity_document_file_ins' => Lib_Date::datetime(),
				'admin_id' => $admin->admin_id
			);
			$idIns = $ValidityDocumentFile->insert($data);
			
			$information->addValiditySuccess = true; 
			if( isset($v['id']) ){			
				$this->_helper->redirector('view','validity',null,array('id' => $v['id']));
			}else{			
				$this->_helper->redirector('index','validity');
			}
			
		}
		
		$dataClient = $db->query("SELECT * FROM `client` WHERE client_activ_validity_documents = 1 ORDER BY client_name ASC")->fetchAll();
		$this->view->dataTableClient = $dataClient;
		$this->view->idClient = $v['id'];
	
		$ValidityDocumentType = new ValidityDocumentType();
		$dataValidityDocumentType = $ValidityDocumentType->fetchAll(null, 'validity_document_type_id ASC'); 		
		$this->view->dataTableValidityDocumentType = $dataValidityDocumentType;	
		$this->view->idType = $v['idt'];
	
	}
	
	public function editAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		$v = $this->_request->getParams();			
		$db = Zend_Registry::get('db');

		$ValidityDocumentFile = new ValidityDocumentFile();
		$select = $ValidityDocumentFile->select();
		$select->where('validity_document_file_id = ?', $v['id']);	
		$row = $ValidityDocumentFile->fetchRow($select);
		$this->view->rowValidityDocumentFile = $row;
		
		$Client = new Client();
		$select = $Client->select();
		$select->where('client_id = ?', $row->client_id);	
		$select->where('client_activ_validity_documents = ?', 1);	
		$rowClient = $Client->fetchRow($select);
		$this->view->rowClient = $rowClient;
		
	
		if( isset($v['admin_editvalidity']) ){		
			
			$ValidityDocumentFile = new ValidityDocumentFile();
			
			$data = array(
				'validity_document_type_id' => $v['validity_document_type_id'],
				'validity_document_file_date_issue' => Lib_Date::convertDate($v['validity_document_file_date_issue'],"d-m-Y","Y-m-d"),
				'validity_document_file_date' => Lib_Date::convertDate($v['validity_document_file_date'],"d-m-Y","Y-m-d"),
				'validity_document_file_datev' => Lib_Date::convertDate($v['validity_document_file_datev'],"d-m-Y","Y-m-d"),			
				'validity_document_file_note' => $v['validity_document_file_note']
			);
			$ValidityDocumentFile->update($data, ' validity_document_file_id='.$v['id']);			
			
			if( $_FILES["validity_document_file_file"]["tmp_name"] ){
				$upload_path = "validitydocuments/";
				$filename_name = $admin->admin_id.'_'.$rowClient['client_id'].'_'.$v['validity_document_type_id'].'_'.hash('sha256',$admin->admin_id.time().rand(0,100000));
				move_uploaded_file($_FILES["validity_document_file_file"]["tmp_name"], $upload_path.$filename_name);
				$data = array(
					'validity_document_file_filename' => $_FILES["validity_document_file_file"]["name"],
					'validity_document_file_filesize' => $_FILES["validity_document_file_file"]["size"],
					'validity_document_file_filetype' => $_FILES["validity_document_file_file"]["type"],
					'validity_document_file_file' => $filename_name
				);
				$ValidityDocumentFile->update($data, ' validity_document_file_id='.$v['id']);	
			}
			
			$information->editValiditySuccess = true; 
			$this->_helper->redirector('view','validity',null,array('id' => $rowClient['client_id']));
			
		}
		
		$dataClient = $db->query("SELECT * FROM `client` WHERE client_activ_validity_documents = 1 ORDER BY client_name ASC")->fetchAll();
		$this->view->dataTableClient = $dataClient;
		$this->view->idClient = $v['id'];
	
		$ValidityDocumentType = new ValidityDocumentType();
		$dataValidityDocumentType = $ValidityDocumentType->fetchAll(null, 'validity_document_type_id ASC'); 		
		$this->view->dataTableValidityDocumentType = $dataValidityDocumentType;	
		$this->view->idType = $v['idt'];
	
	}
	
	public function openAction(){
		$admin = new Zend_Session_Namespace('admin');
		$v = $this->_request->getParams();
		$information = new Zend_Session_Namespace('information');
		
		$ValidityDocumentFile = new ValidityDocumentFile();
		$select = $ValidityDocumentFile->select();
		$select->where('validity_document_file_id = ?', $v['id']);	
		$row = $ValidityDocumentFile->fetchRow($select);
		
		if ( isset($row->validity_document_file_id) ) { 			
			$countVisit = $row['validity_document_file_views']+1;
			$data = array(
				'validity_document_file_views' => $countVisit
			);
			$ValidityDocumentFile->update( $data , 'validity_document_file_id='.$v['id'] );
		
			$file = 'validitydocuments/'.$row->validity_document_file_file;
			header('Content-Type: '.$row->validity_document_file_filetype);
			header('Content-Disposition: inline; filename="'.$row->validity_document_file_filename.'"');
			header('Content-Length: '.filesize($file));	
	
			ob_clean();
			flush();
			@readfile($file);
			exit;
		}else{
			$this->_helper->redirector('index','validity');			
		}
		
        $this->_helper->viewRenderer->setNoRender();
	}
	
}
