Mage_Adminhtml_Model_Giftmessage_Save Class Reference

Inheritance diagram for Mage_Adminhtml_Model_Giftmessage_Save:

Varien_Object

List of all members.

Public Member Functions

 saveAllInQuote ()
 getSaved ()
 saveAllInOrder ()
 setAllowQuoteItems ($items)
 addAllowQuoteItem ($item)
 getAllowQuoteItems ()
 getAllowQuoteItemsProducts ()
 getIsAllowedQuoteItem ($item)
 isGiftMessagesAvailable ($item)
 importAllowQuoteItemsFromProducts ($products)
 importAllowQuoteItemsFromItems ($items)

Protected Member Functions

 _saveOne ($entityId, $giftmessage)
 _deleteOne ($entityModel, $giftmessageModel=null)
 _getMappedType ($type)
 _getSession ()
 _getQuote ()

Protected Attributes

 $_saved = false


Detailed Description

Definition at line 35 of file Save.php.


Member Function Documentation

_deleteOne ( entityModel,
giftmessageModel = null 
) [protected]

Delete a single gift message from entity

Parameters:
Mage_GiftMessage_Model_Message|null $giftmessageModel
Varien_Object $entityModel
Returns:
Mage_Adminhtml_Model_Giftmessage_Save

Definition at line 140 of file Save.php.

00141     {
00142         if(is_null($giftmessageModel)) {
00143             $giftmessageModel = Mage::getModel('giftmessage/message')
00144                 ->load($entityModel->getGiftMessageId());
00145         }
00146         $giftmessageModel->delete();
00147         $entityModel->setGiftMessageId(0)
00148             ->save();
00149         return $this;
00150     }

_getMappedType ( type  )  [protected]

Retrive mapped type for entity

Parameters:
string $type
Returns:
string

Definition at line 316 of file Save.php.

00317     {
00318         $map = array(
00319             'main'          =>  'quote',
00320             'item'          =>  'quote_item',
00321             'order'         =>  'order',
00322             'order_item'    =>  'order_item'
00323         );
00324 
00325         if (isset($map[$type])) {
00326             return $map[$type];
00327         }
00328 
00329         return null;
00330     }

_getQuote (  )  [protected]

Retrieve quote object

Returns:
Mage_Sales_Model_Quote

Definition at line 347 of file Save.php.

00348     {
00349         return $this->_getSession()->getQuote();
00350     }

_getSession (  )  [protected]

Retrieve session object

Returns:
Mage_Adminhtml_Model_Session_Quote

Definition at line 337 of file Save.php.

00338     {
00339         return Mage::getSingleton('adminhtml/session_quote');
00340     }

_saveOne ( entityId,
giftmessage 
) [protected]

Save a single gift message

Parameters:
integer $entityId
array $giftmessage
Returns:
Mage_Adminhtml_Model_Giftmessage_Save

Definition at line 93 of file Save.php.

00093                                                          {
00094         $giftmessageModel = Mage::getModel('giftmessage/message');
00095 
00096         if ($this->_getMappedType($giftmessage['type'])!='quote_item') {
00097             $entityModel = $giftmessageModel->getEntityModelByType($this->_getMappedType($giftmessage['type']));
00098         } else {
00099             $entityModel = $this->_getQuote()->getItemById($entityId);
00100         }
00101 
00102 
00103 
00104         if ($this->_getMappedType($giftmessage['type'])=='quote') {
00105             $entityModel->setStoreId($this->_getQuote()->getStoreId());
00106         }
00107 
00108         if ($this->_getMappedType($giftmessage['type'])!='quote_item') {
00109             $entityModel->load($entityId);
00110         }
00111 
00112 
00113         if ($entityModel->getGiftMessageId()) {
00114             $giftmessageModel->load($entityModel->getGiftMessageId());
00115         }
00116 
00117         $giftmessageModel->addData($giftmessage);
00118 
00119         if ($giftmessageModel->isMessageEmpty() && $giftmessageModel->getId()) {
00120             // remove empty giftmessage
00121             $this->_deleteOne($entityModel, $giftmessageModel);
00122             $this->_saved = false;
00123         } elseif (!$giftmessageModel->isMessageEmpty()) {
00124             $giftmessageModel->save();
00125             $entityModel->setGiftMessageId($giftmessageModel->getId())
00126                 ->save();
00127             $this->_saved = true;
00128         }
00129 
00130         return $this;
00131     }

addAllowQuoteItem ( item  ) 

Add allowed quote item for gift messages

Parameters:
int $item
Returns:
Mage_Adminhtml_Model_Giftmessage_Save

Definition at line 170 of file Save.php.

00171     {
00172         $items = $this->getAllowQuoteItems();
00173         if (!in_array($item, $items)) {
00174             $items[] = $item;
00175         }
00176         $this->setAllowQuoteItems($items);
00177 
00178         return $this;
00179     }

getAllowQuoteItems (  ) 

Retrive allowed quote items for gift messages

Returns:
array

Definition at line 186 of file Save.php.

00187     {
00188         if(!is_array($this->_getSession()->getAllowQuoteItemsGiftMessage())) {
00189             $this->setAllowQuoteItems(array());
00190         }
00191 
00192         return $this->_getSession()->getAllowQuoteItemsGiftMessage();
00193     }

getAllowQuoteItemsProducts (  ) 

Retrive allowed quote items products for gift messages

Returns:
array

Definition at line 200 of file Save.php.

00201     {
00202         $result = array();
00203         foreach ($this->getAllowQuoteItems() as $itemId) {
00204             $item = $this->_getQuote()->getItemById($itemId);
00205             if(!$item) {
00206                 continue;
00207             }
00208             $result[] = $item->getProduct()->getId();
00209         }
00210         return $result;
00211     }

getIsAllowedQuoteItem ( item  ) 

Checks allowed quote item for gift messages

Parameters:
Varien_Object $item
Returns:
boolean

Definition at line 219 of file Save.php.

00220     {
00221         if(!in_array($item->getId(), $this->getAllowQuoteItems())) {
00222             if ($item->getGiftMessageId() && $this->isGiftMessagesAvailable($item)) {
00223                 $this->addAllowQuoteItem($item->getId());
00224                 return true;
00225             }
00226             return false;
00227         }
00228 
00229         return true;
00230     }

getSaved (  ) 

Definition at line 66 of file Save.php.

00067     {
00068         return $this->_saved;
00069     }

importAllowQuoteItemsFromItems ( items  ) 

Definition at line 281 of file Save.php.

00282     {
00283         $allowedItems = $this->getAllowQuoteItems();
00284         $deleteAllowedItems = array();
00285         foreach ($items as $itemId=>$data) {
00286 
00287             $item = $this->_getQuote()->getItemById($itemId);
00288 
00289             if(!$item) {
00290                 // Clean not exists items
00291                 $deleteAllowedItems[] = $itemId;
00292                 continue;
00293             }
00294 
00295             if (in_array($item->getId(), $allowedItems)
00296                 && !isset($data['giftmessage'])) {
00297                 $deleteAllowedItems[] = $item->getId();
00298             } elseif (!in_array($item->getId(), $allowedItems)
00299                       && isset($data['giftmessage'])) {
00300                 $allowedItems[] = $item->getId();
00301             }
00302 
00303         }
00304 
00305         $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
00306         $this->setAllowQuoteItems($allowedItems);
00307         return $this;
00308     }

importAllowQuoteItemsFromProducts ( products  ) 

Imports quote items for gift messages from products data

Parameters:
unknown_type $products
Returns:
unknown

Definition at line 251 of file Save.php.

00252     {
00253         $allowedItems = $this->getAllowQuoteItems();
00254         $deleteAllowedItems = array();
00255         foreach ($products as $productId=>$data) {
00256             $product = Mage::getModel('catalog/product')
00257                 ->setStore($this->_getSession()->getStore())
00258                 ->load($productId);
00259             $item = $this->_getQuote()->getItemByProduct($product);
00260 
00261             if(!$item) {
00262                 continue;
00263             }
00264 
00265             if (in_array($item->getId(), $allowedItems)
00266                 && !isset($data['giftmessage'])) {
00267                 $deleteAllowedItems[] = $item->getId();
00268             } elseif (!in_array($item->getId(), $allowedItems)
00269                       && isset($data['giftmessage'])) {
00270                 $allowedItems[] = $item->getId();
00271             }
00272 
00273         }
00274 
00275         $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
00276 
00277         $this->setAllowQuoteItems($allowedItems);
00278         return $this;
00279     }

isGiftMessagesAvailable ( item  ) 

Retrieve is gift message available for item (product)

Parameters:
Varien_Object $item
Returns:
bool

Definition at line 238 of file Save.php.

00239     {
00240         return Mage::helper('giftmessage/message')->getIsMessagesAvailable(
00241             'item', $item, $item->getStore()
00242         );
00243     }

saveAllInOrder (  ) 

Definition at line 71 of file Save.php.

00072     {
00073         $giftmessages = $this->getGiftmessages();
00074 
00075         if (!is_array($giftmessages)) {
00076             return $this;
00077         }
00078 
00079         foreach ($giftmessages as $entityId=>$giftmessage) {
00080             $this->_saveOne($entityId, $giftmessage);
00081         }
00082 
00083         return $this;
00084     }

saveAllInQuote (  ) 

Save all seted giftmessages

Returns:
Mage_Adminhtml_Model_Giftmessage_Save

Definition at line 44 of file Save.php.

00045     {
00046         $giftmessages = $this->getGiftmessages();
00047 
00048         // remove disabled giftmessages
00049         foreach ($this->_getQuote()->getAllItems() as $item) {
00050             if($item->getGiftMessageId() && !in_array($item->getId(), $this->getAllowQuoteItems())) {
00051                 $this->_deleteOne($item);
00052             }
00053         }
00054 
00055         if (!is_array($giftmessages)) {
00056             return $this;
00057         }
00058 
00059         foreach ($giftmessages as $entityId=>$giftmessage) {
00060             $this->_saveOne($entityId, $giftmessage);
00061         }
00062 
00063         return $this;
00064     }

setAllowQuoteItems ( items  ) 

Set allowed quote items for gift messages

Parameters:
array $items
Returns:
Mage_Adminhtml_Model_Giftmessage_Save

Definition at line 158 of file Save.php.

00159     {
00160         $this->_getSession()->setAllowQuoteItemsGiftMessage($items);
00161         return $this;
00162     }


Member Data Documentation

$_saved = false [protected]

Definition at line 37 of file Save.php.


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

Generated on Sat Jul 4 17:23:12 2009 for Magento by  doxygen 1.5.8