Ncrights.php 6.86 KB
<?php
/**
 * @package  anc_lib 
 * @category magento
 * @mailto	code [at] netz.coop
 * @author	netz.coop eG* 
 * @copyright (c) 2014, netz.coop eG
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */ 
class Anc_Lib_Helper_Ncrights extends Mage_Core_Helper_Abstract {
	/**
	 * @todo diese info muss eigentlich in die jeweiligen extension ausgelagert werden
	 * @var type 
	 */
	private $loadedNcModels = array(
		'anc_playlist/ncplaylist' => array(), 
		'anc_album/ncalbum' => array(), 
		'anc_image/ncimage' => array(), 
		'anc_text/nctext' => array(), 
		'anc_addressimport/ncaddressimport' => array(),
		'anc_addressimport/ncaddressimport_item' => array(),
	);
	
	private function checkReadRightGroups(Mage_Core_Model_Abstract $param_model) {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		
		$ncrightsgroups = Mage::helper('anc_lib/ncmodel')->getNcRightgroups($param_model->getNcrightId());
		if(is_object($ncrightsgroups)) {
			foreach($ncrightsgroups->getData() as $ncrightsgroup) {
				if($customer->getGroupId() === $ncrightsgroup['customer_group_id']) {
					return true;
				}
			}		
			return false;
		}

		return true;
	}
	private function checkReadRightCustomers(Mage_Core_Model_Abstract $param_model) {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		$ncrightscustomers = Mage::helper('anc_lib/ncmodel')->getNcRightcustomers($param_model->getNcrightId());
		if(is_object($ncrightscustomers)) {
			foreach($ncrightscustomers->getData() as $ncrightscustomer) {
				if($ncrightscustomer['customer_id']=== $customer->getId()) {
					return true;
				}
			}			
			return false;
		}
		return true;
	}
	
	private function checkIfReadRightCustomersGroupEmpty(Mage_Core_Model_Abstract $param_model) {
		$ncrightscustomers = Mage::helper('anc_lib/ncmodel')->getNcRightcustomers($param_model->getNcrightId());
		$ncrightsgroups = Mage::helper('anc_lib/ncmodel')->getNcRightgroups($param_model->getNcrightId());
		if(is_object($ncrightsgroups)) {
			if(empty($ncrightscustomers->getData()) && empty($ncrightsgroups->getData())) {
				return true;
			} 			
			return false;
		}
		return true;

	}	
	private function checkReadRight(Mage_Core_Model_Abstract $param_model) {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		if(!$param_model->getId()) {
			return true;
		} else if($param_model->getCustomerId() === $customer->getId()) {
			return true;
		} else if($param_model->getNcrightId() === 0) {
			return true;				
		} else if($this->checkReadRightCustomers($param_model)) {
			return true;
		} else if($this->checkReadRightGroups($param_model)) {
			return true;			
		} else if($this->checkIfReadRightCustomersGroupEmpty($param_model)) {
			return true;						
		} else {
			return false;
		}
	}
	private function checkWriteRight(Mage_Core_Model_Abstract $param_model) {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		if(!$param_model->getId()) {
			return true;
		} else if($param_model->getCustomerId() === $customer->getId()) {
			return true;
		} else {
			return false;
		}
	}
	
	private function checkIfIsSupported($param_resourcename) {
		if(is_object($param_resourcename)) {
			$param_resourcename = $param_resourcename->getResourceName();
		} 
		
		if(array_key_exists($param_resourcename, $this->loadedNcModels) && is_array($this->loadedNcModels[$param_resourcename])) {
			return true;
		} else {
			D::li("checkIfIsSupported($param_resourcename): $param_resourcename hat noch kein ncrights !!!!");
			return false;
		}
	}
	
	public function loadNcModel($param_resourcename, $param_id) {
		
		if($this->checkIfIsSupported($param_resourcename)) {
			if(!array_key_exists($param_id, $this->loadedNcModels[$param_resourcename])) {
				$ncmodel = Mage::getModel($param_resourcename)->load($param_id);
				$this->loadedNcModels[$param_resourcename][$param_id] = $ncmodel;
			}
			if($this->checkReadRight($this->loadedNcModels[$param_resourcename][$param_id])) {
				return $this->loadedNcModels[$param_resourcename][$param_id];
			} else {
				return false;
			}
			
		} else {
			return false;
		}
		
	}
	/**
	 * 
	 * @param string $param_resourcename
	 * @param type $param_filtername
	 * @param type $param_filtervalue
	 * @return type
	 */
	public function getNcModelsByOneFilter( $param_resourcename, $param_filtername, $param_filtervalue) {

		if($this->checkIfIsSupported($param_resourcename) ) {
			$Collection= Mage::getModel($param_resourcename)->getCollection();
			if(is_array($param_filtername)){
				foreach($param_filtername as $key =>$value){
					$Collection = $Collection->addFieldToFilter($value, $param_filtervalue[$key]);
				}
			}else{
				$Collection = $Collection->addFieldToFilter($param_filtername, $param_filtervalue);
			}
			
			$result_array = array();
			foreach($Collection->getData() as $key =>  $Collectrow) {
				$ncmodel = $this->loadNcModel($param_resourcename, $Collectrow['entity_id']);
				if(is_object($ncmodel)) {
					$result_array[] = $ncmodel->getData(); 
				}
			}
			
			$Collection->setDataAnc($result_array);
			
			return $Collection;//Mage::getModel($param_resourcename)->getCollection()->addFieldToFilter($param_filtername, $param_filtervalue);
		}
	}
	
	public function getNcModelBy($param_resourcename, $param_column, $param_value) {
		if($this->checkIfIsSupported($param_resourcename)) {
			$ncmodel =  Mage::getModel($param_resourcename)->load($param_value, $param_column);
			$this->loadedNcModels[$param_resourcename][$ncmodel->getId()] = $ncmodel;

			if($this->checkReadRight($this->loadedNcModels[$param_resourcename][$ncmodel->getId()])) {
				return $this->loadedNcModels[$param_resourcename][$ncmodel->getId()];
			} else {
				return false;
			}			
		} else {
			return false;
		}	
	}
	public function saveNcModel(Mage_Core_Model_Abstract $param_model) {
		if($this->checkIfIsSupported($param_model) && $this->checkWriteRight($param_model)) {
			if(is_object($param_model)) {
				$param_model->save();
				return $param_model;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	public function deleteNcModel(Mage_Core_Model_Abstract $param_model) {
		if($this->checkIfIsSupported($param_model) && $this->checkWriteRight($param_model)) {
			if(is_object($param_model)) {
				return $param_model->delete();
	//			return $param_model;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}
?>