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_GiftMessage_Helper_Message extends Mage_Core_Helper_Data
36: {
37: 38: 39: 40:
41: const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items';
42: const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order';
43:
44: 45: 46: 47: 48:
49: protected $_nextId = 0;
50:
51: 52: 53: 54: 55:
56: protected $_innerCache = array();
57:
58: 59: 60: 61: 62: 63: 64:
65: public function getButton($type, Varien_Object $entity)
66: {
67: if (!$this->isMessagesAvailable($type, $entity)) {
68: return ' ';
69: }
70:
71: return Mage::getSingleton('core/layout')->createBlock('giftmessage/message_helper')
72: ->setId('giftmessage_button_' . $this->_nextId++)
73: ->setCanDisplayContainer(true)
74: ->setEntity($entity)
75: ->setType($type)->toHtml();
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: public function getInline($type, Varien_Object $entity, $dontDisplayContainer=false)
87: {
88: if (!in_array($type, array('onepage_checkout','multishipping_adress')) && !$this->isMessagesAvailable($type, $entity)) {
89: return '';
90: }
91:
92: return Mage::getSingleton('core/layout')->createBlock('giftmessage/message_inline')
93: ->setId('giftmessage_form_' . $this->_nextId++)
94: ->setDontDisplayContainer($dontDisplayContainer)
95: ->setEntity($entity)
96: ->setType($type)->toHtml();
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106:
107: public function isMessagesAvailable($type, Varien_Object $entity, $store = null)
108: {
109: if ($type == 'items') {
110: $items = $entity->getAllItems();
111: if(!is_array($items) || empty($items)) {
112: return Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $store);
113: }
114: if($entity instanceof Mage_Sales_Model_Quote) {
115: $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
116: }
117: else {
118: $_type = 'order_item';
119: }
120:
121: foreach ($items as $item) {
122: if ($item->getParentItem()) {
123: continue;
124: }
125: if ($this->isMessagesAvailable($_type, $item, $store)) {
126: return true;
127: }
128: }
129: } elseif ($type == 'item') {
130: return $this->_getDependenceFromStoreConfig(
131: $entity->getProduct()->getGiftMessageAvailable(),
132: $store
133: );
134: } elseif ($type == 'order_item') {
135: return $this->_getDependenceFromStoreConfig(
136: $entity->getGiftMessageAvailable(),
137: $store
138: );
139: } elseif ($type == 'address_item') {
140: $storeId = is_numeric($store) ? $store : Mage::app()->getStore($store)->getId();
141:
142: if (!$this->isCached('address_item_' . $entity->getProductId())) {
143: $this->setCached(
144: 'address_item_' . $entity->getProductId(),
145: Mage::getModel('catalog/product')
146: ->setStoreId($storeId)
147: ->load($entity->getProductId())
148: ->getGiftMessageAvailable()
149: );
150: }
151: return $this->_getDependenceFromStoreConfig(
152: $this->getCached('address_item_' . $entity->getProductId()),
153: $store
154: );
155: } else {
156: return Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, $store);
157: }
158:
159: return false;
160: }
161:
162: 163: 164: 165: 166: 167: 168:
169: protected function _getDependenceFromStoreConfig($productGiftMessageAllow, $store=null)
170: {
171: $result = Mage::getStoreConfig(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $store);
172: if ($productGiftMessageAllow === '' || is_null($productGiftMessageAllow)) {
173: return $result;
174: } else {
175: return $productGiftMessageAllow;
176: }
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186:
187: public function getIsMessagesAvailable($type, Varien_Object $entity, $store=null)
188: {
189: return $this->isMessagesAvailable($type, $entity, $store);
190: }
191:
192: 193: 194: 195: 196: 197:
198: public function getEscapedGiftMessage(Varien_Object $entity)
199: {
200: $message = $this->getGiftMessageForEntity($entity);
201: if ($message) {
202: return nl2br($this->htmlEscape($message->getMessage()));
203: }
204: return null;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function getGiftMessageForEntity(Varien_Object $entity)
214: {
215: if($entity->getGiftMessageId() && !$entity->getGiftMessage()) {
216: $message = $this->getGiftMessage($entity->getGiftMessageId());
217: $entity->setGiftMessage($message);
218: }
219: return $entity->getGiftMessage();
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229:
230: public function getCached($key)
231: {
232: if($this->isCached($key)) {
233: return $this->_innerCache[$key];
234: }
235:
236: return null;
237: }
238:
239: 240: 241: 242: 243: 244:
245: public function isCached($key)
246: {
247: return isset($this->_innerCache[$key]);
248: }
249:
250: 251: 252: 253: 254: 255: 256:
257: public function setCached($key, $value)
258: {
259: $this->_innerCache[$key] = $value;
260: return $this;
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: public function getAvailableForQuoteItems($quote, $store=null)
271: {
272: foreach($quote->getAllItems() as $item) {
273: if($this->isMessagesAvailable('item', $item, $store)) {
274: return true;
275: }
276: }
277:
278: return false;
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: public function getAvailableForAddressItems($items, $store=null)
289: {
290: foreach($items as $item) {
291: if($this->isMessagesAvailable('address_item', $item, $store)) {
292: return true;
293: }
294: }
295: return false;
296: }
297:
298: 299: 300: 301: 302: 303:
304: public function getGiftMessage($messageId=null)
305: {
306: $message = Mage::getModel('giftmessage/message');
307: if(!is_null($messageId)) {
308: $message->load($messageId);
309: }
310:
311: return $message;
312: }
313:
314: }
315: