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_Adminhtml
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: * Edit order giftmessage block
29: *
30: * @category Mage
31: * @package Mage_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Adminhtml_Block_Sales_Order_View_Giftmessage extends Mage_Adminhtml_Block_Widget
35: {
36: /**
37: * Entity for editing of gift message
38: *
39: * @var Mage_Eav_Model_Entity_Abstract
40: */
41: protected $_entity;
42:
43: /**
44: * Retrieve order model instance
45: *
46: * @return Mage_Sales_Model_Order
47: */
48: public function getOrder()
49: {
50: return Mage::registry('current_order');
51: }
52:
53: /**
54: * Giftmessage object
55: *
56: * @var Mage_GiftMessage_Model_Message
57: */
58: protected $_giftMessage;
59:
60: protected function _beforeToHtml()
61: {
62: if ($this->getParentBlock() && ($order = $this->getOrder())) {
63: $this->setEntity($order);
64: }
65: parent::_beforeToHtml();
66: }
67:
68: /**
69: * Prepares layout of block
70: *
71: * @return Mage_Adminhtml_Block_Sales_Order_View_Giftmessage
72: */
73: protected function _prepareLayout()
74: {
75: $this->setChild('save_button',
76: $this->getLayout()->createBlock('adminhtml/widget_button')
77: ->setData(array(
78: 'label' => Mage::helper('giftmessage')->__('Save Gift Message'),
79: 'class' => 'save'
80: ))
81: );
82:
83: return $this;
84: }
85:
86: /**
87: * Retrive save button html
88: *
89: * @return string
90: */
91: public function getSaveButtonHtml()
92: {
93: $this->getChild('save_button')->setOnclick(
94: 'giftMessagesController.saveGiftMessage(\''. $this->getHtmlId() .'\')'
95: );
96:
97: return $this->getChildHtml('save_button');
98: }
99:
100: /**
101: * Set entity for form
102: *
103: * @param Varien_Object $entity
104: * @return Mage_Adminhtml_Block_Sales_Order_View_Giftmessage
105: */
106: public function setEntity(Varien_Object $entity)
107: {
108: $this->_entity = $entity;
109: return $this;
110: }
111:
112: /**
113: * Retrive entity for form
114: *
115: * @return Varien_Object
116: */
117: public function getEntity()
118: {
119: if(is_null($this->_entity)) {
120: $this->setEntity(Mage::getModel('giftmessage/message')->getEntityModelByType('order'));
121: $this->getEntity()->load($this->getRequest()->getParam('entity'));
122: }
123: return $this->_entity;
124: }
125:
126: /**
127: * Retrive default value for giftmessage sender
128: *
129: * @return string
130: */
131: public function getDefaultSender()
132: {
133: if(!$this->getEntity()) {
134: return '';
135: }
136:
137: if($this->getEntity()->getOrder()) {
138: return $this->getEntity()->getOrder()->getCustomerName();
139: }
140:
141: return $this->getEntity()->getCustomerName();
142: }
143:
144: /**
145: * Retrive default value for giftmessage recipient
146: *
147: * @return string
148: */
149: public function getDefaultRecipient()
150: {
151: if(!$this->getEntity()) {
152: return '';
153: }
154:
155: if($this->getEntity()->getOrder()) {
156: if ($this->getEntity()->getOrder()->getShippingAddress()) {
157: return $this->getEntity()->getOrder()->getShippingAddress()->getName();
158: } else if ($this->getEntity()->getOrder()->getBillingAddress()) {
159: return $this->getEntity()->getOrder()->getBillingAddress()->getName();
160: }
161: }
162:
163: if ($this->getEntity()->getShippingAddress()) {
164: return $this->getEntity()->getShippingAddress()->getName();
165: } else if ($this->getEntity()->getBillingAddress()) {
166: return $this->getEntity()->getBillingAddress()->getName();
167: }
168:
169: return '';
170: }
171:
172: /**
173: * Retrive real name for field
174: *
175: * @param string $name
176: * @return string
177: */
178: public function getFieldName($name)
179: {
180: return 'giftmessage[' . $this->getEntity()->getId() . '][' . $name . ']';
181: }
182:
183: /**
184: * Retrive real html id for field
185: *
186: * @param string $name
187: * @return string
188: */
189: public function getFieldId($id)
190: {
191: return $this->getFieldIdPrefix() . $id;
192: }
193:
194: /**
195: * Retrive field html id prefix
196: *
197: * @return string
198: */
199: public function getFieldIdPrefix()
200: {
201: return 'giftmessage_order_' . $this->getEntity()->getId() . '_';
202: }
203:
204: /**
205: * Initialize gift message for entity
206: *
207: * @return Mage_Adminhtml_Block_Sales_Order_View_Giftmessage
208: */
209: protected function _initMessage()
210: {
211: $this->_giftMessage = $this->helper('giftmessage/message')->getGiftMessage(
212: $this->getEntity()->getGiftMessageId()
213: );
214:
215: // init default values for giftmessage form
216: if(!$this->getMessage()->getSender()) {
217: $this->getMessage()->setSender($this->getDefaultSender());
218: }
219: if(!$this->getMessage()->getRecipient()) {
220: $this->getMessage()->setRecipient($this->getDefaultRecipient());
221: }
222:
223: return $this;
224: }
225:
226: /**
227: * Retrive gift message for entity
228: *
229: * @return Mage_GiftMessage_Model_Message
230: */
231: public function getMessage()
232: {
233: if(is_null($this->_giftMessage)) {
234: $this->_initMessage();
235: }
236:
237: return $this->_giftMessage;
238: }
239:
240: public function getSaveUrl()
241: {
242: return $this->getUrl('*/sales_order_view_giftmessage/save',
243: array(
244: 'entity'=>$this->getEntity()->getId(),
245: 'type' =>'order',
246: 'reload' => 1
247: )
248: );
249: }
250:
251: /**
252: * Retrive block html id
253: *
254: * @return string
255: */
256: public function getHtmlId()
257: {
258: return substr($this->getFieldIdPrefix(), 0, -1);
259: }
260:
261: /**
262: * Indicates that block can display giftmessages form
263: *
264: * @return boolean
265: */
266: public function canDisplayGiftmessage()
267: {
268: return $this->helper('giftmessage/message')->getIsMessagesAvailable(
269: 'order', $this->getEntity(), $this->getEntity()->getStoreId()
270: );
271: }
272:
273: }
274: