00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Block_Sales_Order_Create_Giftmessage_Form extends Mage_Adminhtml_Block_Widget_Form
00036 {
00037
00038
00039
00040
00041
00042 protected $_entity;
00043
00044
00045
00046
00047
00048
00049 protected $_giftMessage;
00050
00051
00052
00053
00054
00055
00056
00057 public function setEntity(Varien_Object $entity)
00058 {
00059 $this->_entity = $entity;
00060 return $this;
00061 }
00062
00063
00064
00065
00066
00067
00068 public function getEntity()
00069 {
00070 return $this->_entity;
00071 }
00072
00073 protected function _getSession()
00074 {
00075 return Mage::getSingleton('adminhtml/session_quote');
00076 }
00077
00078
00079
00080
00081
00082
00083 public function getDefaultSender()
00084 {
00085 if(!$this->getEntity()) {
00086 return '';
00087 }
00088
00089 if($this->_getSession()->getCustomer()->getId()) {
00090 return $this->_getSession()->getCustomer()->getName();
00091 }
00092
00093 $object = $this->getEntity();
00094
00095 if ($this->getEntity()->getQuote()) {
00096 $object = $this->getEntity()->getQuote();
00097 }
00098
00099 return $object->getBillingAddress()->getName();
00100 }
00101
00102
00103
00104
00105
00106
00107 public function getDefaultRecipient()
00108 {
00109 if(!$this->getEntity()) {
00110 return '';
00111 }
00112
00113 if($this->getEntity()->getOrder()) {
00114 if ($this->getEntity()->getOrder()->getShippingAddress()) {
00115 return $this->getEntity()->getOrder()->getShippingAddress()->getName();
00116 } else if ($this->getEntity()->getOrder()->getBillingAddress()) {
00117 return $this->getEntity()->getOrder()->getBillingAddress()->getName();
00118 }
00119 }
00120
00121 if ($this->getEntity()->getShippingAddress()) {
00122 return $this->getEntity()->getShippingAddress()->getName();
00123 } else if ($this->getEntity()->getBillingAddress()) {
00124 return $this->getEntity()->getBillingAddress()->getName();
00125 }
00126
00127 return '';
00128 }
00129
00130
00131
00132
00133
00134
00135 public function _prepareForm()
00136 {
00137 $form = new Varien_Data_Form();
00138 $fieldset = $form->addFieldset('main', array('no_container'=>true));
00139
00140 $fieldset->addField('type','hidden',
00141 array(
00142 'name' => $this->_getFieldName('type'),
00143 )
00144 );
00145
00146 $form->setHtmlIdPrefix($this->_getFieldIdPrefix());
00147
00148 $fieldset->addField('sender','text',
00149 array(
00150 'name' => $this->_getFieldName('sender'),
00151 'label' => Mage::helper('sales')->__('From'),
00152 'class' => $this->getMessage()->getMessage() ? 'required-entry' : ''
00153 )
00154 );
00155 $fieldset->addField('recipient','text',
00156 array(
00157 'name' => $this->_getFieldName('recipient'),
00158 'label' => Mage::helper('sales')->__('To'),
00159 'class' => $this->getMessage()->getMessage() ? 'required-entry' : ''
00160 )
00161 );
00162
00163 $fieldset->addField('message', 'textarea',
00164 array(
00165 'name' => $this->_getFieldName('message'),
00166 'label' => Mage::helper('sales')->__('Message'),
00167 'rows' => '5',
00168 'cols' => '20',
00169 'onchange' => 'toogleRequired(\'' . $this->_getFieldId('message')
00170 . '\', [\'' . $this->_getFieldId('sender')
00171 . '\', \'' . $this->_getFieldId('recipient') . '\']);'
00172 )
00173 );
00174
00175 // Set default sender and recipient from billing and shipping adresses
00176 if(!$this->getMessage()->getSender()) {
00177 $this->getMessage()->setSender($this->getDefaultSender());
00178 }
00179
00180 if(!$this->getMessage()->getRecipient()) {
00181 $this->getMessage()->setRecipient($this->getDefaultRecipient());
00182 }
00183
00184 $this->getMessage()->setType($this->getEntityType());
00185
00186 // Overriden default data with edited when block reloads througth Ajax
00187 $this->_applyPostData();
00188
00189 $form->setValues($this->getMessage()->getData());
00190
00191 $this->setForm($form);
00192 return $this;
00193 }
00194
00195
00196
00197
00198
00199
00200 protected function _initMessage()
00201 {
00202 $this->_giftMessage = $this->helper('giftmessage/message')->getGiftMessage(
00203 $this->getEntity()->getGiftMessageId()
00204 );
00205 return $this;
00206 }
00207
00208
00209
00210
00211
00212
00213 public function getMessage()
00214 {
00215 if(is_null($this->_giftMessage)) {
00216 $this->_initMessage();
00217 }
00218
00219 return $this->_giftMessage;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 protected function _getFieldName($name)
00229 {
00230 return 'giftmessage[' . $this->getEntity()->getId() . '][' . $name . ']';
00231 }
00232
00233
00234
00235
00236
00237
00238
00239 protected function _getFieldId($id)
00240 {
00241 return $this->_getFieldIdPrefix() . $id;
00242 }
00243
00244
00245
00246
00247
00248
00249 protected function _getFieldIdPrefix()
00250 {
00251 return 'giftmessage_' . $this->getEntity()->getId() . '_';
00252 }
00253
00254
00255
00256
00257
00258
00259 protected function _applyPostData()
00260 {
00261 if(is_array($giftmessages = $this->getRequest()->getParam('giftmessage'))
00262 && isset($giftmessages[$this->getEntity()->getId()])) {
00263 $this->getMessage()->addData($giftmessages[$this->getEntity()->getId()]);
00264 }
00265
00266 return $this;
00267 }
00268
00269 }