Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Inline.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
21  protected $_entity = null;
22 
26  protected $_type = null;
27 
31  protected $_giftMessage = null;
32 
36  protected $_template = 'Magento_GiftMessage::inline.phtml';
37 
43  protected $_giftMessageMessage = null;
44 
48  protected $_customerSession;
49 
53  protected $imageBuilder;
54 
58  protected $httpContext;
59 
65  protected $checkoutType;
66 
75  public function __construct(
76  \Magento\Framework\View\Element\Template\Context $context,
77  \Magento\Customer\Model\Session $customerSession,
78  \Magento\GiftMessage\Helper\Message $giftMessageMessage,
79  \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
80  \Magento\Framework\App\Http\Context $httpContext,
81  array $data = []
82  ) {
83  $this->imageBuilder = $imageBuilder;
84  $this->_giftMessageMessage = $giftMessageMessage;
85  $this->_customerSession = $customerSession;
86  parent::__construct($context, $data);
87  $this->_isScopePrivate = true;
88  $this->httpContext = $httpContext;
89  }
90 
98  public function setEntity($entity)
99  {
100  $this->_entity = $entity;
101  return $this;
102  }
103 
110  public function getEntity()
111  {
112  return $this->_entity;
113  }
114 
122  public function setType($type)
123  {
124  $this->_type = $type;
125  return $this;
126  }
127 
134  public function getType()
135  {
136  return $this->_type;
137  }
138 
146  public function setCheckoutType($type)
147  {
148  $this->checkoutType = $type;
149  return $this;
150  }
151 
158  public function getCheckoutType()
159  {
160  return $this->checkoutType;
161  }
162 
168  public function hasGiftMessage()
169  {
170  return $this->getEntity()->getGiftMessageId() > 0;
171  }
172 
178  protected function _initMessage()
179  {
180  $this->_giftMessage = $this->_giftMessageMessage->getGiftMessage($this->getEntity()->getGiftMessageId());
181  return $this;
182  }
183 
189  public function getDefaultFrom()
190  {
191  if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
192  return $this->_customerSession->getCustomer()->getName();
193  } else {
194  return $this->getEntity()->getBillingAddress()->getName();
195  }
196  }
197 
203  public function getDefaultTo()
204  {
205  if ($this->getEntity()->getShippingAddress()) {
206  return $this->getEntity()->getShippingAddress()->getName();
207  } else {
208  return $this->getEntity()->getName();
209  }
210  }
211 
218  public function getMessage($entity = null)
219  {
220  if ($this->_giftMessage === null) {
221  $this->_initMessage();
222  }
223 
224  if ($entity) {
225  if (!$entity->getGiftMessage()) {
226  $entity->setGiftMessage($this->_giftMessageMessage->getGiftMessage($entity->getGiftMessageId()));
227  }
228  return $entity->getGiftMessage();
229  }
230 
231  return $this->_giftMessage;
232  }
233 
239  public function getItems()
240  {
241  if (!$this->getData('items')) {
242  $items = [];
243 
244  $entityItems = $this->getEntity()->getAllItems();
245  $this->_eventManager->dispatch('gift_options_prepare_items', ['items' => $entityItems]);
246 
247  foreach ($entityItems as $item) {
248  if ($item->getParentItem()) {
249  continue;
250  }
251  if ($this->isItemMessagesAvailable($item) || $item->getIsGiftOptionsAvailable()) {
252  $items[] = $item;
253  }
254  }
255  $this->setData('items', $items);
256  }
257  return $this->getData('items');
258  }
259 
265  public function isItemsAvailable()
266  {
267  return count($this->getItems()) > 0;
268  }
269 
275  public function countItems()
276  {
277  return count($this->getItems());
278  }
279 
284  public function getItemsHasMesssages()
285  {
286  return $this->getItemsHasMessages();
287  }
288 
295  public function getItemsHasMessages()
296  {
297  foreach ($this->getItems() as $item) {
298  if ($item->getGiftMessageId()) {
299  return true;
300  }
301  }
302  return false;
303  }
304 
311  public function getEntityHasMessage()
312  {
313  return $this->getEntity()->getGiftMessageId() > 0;
314  }
315 
323  public function getEscaped($value, $defaultValue = '')
324  {
325  return $this->escapeHtml(trim($value) != '' ? $value : $defaultValue);
326  }
327 
333  public function isMessagesAvailable()
334  {
335  return $this->_giftMessageMessage->isMessagesAllowed('quote', $this->getEntity());
336  }
337 
345  {
346  $type = substr($this->getType(), 0, 5) == 'multi' ? 'address_item' : 'item';
347  return $this->_giftMessageMessage->isMessagesAllowed($type, $item);
348  }
349 
355  protected function _toHtml()
356  {
357  // render HTML when messages are allowed for order or for items only
358  if ($this->isItemsAvailable() || $this->isMessagesAvailable()) {
359  return parent::_toHtml();
360  }
361  return '';
362  }
363 
372  public function getImage($product, $imageId, $attributes = [])
373  {
374  return $this->imageBuilder->create($product, $imageId, $attributes);
375  }
376 }
getData($key='', $index=null)
Definition: DataObject.php:119
getImage($product, $imageId, $attributes=[])
Definition: Inline.php:372
getEscaped($value, $defaultValue='')
Definition: Inline.php:323
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\GiftMessage\Helper\Message $giftMessageMessage, \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder, \Magento\Framework\App\Http\Context $httpContext, array $data=[])
Definition: Inline.php:75
$entity
Definition: element.phtml:22
$attributes
Definition: matrix.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72
$items