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:
35: class Mage_Adminhtml_Model_Giftmessage_Save extends Varien_Object
36: {
37: protected $_saved = false;
38:
39: 40: 41: 42: 43:
44: public function saveAllInQuote()
45: {
46: $giftmessages = $this->getGiftmessages();
47:
48: if (!is_array($giftmessages)) {
49: return $this;
50: }
51:
52: foreach ($giftmessages as $entityId=>$giftmessage) {
53: $this->_saveOne($entityId, $giftmessage);
54: }
55:
56: return $this;
57: }
58:
59: public function getSaved()
60: {
61: return $this->_saved;
62: }
63:
64: public function saveAllInOrder()
65: {
66: $giftmessages = $this->getGiftmessages();
67:
68: if (!is_array($giftmessages)) {
69: return $this;
70: }
71:
72: foreach ($giftmessages as $entityId=>$giftmessage) {
73: $this->_saveOne($entityId, $giftmessage);
74: }
75:
76: return $this;
77: }
78:
79: 80: 81: 82: 83: 84: 85:
86: protected function _saveOne($entityId, $giftmessage) {
87:
88: $giftmessageModel = Mage::getModel('giftmessage/message');
89: $entityType = $this->_getMappedType($giftmessage['type']);
90:
91: switch($entityType) {
92: case 'quote':
93: $entityModel = $this->_getQuote();
94: break;
95:
96: case 'quote_item':
97: $entityModel = $this->_getQuote()->getItemById($entityId);
98: break;
99:
100: default:
101: $entityModel = $giftmessageModel->getEntityModelByType($entityType)
102: ->load($entityId);
103: break;
104: }
105:
106: if (!$entityModel) {
107: return $this;
108: }
109:
110: if ($entityModel->getGiftMessageId()) {
111: $giftmessageModel->load($entityModel->getGiftMessageId());
112: }
113:
114: $giftmessageModel->addData($giftmessage);
115:
116: if ($giftmessageModel->isMessageEmpty() && $giftmessageModel->getId()) {
117:
118: $this->_deleteOne($entityModel, $giftmessageModel);
119: $this->_saved = false;
120: } elseif (!$giftmessageModel->isMessageEmpty()) {
121: $giftmessageModel->save();
122: $entityModel->setGiftMessageId($giftmessageModel->getId());
123: if($entityType != 'quote') {
124: $entityModel->save();
125: }
126: $this->_saved = true;
127: }
128:
129: return $this;
130: }
131:
132: 133: 134: 135: 136: 137: 138:
139: protected function _deleteOne($entityModel, $giftmessageModel=null)
140: {
141: if(is_null($giftmessageModel)) {
142: $giftmessageModel = Mage::getModel('giftmessage/message')
143: ->load($entityModel->getGiftMessageId());
144: }
145: $giftmessageModel->delete();
146: $entityModel->setGiftMessageId(0)
147: ->save();
148: return $this;
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function setAllowQuoteItems($items)
158: {
159: $this->_getSession()->setAllowQuoteItemsGiftMessage($items);
160: return $this;
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function addAllowQuoteItem($item)
170: {
171: $items = $this->getAllowQuoteItems();
172: if (!in_array($item, $items)) {
173: $items[] = $item;
174: }
175: $this->setAllowQuoteItems($items);
176:
177: return $this;
178: }
179:
180: 181: 182: 183: 184:
185: public function getAllowQuoteItems()
186: {
187: if(!is_array($this->_getSession()->getAllowQuoteItemsGiftMessage())) {
188: $this->setAllowQuoteItems(array());
189: }
190:
191: return $this->_getSession()->getAllowQuoteItemsGiftMessage();
192: }
193:
194: 195: 196: 197: 198:
199: public function getAllowQuoteItemsProducts()
200: {
201: $result = array();
202: foreach ($this->getAllowQuoteItems() as $itemId) {
203: $item = $this->_getQuote()->getItemById($itemId);
204: if(!$item) {
205: continue;
206: }
207: $result[] = $item->getProduct()->getId();
208: }
209: return $result;
210: }
211:
212: 213: 214: 215: 216: 217:
218: public function getIsAllowedQuoteItem($item)
219: {
220: if(!in_array($item->getId(), $this->getAllowQuoteItems())) {
221: if ($item->getGiftMessageId() && $this->isGiftMessagesAvailable($item)) {
222: $this->addAllowQuoteItem($item->getId());
223: return true;
224: }
225: return false;
226: }
227:
228: return true;
229: }
230:
231: 232: 233: 234: 235: 236:
237: public function isGiftMessagesAvailable($item)
238: {
239: return Mage::helper('giftmessage/message')->getIsMessagesAvailable(
240: 'item', $item, $item->getStore()
241: );
242: }
243:
244: 245: 246: 247: 248: 249:
250: public function importAllowQuoteItemsFromProducts($products)
251: {
252: $allowedItems = $this->getAllowQuoteItems();
253: $deleteAllowedItems = array();
254: foreach ($products as $productId=>$data) {
255: $product = Mage::getModel('catalog/product')
256: ->setStore($this->_getSession()->getStore())
257: ->load($productId);
258: $item = $this->_getQuote()->getItemByProduct($product);
259:
260: if(!$item) {
261: continue;
262: }
263:
264: if (in_array($item->getId(), $allowedItems)
265: && !isset($data['giftmessage'])) {
266: $deleteAllowedItems[] = $item->getId();
267: } elseif (!in_array($item->getId(), $allowedItems)
268: && isset($data['giftmessage'])) {
269: $allowedItems[] = $item->getId();
270: }
271:
272: }
273:
274: $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
275:
276: $this->setAllowQuoteItems($allowedItems);
277: return $this;
278: }
279:
280: public function importAllowQuoteItemsFromItems($items)
281: {
282: $allowedItems = $this->getAllowQuoteItems();
283: $deleteAllowedItems = array();
284: foreach ($items as $itemId=>$data) {
285:
286: $item = $this->_getQuote()->getItemById($itemId);
287:
288: if(!$item) {
289:
290: $deleteAllowedItems[] = $itemId;
291: continue;
292: }
293:
294: if (in_array($item->getId(), $allowedItems)
295: && !isset($data['giftmessage'])) {
296: $deleteAllowedItems[] = $item->getId();
297: } elseif (!in_array($item->getId(), $allowedItems)
298: && isset($data['giftmessage'])) {
299: $allowedItems[] = $item->getId();
300: }
301:
302: }
303:
304: $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
305: $this->setAllowQuoteItems($allowedItems);
306: return $this;
307: }
308:
309: 310: 311: 312: 313: 314:
315: protected function _getMappedType($type)
316: {
317: $map = array(
318: 'main' => 'quote',
319: 'item' => 'quote_item',
320: 'order' => 'order',
321: 'order_item' => 'order_item'
322: );
323:
324: if (isset($map[$type])) {
325: return $map[$type];
326: }
327:
328: return null;
329: }
330:
331: 332: 333: 334: 335:
336: protected function _getSession()
337: {
338: return Mage::getSingleton('adminhtml/session_quote');
339: }
340:
341: 342: 343: 344: 345:
346: protected function _getQuote()
347: {
348: return $this->_getSession()->getQuote();
349: }
350:
351: }
352: