PageRenderTime 120ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Wishlist/Model/Resource/Item/Collection.php

https://bitbucket.org/kdms/sh-magento
PHP | 491 lines | 236 code | 59 blank | 196 comment | 18 complexity | b48f05b7238a4df9832256dab57c3a16 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 Mage
  22. * @package Mage_Wishlist
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Wishlist item collection
  28. *
  29. * @category Mage
  30. * @package Mage_Wishlist
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Wishlist_Model_Resource_Item_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Product Visibility Filter to product collection flag
  37. *
  38. * @var bool
  39. */
  40. protected $_productVisible = false;
  41. /**
  42. * Product Salable Filter to product collection flag
  43. *
  44. * @var bool
  45. */
  46. protected $_productSalable = false;
  47. /**
  48. * If product out of stock, its item will be removed after load
  49. *
  50. * @var bool
  51. */
  52. protected $_productInStock = false;
  53. /**
  54. * Product Ids array
  55. *
  56. * @var array
  57. */
  58. protected $_productIds = array();
  59. /**
  60. * Store Ids array
  61. *
  62. * @var array
  63. */
  64. protected $_storeIds = array();
  65. /**
  66. * Add days in whishlist filter of product collection
  67. *
  68. * @var boolean
  69. */
  70. protected $_addDaysInWishlist = false;
  71. /**
  72. * Sum of items collection qty
  73. *
  74. * @var int
  75. */
  76. protected $_itemsQty;
  77. /**
  78. * Whether product name attribute value table is joined in select
  79. *
  80. * @var boolean
  81. */
  82. protected $_isProductNameJoined = false;
  83. /**
  84. * Initialize resource model for collection
  85. *
  86. * @return void
  87. */
  88. public function _construct()
  89. {
  90. $this->_init('wishlist/item');
  91. $this->addFilterToMap('store_id', 'main_table.store_id');
  92. }
  93. /**
  94. * After load processing
  95. *
  96. * @return Mage_Wishlist_Model_Resource_Item_Collection
  97. */
  98. protected function _afterLoad()
  99. {
  100. parent::_afterLoad();
  101. /**
  102. * Assign products
  103. */
  104. $this->_assignOptions();
  105. $this->_assignProducts();
  106. $this->resetItemsDataChanged();
  107. $this->getPageSize();
  108. return $this;
  109. }
  110. /**
  111. * Add options to items
  112. *
  113. * @return Mage_Wishlist_Model_Resource_Item_Collection
  114. */
  115. protected function _assignOptions()
  116. {
  117. $itemIds = array_keys($this->_items);
  118. /* @var $optionCollection Mage_Wishlist_Model_Resource_Item_Option_Collection */
  119. $optionCollection = Mage::getModel('wishlist/item_option')->getCollection();
  120. $optionCollection->addItemFilter($itemIds);
  121. /* @var $item Mage_Wishlist_Model_Item */
  122. foreach ($this as $item) {
  123. $item->setOptions($optionCollection->getOptionsByItem($item));
  124. }
  125. $productIds = $optionCollection->getProductIds();
  126. $this->_productIds = array_merge($this->_productIds, $productIds);
  127. return $this;
  128. }
  129. /**
  130. * Add products to items and item options
  131. *
  132. * @return Mage_Wishlist_Model_Resource_Item_Collection
  133. */
  134. protected function _assignProducts()
  135. {
  136. Varien_Profiler::start('WISHLIST:'.__METHOD__);
  137. $productIds = array();
  138. $isStoreAdmin = Mage::app()->getStore()->isAdmin();
  139. $storeIds = array();
  140. foreach ($this as $item) {
  141. $productIds[$item->getProductId()] = 1;
  142. if ($isStoreAdmin && !in_array($item->getStoreId(), $storeIds)) {
  143. $storeIds[] = $item->getStoreId();
  144. }
  145. }
  146. if (!$isStoreAdmin) {
  147. $storeIds = $this->_storeIds;
  148. }
  149. $this->_productIds = array_merge($this->_productIds, array_keys($productIds));
  150. $attributes = Mage::getSingleton('wishlist/config')->getProductAttributes();
  151. $productCollection = Mage::getModel('catalog/product')->getCollection();
  152. foreach ($storeIds as $id) {
  153. $productCollection->addStoreFilter($id);
  154. }
  155. if ($this->_productVisible) {
  156. Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection);
  157. }
  158. $productCollection->addPriceData()
  159. ->addTaxPercents()
  160. ->addIdFilter($this->_productIds)
  161. ->addAttributeToSelect($attributes)
  162. ->addOptionsToResult()
  163. ->addUrlRewrite();
  164. if ($this->_productSalable) {
  165. $productCollection = Mage::helper('adminhtml/sales')->applySalableProductTypesFilter($productCollection);
  166. }
  167. Mage::dispatchEvent('wishlist_item_collection_products_after_load', array(
  168. 'product_collection' => $productCollection
  169. ));
  170. $checkInStock = $this->_productInStock && !Mage::helper('cataloginventory')->isShowOutOfStock();
  171. foreach ($this as $item) {
  172. $product = $productCollection->getItemById($item->getProductId());
  173. if ($product) {
  174. if ($checkInStock && !$product->isInStock()) {
  175. $this->removeItemByKey($item->getId());
  176. } else {
  177. $product->setCustomOptions(array());
  178. $item->setProduct($product);
  179. $item->setProductName($product->getName());
  180. $item->setName($product->getName());
  181. $item->setPrice($product->getPrice());
  182. }
  183. } else {
  184. $item->isDeleted(true);
  185. }
  186. }
  187. Varien_Profiler::stop('WISHLIST:'.__METHOD__);
  188. return $this;
  189. }
  190. /**
  191. * Add filter by wishlist object
  192. *
  193. * @param Mage_Wishlist_Model_Wishlist $wishlist
  194. * @return Mage_Wishlist_Model_Resource_Item_Collection
  195. */
  196. public function addWishlistFilter(Mage_Wishlist_Model_Wishlist $wishlist)
  197. {
  198. $this->addFieldToFilter('wishlist_id', $wishlist->getId());
  199. return $this;
  200. }
  201. /**
  202. * Add filtration by customer id
  203. *
  204. * @param int $customerId
  205. * @return Mage_Wishlist_Model_Resource_Item_Collection
  206. */
  207. public function addCustomerIdFilter($customerId)
  208. {
  209. $this->getSelect()
  210. ->join(
  211. array('wishlist' => $this->getTable('wishlist/wishlist')),
  212. 'main_table.wishlist_id = wishlist.wishlist_id',
  213. array()
  214. )
  215. ->where('wishlist.customer_id = ?', $customerId);
  216. return $this;
  217. }
  218. /**
  219. * Add filter by shared stores
  220. *
  221. * @param array $storeIds
  222. *
  223. * @return Mage_Wishlist_Model_Resource_Item_Collection
  224. */
  225. public function addStoreFilter($storeIds = array())
  226. {
  227. if (!is_array($storeIds)) {
  228. $storeIds = array($storeIds);
  229. }
  230. $this->_storeIds = $storeIds;
  231. $this->addFieldToFilter('store_id', array('in' => $this->_storeIds));
  232. return $this;
  233. }
  234. /**
  235. * Add items store data to collection
  236. *
  237. * @return Mage_Wishlist_Model_Resource_Item_Collection
  238. */
  239. public function addStoreData()
  240. {
  241. $storeTable = Mage::getSingleton('core/resource')->getTableName('core/store');
  242. $this->getSelect()->join(array('store'=>$storeTable), 'main_table.store_id=store.store_id', array(
  243. 'store_name'=>'name',
  244. 'item_store_id' => 'store_id'
  245. ));
  246. return $this;
  247. }
  248. /**
  249. * Add wishlist sort order
  250. *
  251. * @deprecated after 1.6.0.0-rc2
  252. * @see Varien_Data_Collection_Db::setOrder() is used instead
  253. *
  254. * @param string $attribute
  255. * @param string $dir
  256. * @return Mage_Wishlist_Model_Resource_Item_Collection
  257. */
  258. public function addWishListSortOrder($attribute = 'added_at', $dir = 'desc')
  259. {
  260. $this->setOrder($attribute, $dir);
  261. return $this;
  262. }
  263. /**
  264. * Reset sort order
  265. *
  266. * @return Mage_Wishlist_Model_Resource_Item_Collection
  267. */
  268. public function resetSortOrder()
  269. {
  270. $this->getSelect()->reset(Zend_Db_Select::ORDER);
  271. return $this;
  272. }
  273. /**
  274. * Set product Visibility Filter to product collection flag
  275. *
  276. * @param bool $flag
  277. * @return Mage_Wishlist_Model_Resource_Item_Collection
  278. */
  279. public function setVisibilityFilter($flag = true)
  280. {
  281. $this->_productVisible = (bool)$flag;
  282. return $this;
  283. }
  284. /**
  285. * Set Salable Filter.
  286. * This filter apply Salable Product Types Filter to product collection.
  287. *
  288. * @param bool $flag
  289. * @return Mage_Wishlist_Model_Resource_Item_Collection
  290. */
  291. public function setSalableFilter($flag = true)
  292. {
  293. $this->_productSalable = (bool)$flag;
  294. return $this;
  295. }
  296. /**
  297. * Set In Stock Filter.
  298. * This filter remove items with no salable product.
  299. *
  300. * @param bool $flag
  301. * @return Mage_Wishlist_Model_Resource_Item_Collection
  302. */
  303. public function setInStockFilter($flag = true)
  304. {
  305. $this->_productInStock = (bool)$flag;
  306. return $this;
  307. }
  308. /**
  309. * Set add days in whishlist
  310. *
  311. * This method appears in 1.5.0.0 in deprecated state, because:
  312. * - we need it to make wishlist item collection interface as much as possible compatible with old
  313. * wishlist product collection
  314. * - this method is useless because we can calculate days in php, and don't use MySQL for it
  315. *
  316. * @deprecated after 1.4.2.0
  317. * @return Mage_Wishlist_Model_Resource_Item_Collection
  318. */
  319. public function addDaysInWishlist()
  320. {
  321. $this->_addDaysInWishlist = true;
  322. $adapter = $this->getConnection();
  323. $dateModel = Mage::getSingleton('core/date');
  324. $resHelper = Mage::getResourceHelper('core');
  325. $offsetFromDb = (int) $dateModel->getGmtOffset();
  326. $startDate = $adapter->getDateAddSql('added_at', $offsetFromDb, Varien_Db_Adapter_Interface::INTERVAL_SECOND);
  327. $nowDate = $dateModel->date();
  328. $dateDiff = $resHelper->getDateDiff($startDate, $adapter->formatDate($nowDate));
  329. $this->getSelect()->columns(array('days_in_wishlist' => $dateDiff));
  330. return $this;
  331. }
  332. /**
  333. * Adds filter on days in wishlist
  334. *
  335. * $constraints may contain 'from' and 'to' indexes with number of days to look for items
  336. *
  337. * @param array $constraints
  338. * @return Mage_Wishlist_Model_Resource_Item_Collection
  339. */
  340. public function addDaysFilter($constraints)
  341. {
  342. if (!is_array($constraints)) {
  343. return $this;
  344. }
  345. $filter = array();
  346. $now = Mage::getSingleton('core/date')->date();
  347. $gmtOffset = (int) Mage::getSingleton('core/date')->getGmtOffset();
  348. if (isset($constraints['from'])) {
  349. $lastDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  350. $lastDay->subSecond($gmtOffset)
  351. ->subDay($constraints['from'] - 1);
  352. $filter['to'] = $lastDay;
  353. }
  354. if (isset($constraints['to'])) {
  355. $firstDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  356. $firstDay->subSecond($gmtOffset)
  357. ->subDay($constraints['to']);
  358. $filter['from'] = $firstDay;
  359. }
  360. if ($filter) {
  361. $filter['datetime'] = true;
  362. $this->addFieldToFilter('added_at', $filter);
  363. }
  364. return $this;
  365. }
  366. /**
  367. * Joins product name attribute value to use it in WHERE and ORDER clauses
  368. *
  369. * @return Mage_Wishlist_Model_Resource_Item_Collection
  370. */
  371. protected function _joinProductNameTable()
  372. {
  373. if (!$this->_isProductNameJoined) {
  374. $entityTypeId = Mage::getResourceModel('catalog/config')
  375. ->getEntityTypeId();
  376. $attribute = Mage::getModel('catalog/entity_attribute')
  377. ->loadByCode($entityTypeId, 'name');
  378. $storeId = Mage::app()->getStore()->getId();
  379. $this->getSelect()
  380. ->join(
  381. array('product_name_table' => $attribute->getBackendTable()),
  382. 'product_name_table.entity_id=main_table.product_id' .
  383. ' AND product_name_table.store_id=' . $storeId .
  384. ' AND product_name_table.attribute_id=' . $attribute->getId().
  385. ' AND product_name_table.entity_type_id=' . $entityTypeId,
  386. array()
  387. );
  388. $this->_isProductNameJoined = true;
  389. }
  390. return $this;
  391. }
  392. /**
  393. * Adds filter on product name
  394. *
  395. * @param string $productName
  396. * @return Mage_Wishlist_Model_Resource_Item_Collection
  397. */
  398. public function addProductNameFilter($productName)
  399. {
  400. $this->_joinProductNameTable();
  401. $this->getSelect()
  402. ->where('INSTR(product_name_table.value, ?)', $productName);
  403. return $this;
  404. }
  405. /**
  406. * Sets ordering by product name
  407. *
  408. * @param string $dir
  409. * @return Mage_Wishlist_Model_Resource_Item_Collection
  410. */
  411. public function setOrderByProductName($dir)
  412. {
  413. $this->_joinProductNameTable();
  414. $this->getSelect()->order('product_name_table.value ' . $dir);
  415. return $this;
  416. }
  417. /**
  418. * Get sum of items collection qty
  419. *
  420. * @return int
  421. */
  422. public function getItemsQty(){
  423. if (is_null($this->_itemsQty)) {
  424. $this->_itemsQty = 0;
  425. foreach ($this as $wishlistItem) {
  426. $qty = $wishlistItem->getQty();
  427. $this->_itemsQty += ($qty === 0) ? 1 : $qty;
  428. }
  429. }
  430. return (int)$this->_itemsQty;
  431. }
  432. }