Ncmodel.php 7.39 KB
<?php
/**
 * @package  anc_text 
 * @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_Text_Helper_Ncmodel extends Mage_Core_Helper_Abstract {

	/**
	 * 	cache für anc_text/nctext models
	 * @var array
	 */
	private $NcTexts = array();

	/**
	 * gibt zurück wie oft das bild schon für den kauf verwendet wurde
	 * 
	 * @param int $param_nctext_id
	 * @return Anc_Text_Model_Resource_Mysql4_Nctext_Quoteitemoption_Collection
	 */
	public function getNcTextMetaData($param_nctext_id) {
		$collection = Mage::getModel('anc_text/nctext_quoteitemoption')->getCollection();
		$Quoteitemoption_Collection = $collection->addFieldToFilter('nctext_id', $param_nctext_id);
		return $Quoteitemoption_Collection;
	}

	/**
	 * 
	 * @param int $param_nctext_id
	 * @return anc_text/nctext
	 */
	public function getNcText($param_nctext_id) {
		if ($param_nctext_id) {
			if (!array_key_exists($param_nctext_id, $this->NcTexts)) {
				$nctext = Mage::helper('anc_lib/ncrights')->loadNcModel('anc_text/nctext', $param_nctext_id);

				$this->NcTexts[$param_nctext_id] = $nctext;
			}
			return $this->NcTexts[$param_nctext_id];
		} else {
			return false;
		}
	}

	public function createNCNew() {
		$customer = Mage::getSingleton('customer/session')->getCustomer()->getId();
		$textdata= array();
		$textdata['customer_id'] = $customer;
		$var_albums = Mage::helper('anc_album/ncmodel')->getNcAlbumsForCustomer()->getData();
		if (!$var_albums[0]) {
			$albumid = Mage::helper('anc_album/ncmodel')->createNcAlbumForCustomer();
			$var_albums = Mage::helper('anc_album/ncmodel')->getNcAlbumsForCustomer()->getData();
		} else {
			$albumid = $var_albums[0]['entity_id'];
		}
		$textdata['ncalbum_id'] = $albumid;
		$textdata['name']='Neuer Text';
		$textdata['content']=' ';
		try {
			$model = Mage::getModel('anc_text/nctext');
			$model->setData($textdata);
			$model->save();
		} catch (Exception $e) {
//				D::show($e->getMessage());
		}
		return $model->getId();
	}

	public function prepareSaveNcText(array $param_save, array $original_item = null) {
		
	$params = Mage::app()->getRequest()->getParams();
	$var_controller= Mage::app()->getRequest()->getControllerName();
	$var_action =Mage::app()->getRequest()->getActionName();
	$var_getRequest =Mage::app()->getRequest();
	
		/**
		 * 
		 * 
		 * HIER GET PARAMS 
		 * 
		 */
		$new = false;
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		$textdata = array();
		if ($param_save['ncinput_id'] > 0) {
			$textdata['entity_id'] = $param_save['ncinput_id'];
		} else {
			$new = true;
		}
		if ($param_save['special'] === 'custemerbe') {
			
		} else {
			if ($param_save['option_id'] != $param_save['used_byitem_id']) {
				$new = true;
				unset($textdata['entity_id']);
			}
			if ($param_save['option_id']) {
				$textdata['used_byitem_id'] = $param_save['option_id'];
			}
		}

		$textdata['customer_id'] = $customer->getId();
		$textdata['admin_user_id'] = 0;
		D::s($params,'$params',5,1);
		D::s($param_save,'$param_save',5,1);
		D::s($var_getRequest,'$var_getRequest',5,1);
		if ($param_save['varcontent'] && $param_save['varcontent'] != $original_item['varcontent']) {
			/**
			 * Speichere veränderten neu ab mit neuer idtext ab 
			 */

			$textdata['content'] = $param_save['varcontent'];
			if($param_save['ordered']== 1 || key_exists('option_id', $param_save) && $param_save['option_id'] > 0){
				$new = true;
				unset($textdata['entity_id']);
				unset($textdata['ordered']);
			}
		} else {
//			return $textdata['ncinput_id'];
		}

		if ($param_save['admin_user_id'] > 0) {
			$textdata['original_id'] = $param_save['original_id'];
			$new = true;
			unset($textdata['entity_id']);
		}
		if ($param_save['album_id']) {
			$textdata['ncalbum_id'] = $param_save['album_id'];
		} else {
//			return;
		}

		$textdata['visibleingallery'] = 0;
		$textdata['admin_user_id'] = '0';
		if ($param_save['name']) {
			$textdata['name'] = $param_save['name'];
		} else {
			$textdata['name'] = 'Text ' . $customer->getName() . ' ID ' . $customer->getId();
		}
		if ($param_save['comment']) {
			$textdata['comment'] = $param_save['comment'];
		}

		$textdata['url_rewrite'] = 'kundenalbum' . $customer->getId();
		$textdata['isplaylist'] = 0;
		$textdata['iscategory'] = 0;
		$model = Mage::getModel('anc_text/nctext');

		if ($new) {
			try {
				$model->setData($textdata);
				$model->save();
			} catch (Exception $e) {
//				D::show($e->getMessage());
			}
		} else {
			$model->load($textdata['entity_id'])->addData($textdata);
			try {
				$model->setId($textdata['entity_id'])->save();
			} catch (Exception $e) {
//				D::show($e->getMessage());
			}
		}
		return $model->getId();
	}

	
	

	
	
	
	
	public function getNcTextsForCustomer() {
		$customer = Mage::getSingleton('customer/session')->getCustomer();
		$param_arrayFields = array(
					'customer_id',
					'ordered'
				);
				$param_arrayContent = array(
					array('eq' => $customer->getId()),
					array('null' => true)
				);
		
		return Mage::helper('anc_lib/ncrights')->getNcModelsByOneFilter('anc_text/nctext', $param_arrayFields, $param_arrayContent);
	}

	public function getNcTextsForAlbum($param_album_id, array $param_filter=null,array $param_content=null) {
		if($param_filter&&$param_content){
			return Mage::helper('anc_lib/ncrights')->getNcModelsByOneFilter('anc_text/nctext', $param_filter, $param_content);
		}else{
			return Mage::helper('anc_lib/ncrights')->getNcModelsByOneFilter('anc_text/nctext', 'ncalbum_id', $param_album_id);
		}
	}

	public function saveNcText(Anc_Text_Model_Nctext $param_nctext) {
		if (is_object($param_nctext)) {
			$param_nctext->save();
			$this->NcTexts[$param_nctext->getId()] = $param_nctext;
			return $this->NcTexts[$param_nctext->getId()];
		} else {
			return false;
		}
	}

	public function deleteNcText(Anc_Text_Model_Nctext $param_nctext) {
		if (is_object($param_nctext)) {
			$customer = Mage::getSingleton('customer/session')->getCustomer();
			if ($customer->getId() && $customer->getId() === $param_nctext->getCustomerId()) {
				Mage::helper('anc_text/ncmodel')->deleteNcTextFiles($param_nctext);
				$param_nctext->delete();
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

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

	public function getBlancoNcTextQuoteitemoption() {
		return Mage::getModel('anc_text/nctext_quoteitemoption');
	}
}