Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Save Class Reference
Inheritance diagram for Save:
DataObject

Public Member Functions

 __construct (\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\GiftMessage\Model\MessageFactory $messageFactory, \Magento\Backend\Model\Session\Quote $session, \Magento\GiftMessage\Helper\Message $giftMessageMessage)
 
 saveAllInQuote ()
 
 getSaved ()
 
 saveAllInOrder ()
 
 setAllowQuoteItems ($items)
 
 addAllowQuoteItem ($item)
 
 getAllowQuoteItems ()
 
 getAllowQuoteItemsProducts ()
 
 getIsAllowedQuoteItem ($item)
 
 isGiftMessagesAvailable ($item)
 
 importAllowQuoteItemsFromProducts ($products)
 
 importAllowQuoteItemsFromItems ($items)
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Member Functions

 _saveOne ($entityId, $giftmessage, $entityType)
 
 _deleteOne ($entityModel, $giftmessageModel=null)
 
 getMappedType ($type)
 
 _getQuote ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_saved = false
 
 $_giftMessageMessage = null
 
 $_session
 
 $_messageFactory
 
 $productRepository
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Adminhtml giftmessage save model

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m

Definition at line 13 of file Save.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
\Magento\GiftMessage\Model\MessageFactory  $messageFactory,
\Magento\Backend\Model\Session\Quote  $session,
\Magento\GiftMessage\Helper\Message  $giftMessageMessage 
)
Parameters
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
\Magento\GiftMessage\Model\MessageFactory$messageFactory
\Magento\Backend\Model\Session\Quote$session
\Magento\GiftMessage\Helper\Message$giftMessageMessage

Definition at line 48 of file Save.php.

53  {
54  $this->productRepository = $productRepository;
55  $this->_messageFactory = $messageFactory;
56  $this->_session = $session;
57  $this->_giftMessageMessage = $giftMessageMessage;
58  }
$messageFactory
Definition: messages.php:10

Member Function Documentation

◆ _deleteOne()

_deleteOne (   $entityModel,
  $giftmessageModel = null 
)
protected

Delete a single gift message from entity

Parameters
\Magento\Framework\DataObject$entityModel
\Magento\GiftMessage\Model\Message | null$giftmessageModel
Returns
$this

Definition at line 170 of file Save.php.

171  {
172  if ($giftmessageModel === null) {
173  $giftmessageModel = $this->_messageFactory->create()->load($entityModel->getGiftMessageId());
174  }
175  $giftmessageModel->delete();
176  $entityModel->setGiftMessageId(0)->save();
177  return $this;
178  }

◆ _getQuote()

_getQuote ( )
protected

Retrieve quote object

Returns
\Magento\Quote\Model\Quote @codeCoverageIgnore

Definition at line 359 of file Save.php.

360  {
361  return $this->_session->getQuote();
362  }

◆ _saveOne()

_saveOne (   $entityId,
  $giftmessage,
  $entityType 
)
protected

Save a single gift message

Parameters
int$entityId
array$giftmessage
string$entityType
Returns
$this

Definition at line 118 of file Save.php.

119  {
120  /* @var $giftmessageModel \Magento\GiftMessage\Model\Message */
121  $giftmessageModel = $this->_messageFactory->create();
122 
123  switch ($entityType) {
124  case 'quote':
125  $entityModel = $this->_getQuote();
126  break;
127 
128  case 'quote_item':
129  $entityModel = $this->_getQuote()->getItemById($entityId);
130  break;
131 
132  default:
133  $entityModel = $giftmessageModel->getEntityModelByType($entityType)->load($entityId);
134  break;
135  }
136 
137  if (!$entityModel) {
138  return $this;
139  }
140 
141  if ($entityModel->getGiftMessageId()) {
142  $giftmessageModel->load($entityModel->getGiftMessageId());
143  }
144 
145  $giftmessageModel->addData($giftmessage);
146 
147  if ($giftmessageModel->isMessageEmpty() && $giftmessageModel->getId()) {
148  // remove empty giftmessage
149  $this->_deleteOne($entityModel, $giftmessageModel);
150  $this->_saved = false;
151  } elseif (!$giftmessageModel->isMessageEmpty()) {
152  $giftmessageModel->save();
153  $entityModel->setGiftMessageId($giftmessageModel->getId());
154  if ($entityType != 'quote') {
155  $entityModel->save();
156  }
157  $this->_saved = true;
158  }
159 
160  return $this;
161  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_deleteOne($entityModel, $giftmessageModel=null)
Definition: Save.php:170

◆ addAllowQuoteItem()

addAllowQuoteItem (   $item)

Add allowed quote item for gift messages

Parameters
int$item
Returns
$this

Definition at line 198 of file Save.php.

199  {
200  $items = $this->getAllowQuoteItems();
201  if (!in_array($item, $items)) {
202  $items[] = $item;
203  }
204  $this->setAllowQuoteItems($items);
205 
206  return $this;
207  }
$items

◆ getAllowQuoteItems()

getAllowQuoteItems ( )

Retrieve allowed quote items for gift messages

Returns
array

Definition at line 214 of file Save.php.

215  {
216  if (!is_array($this->_session->getAllowQuoteItemsGiftMessage())) {
217  $this->setAllowQuoteItems([]);
218  }
219 
220  return $this->_session->getAllowQuoteItemsGiftMessage();
221  }

◆ getAllowQuoteItemsProducts()

getAllowQuoteItemsProducts ( )

Retrieve allowed quote items products for gift messages

Returns
array

Definition at line 228 of file Save.php.

229  {
230  $result = [];
231  foreach ($this->getAllowQuoteItems() as $itemId) {
232  $item = $this->_getQuote()->getItemById($itemId);
233  if (!$item) {
234  continue;
235  }
236  $result[] = $item->getProduct()->getId();
237  }
238  return $result;
239  }

◆ getIsAllowedQuoteItem()

getIsAllowedQuoteItem (   $item)

Checks allowed quote item for gift messages

Parameters
\Magento\Framework\DataObject$item
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 248 of file Save.php.

249  {
250  if (!in_array($item->getId(), $this->getAllowQuoteItems())) {
251  if ($item->getGiftMessageId() && $this->isGiftMessagesAvailable($item)) {
252  $this->addAllowQuoteItem($item->getId());
253  return true;
254  }
255  return false;
256  }
257 
258  return true;
259  }

◆ getMappedType()

getMappedType (   $type)
protected

Retrieve mapped type for entity

Parameters
string$type
Returns
string|null

Definition at line 338 of file Save.php.

339  {
340  $map = [
341  'main' => 'quote',
342  'item' => 'quote_item',
343  'order' => 'order',
344  'order_item' => 'order_item',
345  ];
346 
347  if (isset($map[$type])) {
348  return $map[$type];
349  }
350  return null;
351  }
$type
Definition: item.phtml:13

◆ getSaved()

getSaved ( )
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName) @codeCoverageIgnore

Definition at line 86 of file Save.php.

87  {
88  return $this->_saved;
89  }

◆ importAllowQuoteItemsFromItems()

importAllowQuoteItemsFromItems (   $items)
Parameters
mixed$items
Returns
$this

Definition at line 307 of file Save.php.

308  {
309  $allowedItems = $this->getAllowQuoteItems();
310  $deleteAllowedItems = [];
311  foreach ($items as $itemId => $data) {
312  $item = $this->_getQuote()->getItemById($itemId);
313 
314  if (!$item) {
315  // Clean not exists items
316  $deleteAllowedItems[] = $itemId;
317  continue;
318  }
319 
320  if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
321  $deleteAllowedItems[] = $item->getId();
322  } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
323  $allowedItems[] = $item->getId();
324  }
325  }
326 
327  $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
328  $this->setAllowQuoteItems($allowedItems);
329  return $this;
330  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$items

◆ importAllowQuoteItemsFromProducts()

importAllowQuoteItemsFromProducts (   $products)

Imports quote items for gift messages from products data

Parameters
mixed$products
Returns
$this

Definition at line 278 of file Save.php.

279  {
280  $allowedItems = $this->getAllowQuoteItems();
281  $deleteAllowedItems = [];
282  foreach ($products as $productId => $data) {
283  $product = $this->productRepository->getById($productId, false, $this->_session->getStore()->getId());
284  $item = $this->_getQuote()->getItemByProduct($product);
285 
286  if (!$item) {
287  continue;
288  }
289 
290  if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
291  $deleteAllowedItems[] = $item->getId();
292  } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
293  $allowedItems[] = $item->getId();
294  }
295  }
296 
297  $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
298 
299  $this->setAllowQuoteItems($allowedItems);
300  return $this;
301  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ isGiftMessagesAvailable()

isGiftMessagesAvailable (   $item)

Retrieve is gift message available for item (product)

Parameters
\Magento\Framework\DataObject$item
Returns
bool

Definition at line 267 of file Save.php.

268  {
269  return $this->_giftMessageMessage->isMessagesAllowed('item', $item, $item->getStore());
270  }

◆ saveAllInOrder()

saveAllInOrder ( )
Returns
$this

Definition at line 94 of file Save.php.

95  {
96  $giftMessages = $this->getGiftmessages();
97 
98  if (!is_array($giftMessages)) {
99  return $this;
100  }
101 
102  foreach ($giftMessages as $entityId => $giftMessage) {
103  $entityType = $this->getMappedType($giftMessage['type']);
104  $this->_saveOne($entityId, $giftMessage, $entityType);
105  }
106 
107  return $this;
108  }
$giftMessage
Definition: items.phtml:47
_saveOne($entityId, $giftmessage, $entityType)
Definition: Save.php:118

◆ saveAllInQuote()

saveAllInQuote ( )

Save all seted giftmessages

Returns
$this

Definition at line 65 of file Save.php.

66  {
67  $giftmessages = $this->getGiftmessages();
68 
69  if (!is_array($giftmessages)) {
70  return $this;
71  }
72 
73  foreach ($giftmessages as $entityId => $giftmessage) {
74  $entityType = $this->getMappedType($giftmessage['type']);
75  $this->_saveOne($entityId, $giftmessage, $entityType);
76  }
77 
78  return $this;
79  }
_saveOne($entityId, $giftmessage, $entityType)
Definition: Save.php:118

◆ setAllowQuoteItems()

setAllowQuoteItems (   $items)

Set allowed quote items for gift messages

Parameters
array$items
Returns
$this

Definition at line 186 of file Save.php.

187  {
188  $this->_session->setAllowQuoteItemsGiftMessage($items);
189  return $this;
190  }
$items

Field Documentation

◆ $_giftMessageMessage

$_giftMessageMessage = null
protected

Definition at line 25 of file Save.php.

◆ $_messageFactory

$_messageFactory
protected

Definition at line 35 of file Save.php.

◆ $_saved

$_saved = false
protected

Definition at line 18 of file Save.php.

◆ $_session

$_session
protected

Definition at line 30 of file Save.php.

◆ $productRepository

$productRepository
protected

Definition at line 40 of file Save.php.


The documentation for this class was generated from the following file: