Observer.php 3.58 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_Model_Observer {

	private $itemarray = array();

	public function saveTextOrderLink(array $param_values) {
//		D::s('saveTextOrderLink',5,1);
		$order = $param_values->getOrder();
		$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
		$ListQuoteItemsByQuoteId = Mage::helper('anc_lib/quoteitem')->getListQuoteItemsByQuoteId($order->getQuoteId());
		foreach ($ListQuoteItemsByQuoteId as $ListQuoteItem) {
//			D::ulli('foreach',5,1);
			$ItemProductOptions = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionType($ListQuoteItem['item_id'], 'anctext_type');
			if (is_array($ItemProductOptions) && !empty($ItemProductOptions)) {
//				D::ulli('if',5,1);
				foreach ($ItemProductOptions as $option_id => $nctext_id) {
//					D::ulli('if foreach'. $nctext_id,5,1);
					/**
					 * Kopiere das Text- Item damit Der Benutzer es bearbeiten kann und das Oreginalitem nicht mehr verändert wird
					 */
					if (!array_search($nctext_id, $this->itemarray)) {
						try {
							D::s($this->itemarray,'$this->itemarray',5,1);
							$this->itemarray[] = $nctext_id;
							$model = Mage::getModel('anc_text/nctext');
							$textitem = $model->load($nctext_id)->getData();
							unset($textitem['entity_id']);
							unset($textitem['ordered']);
							unset($textitem['admin_user_id']);
							
							$textitem['customer_id']= $customerId; 
//							$textitem['comment'] = $textitem['comment'] . ' Kopie von ' . $textitem['entity_id'] . ' ItemId: ' . $textitem['used_byitem_id'];
							$model->setData($textitem);
							$model->save();
						} catch (Exception $e) {
							//				D::show($e->getMessage());
						}
					}
					Mage::helper('anc_lib/sql')->updateOneCell(
							'anc_text/nctext', 'ordered', '1', array(
						'entity_id' => $nctext_id,
							)
					);
					$nctext_quoteitemoption_id = (int) Mage::helper('anc_lib/sql')->selectFetchOne(
									'entity_id', 'anc_text/nctext_quoteitemoption', array(
								'customer_id' => $order->getCustomerId(),
								'nctext_id' => $nctext_id,
								'order_id' => $order->getId(),
								'quote_item_id' => $ListQuoteItem['item_id'],
								'ordertime' => $order->getCreatedAt()
									)
					);

					if (!$nctext_quoteitemoption_id) {
//						$nctext_quoteitemoption =  Mage::getModel('anc_text/nctext_quoteitemoption');
						$nctext_quoteitemoption = Mage::helper('anc_text/ncmodel')->getBlancoNcTextQuoteitemoption();
						$nctext_quoteitemoption->setCustomerId($order->getCustomerId());
						$nctext_quoteitemoption->setNctextId($nctext_id);
						$nctext_quoteitemoption->setOrderId($order->getId());
						$nctext_quoteitemoption->setQuoteItemId($ListQuoteItem['item_id']);
						$nctext_quoteitemoption->setOrdertime($order->getCreatedAt());
						$nctext_quoteitemoption->save();
					}
				}
			}
		}
	}

}