PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/magento/module-gift-message/Block/Message/Inline.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 370 lines | 162 code | 36 blank | 172 comment | 16 complexity | 501a175238e1f249721003fc2b636267 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Block\Message;
  7. use Magento\Customer\Model\Context;
  8. use Magento\GiftMessage\Model\Message;
  9. /**
  10. * Gift message inline edit form
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Inline extends \Magento\Framework\View\Element\Template
  15. {
  16. /**
  17. * @var mixed
  18. */
  19. protected $_entity = null;
  20. /**
  21. * @var string|null
  22. */
  23. protected $_type = null;
  24. /**
  25. * @var Message|null
  26. */
  27. protected $_giftMessage = null;
  28. /**
  29. * @var string
  30. */
  31. protected $_template = 'inline.phtml';
  32. /**
  33. * Gift message message
  34. *
  35. * @var \Magento\GiftMessage\Helper\Message|null
  36. */
  37. protected $_giftMessageMessage = null;
  38. /**
  39. * @var \Magento\Customer\Model\Session
  40. */
  41. protected $_customerSession;
  42. /**
  43. * @var \Magento\Catalog\Block\Product\ImageBuilder
  44. */
  45. protected $imageBuilder;
  46. /**
  47. * @var \Magento\Framework\App\Http\Context
  48. */
  49. protected $httpContext;
  50. /**
  51. * Checkout type. 'onepage_checkout' and 'multishipping_address' are standard types
  52. *
  53. * @var string
  54. */
  55. protected $checkoutType;
  56. /**
  57. * @param \Magento\Framework\View\Element\Template\Context $context
  58. * @param \Magento\Customer\Model\Session $customerSession
  59. * @param \Magento\GiftMessage\Helper\Message $giftMessageMessage
  60. * @param \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder
  61. * @param \Magento\Framework\App\Http\Context $httpContext
  62. * @param array $data
  63. */
  64. public function __construct(
  65. \Magento\Framework\View\Element\Template\Context $context,
  66. \Magento\Customer\Model\Session $customerSession,
  67. \Magento\GiftMessage\Helper\Message $giftMessageMessage,
  68. \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
  69. \Magento\Framework\App\Http\Context $httpContext,
  70. array $data = []
  71. ) {
  72. $this->imageBuilder = $imageBuilder;
  73. $this->_giftMessageMessage = $giftMessageMessage;
  74. $this->_customerSession = $customerSession;
  75. parent::__construct($context, $data);
  76. $this->_isScopePrivate = true;
  77. $this->httpContext = $httpContext;
  78. }
  79. /**
  80. * Set entity
  81. *
  82. * @param mixed $entity
  83. * @return $this
  84. * @codeCoverageIgnore
  85. */
  86. public function setEntity($entity)
  87. {
  88. $this->_entity = $entity;
  89. return $this;
  90. }
  91. /**
  92. * Get entity
  93. *
  94. * @return mixed
  95. * @codeCoverageIgnore
  96. */
  97. public function getEntity()
  98. {
  99. return $this->_entity;
  100. }
  101. /**
  102. * Set type
  103. *
  104. * @param string $type
  105. * @return $this
  106. * @codeCoverageIgnore
  107. */
  108. public function setType($type)
  109. {
  110. $this->_type = $type;
  111. return $this;
  112. }
  113. /**
  114. * Get type
  115. *
  116. * @return string
  117. * @codeCoverageIgnore
  118. */
  119. public function getType()
  120. {
  121. return $this->_type;
  122. }
  123. /**
  124. * Define checkout type
  125. *
  126. * @param $type string
  127. * @return $this
  128. * @codeCoverageIgnore
  129. */
  130. public function setCheckoutType($type)
  131. {
  132. $this->checkoutType = $type;
  133. return $this;
  134. }
  135. /**
  136. * Return checkout type. Typical values are 'onepage_checkout' and 'multishipping_address'
  137. *
  138. * @return string|null
  139. * @codeCoverageIgnore
  140. */
  141. public function getCheckoutType()
  142. {
  143. return $this->checkoutType;
  144. }
  145. /**
  146. * Check if entity has gift message
  147. *
  148. * @return bool
  149. */
  150. public function hasGiftMessage()
  151. {
  152. return $this->getEntity()->getGiftMessageId() > 0;
  153. }
  154. /**
  155. * Init message
  156. *
  157. * @return $this
  158. */
  159. protected function _initMessage()
  160. {
  161. $this->_giftMessage = $this->_giftMessageMessage->getGiftMessage($this->getEntity()->getGiftMessageId());
  162. return $this;
  163. }
  164. /**
  165. * Get default value for From field
  166. *
  167. * @return string
  168. */
  169. public function getDefaultFrom()
  170. {
  171. if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
  172. return $this->_customerSession->getCustomer()->getName();
  173. } else {
  174. return $this->getEntity()->getBillingAddress()->getName();
  175. }
  176. }
  177. /**
  178. * Get default value for To field
  179. *
  180. * @return string
  181. */
  182. public function getDefaultTo()
  183. {
  184. if ($this->getEntity()->getShippingAddress()) {
  185. return $this->getEntity()->getShippingAddress()->getName();
  186. } else {
  187. return $this->getEntity()->getName();
  188. }
  189. }
  190. /**
  191. * Retrieve message
  192. *
  193. * @param mixed $entity
  194. * @return string
  195. */
  196. public function getMessage($entity = null)
  197. {
  198. if ($this->_giftMessage === null) {
  199. $this->_initMessage();
  200. }
  201. if ($entity) {
  202. if (!$entity->getGiftMessage()) {
  203. $entity->setGiftMessage($this->_giftMessageMessage->getGiftMessage($entity->getGiftMessageId()));
  204. }
  205. return $entity->getGiftMessage();
  206. }
  207. return $this->_giftMessage;
  208. }
  209. /**
  210. * Retrieve items
  211. *
  212. * @return array
  213. */
  214. public function getItems()
  215. {
  216. if (!$this->getData('items')) {
  217. $items = [];
  218. $entityItems = $this->getEntity()->getAllItems();
  219. $this->_eventManager->dispatch('gift_options_prepare_items', ['items' => $entityItems]);
  220. foreach ($entityItems as $item) {
  221. if ($item->getParentItem()) {
  222. continue;
  223. }
  224. if ($this->isItemMessagesAvailable($item) || $item->getIsGiftOptionsAvailable()) {
  225. $items[] = $item;
  226. }
  227. }
  228. $this->setData('items', $items);
  229. }
  230. return $this->getData('items');
  231. }
  232. /**
  233. * Check if gift messages for separate items are allowed
  234. *
  235. * @return bool
  236. */
  237. public function isItemsAvailable()
  238. {
  239. return count($this->getItems()) > 0;
  240. }
  241. /**
  242. * Return items count
  243. *
  244. * @return int
  245. */
  246. public function countItems()
  247. {
  248. return count($this->getItems());
  249. }
  250. /**
  251. * Check if items has messages
  252. *
  253. * @return bool
  254. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  255. */
  256. public function getItemsHasMesssages()
  257. {
  258. foreach ($this->getItems() as $item) {
  259. if ($item->getGiftMessageId()) {
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. /**
  266. * Check if entity has message
  267. *
  268. * @return bool
  269. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  270. */
  271. public function getEntityHasMessage()
  272. {
  273. return $this->getEntity()->getGiftMessageId() > 0;
  274. }
  275. /**
  276. * Return escaped value
  277. *
  278. * @param string $value
  279. * @param string $defaultValue
  280. * @return string
  281. */
  282. public function getEscaped($value, $defaultValue = '')
  283. {
  284. return $this->escapeHtml(trim($value) != '' ? $value : $defaultValue);
  285. }
  286. /**
  287. * Check availability of giftmessages on order level
  288. *
  289. * @return bool
  290. */
  291. public function isMessagesAvailable()
  292. {
  293. return $this->_giftMessageMessage->isMessagesAllowed('quote', $this->getEntity());
  294. }
  295. /**
  296. * Check availability of giftmessages for specified entity item
  297. *
  298. * @param \Magento\Framework\DataObject $item
  299. * @return bool
  300. */
  301. public function isItemMessagesAvailable($item)
  302. {
  303. $type = substr($this->getType(), 0, 5) == 'multi' ? 'address_item' : 'item';
  304. return $this->_giftMessageMessage->isMessagesAllowed($type, $item);
  305. }
  306. /**
  307. * Render HTML code referring to config settings
  308. *
  309. * @return string
  310. */
  311. protected function _toHtml()
  312. {
  313. // render HTML when messages are allowed for order or for items only
  314. if ($this->isItemsAvailable() || $this->isMessagesAvailable()) {
  315. return parent::_toHtml();
  316. }
  317. return '';
  318. }
  319. /**
  320. * Retrieve product image
  321. *
  322. * @param \Magento\Catalog\Model\Product $product
  323. * @param string $imageId
  324. * @param array $attributes
  325. * @return \Magento\Catalog\Block\Product\Image
  326. */
  327. public function getImage($product, $imageId, $attributes = [])
  328. {
  329. return $this->imageBuilder->setProduct($product)
  330. ->setImageId($imageId)
  331. ->setAttributes($attributes)
  332. ->create();
  333. }
  334. }