1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_GiftMessage
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Gift Message Observer Model
30: *
31: * @category Mage
32: * @package Mage_GiftMessage
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_GiftMessage_Model_Observer extends Varien_Object
36: {
37:
38: /**
39: * Set gift messages to order item on import item
40: *
41: * @param Varien_Object $observer
42: * @return Mage_GiftMessage_Model_Observer
43: */
44: public function salesEventConvertQuoteItemToOrderItem($observer)
45: {
46: $orderItem = $observer->getEvent()->getOrderItem();
47: $quoteItem = $observer->getEvent()->getItem();
48:
49: $isAvailable = Mage::helper('giftmessage/message')->getIsMessagesAvailable(
50: 'item',
51: $quoteItem,
52: $quoteItem->getStoreId()
53: );
54:
55: $orderItem->setGiftMessageId($quoteItem->getGiftMessageId())
56: ->setGiftMessageAvailable($isAvailable);
57: return $this;
58: }
59:
60: /**
61: * Set gift messages to order from quote address
62: *
63: * @param Varien_Object $observer
64: * @return Mage_GiftMessage_Model_Observer
65: */
66: public function salesEventConvertQuoteAddressToOrder($observer)
67: {
68: if($observer->getEvent()->getAddress()->getGiftMessageId()) {
69: $observer->getEvent()->getOrder()
70: ->setGiftMessageId($observer->getEvent()->getAddress()->getGiftMessageId());
71: }
72: return $this;
73: }
74:
75: /**
76: * Set gift messages to order from quote address
77: *
78: * @param Varien_Object $observer
79: * @return Mage_GiftMessage_Model_Observer
80: */
81: public function salesEventConvertQuoteToOrder($observer)
82: {
83: $observer->getEvent()->getOrder()
84: ->setGiftMessageId($observer->getEvent()->getQuote()->getGiftMessageId());
85: return $this;
86: }
87:
88: /**
89: * Geter for available gift messages value from product
90: *
91: * @deprecated after 1.5.0.0
92: * @param Mage_Catalog_Model_Product|integer $product
93: * @return integer|null
94: */
95: protected function _getAvailable($product)
96: {
97: if(is_object($product)) {
98: return $product->getGiftMessageAvailable();
99: }
100: return Mage::getModel('catalog/product')->load($product)->getGiftMessageAvailable();
101: }
102:
103: /**
104: * Operate with gift messages on checkout proccess
105: *
106: * @param Varieb_Object $observer
107: * @return Mage_GiftMessage_Model_Observer
108: */
109: public function checkoutEventCreateGiftMessage($observer)
110: {
111: $giftMessages = $observer->getEvent()->getRequest()->getParam('giftmessage');
112: $quote = $observer->getEvent()->getQuote();
113: /* @var $quote Mage_Sales_Model_Quote */
114: if(is_array($giftMessages)) {
115: foreach ($giftMessages as $entityId=>$message) {
116:
117: $giftMessage = Mage::getModel('giftmessage/message');
118:
119: switch ($message['type']) {
120: case 'quote':
121: $entity = $quote;
122: break;
123: case 'quote_item':
124: $entity = $quote->getItemById($entityId);
125: break;
126: case 'quote_address':
127: $entity = $quote->getAddressById($entityId);
128: break;
129: case 'quote_address_item':
130: $entity = $quote->getAddressById($message['address'])->getItemById($entityId);
131: break;
132: default:
133: $entity = $quote;
134: break;
135: }
136:
137: if($entity->getGiftMessageId()) {
138: $giftMessage->load($entity->getGiftMessageId());
139: }
140:
141: if(trim($message['message'])=='') {
142: if($giftMessage->getId()) {
143: try{
144: $giftMessage->delete();
145: $entity->setGiftMessageId(0)
146: ->save();
147: }
148: catch (Exception $e) { }
149: }
150: continue;
151: }
152:
153: try {
154: $giftMessage->setSender($message['from'])
155: ->setRecipient($message['to'])
156: ->setMessage($message['message'])
157: ->save();
158:
159: $entity->setGiftMessageId($giftMessage->getId())
160: ->save();
161:
162: }
163: catch (Exception $e) { }
164: }
165: }
166: return $this;
167: }
168:
169: /**
170: * Set giftmessage available default value to product
171: * on catalog products collection load
172: *
173: * @deprecated after 1.4.2.0-beta1
174: * @param Varien_Object $observer
175: * @return Mage_GiftMessage_Model_Observer
176: */
177: public function catalogEventProductCollectionAfterLoad($observer)
178: {
179: return $this;
180: }
181:
182: /**
183: * Duplicates giftmessage from order to quote on import or reorder
184: *
185: * @param Varien_Event_Observer $observer
186: * @return Mage_GiftMessage_Model_Observer
187: */
188: public function salesEventOrderToQuote($observer)
189: {
190: $order = $observer->getEvent()->getOrder();
191: // Do not import giftmessage data if order is reordered
192: if ($order->getReordered()) {
193: return $this;
194: }
195:
196: if (!Mage::helper('giftmessage/message')->isMessagesAvailable('order', $order, $order->getStore())){
197: return $this;
198: }
199: $giftMessageId = $order->getGiftMessageId();
200: if($giftMessageId) {
201: $giftMessage = Mage::getModel('giftmessage/message')->load($giftMessageId)
202: ->setId(null)
203: ->save();
204: $observer->getEvent()->getQuote()->setGiftMessageId($giftMessage->getId());
205: }
206:
207: return $this;
208: }
209:
210: /**
211: * Duplicates giftmessage from order item to quote item on import or reorder
212: *
213: * @param Varien_Event_Observer $observer
214: * @return Mage_GiftMessage_Model_Observer
215: */
216: public function salesEventOrderItemToQuoteItem($observer)
217: {
218: /** @var $orderItem Mage_Sales_Model_Order_Item */
219: $orderItem = $observer->getEvent()->getOrderItem();
220: // Do not import giftmessage data if order is reordered
221: $order = $orderItem->getOrder();
222: if ($order && $order->getReordered()) {
223: return $this;
224: }
225:
226: $isAvailable = Mage::helper('giftmessage/message')->isMessagesAvailable(
227: 'order_item',
228: $orderItem,
229: $orderItem->getStoreId()
230: );
231: if (!$isAvailable) {
232: return $this;
233: }
234:
235: /** @var $quoteItem Mage_Sales_Model_Quote_Item */
236: $quoteItem = $observer->getEvent()->getQuoteItem();
237: if ($giftMessageId = $orderItem->getGiftMessageId()) {
238: $giftMessage = Mage::getModel('giftmessage/message')->load($giftMessageId)
239: ->setId(null)
240: ->save();
241: $quoteItem->setGiftMessageId($giftMessage->getId());
242: }
243: return $this;
244: }
245: }
246: