Observer.php 3.41 KB
<?php
/**
 * @package  anc_image
 * @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_Image_Model_Observer {

	public function saveImageOrderLink(array $param_values) {
		$order = $param_values->getOrder();
		$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
		$ListQuoteItemsByQuoteId = Mage::helper('anc_lib/quoteitem')->getListQuoteItemsByQuoteId($order->getQuoteId());
		foreach ($ListQuoteItemsByQuoteId as $ListQuoteItem) {
			$ItemProductOptions = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionType($ListQuoteItem['item_id'], 'ancimage_type');
			if (is_array($ItemProductOptions) && !empty($ItemProductOptions)) {
				foreach ($ItemProductOptions as $option_id => $ncimage_id) {

					if (!array_search($ncimage_id, $this->itemarray)) {
						try {
							D::s($this->itemarray, '$this->itemarray', 5, 1);
							$this->itemarray[] = $ncimage_id;
							$model = Mage::getModel('anc_image/ncimage');
							$imageitem = $model->load($ncimage_id)->getData();
							if ($imageitem['admin_user_id'] < 1) {
								unset($imageitem['entity_id']);
								unset($imageitem['ordered']);
								unset($imageitem['admin_user_id']);


								$imageitem['customer_id'] = $customerId;
//							$imageitem['comment'] = $imageitem['comment'] . ' Kopie ' . $imageitem['entity_id'] ;
								$model->setData($imageitem);
								$model->save();
								$var_own_image = true;
							}
						} catch (Exception $e) {
							//				D::show($e->getMessage());
						}
					}

					if ($var_own_image) {
						Mage::helper('anc_lib/sql')->updateOneCell(
								'anc_image/ncimage', 'ordered', '1', array(
							'entity_id' => $ncimage_id,
								)
						);
					}


					$ncimage_quoteitemoption_id = (int) Mage::helper('anc_lib/sql')->selectFetchOne(
									'entity_id', 'anc_image/ncimage_quoteitemoption', array(
								'customer_id' => $order->getCustomerId(),
								'ncimage_id' => $ncimage_id,
								'order_id' => $order->getId(),
								'quote_item_id' => $ListQuoteItem['item_id'],
								'ordertime' => $order->getCreatedAt()
									)
					);

					if (!$ncimage_quoteitemoption_id) {
//						$ncimage_quoteitemoption =  Mage::getModel('anc_image/ncimage_quoteitemoption');
						$ncimage_quoteitemoption = Mage::helper('anc_image/ncmodel')->getBlancoNcImageQuoteitemoption();
						$ncimage_quoteitemoption->setCustomerId($order->getCustomerId());
						$ncimage_quoteitemoption->setNcimageId($ncimage_id);
						$ncimage_quoteitemoption->setOrderId($order->getId());
						$ncimage_quoteitemoption->setQuoteItemId($ListQuoteItem['item_id']);
						$ncimage_quoteitemoption->setOrdertime($order->getCreatedAt());
						$ncimage_quoteitemoption->save();
					}
				}
			}
		}
	}

}