Items.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Block\Adminhtml\Sales\Order\View;
  7. /**
  8. * Gift message adminhtml sales order view items
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Items extends \Magento\Backend\Block\Template
  15. {
  16. /**
  17. * Gift message array
  18. *
  19. * @var array
  20. */
  21. protected $_giftMessage = [];
  22. /**
  23. * @var \Magento\GiftMessage\Helper\Message
  24. */
  25. protected $_messageHelper;
  26. /**
  27. * @param \Magento\Backend\Block\Template\Context $context
  28. * @param \Magento\GiftMessage\Helper\Message $messageHelper
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Backend\Block\Template\Context $context,
  33. \Magento\GiftMessage\Helper\Message $messageHelper,
  34. array $data = []
  35. ) {
  36. $this->_messageHelper = $messageHelper;
  37. parent::__construct($context, $data);
  38. }
  39. /**
  40. * Get Order Item
  41. *
  42. * @return \Magento\Sales\Model\Order\Item
  43. * @codeCoverageIgnore
  44. */
  45. public function getItem()
  46. {
  47. return $this->getParentBlock()->getItem();
  48. }
  49. /**
  50. * Retrieve default value for giftmessage sender
  51. *
  52. * @return string
  53. */
  54. public function getDefaultSender()
  55. {
  56. if (!$this->getItem()) {
  57. return '';
  58. }
  59. if ($this->getItem()->getOrder()) {
  60. return $this->getItem()->getOrder()->getBillingAddress()->getName();
  61. }
  62. return $this->getItem()->getBillingAddress()->getName();
  63. }
  64. /**
  65. * Retrieve default value for giftmessage recipient
  66. *
  67. * @return string
  68. */
  69. public function getDefaultRecipient()
  70. {
  71. if (!$this->getItem()) {
  72. return '';
  73. }
  74. if ($this->getItem()->getOrder()) {
  75. if ($this->getItem()->getOrder()->getShippingAddress()) {
  76. return $this->getItem()->getOrder()->getShippingAddress()->getName();
  77. } elseif ($this->getItem()->getOrder()->getBillingAddress()) {
  78. return $this->getItem()->getOrder()->getBillingAddress()->getName();
  79. }
  80. }
  81. if ($this->getItem()->getShippingAddress()) {
  82. return $this->getItem()->getShippingAddress()->getName();
  83. } elseif ($this->getItem()->getBillingAddress()) {
  84. return $this->getItem()->getBillingAddress()->getName();
  85. }
  86. return '';
  87. }
  88. /**
  89. * Retrieve real name for field
  90. *
  91. * @param string $name
  92. * @return string
  93. * @codeCoverageIgnore
  94. */
  95. public function getFieldName($name)
  96. {
  97. return 'giftmessage[' . $this->getItem()->getId() . '][' . $name . ']';
  98. }
  99. /**
  100. * Retrieve real html id for field
  101. *
  102. * @param string $id
  103. * @return string
  104. * @codeCoverageIgnore
  105. */
  106. public function getFieldId($id)
  107. {
  108. return $this->getFieldIdPrefix() . $id;
  109. }
  110. /**
  111. * Retrieve field html id prefix
  112. *
  113. * @return string
  114. * @codeCoverageIgnore
  115. */
  116. public function getFieldIdPrefix()
  117. {
  118. return 'giftmessage_' . $this->getItem()->getId() . '_';
  119. }
  120. /**
  121. * Initialize gift message for entity
  122. *
  123. * @return $this
  124. */
  125. protected function _initMessage()
  126. {
  127. $this->_giftMessage[$this->getItem()->getGiftMessageId()] = $this->_messageHelper->getGiftMessage(
  128. $this->getItem()->getGiftMessageId()
  129. );
  130. // init default values for giftmessage form
  131. if (!$this->getMessage()->getSender()) {
  132. $this->getMessage()->setSender($this->getDefaultSender());
  133. }
  134. if (!$this->getMessage()->getRecipient()) {
  135. $this->getMessage()->setRecipient($this->getDefaultRecipient());
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Retrieve gift message for entity
  141. *
  142. * @return \Magento\GiftMessage\Model\Message
  143. */
  144. public function getMessage()
  145. {
  146. if (!isset($this->_giftMessage[$this->getItem()->getGiftMessageId()])) {
  147. $this->_initMessage();
  148. }
  149. return $this->_giftMessage[$this->getItem()->getGiftMessageId()];
  150. }
  151. /**
  152. * Retrieve save url
  153. *
  154. * @return array
  155. * @codeCoverageIgnore
  156. */
  157. public function getSaveUrl()
  158. {
  159. return $this->getUrl(
  160. 'sales/order_view_giftmessage/save',
  161. ['entity' => $this->getItem()->getId(), 'type' => 'order_item', 'reload' => true]
  162. );
  163. }
  164. /**
  165. * Retrieve block html id
  166. *
  167. * @return string
  168. * @codeCoverageIgnore
  169. */
  170. public function getHtmlId()
  171. {
  172. return substr($this->getFieldIdPrefix(), 0, -1);
  173. }
  174. /**
  175. * Indicates that block can display giftmessages form
  176. *
  177. * @return boolean
  178. */
  179. public function canDisplayGiftmessage()
  180. {
  181. return $this->getItem()->getGiftMessageId();
  182. }
  183. /**
  184. * Retrieve gift message sender
  185. *
  186. * @return string
  187. */
  188. public function getSender()
  189. {
  190. return $this->escapeHtml($this->getMessage()->getSender());
  191. }
  192. /**
  193. * Retrieve gift message recipient
  194. *
  195. * @return string
  196. */
  197. public function getRecipient()
  198. {
  199. return $this->escapeHtml($this->getMessage()->getRecipient());
  200. }
  201. /**
  202. * Retrieve gift message text
  203. *
  204. * @return string
  205. */
  206. public function getMessageText()
  207. {
  208. return $this->escapeHtml($this->getMessage()->getMessage());
  209. }
  210. }