PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Enterprise/GiftRegistry/Model/Item.php

https://bitbucket.org/kdms/sh-magento
PHP | 499 lines | 252 code | 46 blank | 201 comment | 39 complexity | fefcceecb056c4b92459133bec3d0e79 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Enterprise
  22. * @package Enterprise_GiftRegistry
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Entity items data model
  28. *
  29. * @method Enterprise_GiftRegistry_Model_Resource_Item _getResource()
  30. * @method Enterprise_GiftRegistry_Model_Resource_Item getResource()
  31. * @method Enterprise_GiftRegistry_Model_Item setEntityId(int $value)
  32. * @method int getProductId()
  33. * @method Enterprise_GiftRegistry_Model_Item setProductId(int $value)
  34. * @method float getQty()
  35. * @method Enterprise_GiftRegistry_Model_Item setQty(float $value)
  36. * @method float getQtyFulfilled()
  37. * @method Enterprise_GiftRegistry_Model_Item setQtyFulfilled(float $value)
  38. * @method string getNote()
  39. * @method Enterprise_GiftRegistry_Model_Item setNote(string $value)
  40. * @method string getAddedAt()
  41. * @method Enterprise_GiftRegistry_Model_Item setAddedAt(string $value)
  42. * @method string getCustomOptions()
  43. * @method Enterprise_GiftRegistry_Model_Item setCustomOptions(string $value)
  44. *
  45. * @category Enterprise
  46. * @package Enterprise_GiftRegistry
  47. * @author Magento Core Team <core@magentocommerce.com>
  48. */
  49. class Enterprise_GiftRegistry_Model_Item extends Mage_Core_Model_Abstract
  50. implements Mage_Catalog_Model_Product_Configuration_Item_Interface
  51. {
  52. /**
  53. * List of options related to item
  54. *
  55. * @var array
  56. */
  57. protected $_options = array();
  58. /**
  59. * Assoc array of item options
  60. * Option codes are used as array keys
  61. *
  62. * @var array
  63. */
  64. protected $_optionsByCode = array();
  65. /**
  66. * Flag stating that options were successfully saved
  67. *
  68. */
  69. protected $_flagOptionsSaved = null;
  70. function _construct() {
  71. $this->_init('enterprise_giftregistry/item');
  72. }
  73. /**
  74. * Load item by registry id and product id
  75. *
  76. * @param int $registryId
  77. * @param int $productId
  78. * @return Enterprise_GiftRegistry_Model_Item
  79. */
  80. public function loadByProductRegistry($registryId, $productId)
  81. {
  82. $this->_getResource()->loadByProductRegistry($this, $registryId, $productId);
  83. return $this;
  84. }
  85. /**
  86. * Add or Move item product to shopping cart
  87. *
  88. * Return true if product was successful added or exception with code
  89. * Return false for disabled or unvisible products
  90. *
  91. * @throws Mage_Core_Exception
  92. * @param Mage_Checkout_Model_Cart $cart
  93. * @param int $qty
  94. * @return bool
  95. */
  96. public function addToCart(Mage_Checkout_Model_Cart $cart, $qty)
  97. {
  98. $product = $this->_getProduct();
  99. $storeId = $this->getStoreId();
  100. if ($this->getQty() < ($qty + $this->getQtyFulfilled())) {
  101. $qty = $this->getQty() - $this->getQtyFulfilled();
  102. }
  103. if ($product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
  104. return false;
  105. }
  106. if (!$product->isVisibleInSiteVisibility()) {
  107. if ($product->getStoreId() == $storeId) {
  108. return false;
  109. }
  110. $urlData = Mage::getResourceSingleton('catalog/url')
  111. ->getRewriteByProductStore(array($product->getId() => $storeId));
  112. if (!isset($urlData[$product->getId()])) {
  113. return false;
  114. }
  115. $product->setUrlDataObject(new Varien_Object($urlData));
  116. $visibility = $product->getUrlDataObject()->getVisibility();
  117. if (!in_array($visibility, $product->getVisibleInSiteVisibilities())) {
  118. return false;
  119. }
  120. }
  121. if (!$product->isSalable()) {
  122. Mage::throwException(
  123. Mage::helper('enterprise_giftregistry')->__('This product(s) is currently out of stock.'));
  124. }
  125. $product->setGiftregistryItemId($this->getId());
  126. $product->addCustomOption('giftregistry_id', $this->getEntityId());
  127. $request = $this->getBuyRequest();
  128. $request->setQty($qty);
  129. $cart->addProduct($product, $request);
  130. $relatedProduct = $request->getRelatedProduct();
  131. if (!empty($relatedProduct)) {
  132. $cart->addProductsByIds(explode(',', $relatedProduct));
  133. }
  134. if (!$product->isVisibleInSiteVisibility()) {
  135. $cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
  136. }
  137. }
  138. /**
  139. * Check product representation in item
  140. *
  141. * @param Mage_Catalog_Model_Product $product
  142. * @return bool
  143. */
  144. public function isRepresentProduct($product)
  145. {
  146. if ($this->getProductId() != $product->getId()) {
  147. return false;
  148. }
  149. $itemOptions = $this->getOptionsByCode();
  150. $productOptions = $product->getCustomOptions();
  151. if (!$this->_compareOptions($itemOptions, $productOptions)) {
  152. return false;
  153. }
  154. if (!$this->_compareOptions($productOptions, $itemOptions)) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * Check if two option sets are identical
  161. *
  162. * @param array $options1
  163. * @param array $options2
  164. * @return bool
  165. */
  166. protected function _compareOptions($options1, $options2)
  167. {
  168. $skipOptions = array('qty','info_buyRequest');
  169. foreach ($options1 as $option) {
  170. $code = $option->getCode();
  171. if (in_array($code, $skipOptions)) {
  172. continue;
  173. }
  174. if ( !isset($options2[$code])
  175. || ($options2[$code]->getValue() === null)
  176. || $options2[$code]->getValue() != $option->getValue()) {
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. /**
  183. * Set product attributes to item
  184. *
  185. * @param Mage_Catalog_Model_Product $product
  186. * @return Enterprise_GiftRegistry_Model_Item
  187. */
  188. public function setProduct($product)
  189. {
  190. $this->setName($product->getName());
  191. $this->setData('product', $product);
  192. return $this;
  193. }
  194. /**
  195. * Return product url
  196. *
  197. * @return bool
  198. */
  199. public function getProductUrl()
  200. {
  201. return $this->getProduct()->getProductUrl();
  202. }
  203. /**
  204. * Return item product
  205. *
  206. * @return Mage_Catalog_Model_Product
  207. */
  208. protected function _getProduct()
  209. {
  210. if (!$this->_getData('product')) {
  211. $product = Mage::getModel('catalog/product')->load($this->getProductId());
  212. if (!$product->getId()) {
  213. Mage::throwException(
  214. Mage::helper('enterprise_giftregistry')->__('Invalid product for adding item to quote.'));
  215. }
  216. $this->setProduct($product);
  217. }
  218. return $this->_getData('product');
  219. }
  220. /**
  221. * Return item product
  222. *
  223. * @return Mage_Catalog_Model_Product
  224. */
  225. public function getProduct()
  226. {
  227. return $this->_getProduct();
  228. }
  229. /**
  230. * Checks if item model has data changes
  231. *
  232. * @return boolean
  233. */
  234. protected function _hasModelChanged()
  235. {
  236. if (!$this->hasDataChanges()) {
  237. return false;
  238. }
  239. return $this->_getResource()->hasDataChanged($this);
  240. }
  241. /**
  242. * Save item options after item is saved
  243. *
  244. * @return Enterprise_GiftRegistry_Model_Item
  245. */
  246. protected function _afterSave()
  247. {
  248. $this->_saveItemOptions();
  249. return parent::_afterSave();
  250. }
  251. /**
  252. * Save item options
  253. *
  254. * @return Enterprise_GiftRegistry_Model_Item
  255. */
  256. protected function _saveItemOptions()
  257. {
  258. foreach ($this->_options as $index => $option) {
  259. if ($option->isDeleted()) {
  260. $option->delete();
  261. unset($this->_options[$index]);
  262. unset($this->_optionsByCode[$option->getCode()]);
  263. } else {
  264. $option->save();
  265. }
  266. }
  267. $this->_flagOptionsSaved = true; // Report to watchers that options were saved
  268. return $this;
  269. }
  270. /**
  271. * Save model plus its options
  272. * Ensures saving options in case when resource model was not changed
  273. */
  274. public function save()
  275. {
  276. $hasDataChanges = $this->hasDataChanges();
  277. $this->_flagOptionsSaved = false;
  278. parent::save();
  279. if ($hasDataChanges && !$this->_flagOptionsSaved) {
  280. $this->_saveItemOptions();
  281. }
  282. }
  283. /**
  284. * Initialize item options
  285. *
  286. * @param array $options
  287. * @return Enterprise_GiftRegistry_Model_Item
  288. */
  289. public function setOptions($options)
  290. {
  291. foreach ($options as $option) {
  292. $this->addOption($option);
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Retrieve all item options
  298. *
  299. * @return array
  300. */
  301. public function getOptions()
  302. {
  303. return $this->_options;
  304. }
  305. /**
  306. * Retrieve all item options as assoc array with option codes as array keys
  307. *
  308. * @return array
  309. */
  310. public function getOptionsByCode()
  311. {
  312. return $this->_optionsByCode;
  313. }
  314. /**
  315. * Remove option from item options
  316. *
  317. * @param string $code
  318. * @return Enterprise_GiftRegistry_Model_Item
  319. */
  320. public function removeOption($code)
  321. {
  322. $option = $this->getOptionByCode($code);
  323. if ($option) {
  324. $option->isDeleted(true);
  325. }
  326. return $this;
  327. }
  328. /**
  329. * Add option to item
  330. *
  331. * @throws Mage_Core_Exception
  332. * @param Enterprise_GiftRegistry_Model_Item_Option $option
  333. * @return Enterprise_GiftRegistry_Model_Item
  334. */
  335. public function addOption($option)
  336. {
  337. if (is_array($option)) {
  338. $option = Mage::getModel('enterprise_giftregistry/item_option')->setData($option)
  339. ->setItem($this);
  340. } elseif ($option instanceof Mage_Sales_Model_Quote_Item_Option) {
  341. // import data from existing quote item option
  342. $option = Mage::getModel('enterprise_giftregistry/item_option')->setProduct($option->getProduct())
  343. ->setCode($option->getCode())
  344. ->setValue($option->getValue())
  345. ->setItem($this);
  346. } elseif (($option instanceof Varien_Object)
  347. && !($option instanceof Enterprise_GiftRegistry_Model_Item_Option)
  348. ) {
  349. $option = Mage::getModel('enterprise_giftregistry/item_option')->setData($option->getData())
  350. ->setProduct($option->getProduct())
  351. ->setItem($this);
  352. } elseif($option instanceof Enterprise_GiftRegistry_Model_Item_Option) {
  353. $option->setItem($this);
  354. } else {
  355. Mage::throwException(Mage::helper('enterprise_giftregistry')->__('Invalid item option format.'));
  356. }
  357. $exOption = $this->getOptionByCode($option->getCode());
  358. if (!is_null($exOption)) {
  359. $exOption->addData($option->getData());
  360. } else {
  361. $this->_addOptionCode($option);
  362. $this->_options[] = $option;
  363. }
  364. return $this;
  365. }
  366. /**
  367. * Register option code
  368. *
  369. * @throws Mage_Core_Exception
  370. * @param Enterprise_GiftRegistry_Model_Item_Option $option
  371. * @return Enterprise_GiftRegistry_Model_Item
  372. */
  373. protected function _addOptionCode($option)
  374. {
  375. if (!isset($this->_optionsByCode[$option->getCode()])) {
  376. $this->_optionsByCode[$option->getCode()] = $option;
  377. } else {
  378. Mage::throwException(Mage::helper('enterprise_giftregistry')->__('An item option with code %s already exists.', $option->getCode()));
  379. }
  380. return $this;
  381. }
  382. /**
  383. * Retrieve item option by code
  384. *
  385. * @param string $code
  386. * @return Enterprise_GiftRegistry_Model_Item_Option|null
  387. */
  388. public function getOptionByCode($code)
  389. {
  390. if (isset($this->_optionsByCode[$code]) && !$this->_optionsByCode[$code]->isDeleted()) {
  391. return $this->_optionsByCode[$code];
  392. }
  393. return null;
  394. }
  395. /**
  396. * Returns formatted buy request - object, holding request received from
  397. * product view page with keys and options for configured product
  398. *
  399. * @return Varien_Object
  400. */
  401. public function getBuyRequest()
  402. {
  403. $option = $this->getOptionByCode('info_buyRequest');
  404. $buyRequest = new Varien_Object($option ? unserialize($option->getValue()) : null);
  405. $buyRequest->setOriginalQty($buyRequest->getQty())
  406. ->setQty($this->getQty() * 1); // Qty value that is stored in buyRequest can be out-of-date
  407. return $buyRequest;
  408. }
  409. /**
  410. * Clone gift registry item
  411. *
  412. * @return Enterprise_GiftRegistry_Model_Item
  413. */
  414. public function __clone()
  415. {
  416. $options = $this->getOptions();
  417. $this->_options = array();
  418. $this->_optionsByCode = array();
  419. foreach ($options as $option) {
  420. $this->addOption(clone $option);
  421. }
  422. return $this;
  423. }
  424. /**
  425. * Returns special download params (if needed) for custom option with type = 'file'
  426. * Needed to implement Mage_Catalog_Model_Product_Configuration_Item_Interface.
  427. * Currently returns null, as far as we don't show file options and don't need controllers to give file.
  428. *
  429. * @return null|Varien_Object
  430. */
  431. public function getFileDownloadParams()
  432. {
  433. return null;
  434. }
  435. /**
  436. * Validates and sets quantity for the related product
  437. *
  438. * @param int|float $quantity New item quantity
  439. * @throws Mage_Core_Exception
  440. * @return Enterprise_GiftRegistry_Model_Item
  441. */
  442. public function setQty($quantity)
  443. {
  444. $quantity = (float)$quantity;
  445. if (!$this->_getProduct()->getTypeInstance()->canUseQtyDecimals()) {
  446. $quantity = round($quantity);
  447. }
  448. if ($quantity <= 0) {
  449. $quantity = 1;
  450. }
  451. return $this->setData('qty', $quantity);
  452. }
  453. }