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_Block_Sales_Order_Create_Giftmessage_Form extends Mage_Adminhtml_Block_Widget_Form
36: {
37: 38: 39: 40: 41:
42: protected $_entity;
43:
44: 45: 46: 47: 48:
49: protected $_giftMessage;
50:
51: 52: 53: 54: 55: 56:
57: public function setEntity(Varien_Object $entity)
58: {
59: $this->_entity = $entity;
60: return $this;
61: }
62:
63: 64: 65: 66: 67:
68: public function getEntity()
69: {
70: return $this->_entity;
71: }
72:
73: protected function _getSession()
74: {
75: return Mage::getSingleton('adminhtml/session_quote');
76: }
77:
78: 79: 80: 81: 82:
83: public function getDefaultSender()
84: {
85: if(!$this->getEntity()) {
86: return '';
87: }
88:
89: if($this->_getSession()->getCustomer()->getId()) {
90: return $this->_getSession()->getCustomer()->getName();
91: }
92:
93: $object = $this->getEntity();
94:
95: if ($this->getEntity()->getQuote()) {
96: $object = $this->getEntity()->getQuote();
97: }
98:
99: return $object->getBillingAddress()->getName();
100: }
101:
102: 103: 104: 105: 106:
107: public function getDefaultRecipient()
108: {
109: if(!$this->getEntity()) {
110: return '';
111: }
112:
113: $object = $this->getEntity();
114:
115: if ($this->getEntity()->getOrder()) {
116: $object = $this->getEntity()->getOrder();
117: }
118: else if ($this->getEntity()->getQuote()){
119: $object = $this->getEntity()->getQuote();
120: }
121:
122: if ($object->getShippingAddress()) {
123: return $object->getShippingAddress()->getName();
124: }
125: else if ($object->getBillingAddress()) {
126: return $object->getBillingAddress()->getName();
127: }
128:
129: return '';
130: }
131:
132: 133: 134: 135: 136:
137: public function _prepareForm()
138: {
139: $form = new Varien_Data_Form();
140: $fieldset = $form->addFieldset('main', array('no_container'=>true));
141:
142: $fieldset->addField('type','hidden',
143: array(
144: 'name' => $this->_getFieldName('type'),
145: )
146: );
147:
148: $form->setHtmlIdPrefix($this->_getFieldIdPrefix());
149:
150: if ($this->getEntityType() == 'item') {
151: $this->_prepareHiddenFields($fieldset);
152: } else {
153: $this->_prepareVisibleFields($fieldset);
154: }
155:
156:
157: if(!$this->getMessage()->getSender()) {
158: $this->getMessage()->setSender($this->getDefaultSender());
159: }
160:
161: if(!$this->getMessage()->getRecipient()) {
162: $this->getMessage()->setRecipient($this->getDefaultRecipient());
163: }
164:
165: $this->getMessage()->setType($this->getEntityType());
166:
167:
168: $this->_applyPostData();
169:
170: $form->setValues($this->getMessage()->getData());
171:
172: $this->setForm($form);
173: return $this;
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183:
184: protected function _prepareHiddenFields(Varien_Data_Form_Element_Fieldset $fieldset)
185: {
186: $fieldset->addField('sender', 'hidden',
187: array(
188: 'name' => $this->_getFieldName('sender')
189: )
190: );
191: $fieldset->addField('recipient', 'hidden',
192: array(
193: 'name' => $this->_getFieldName('recipient')
194: )
195: );
196:
197: $fieldset->addField('message', 'hidden',
198: array(
199: 'name' => $this->_getFieldName('message')
200: )
201: );
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212:
213: protected function _prepareVisibleFields(Varien_Data_Form_Element_Fieldset $fieldset)
214: {
215: $fieldset->addField('sender', 'text',
216: array(
217: 'name' => $this->_getFieldName('sender'),
218: 'label' => Mage::helper('sales')->__('From'),
219: 'required' => $this->getMessage()->getMessage() ? true : false
220: )
221: );
222: $fieldset->addField('recipient', 'text',
223: array(
224: 'name' => $this->_getFieldName('recipient'),
225: 'label' => Mage::helper('sales')->__('To'),
226: 'required' => $this->getMessage()->getMessage() ? true : false
227: )
228: );
229:
230: $fieldset->addField('message', 'textarea',
231: array(
232: 'name' => $this->_getFieldName('message'),
233: 'label' => Mage::helper('sales')->__('Message'),
234: 'rows' => '5',
235: 'cols' => '20',
236: )
237: );
238: return $this;
239: }
240:
241: 242: 243: 244: 245:
246: protected function _initMessage()
247: {
248: $this->_giftMessage = $this->helper('giftmessage/message')->getGiftMessage(
249: $this->getEntity()->getGiftMessageId()
250: );
251: return $this;
252: }
253:
254: 255: 256: 257: 258:
259: public function getMessage()
260: {
261: if(is_null($this->_giftMessage)) {
262: $this->_initMessage();
263: }
264:
265: return $this->_giftMessage;
266: }
267:
268: 269: 270: 271: 272: 273:
274: protected function _getFieldName($name)
275: {
276: return 'giftmessage[' . $this->getEntity()->getId() . '][' . $name . ']';
277: }
278:
279: 280: 281: 282: 283: 284:
285: protected function _getFieldId($id)
286: {
287: return $this->_getFieldIdPrefix() . $id;
288: }
289:
290: 291: 292: 293: 294:
295: protected function _getFieldIdPrefix()
296: {
297: return 'giftmessage_' . $this->getEntity()->getId() . '_';
298: }
299:
300: 301: 302: 303: 304:
305: protected function _applyPostData()
306: {
307: if(is_array($giftmessages = $this->getRequest()->getParam('giftmessage'))
308: && isset($giftmessages[$this->getEntity()->getId()])) {
309: $this->getMessage()->addData($giftmessages[$this->getEntity()->getId()]);
310: }
311:
312: return $this;
313: }
314:
315: }
316: