<?php
/**
 * ISO project
 * 
 * @author  Lukáš Havlíček
 * @version 1.0
 * 
 * Model for DB table documents versions.
 */
class History extends Zend_Db_Table_Abstract 
{
	protected $_name = 'iso_history';
	protected $_primary = 'id_history';
	
	public function getHistory($idUser, $isAdmin, $count = 0)
	{
		$rights = new Rights();
		$rightsA = $rights->getCategoryRights($idUser, $isAdmin);
		
		$rightsInc = array();
		foreach ($rightsA as $k => $v) {
			
			
			if ((int)$v['read'] == 1) {
				$rightsInc[] = (int) $k;	
			}
		}
		
		$table = new Documents();
		
		
		if (!count($rightsInc)) return false;
		$rows = $table->fetchAll(
							$table->select()
							      ->where('doc_active = 1')
								  ->where('id_category IN (' . implode(',', $rightsInc) . ')'));
		
						
		$docInc = array();
		foreach ($rows as $i) {
			$docInc[] = $i->id_document;
		}
		
		$select = $this->select()->setIntegrityCheck(false)
					   ->from(array('h' => 'iso_history'));

	 	if (count($docInc)) {
	 		$select->where('h.id_document IN (' . implode(',', $docInc) .')');
	 	}
	 	
		$select->joinLeft(array('d' => 'iso_documents'), 'd.id_document = h.id_document', 'd.doc_name')
			   ->joinLeft(array('u' => 'iso_users'), 'u.id_user = h.id_user', array('u.user_firstname', 'u.user_surname'));
			
		if ($count) {
			$select->limit((int) $count);
		}
		
		$select->order('history_added DESC');
		
		return $select;
	}
}
?>