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 inline edit form
30: *
31: * @category Mage
32: * @package Mage_GiftMessage
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_GiftMessage_Block_Message_Inline extends Mage_Core_Block_Template
36: {
37:
38: protected $_entity = null;
39: protected $_type = null;
40: protected $_giftMessage = null;
41:
42: protected function _construct()
43: {
44: parent::_construct();
45: $this->setTemplate('giftmessage/inline.phtml');
46: }
47:
48: /**
49: * Set entity
50: *
51: * @return mixed
52: */
53: public function setEntity($entity)
54: {
55: $this->_entity = $entity;
56: return $this;
57: }
58:
59: /**
60: * Get entity
61: *
62: * @return mixed
63: */
64: public function getEntity()
65: {
66: return $this->_entity;
67: }
68:
69: /**
70: * Set type
71: *
72: * @param string $type
73: * @return Mage_GiftMessage_Block_Message_Inline
74: */
75: public function setType($type)
76: {
77: $this->_type = $type;
78: return $this;
79: }
80:
81: /**
82: * Get type
83: *
84: * @return string
85: */
86: public function getType()
87: {
88: return $this->_type;
89: }
90:
91: /**
92: * Check if entity has gift message
93: *
94: * @return bool
95: */
96: public function hasGiftMessage()
97: {
98: return $this->getEntity()->getGiftMessageId() > 0;
99: }
100:
101: /**
102: * Init message
103: *
104: * @return Mage_GiftMessage_Block_Message_Inline
105: */
106: protected function _initMessage()
107: {
108: $this->_giftMessage = $this->helper('giftmessage/message')->getGiftMessage(
109: $this->getEntity()->getGiftMessageId()
110: );
111: return $this;
112: }
113:
114: /**
115: * Get default value for From field
116: *
117: * @return string
118: */
119: public function getDefaultFrom()
120: {
121: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
122: return Mage::getSingleton('customer/session')->getCustomer()->getName();
123: } else {
124: return $this->getEntity()->getBillingAddress()->getName();
125: }
126: }
127:
128: /**
129: * Get default value for To field
130: *
131: * @return string
132: */
133: public function getDefaultTo()
134: {
135: if ($this->getEntity()->getShippingAddress()) {
136: return $this->getEntity()->getShippingAddress()->getName();
137: } else {
138: return $this->getEntity()->getName();
139: }
140: }
141:
142: /**
143: * Retrieve message
144: *
145: * @param mixed $entity
146: * @return string
147: */
148: public function getMessage($entity=null)
149: {
150: if (is_null($this->_giftMessage)) {
151: $this->_initMessage();
152: }
153:
154: if ($entity) {
155: if (!$entity->getGiftMessage()) {
156: $entity->setGiftMessage(
157: $this->helper('giftmessage/message')->getGiftMessage($entity->getGiftMessageId())
158: );
159: }
160: return $entity->getGiftMessage();
161: }
162:
163: return $this->_giftMessage;
164: }
165:
166: /**
167: * Retrieve items
168: *
169: * @return array
170: */
171: public function getItems()
172: {
173: if (!$this->getData('items')) {
174: $items = array();
175:
176: $entityItems = $this->getEntity()->getAllItems();
177: Mage::dispatchEvent('gift_options_prepare_items', array('items' => $entityItems));
178:
179: foreach ($entityItems as $item) {
180: if ($item->getParentItem()) {
181: continue;
182: }
183: if ($this->isItemMessagesAvailable($item) || $item->getIsGiftOptionsAvailable()) {
184: $items[] = $item;
185: }
186: }
187: $this->setData('items', $items);
188: }
189: return $this->getData('items');
190: }
191:
192: /**
193: * Retrieve additional url
194: *
195: * @return bool
196: */
197: public function getAdditionalUrl()
198: {
199: return $this->getUrl('*/*/getAdditional');
200: }
201:
202: /**
203: * Check if items are available
204: *
205: * @return bool
206: */
207: public function isItemsAvailable()
208: {
209: return count($this->getItems()) > 0;
210: }
211:
212: /**
213: * Return items count
214: *
215: * @return int
216: */
217: public function countItems()
218: {
219: return count($this->getItems());
220: }
221:
222: /**
223: * Check if items has messages
224: *
225: * @return bool
226: */
227: public function getItemsHasMesssages()
228: {
229: foreach ($this->getItems() as $item) {
230: if ($item->getGiftMessageId()) {
231: return true;
232: }
233: }
234: return false;
235: }
236:
237: /**
238: * Check if entity has message
239: *
240: * @return bool
241: */
242: public function getEntityHasMessage()
243: {
244: return $this->getEntity()->getGiftMessageId() > 0;
245: }
246:
247: /**
248: * Return escaped value
249: *
250: * @param string $value
251: * @param string $defaultValue
252: * @return string
253: */
254: public function getEscaped($value, $defaultValue='')
255: {
256: return $this->htmlEscape(trim($value)!='' ? $value : $defaultValue);
257: }
258:
259: /**
260: * Check availability of giftmessages for specified entity
261: *
262: * @return bool
263: */
264: public function isMessagesAvailable()
265: {
266: return Mage::helper('giftmessage/message')->isMessagesAvailable('quote', $this->getEntity());
267: }
268:
269: /**
270: * Check availability of giftmessages for specified entity item
271: *
272: * @return bool
273: */
274: public function isItemMessagesAvailable($item)
275: {
276: $type = substr($this->getType(), 0, 5) == 'multi' ? 'address_item' : 'item';
277: return Mage::helper('giftmessage/message')->isMessagesAvailable($type, $item);
278: }
279: }
280: