<?php
class Rights extends Zend_Db_Table_Abstract {
	protected $_name = 'iso_rights_group_category';
	protected $_primary = 'id_rights';
	
	public function isAllowed($idUser, $idDocument, $action, $isAdmin = false)
	{
		if ($isAdmin) return true;
		
		$array = $this->getDocRights($idUser, $idDocument, $isAdmin);
		
		
		if (in_array($action, array_keys($array))) {

			return (bool) $array[$action];
			
		}
		
		return false;
	}
	
	public function getDocRights($idUser, $idDocument, $isAdmin = false)
	{
		if ($isAdmin) return 
			array('read' => 1, 'add' => 1, 'edit' => 1, 'del' => 1);
		
		$select = $this->select()->setIntegrityCheck(false)
					   ->from(array('r' => 'iso_rights_group_category'))
					   ->joinLeft(array('ug' => 'iso_user_group'), 'ug.id_group = r.id_group', 'ug.id_group')
					   ->joinLeft(array('g' => 'iso_groups'), 'g.id_group = r.id_group', 'g.id_group')
					   ->joinLeft(array('u' => 'iso_users'), 'u.id_user = ug.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 = c.id_category', 'd.id_category')
					   ->where('u.id_user = ?', $idUser)
					   ->where('d.id_document = ?', $idDocument)
					   ->where('r.rights_active = 1')
					   ->where('ug.active = 1')
					   ->where('u.user_active = 1')
					   ->where('g.group_active = 1')
					   ->where('c.category_active = 1')
					   ->where('d.doc_active = 1');
		
		$row = $this->fetchRow($select);
		
		if (!$row) return false;
		
		
		return $row->toArray();
	}
	
	public function getDocuments($idUser, $categoryies, $isAdmin = false)
	{
		if (!$isAdmin)
		{
			$select = $this->select()->setIntegrityCheck(false)
						   ->from(array('r' => 'iso_rights_group_category'),
						   				array(
						   					'd.id_document',
			       							'd.id_category',
			       							'd.doc_name',
			       							'd.doc_active',
			       							'c.category_name AS cat_name',
			       							'r.read AS r_read',
			       							'r.del AS r_del',
			       							'r.edit AS r_edit',
			       							'r.add AS r_add'
						   				)
						   		  )
						   ->joinLeft(array('ug' => 'iso_user_group'), 'ug.id_group = r.id_group', 'ug.id_group')
						   ->joinLeft(array('g' => 'iso_groups'), 'g.id_group = r.id_group', 'g.id_group')
						   ->joinLeft(array('u' => 'iso_users'), 'u.id_user = ug.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 = c.id_category', 'd.id_category')
						   ->where('u.id_user = ?', $idUser)
						   ->where('r.rights_active = 1')
						   ->where('d.id_category IN (' . implode(',', $categoryies) . ')')
						   ->where('ug.active = 1')
						   ->where('u.user_active = 1')
						   ->where('g.group_active = 1')
						   ->where('c.category_active = 1')
						   ->where('d.doc_active = 1')
						   ->where('r.read = 1')
						   ->order(new Zend_Db_Expr('d.doc_name ASC'));
		
		} 
		else 
		{
	       	$select = $this->select()->setIntegrityCheck(false)
	       				    ->from(array('d' => 'iso_documents'),
	       						array(
	       							'd.id_document',
	       							'd.id_category',
	       							'd.doc_name',
	       							'd.doc_active',
	       							'c.category_name AS cat_name'
	       						)
	       					)
	       					->where('d.id_category IN (' . implode(',', $categoryies) . ')')
	       					->joinLeft(array('c' => 'iso_categoryies'), 'c.id_category = d.id_category')
	       					->order(new Zend_Db_Expr('d.doc_name ASC'));
		}
		
		return $select;
	}
	
	public function getCategoryRights($idUser, $isAdmin)
	{
		
		if (!$isAdmin) 
		{
			$select = $this->select()->setIntegrityCheck(false)
							   ->from(array('r' => 'iso_rights_group_category'),
							   				array(
				       							'r.id_category',
				       							'r.read',
				       							'r.del',
				       							'r.edit',
				       							'r.add'
							   				)
							   		  )
							   ->joinLeft(array('ug' => 'iso_user_group'), 'ug.id_group = r.id_group', 'ug.id_group')
							   ->joinLeft(array('g' => 'iso_groups'), 'g.id_group = r.id_group', 'g.id_group')
							   ->joinLeft(array('u' => 'iso_users'), 'u.id_user = ug.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 = c.id_category', 'd.id_category')
							   ->where('u.id_user = ?', $idUser)
							   ->where('r.rights_active = 1')
							   ->where('ug.active = 1')
							   ->where('u.user_active = 1')
							   ->where('g.group_active = 1')
							   ->where('c.category_active = 1');
//							   ->where('d.doc_active = 1');
		}
		else
		{
						  
			$select = $this->select()->setIntegrityCheck(false)
							   ->from(array('c' => 'iso_categoryies'),
							   				array(
							   					'c.id_category'
							   				)
							   		  );
		}
						   
		$rows = $this->fetchAll($select);
		
		$list = array();
		foreach ($rows as $i) {
			$list[$i->id_category] = $i->toArray();
			
			if ($isAdmin) {
				$list[$i->id_category]['read'] = 1;
				$list[$i->id_category]['del'] = 1;
				$list[$i->id_category]['edit'] = 1;
				$list[$i->id_category]['add'] = 1;
			}
			
			
		}
		
		
		return $list;
	}
}
?>