Ncmodel.php 7.35 KB
<?php
/**
 * @package  anc_album
 * @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_Album_Helper_Ncmodel extends Mage_Core_Helper_Abstract {

	/**

	  private $NcAlbums = array();

	  /**
	 * 
	 * @param int $param_ncimage_id
	 * @return anc_image/ncimage
	 */
	public function getNcAlbum($param_ncalbum_id) {
		if ($param_ncalbum_id) {
			if (!array_key_exists($param_ncalbum_id, $this->NcAlbums)) {
//				$ncimage = Mage::getModel('anc_image/ncimage')->load($param_ncimage_id);
				$ncalbum = Mage::helper('anc_lib/ncrights')->loadNcModel('anc_album/ncalbum', $param_ncalbum_id);
				$this->NcAlbums[$param_ncalbum_id] = $ncalbum;
			}
			return $this->NcAlbums[$param_ncalbum_id];
		} else {
			return false;
		}
	}

	public function getNcAlbumsForCustomer() {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
//		return Mage::getModel('anc_image/ncimage')->getCollection()->addFieldToFilter('customer_id', $customer->getId());
		return Mage::helper('anc_lib/ncrights')->getNcModelsByOneFilter('anc_album/ncalbum', 'customer_id', $customer->getId());
	}

	public function createNcAlbumForCustomer() {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		$albumdata = array();
		$albumdata['customer_id'] = $customer->getId();
		$albumdata['admin_user_id'] = '0';
		$albumdata['name'] = 'Benutzeralbum ' . $customer->getName() . ' ' . $customer->getId();
		$albumdata['url_rewrite'] = 'kundenalbum' . $customer->getId();
		$albumdata['isplaylist'] = 0;
		$albumdata['iscategory'] = 0;
		$model = Mage::getModel('anc_album/ncalbum');
		$model->addData($albumdata);
		try {
			$model->save();
		} catch (Exception $e) {
//            D::show($e->getMessage());
		}
		return $model->getId();
	}

	public function saveNcImage(Anc_Image_Model_Ncimage $param_ncimage) {
		if (is_object($param_ncimage)) {
			$param_ncimage->save();
			$this->NcImages[$param_ncimage->getId()] = $param_ncimage;
			return $this->NcImages[$param_ncimage->getId()];
		} else {
			return false;
		}
	}

	public function deleteNcImage(Anc_Image_Model_Ncimage $param_ncimage) {
		if (is_object($param_ncimage)) {
			$customer = Mage::getSingleton('customer/session')->getCustomer();
			if ($customer->getId() && $customer->getId() === $param_ncimage->getCustomerId()) {
				Mage::helper('anc_image/ncmodel')->deleteNcImageFiles($param_ncimage);
				$param_ncimage->delete();
//				D::s($param_ncimage, '$param_ncimage');
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public function deleteNcImageFiles(Anc_Image_Model_Ncimage $param_ncimage) {
		if (is_object($param_ncimage)) {
			$customer = Mage::getSingleton('customer/session')->getCustomer();
			if ($customer->getId() && $customer->getId() === $param_ncimage->getCustomerId()) {
//				mcFile::deleteFile(Mage::getBaseDir().$param_ncimage->getPath());								
				mcFile::deleteFile(Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath() . $param_ncimage->getPath());
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public function getBlancoNcImageQuoteitemoption() {
		return Mage::getModel('anc_image/ncimage_quoteitemoption');
	}

	/**
	 * gibt die NcAlben für Kunden zurück
	 * @todo ncrights
	 * 
	 * @return type
	 */
	private function getForCustomer() {
		$ItemInfos = Mage::helper('anc_lib/data')->getCheckoutCartItemInfos();
		$current_product = $ItemInfos['item_product'];
		$ParentItemInfos = Mage::helper('anc_lib/data')->getCheckoutCartParentItemInfos();
		$bundle_product = $ParentItemInfos['parent_item_product'];
//		Mage::app()->getRequest()->getParam('product_id', 44);
		
		if (is_object($bundle_product) && is_object($current_product)) {
			$bundle_product_array = $bundle_product->getData();
			$current_product_array = $current_product->getData();
			D::ulli('ANCOBJSPEC: ' . $current_product_array['ancobjspecial'], 1);
			if ($bundle_product_array['ancobjectncalbumadvertisingid'] > 0 && $current_product_array['ancobjspecial'] === 'isadvertising') {
				$album_id = $bundle_product_array['ancobjectncalbumadvertisingid'];
				$show_single = $bundle_product_array['ancobjncalbumadvertsingle'];
				Mage::app()->getRequest()->setParam('ncobject_album_id', $album_id);
				Mage::app()->getRequest()->setParam('ncobjectsingle', $show_single);
			} elseif ($bundle_product_array['ancobjectnctextadressid'] > 0 && $current_product_array['ancobjspecial'] === 'adresse') {
				$album_id = $bundle_product_array['ancobjectnctextadressid'];
				$show_single = $bundle_product_array['ancobjnctextadresssingle'];
				Mage::app()->getRequest()->setParam('ncobject_album_id', $album_id);
				Mage::app()->getRequest()->setParam('ncobjectsingle', $show_single);
				
			} elseif ($current_product_array['ancobjspecial'] === 'personalisiert') {
				$personalisiert = TRUE;
				Mage::app()->getRequest()->setParam('ncpersonalisiert', $show_single);
			}
		}
		/**
		 * Album Ids werden im Bündelprodukt angegeben
		 * show Single auch
		 */
		if ($album_id) {
			if ($show_single) {
				$param_arrayFields = array(
					'entity_id',
				);
				$param_arrayContent = array(
					array('eq' => $album_id),
				);
			} else {
				$param_arrayFields = array(
					'entity_id',
					'iscategory'
				);
				$param_arrayContent = array(
					array('eq' => $album_id),
					array('eq' => '1')
				);
			}
		} else {
			/**
			 *  Or Condition für Field to filter
			 */
			if ($personalisiert) {
				$param_arrayFields = array(
					'iscategory',
//					'ispersonalized'
				);
				$param_arrayContent = array(
					array('eq' => '1'),
//					array('eq' => '1')
				);
			} else {
				$param_arrayFields = array(
					'iscategory'
				);
				$param_arrayContent = array(
					array('eq' => '1')
				);
			}
		}

		$customer = Mage::getSingleton('customer/session')->getCustomer();
		$collection = Mage::getModel('anc_album/ncalbum')->getCollection()
				->addFieldToFilter('ncright_id', 0)
//				->addFieldToFilter('iscategory', 1)
				->addFieldToFilter($param_arrayFields, $param_arrayContent)
				->addFieldToFilter('customer_id', 0);
		return $collection->getData();
	}

	public function getAllCategoryNcAlbums() {
		return $this->getForCustomer();
	}

	private function getForCustomerPlaylistNcAlbums() {
		$collection = Mage::getModel('anc_album/ncalbum')->getCollection()
				->addFieldToFilter('ncright_id', 0)
				->addFieldToFilter('isplaylist', 1)
				->addFieldToFilter('customer_id', 0);
		return $collection->getData();		
	}
	
	public function getAllPlaylistNcAlbums() {
		return $this->getForCustomerPlaylistNcAlbums();
	}

	public function getFirstImageFromNcalbum($param_id) {
		return Mage::helper('anc_lib/ncrights')->getNcModelBy('anc_image/ncimage', 'ncalbum_id', $param_id);
	}

}