PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/rgranadino/magento-mirror
PHP | 468 lines | 228 code | 56 blank | 184 comment | 19 complexity | 5c3a9fe9cc827ccf003c2fd8d7aa6268 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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. $optionCollection = Mage::getModel('wishlist/item_option')->getCollection()
  119. ->addItemFilter($itemIds);
  120. foreach ($this as $item) {
  121. $item->setOptions($optionCollection->getOptionsByItem($item));
  122. }
  123. $productIds = $optionCollection->getProductIds();
  124. $this->_productIds = array_merge($this->_productIds, $productIds);
  125. return $this;
  126. }
  127. /**
  128. * Add products to items and item options
  129. *
  130. * @return Mage_Wishlist_Model_Resource_Item_Collection
  131. */
  132. protected function _assignProducts()
  133. {
  134. Varien_Profiler::start('WISHLIST:'.__METHOD__);
  135. $productIds = array();
  136. $isStoreAdmin = Mage::app()->getStore()->isAdmin();
  137. $storeIds = array();
  138. foreach ($this as $item) {
  139. $productIds[$item->getProductId()] = 1;
  140. if ($isStoreAdmin && !in_array($item->getStoreId(), $storeIds)) {
  141. $storeIds[] = $item->getStoreId();
  142. }
  143. }
  144. if (!$isStoreAdmin) {
  145. $storeIds = $this->_storeIds;
  146. }
  147. $this->_productIds = array_merge($this->_productIds, array_keys($productIds));
  148. $attributes = Mage::getSingleton('wishlist/config')->getProductAttributes();
  149. $productCollection = Mage::getModel('catalog/product')->getCollection();
  150. foreach ($storeIds as $id) {
  151. $productCollection->addStoreFilter($id);
  152. }
  153. $productCollection->addPriceData()
  154. ->addTaxPercents()
  155. ->addIdFilter($this->_productIds)
  156. ->addAttributeToSelect($attributes)
  157. ->addOptionsToResult()
  158. ->addUrlRewrite();
  159. if ($this->_productVisible) {
  160. Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection);
  161. }
  162. if ($this->_productSalable) {
  163. $productCollection = Mage::helper('adminhtml/sales')->applySalableProductTypesFilter($productCollection);
  164. }
  165. Mage::dispatchEvent('wishlist_item_collection_products_after_load', array(
  166. 'product_collection' => $productCollection
  167. ));
  168. $checkInStock = $this->_productInStock && !Mage::helper('cataloginventory')->isShowOutOfStock();
  169. foreach ($this as $item) {
  170. $product = $productCollection->getItemById($item->getProductId());
  171. if ($product) {
  172. if ($checkInStock && !$product->isSalable()) {
  173. $this->removeItemByKey($item->getId());
  174. } else {
  175. $product->setCustomOptions(array());
  176. $item->setProduct($product);
  177. $item->setProductName($product->getName());
  178. $item->setName($product->getName());
  179. $item->setPrice($product->getPrice());
  180. }
  181. } else {
  182. $item->isDeleted(true);
  183. }
  184. }
  185. Varien_Profiler::stop('WISHLIST:'.__METHOD__);
  186. return $this;
  187. }
  188. /**
  189. * Add filter by wishlist object
  190. *
  191. * @param Mage_Wishlist_Model_Wishlist $wishlist
  192. * @return Mage_Wishlist_Model_Resource_Item_Collection
  193. */
  194. public function addWishlistFilter(Mage_Wishlist_Model_Wishlist $wishlist)
  195. {
  196. $this->addFieldToFilter('wishlist_id', $wishlist->getId());
  197. return $this;
  198. }
  199. /**
  200. * Add filter by shared stores
  201. *
  202. * @param int|array $store
  203. * @return Mage_Wishlist_Model_Resource_Item_Collection
  204. */
  205. public function addStoreFilter($store = null)
  206. {
  207. if (is_null($store)) {
  208. $store = Mage::app()->getStore()->getId();
  209. }
  210. if (!is_array($store)) {
  211. $store = array($store);
  212. }
  213. $this->_storeIds = $store;
  214. $this->addFieldToFilter('store_id', $store);
  215. return $this;
  216. }
  217. /**
  218. * Add items store data to collection
  219. *
  220. * @return Mage_Wishlist_Model_Resource_Item_Collection
  221. */
  222. public function addStoreData()
  223. {
  224. $storeTable = Mage::getSingleton('core/resource')->getTableName('core/store');
  225. $this->getSelect()->join(array('store'=>$storeTable), 'main_table.store_id=store.store_id', array(
  226. 'store_name'=>'name',
  227. 'item_store_id' => 'store_id'
  228. ));
  229. return $this;
  230. }
  231. /**
  232. * Add wishlist sort order
  233. *
  234. * @param string $attribute
  235. * @param string $dir
  236. * @return Mage_Wishlist_Model_Resource_Item_Collection
  237. */
  238. public function addWishListSortOrder($attribute = 'added_at', $dir = 'desc')
  239. {
  240. $this->setOrder($attribute, $dir);
  241. return $this;
  242. }
  243. /**
  244. * Reset sort order
  245. *
  246. * @return Mage_Wishlist_Model_Resource_Item_Collection
  247. */
  248. public function resetSortOrder()
  249. {
  250. $this->getSelect()->reset(Zend_Db_Select::ORDER);
  251. return $this;
  252. }
  253. /**
  254. * Set product Visibility Filter to product collection flag
  255. *
  256. * @param bool $flag
  257. * @return Mage_Wishlist_Model_Resource_Item_Collection
  258. */
  259. public function setVisibilityFilter($flag = true)
  260. {
  261. $this->_productVisible = (bool)$flag;
  262. return $this;
  263. }
  264. /**
  265. * Set Salable Filter.
  266. * This filter apply Salable Product Types Filter to product collection.
  267. *
  268. * @param bool $flag
  269. * @return Mage_Wishlist_Model_Resource_Item_Collection
  270. */
  271. public function setSalableFilter($flag = true)
  272. {
  273. $this->_productSalable = (bool)$flag;
  274. return $this;
  275. }
  276. /**
  277. * Set In Stock Filter.
  278. * This filter remove items with no salable product.
  279. *
  280. * @param bool $flag
  281. * @return Mage_Wishlist_Model_Resource_Item_Collection
  282. */
  283. public function setInStockFilter($flag = true)
  284. {
  285. $this->_productInStock = (bool)$flag;
  286. return $this;
  287. }
  288. /**
  289. * Set add days in whishlist
  290. *
  291. * This method appears in 1.5.0.0 in deprecated state, because:
  292. * - we need it to make wishlist item collection interface as much as possible compatible with old
  293. * wishlist product collection
  294. * - this method is useless because we can calculate days in php, and don't use MySQL for it
  295. *
  296. * @deprecated after 1.4.2.0
  297. * @return Mage_Wishlist_Model_Resource_Item_Collection
  298. */
  299. public function addDaysInWishlist()
  300. {
  301. $this->_addDaysInWishlist = true;
  302. $adapter = $this->getConnection();
  303. $dateModel = Mage::getSingleton('core/date');
  304. $resHelper = Mage::getResourceHelper('core');
  305. $offsetFromDb = (int) $dateModel->getGmtOffset();
  306. $startDate = $adapter->getDateAddSql('added_at', $offsetFromDb, Varien_Db_Adapter_Interface::INTERVAL_SECOND);
  307. $nowDate = $dateModel->date();
  308. $dateDiff = $resHelper->getDateDiff($startDate, $adapter->formatDate($nowDate));
  309. $this->getSelect()->columns(array('days_in_wishlist' => $dateDiff));
  310. return $this;
  311. }
  312. /**
  313. * Adds filter on days in wishlist
  314. *
  315. * $constraints may contain 'from' and 'to' indexes with number of days to look for items
  316. *
  317. * @param array $constraints
  318. * @return Mage_Wishlist_Model_Resource_Item_Collection
  319. */
  320. public function addDaysFilter($constraints)
  321. {
  322. if (!is_array($constraints)) {
  323. return $this;
  324. }
  325. $filter = array();
  326. $now = Mage::getSingleton('core/date')->date();
  327. $gmtOffset = (int) Mage::getSingleton('core/date')->getGmtOffset();
  328. if (isset($constraints['from'])) {
  329. $lastDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  330. $lastDay->subSecond($gmtOffset)
  331. ->subDay($constraints['from'] - 1);
  332. $filter['to'] = $lastDay;
  333. }
  334. if (isset($constraints['to'])) {
  335. $firstDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  336. $firstDay->subSecond($gmtOffset)
  337. ->subDay($constraints['to']);
  338. $filter['from'] = $firstDay;
  339. }
  340. if ($filter) {
  341. $filter['datetime'] = true;
  342. $this->addFieldToFilter('added_at', $filter);
  343. }
  344. return $this;
  345. }
  346. /**
  347. * Joins product name attribute value to use it in WHERE and ORDER clauses
  348. *
  349. * @return Mage_Wishlist_Model_Resource_Item_Collection
  350. */
  351. protected function _joinProductNameTable()
  352. {
  353. if (!$this->_isProductNameJoined) {
  354. $entityTypeId = Mage::getResourceModel('catalog/config')
  355. ->getEntityTypeId();
  356. $attribute = Mage::getModel('catalog/entity_attribute')
  357. ->loadByCode($entityTypeId, 'name');
  358. $storeId = Mage::app()->getStore()->getId();
  359. $this->getSelect()
  360. ->join(
  361. array('product_name_table' => $attribute->getBackendTable()),
  362. 'product_name_table.entity_id=main_table.product_id' .
  363. ' AND product_name_table.store_id=' . $storeId .
  364. ' AND product_name_table.attribute_id=' . $attribute->getId().
  365. ' AND product_name_table.entity_type_id=' . $entityTypeId,
  366. array()
  367. );
  368. $this->_isProductNameJoined = true;
  369. }
  370. return $this;
  371. }
  372. /**
  373. * Adds filter on product name
  374. *
  375. * @param string $productName
  376. * @return Mage_Wishlist_Model_Resource_Item_Collection
  377. */
  378. public function addProductNameFilter($productName)
  379. {
  380. $this->_joinProductNameTable();
  381. $this->getSelect()
  382. ->where('INSTR(product_name_table.value, ?)', $productName);
  383. return $this;
  384. }
  385. /**
  386. * Sets ordering by product name
  387. *
  388. * @param string $dir
  389. * @return Mage_Wishlist_Model_Resource_Item_Collection
  390. */
  391. public function setOrderByProductName($dir)
  392. {
  393. $this->_joinProductNameTable();
  394. $this->getSelect()->order('product_name_table.value ' . $dir);
  395. return $this;
  396. }
  397. /**
  398. * Get sum of items collection qty
  399. *
  400. * @return int
  401. */
  402. public function getItemsQty(){
  403. if (is_null($this->_itemsQty)) {
  404. $this->_itemsQty = 0;
  405. foreach ($this as $wishlistItem) {
  406. $qty = $wishlistItem->getQty();
  407. $this->_itemsQty += ($qty === 0) ? 1 : $qty;
  408. }
  409. }
  410. return (int)$this->_itemsQty;
  411. }
  412. }