Giftmessage.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Adminhtml\Order\View;
  7. /**
  8. * Edit order giftmessage block
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Giftmessage extends \Magento\Backend\Block\Widget
  15. {
  16. /**
  17. * Entity for editing of gift message
  18. *
  19. * @var \Magento\Eav\Model\Entity\AbstractEntity
  20. */
  21. protected $_entity;
  22. /**
  23. * Core registry
  24. *
  25. * @var \Magento\Framework\Registry
  26. */
  27. protected $_coreRegistry = null;
  28. /**
  29. * Message factory
  30. *
  31. * @var \Magento\GiftMessage\Model\MessageFactory
  32. */
  33. protected $_messageFactory;
  34. /**
  35. * Message helper
  36. *
  37. * @var \Magento\GiftMessage\Helper\Message
  38. */
  39. protected $_messageHelper;
  40. /**
  41. * @param \Magento\Backend\Block\Template\Context $context
  42. * @param \Magento\GiftMessage\Model\MessageFactory $messageFactory
  43. * @param \Magento\Framework\Registry $registry
  44. * @param \Magento\GiftMessage\Helper\Message $messageHelper
  45. * @param array $data
  46. */
  47. public function __construct(
  48. \Magento\Backend\Block\Template\Context $context,
  49. \Magento\GiftMessage\Model\MessageFactory $messageFactory,
  50. \Magento\Framework\Registry $registry,
  51. \Magento\GiftMessage\Helper\Message $messageHelper,
  52. array $data = []
  53. ) {
  54. $this->_messageHelper = $messageHelper;
  55. $this->_coreRegistry = $registry;
  56. $this->_messageFactory = $messageFactory;
  57. parent::__construct($context, $data);
  58. }
  59. /**
  60. * Retrieve order model instance
  61. *
  62. * @return \Magento\Sales\Model\Order
  63. */
  64. public function getOrder()
  65. {
  66. return $this->_coreRegistry->registry('current_order');
  67. }
  68. /**
  69. * Giftmessage object
  70. *
  71. * @var \Magento\GiftMessage\Model\Message
  72. */
  73. protected $_giftMessage;
  74. /**
  75. * Before rendering html, but after trying to load cache
  76. *
  77. * @return void
  78. */
  79. protected function _beforeToHtml()
  80. {
  81. if ($this->getParentBlock() && ($order = $this->getOrder())) {
  82. $this->setEntity($order);
  83. }
  84. parent::_beforeToHtml();
  85. }
  86. /**
  87. * Prepares layout of block
  88. *
  89. * @return $this
  90. */
  91. protected function _prepareLayout()
  92. {
  93. $this->addChild(
  94. 'save_button',
  95. \Magento\Backend\Block\Widget\Button::class,
  96. ['label' => __('Save Gift Message'), 'class' => 'save']
  97. );
  98. return $this;
  99. }
  100. /**
  101. * Retrieve save button html
  102. *
  103. * @return string
  104. */
  105. public function getSaveButtonHtml()
  106. {
  107. $this->getChildBlock(
  108. 'save_button'
  109. )->setOnclick(
  110. 'giftMessagesController.saveGiftMessage(\'' . $this->getHtmlId() . '\')'
  111. );
  112. return $this->getChildHtml('save_button');
  113. }
  114. /**
  115. * Set entity for form
  116. *
  117. * @param \Magento\Framework\DataObject $entity
  118. * @return $this
  119. */
  120. public function setEntity(\Magento\Framework\DataObject $entity)
  121. {
  122. $this->_entity = $entity;
  123. return $this;
  124. }
  125. /**
  126. * Retrieve entity for form
  127. *
  128. * @return \Magento\Framework\DataObject
  129. */
  130. public function getEntity()
  131. {
  132. if ($this->_entity === null) {
  133. $this->setEntity($this->_messageFactory->create()->getEntityModelByType('order'));
  134. $this->getEntity()->load($this->getRequest()->getParam('entity'));
  135. }
  136. return $this->_entity;
  137. }
  138. /**
  139. * Retrieve default value for giftmessage sender
  140. *
  141. * @return string
  142. */
  143. public function getDefaultSender()
  144. {
  145. if (!$this->getEntity()) {
  146. return '';
  147. }
  148. if ($this->getEntity()->getOrder()) {
  149. return $this->getEntity()->getOrder()->getCustomerName();
  150. }
  151. return $this->getEntity()->getCustomerName();
  152. }
  153. /**
  154. * Retrieve default value for giftmessage recipient
  155. *
  156. * @return string
  157. */
  158. public function getDefaultRecipient()
  159. {
  160. if (!$this->getEntity()) {
  161. return '';
  162. }
  163. if ($this->getEntity()->getOrder()) {
  164. if ($this->getEntity()->getOrder()->getShippingAddress()) {
  165. return $this->getEntity()->getOrder()->getShippingAddress()->getName();
  166. } elseif ($this->getEntity()->getOrder()->getBillingAddress()) {
  167. return $this->getEntity()->getOrder()->getBillingAddress()->getName();
  168. }
  169. }
  170. if ($this->getEntity()->getShippingAddress()) {
  171. return $this->getEntity()->getShippingAddress()->getName();
  172. } elseif ($this->getEntity()->getBillingAddress()) {
  173. return $this->getEntity()->getBillingAddress()->getName();
  174. }
  175. return '';
  176. }
  177. /**
  178. * Retrieve real name for field
  179. *
  180. * @param string $name
  181. * @return string
  182. */
  183. public function getFieldName($name)
  184. {
  185. return 'giftmessage[' . $this->getEntity()->getId() . '][' . $name . ']';
  186. }
  187. /**
  188. * Retrieve real html id for field
  189. *
  190. * @param string $id
  191. * @return string
  192. */
  193. public function getFieldId($id)
  194. {
  195. return $this->getFieldIdPrefix() . $id;
  196. }
  197. /**
  198. * Retrieve field html id prefix
  199. *
  200. * @return string
  201. */
  202. public function getFieldIdPrefix()
  203. {
  204. return 'giftmessage_order_' . $this->getEntity()->getId() . '_';
  205. }
  206. /**
  207. * Initialize gift message for entity
  208. *
  209. * @return $this
  210. */
  211. protected function _initMessage()
  212. {
  213. $this->_giftMessage = $this->_messageHelper->getGiftMessage($this->getEntity()->getGiftMessageId());
  214. // init default values for giftmessage form
  215. if (!$this->getMessage()->getSender()) {
  216. $this->getMessage()->setSender($this->getDefaultSender());
  217. }
  218. if (!$this->getMessage()->getRecipient()) {
  219. $this->getMessage()->setRecipient($this->getDefaultRecipient());
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Retrieve gift message for entity
  225. *
  226. * @return \Magento\GiftMessage\Model\Message
  227. */
  228. public function getMessage()
  229. {
  230. if ($this->_giftMessage === null) {
  231. $this->_initMessage();
  232. }
  233. return $this->_giftMessage;
  234. }
  235. /**
  236. * Get save url
  237. *
  238. * @return string
  239. */
  240. public function getSaveUrl()
  241. {
  242. return $this->getUrl(
  243. 'sales/order_view_giftmessage/save',
  244. ['entity' => $this->getEntity()->getId(), 'type' => 'order', 'reload' => 1]
  245. );
  246. }
  247. /**
  248. * Retrieve block html id
  249. *
  250. * @return string
  251. */
  252. public function getHtmlId()
  253. {
  254. return substr($this->getFieldIdPrefix(), 0, -1);
  255. }
  256. /**
  257. * Indicates that block can display giftmessages form
  258. *
  259. * @return bool
  260. */
  261. public function canDisplayGiftmessage()
  262. {
  263. return $this->_messageHelper->isMessagesAllowed(
  264. 'order',
  265. $this->getEntity(),
  266. $this->getEntity()->getStoreId()
  267. );
  268. }
  269. }