PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/acidel/buykoala
PHP | 471 lines | 225 code | 56 blank | 190 comment | 18 complexity | 6c3c58c21392a4d4c838eb9ad7d8ce91 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. /* @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. $productCollection->addPriceData()
  156. ->addTaxPercents()
  157. ->addIdFilter($this->_productIds)
  158. ->addAttributeToSelect($attributes)
  159. ->addOptionsToResult()
  160. ->addUrlRewrite();
  161. if ($this->_productVisible) {
  162. Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection);
  163. }
  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->isSalable()) {
  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 filter by shared stores
  203. *
  204. * @param array $storeIds
  205. *
  206. * @return Mage_Wishlist_Model_Resource_Item_Collection
  207. */
  208. public function addStoreFilter($storeIds = array())
  209. {
  210. if (!is_array($storeIds)) {
  211. $storeIds = array($storeIds);
  212. }
  213. $this->_storeIds = $storeIds;
  214. $this->addFieldToFilter('store_id', array('in' => $this->_storeIds));
  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. * @deprecated after 1.6.0.0-rc2
  235. * @see Varien_Data_Collection_Db::setOrder() is used instead
  236. *
  237. * @param string $attribute
  238. * @param string $dir
  239. * @return Mage_Wishlist_Model_Resource_Item_Collection
  240. */
  241. public function addWishListSortOrder($attribute = 'added_at', $dir = 'desc')
  242. {
  243. $this->setOrder($attribute, $dir);
  244. return $this;
  245. }
  246. /**
  247. * Reset sort order
  248. *
  249. * @return Mage_Wishlist_Model_Resource_Item_Collection
  250. */
  251. public function resetSortOrder()
  252. {
  253. $this->getSelect()->reset(Zend_Db_Select::ORDER);
  254. return $this;
  255. }
  256. /**
  257. * Set product Visibility Filter to product collection flag
  258. *
  259. * @param bool $flag
  260. * @return Mage_Wishlist_Model_Resource_Item_Collection
  261. */
  262. public function setVisibilityFilter($flag = true)
  263. {
  264. $this->_productVisible = (bool)$flag;
  265. return $this;
  266. }
  267. /**
  268. * Set Salable Filter.
  269. * This filter apply Salable Product Types Filter to product collection.
  270. *
  271. * @param bool $flag
  272. * @return Mage_Wishlist_Model_Resource_Item_Collection
  273. */
  274. public function setSalableFilter($flag = true)
  275. {
  276. $this->_productSalable = (bool)$flag;
  277. return $this;
  278. }
  279. /**
  280. * Set In Stock Filter.
  281. * This filter remove items with no salable product.
  282. *
  283. * @param bool $flag
  284. * @return Mage_Wishlist_Model_Resource_Item_Collection
  285. */
  286. public function setInStockFilter($flag = true)
  287. {
  288. $this->_productInStock = (bool)$flag;
  289. return $this;
  290. }
  291. /**
  292. * Set add days in whishlist
  293. *
  294. * This method appears in 1.5.0.0 in deprecated state, because:
  295. * - we need it to make wishlist item collection interface as much as possible compatible with old
  296. * wishlist product collection
  297. * - this method is useless because we can calculate days in php, and don't use MySQL for it
  298. *
  299. * @deprecated after 1.4.2.0
  300. * @return Mage_Wishlist_Model_Resource_Item_Collection
  301. */
  302. public function addDaysInWishlist()
  303. {
  304. $this->_addDaysInWishlist = true;
  305. $adapter = $this->getConnection();
  306. $dateModel = Mage::getSingleton('core/date');
  307. $resHelper = Mage::getResourceHelper('core');
  308. $offsetFromDb = (int) $dateModel->getGmtOffset();
  309. $startDate = $adapter->getDateAddSql('added_at', $offsetFromDb, Varien_Db_Adapter_Interface::INTERVAL_SECOND);
  310. $nowDate = $dateModel->date();
  311. $dateDiff = $resHelper->getDateDiff($startDate, $adapter->formatDate($nowDate));
  312. $this->getSelect()->columns(array('days_in_wishlist' => $dateDiff));
  313. return $this;
  314. }
  315. /**
  316. * Adds filter on days in wishlist
  317. *
  318. * $constraints may contain 'from' and 'to' indexes with number of days to look for items
  319. *
  320. * @param array $constraints
  321. * @return Mage_Wishlist_Model_Resource_Item_Collection
  322. */
  323. public function addDaysFilter($constraints)
  324. {
  325. if (!is_array($constraints)) {
  326. return $this;
  327. }
  328. $filter = array();
  329. $now = Mage::getSingleton('core/date')->date();
  330. $gmtOffset = (int) Mage::getSingleton('core/date')->getGmtOffset();
  331. if (isset($constraints['from'])) {
  332. $lastDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  333. $lastDay->subSecond($gmtOffset)
  334. ->subDay($constraints['from'] - 1);
  335. $filter['to'] = $lastDay;
  336. }
  337. if (isset($constraints['to'])) {
  338. $firstDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
  339. $firstDay->subSecond($gmtOffset)
  340. ->subDay($constraints['to']);
  341. $filter['from'] = $firstDay;
  342. }
  343. if ($filter) {
  344. $filter['datetime'] = true;
  345. $this->addFieldToFilter('added_at', $filter);
  346. }
  347. return $this;
  348. }
  349. /**
  350. * Joins product name attribute value to use it in WHERE and ORDER clauses
  351. *
  352. * @return Mage_Wishlist_Model_Resource_Item_Collection
  353. */
  354. protected function _joinProductNameTable()
  355. {
  356. if (!$this->_isProductNameJoined) {
  357. $entityTypeId = Mage::getResourceModel('catalog/config')
  358. ->getEntityTypeId();
  359. $attribute = Mage::getModel('catalog/entity_attribute')
  360. ->loadByCode($entityTypeId, 'name');
  361. $storeId = Mage::app()->getStore()->getId();
  362. $this->getSelect()
  363. ->join(
  364. array('product_name_table' => $attribute->getBackendTable()),
  365. 'product_name_table.entity_id=main_table.product_id' .
  366. ' AND product_name_table.store_id=' . $storeId .
  367. ' AND product_name_table.attribute_id=' . $attribute->getId().
  368. ' AND product_name_table.entity_type_id=' . $entityTypeId,
  369. array()
  370. );
  371. $this->_isProductNameJoined = true;
  372. }
  373. return $this;
  374. }
  375. /**
  376. * Adds filter on product name
  377. *
  378. * @param string $productName
  379. * @return Mage_Wishlist_Model_Resource_Item_Collection
  380. */
  381. public function addProductNameFilter($productName)
  382. {
  383. $this->_joinProductNameTable();
  384. $this->getSelect()
  385. ->where('INSTR(product_name_table.value, ?)', $productName);
  386. return $this;
  387. }
  388. /**
  389. * Sets ordering by product name
  390. *
  391. * @param string $dir
  392. * @return Mage_Wishlist_Model_Resource_Item_Collection
  393. */
  394. public function setOrderByProductName($dir)
  395. {
  396. $this->_joinProductNameTable();
  397. $this->getSelect()->order('product_name_table.value ' . $dir);
  398. return $this;
  399. }
  400. /**
  401. * Get sum of items collection qty
  402. *
  403. * @return int
  404. */
  405. public function getItemsQty(){
  406. if (is_null($this->_itemsQty)) {
  407. $this->_itemsQty = 0;
  408. foreach ($this as $wishlistItem) {
  409. $qty = $wishlistItem->getQty();
  410. $this->_itemsQty += ($qty === 0) ? 1 : $qty;
  411. }
  412. }
  413. return (int)$this->_itemsQty;
  414. }
  415. }