1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_GiftMessage_Model_Api extends Mage_Checkout_Model_Api_Resource_Product
35: {
36: 37: 38: 39: 40: 41:
42: protected function _prepareData($arr)
43: {
44: if (is_array($arr)) {
45: return $arr;
46: }
47: return array();
48: }
49:
50: 51: 52: 53: 54: 55: 56: 57:
58: protected function _setGiftMessage($entityId, $request, $quote)
59: {
60:
61: 62: 63: 64: 65:
66: try {
67: Mage::dispatchEvent(
68: 'checkout_controller_onepage_save_shipping_method',
69: array('request' => $request, 'quote' => $quote)
70: );
71: return array('entityId' => $entityId, 'result' => true, 'error' => '');
72: } catch (Exception $e) {
73: return array('entityId' => $entityId, 'result' => false, 'error' => $e->getMessage());
74: }
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84:
85: public function setForQuote($quoteId, $giftMessage, $store = null)
86: {
87:
88: $quote = $this->_getQuote($quoteId, $store);
89:
90: $giftMessage = $this->_prepareData($giftMessage);
91: if (empty($giftMessage)) {
92: $this->_fault('giftmessage_invalid_data');
93: }
94:
95: $giftMessage['type'] = 'quote';
96: $giftMessages = array($quoteId => $giftMessage);
97: $request = new Mage_Core_Controller_Request_Http();
98: $request->setParam("giftmessage", $giftMessages);
99:
100: return $this->_setGiftMessage($quote->getId(), $request, $quote);
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110:
111: public function setForQuoteProduct($quoteId, $productsAndMessages, $store = null)
112: {
113:
114: $quote = $this->_getQuote($quoteId, $store);
115:
116: $productsAndMessages = $this->_prepareData($productsAndMessages);
117: if (empty($productsAndMessages)) {
118: $this->_fault('invalid_data');
119: }
120:
121: if (count($productsAndMessages) == 2
122: && isset($productsAndMessages['product'])
123: && isset($productsAndMessages['message'])) {
124: $productsAndMessages = array($productsAndMessages);
125: }
126:
127: $results = array();
128: foreach ($productsAndMessages as $productAndMessage) {
129: if (isset($productAndMessage['product']) && isset($productAndMessage['message'])) {
130: $product = $this->_prepareData($productAndMessage['product']);
131: if (empty($product)) {
132: $this->_fault('product_invalid_data');
133: }
134: $message = $this->_prepareData($productAndMessage['message']);
135: if (empty($message)) {
136: $this->_fault('giftmessage_invalid_data');
137: }
138:
139: if (isset($product['product_id'])) {
140: $productByItem = $this->_getProduct($product['product_id'], $store, "id");
141: } elseif (isset($product['sku'])) {
142: $productByItem = $this->_getProduct($product['sku'], $store, "sku");
143: } else {
144: continue;
145: }
146:
147: $productObj = $this->_getProductRequest($product);
148: $quoteItem = $this->_getQuoteItemByProduct($quote, $productByItem, $productObj);
149: $results[] = $this->setForQuoteItem($quoteItem->getId(), $message, $store);
150: }
151: }
152:
153: return $results;
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163:
164: public function setForQuoteItem($quoteItemId, $giftMessage, $store = null)
165: {
166:
167: $quoteItem = Mage::getModel('sales/quote_item')->load($quoteItemId);
168: if (is_null($quoteItem->getId())) {
169: $this->_fault("quote_item_not_exists");
170: }
171:
172:
173: $quote = $this->_getQuote($quoteItem->getQuoteId(), $store);
174:
175: $giftMessage = $this->_prepareData($giftMessage);
176: $giftMessage['type'] = 'quote_item';
177:
178: $giftMessages = array($quoteItem->getId() => $giftMessage);
179:
180: $request = new Mage_Core_Controller_Request_Http();
181: $request->setParam("giftmessage", $giftMessages);
182:
183: return $this->_setGiftMessage($quoteItemId, $request, $quote);
184: }
185: }
186: