<?php
/**
 * ISO project
 * 
 * @author  Lukáš Havlíček
 * @version 1.0
 * 
 * Model for DB table messages.
 */
class RevisionConfirm extends Zend_Db_Table_Abstract 
{
	protected $_name = 'iso_revision_confirm';
	protected $_primary = 'id_revision_confirm';
	
	public function getConfirmations($idUser, $confirmed = true, $count = 0)
	{
    	$table = new Users();
    	
    	$row = $table->fetchRow($table->select()->where('id_user = ?', $idUser)->where('user_active = 1'));
    	
    	if (!$row) return array();
    	
    	$rights = new Rights();
	    $select = $rights->select()->setIntegrityCheck(false)
	    				 ->from(array('r' => 'iso_rights_group_category'))
	    				 ->joinLeft(array('g' => 'iso_groups'), 'g.id_group = r.id_group', 'g.id_group')
	    				 ->joinLeft(array('ug' => 'iso_user_group'), 'ug.id_group = g.id_group', 'ug.id_group')
	    				 ->joinLeft(array('u' => 'iso_users'), 'ug.id_user = u.id_user', 'u.id_user')
	    				 ->joinLeft(array('c' => 'iso_categoryies'), 'c.id_category = r.id_category', 'c.id_category')
	    				 ->joinLeft(array('d' => 'iso_documents'), 'd.id_category = r.id_category', 'd.id_document')
	    				 ->where('r.rights_active = 1')
	    				 ->where('ug.active = 1')
	    				 ->where('u.user_active = 1')
	    				 ->where('d.doc_active = 1')
	    				 ->where('c.category_active = 1')
	    				 ->where('r.read = 1');
	    				    
	    $rows = $rights->fetchAll($select);
	    $docs = array();
	    foreach ($rows as $i) {
	    	if (!in_array((int)$i->id_document, $docs)) 
	    		$docs[] = (int) $i->id_document;
	    }
	    
	    if (!count($docs)) return array();

	    $vers = new Versions(); 
	   	$versions = $vers->fetchAll('id_document IN (' . implode(',', $docs) .')');
	   	
	   	if (!$versions->count()) return array();
	   	
	   	$versionsIDS = array();
	   	foreach ($versions as $i) {
	   		$versionsIDS[] = $i->id_version;
	  	}
	  	
	  	
	    if (!count($versionsIDS)) return array();

	    
	    $revs = new Revisions(); 
	   	$revisions = $revs->fetchAll('id_version IN (' . implode(',', $versionsIDS) .')');
	   	
	   	if (!$revisions->count()) return array();
	   	
	   	$revisionsIDS = array();
	   	foreach ($revisions as $i) {
	   		$revisionsIDS[] = $i->id_revision;
	  	}
	  	
	   	$select = $this->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 IN (' . implode(',', $revisionsIDS) . ')')
	   				   ->where('cf.id_user = ?', $idUser);
	   				   
	   	if ($confirmed) {
	   		$select->where('cf.revision_confirm_confirmed IS NOT NULL');
	   	} else {
	   		$select->where('cf.revision_confirm_confirmed IS NULL');
	   	}
	   				   
	   	if ($count) {
	   		$select->limit($count);
	   	}
	   	
	   	return $select;
	}
}
?>