<?php

/**
 * DocumentsController - app core
 * 
 * @author Lukáš Havlíček
 * @version 1.0
 */

class ConfirmationsController extends Zend_Controller_Action 
{ 	
	public function init()
	{
		$this->view->topmenu = 7;
	}
	
	public function indexAction()
	{
		$this->_redirect('/confirmations/versions', array('code', 301));
	}
	
	public function versionsAction() 
    {
//                        $table = new VersionConfirm();
//                        for ($i = 1; $i<100; $i++)
//                        {
//                        	$table->insert(array(
//                        		'id_user' => 2,
//                        		'id_version' => 2,
//                        		'version_confirm_added' => new Zend_Db_Expr('NOW()')
//                        		));
//                        }
                        
                        
                        
    	$this->view->leftmenu = 1;
    	
    	$user = Zend_Registry::get('user');
		$table = new VersionConfirm();
		$select = $table->getConfirmations($user->id_user, false);
		$select->order('version_confirm_added DESC');
		
		$page = (int) $this->_getParam('page');
    	if ($page < 1  || !$page) $page = 1;
    	
    	$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
    	$paginator->setCurrentPageNumber($page);
    	$paginator->setItemCountPerPage(15);
    	$paginator->setPageRange(10);
    	
    	$this->view->data = $paginator;
    	$this->view->page = $page;
    	
    }
    
    public function revisionsAction() 
    {
    	$this->view->leftmenu = 2;
    	
    	$user = Zend_Registry::get('user');
		$table = new RevisionConfirm();
		$select = $table->getConfirmations($user->id_user, false);
		$select->order('revision_confirm_added DESC');
		
		$page = (int) $this->_getParam('page');
    	if ($page < 1  || !$page) $page = 1;
    	
    	$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
    	$paginator->setCurrentPageNumber($page);
    	$paginator->setItemCountPerPage(15);
    	$paginator->setPageRange(10);
    	
    	$this->view->data = $paginator;
    	$this->view->page = $page;
    	
    }
    
    public function doneVersionsAction() 
    {
    	$this->view->leftmenu = 3;
    	
    	$user = Zend_Registry::get('user');
		$table = new VersionConfirm();
		$select = $table->getConfirmations($user->id_user, true);
		$select->order('version_confirm_confirmed DESC');
		
		$page = (int) $this->_getParam('page');
    	if ($page < 1  || !$page) $page = 1;
    	
    	$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
    	$paginator->setCurrentPageNumber($page);
    	$paginator->setItemCountPerPage(15);
    	$paginator->setPageRange(10);
    	
    	$this->view->data = $paginator;
    	$this->view->page = $page;
    }
    
    public function doneRevisionsAction() 
    {
    	$this->view->leftmenu = 4;
    	
    	$user = Zend_Registry::get('user');
		$table = new RevisionConfirm();
		$select = $table->getConfirmations($user->id_user, true);
		$select->order('revision_confirm_confirmed DESC');
		
		$page = (int) $this->_getParam('page');
    	if ($page < 1  || !$page) $page = 1;
    	
    	$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
    	$paginator->setCurrentPageNumber($page);
    	$paginator->setItemCountPerPage(15);
    	$paginator->setPageRange(10);
    	
    	$this->view->data = $paginator;
    	$this->view->page = $page;
    }
	
	
    public function versionConfirmAction() 
    {
    	$user = Zend_Registry::get('user');
		$id = $this->getRequest()->getParam('id', 0);
		if (!$id) $this->_redirect('/confirmations');
    	
		$table = new VersionConfirm();
		$row = $table->fetchRow($table->select()->where('id_version_confirm = ?', $id));
		
		if (!$row) $this->_redirect('/confirmations');
		
		$ver = new Versions();
		$version = $table->fetchRow($ver->select()->where('id_version = ?', $row->id_version));
		
		if (!$version) $this->_redirect('/confirmations');
		
		$rights = new Rights();
		if (!$rights->isAllowed($user->id_user, $version->id_document, 'read', $user->super_admin)) {
			$this->_redirect('/confirmations/versions');
		}
		
		
		if ($this->getRequest()->isPost()) {
			
			$data['version_confirm_confirmed'] = new Zend_Db_Expr('NOW()');
			$where = $table->getAdapter()->quoteInto('id_version_confirm  = ?', $id);
			if ($table->update($data, $where)) {
				$this->_redirect('/confirmations/index/msg/1');	
			}
			
			$this->_redirect('/confirmations/index/msg/2');
			
			
			
		} else {
			
			$select = $table->select()->setIntegrityCheck(false)
	   				   	    ->from(array('cf' => 'iso_version_confirm'))
        			   	    ->joinLeft(array('v' => 'iso_versions'), 'v.id_version = cf.id_version', array('v.id_version', 'ver_number'))
        			        ->joinLeft(array('d' => 'iso_documents'), 'd.id_document = v.id_document', array('d.id_document', 'd.doc_name'))
	   				        ->where('cf.id_version_confirm  = ?', $id)
	   				        ->where('cf.version_confirm_confirmed IS NULL');
	   				       
	   		$this->view->item = $table->fetchRow($select);
		}
    }
    
    public function revisionConfirmAction() 
    {
    	$user = Zend_Registry::get('user');
		$id = $this->getRequest()->getParam('id', 0);
		if (!$id) $this->_redirect('/confirmations');
    	
		$table = new RevisionConfirm();
		$row = $table->fetchRow($table->select()->where('id_revision_confirm = ?', $id));
		
		if (!$row) $this->_redirect('/confirmations');
		
		
		$rev = new Revisions();
		$revision = $table->fetchRow($rev->select()->where('id_revision = ?', $row->id_revision));
		
		if (!$revision) $this->_redirect('/confirmations');
		
		$ver = new Versions();
		$version = $table->fetchRow($ver->select()->where('id_version = ?', $revision->id_version));
		
		if (!$version) $this->_redirect('/confirmations');
		
		$rights = new Rights();
		if (!$rights->isAllowed($user->id_user, $version->id_document, 'read', $user->super_admin)) {
			$this->_redirect('/confirmations');
		}
		
		
		if ($this->getRequest()->isPost()) {
			
			$data['revision_confirm_confirmed'] = new Zend_Db_Expr('NOW()');
			$where = $table->getAdapter()->quoteInto('id_revision_confirm  = ?', $id);
			if ($table->update($data, $where)) {
				$this->_redirect('/confirmations/revisions/msg/1');	
			}
			
			$this->_redirect('/confirmations/revisions/msg/2');
			
			
			
		} else {
			
			$select = $table->select()->setIntegrityCheck(false)
	   				   	    ->from(array('cf' => 'iso_revision_confirm'))
        			   	    ->joinLeft(array('r' => 'iso_revisions'), 'r.id_revision = cf.id_revision', array('r.id_revision', 'r.rev_number'))
        			   	    ->joinLeft(array('v' => 'iso_versions'), 'v.id_version = r.id_version', array('v.id_version', 'v.ver_number'))
        			        ->joinLeft(array('d' => 'iso_documents'), 'd.id_document = v.id_document', array('d.id_document', 'd.doc_name'))
	   				        ->where('cf.id_revision_confirm  = ?', $id)
	   				        ->where('cf.revision_confirm_confirmed IS NULL');
	   				       
	   		$this->view->item = $table->fetchRow($select);
		}
    }	
}
