1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Wishlist_Model_Resource_Item_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_productVisible = false;
43:
44: 45: 46: 47: 48:
49: protected $_productSalable = false;
50:
51: 52: 53: 54: 55:
56: protected $_productInStock = false;
57:
58: 59: 60: 61: 62:
63: protected $_productIds = array();
64:
65: 66: 67: 68: 69:
70: protected $_storeIds = array();
71:
72: 73: 74: 75: 76:
77: protected $_addDaysInWishlist = false;
78:
79: 80: 81: 82: 83:
84: protected $_itemsQty;
85:
86: 87: 88: 89: 90:
91: protected $_isProductNameJoined = false;
92:
93: 94: 95: 96: 97:
98: public function _construct()
99: {
100: $this->_init('wishlist/item');
101: $this->addFilterToMap('store_id', 'main_table.store_id');
102: }
103:
104: 105: 106: 107: 108:
109: protected function _afterLoad()
110: {
111: parent::_afterLoad();
112:
113: 114: 115:
116: $this->_assignOptions();
117: $this->_assignProducts();
118: $this->resetItemsDataChanged();
119:
120: $this->getPageSize();
121:
122: return $this;
123: }
124:
125: 126: 127: 128: 129:
130: protected function _assignOptions()
131: {
132: $itemIds = array_keys($this->_items);
133:
134: $optionCollection = Mage::getModel('wishlist/item_option')->getCollection();
135: $optionCollection->addItemFilter($itemIds);
136:
137:
138: foreach ($this as $item) {
139: $item->setOptions($optionCollection->getOptionsByItem($item));
140: }
141: $productIds = $optionCollection->getProductIds();
142: $this->_productIds = array_merge($this->_productIds, $productIds);
143:
144: return $this;
145: }
146:
147: 148: 149: 150: 151:
152: protected function _assignProducts()
153: {
154: Varien_Profiler::start('WISHLIST:'.__METHOD__);
155: $productIds = array();
156:
157: $isStoreAdmin = Mage::app()->getStore()->isAdmin();
158:
159: $storeIds = array();
160: foreach ($this as $item) {
161: $productIds[$item->getProductId()] = 1;
162: if ($isStoreAdmin && !in_array($item->getStoreId(), $storeIds)) {
163: $storeIds[] = $item->getStoreId();
164: }
165: }
166: if (!$isStoreAdmin) {
167: $storeIds = $this->_storeIds;
168: }
169:
170: $this->_productIds = array_merge($this->_productIds, array_keys($productIds));
171: $attributes = Mage::getSingleton('wishlist/config')->getProductAttributes();
172: $productCollection = Mage::getModel('catalog/product')->getCollection();
173: foreach ($storeIds as $id) {
174: $productCollection->addStoreFilter($id);
175: }
176:
177: if ($this->_productVisible) {
178: Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($productCollection);
179: }
180:
181: $productCollection->addPriceData()
182: ->addTaxPercents()
183: ->addIdFilter($this->_productIds)
184: ->addAttributeToSelect($attributes)
185: ->addOptionsToResult()
186: ->addUrlRewrite();
187:
188: if ($this->_productSalable) {
189: $productCollection = Mage::helper('adminhtml/sales')->applySalableProductTypesFilter($productCollection);
190: }
191:
192: Mage::dispatchEvent('wishlist_item_collection_products_after_load', array(
193: 'product_collection' => $productCollection
194: ));
195:
196: $checkInStock = $this->_productInStock && !Mage::helper('cataloginventory')->isShowOutOfStock();
197:
198: foreach ($this as $item) {
199: $product = $productCollection->getItemById($item->getProductId());
200: if ($product) {
201: if ($checkInStock && !$product->isInStock()) {
202: $this->removeItemByKey($item->getId());
203: } else {
204: $product->setCustomOptions(array());
205: $item->setProduct($product);
206: $item->setProductName($product->getName());
207: $item->setName($product->getName());
208: $item->setPrice($product->getPrice());
209: }
210: } else {
211: $item->isDeleted(true);
212: }
213: }
214:
215: Varien_Profiler::stop('WISHLIST:'.__METHOD__);
216:
217: return $this;
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function addWishlistFilter(Mage_Wishlist_Model_Wishlist $wishlist)
227: {
228: $this->addFieldToFilter('wishlist_id', $wishlist->getId());
229: return $this;
230: }
231:
232: 233: 234: 235: 236: 237:
238: public function addCustomerIdFilter($customerId)
239: {
240: $this->getSelect()
241: ->join(
242: array('wishlist' => $this->getTable('wishlist/wishlist')),
243: 'main_table.wishlist_id = wishlist.wishlist_id',
244: array()
245: )
246: ->where('wishlist.customer_id = ?', $customerId);
247: return $this;
248: }
249:
250: 251: 252: 253: 254: 255: 256:
257: public function addStoreFilter($storeIds = array())
258: {
259: if (!is_array($storeIds)) {
260: $storeIds = array($storeIds);
261: }
262: $this->_storeIds = $storeIds;
263: $this->addFieldToFilter('store_id', array('in' => $this->_storeIds));
264:
265: return $this;
266: }
267:
268: 269: 270: 271: 272:
273: public function addStoreData()
274: {
275: $storeTable = Mage::getSingleton('core/resource')->getTableName('core/store');
276: $this->getSelect()->join(array('store'=>$storeTable), 'main_table.store_id=store.store_id', array(
277: 'store_name'=>'name',
278: 'item_store_id' => 'store_id'
279: ));
280: return $this;
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290: 291: 292:
293: public function addWishListSortOrder($attribute = 'added_at', $dir = 'desc')
294: {
295: $this->setOrder($attribute, $dir);
296: return $this;
297: }
298:
299: 300: 301: 302: 303:
304: public function resetSortOrder()
305: {
306: $this->getSelect()->reset(Zend_Db_Select::ORDER);
307: return $this;
308: }
309:
310: 311: 312: 313: 314: 315:
316: public function setVisibilityFilter($flag = true)
317: {
318: $this->_productVisible = (bool)$flag;
319: return $this;
320: }
321:
322: 323: 324: 325: 326: 327: 328:
329: public function setSalableFilter($flag = true)
330: {
331: $this->_productSalable = (bool)$flag;
332: return $this;
333: }
334:
335: 336: 337: 338: 339: 340: 341:
342: public function setInStockFilter($flag = true)
343: {
344: $this->_productInStock = (bool)$flag;
345: return $this;
346: }
347:
348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358:
359: public function addDaysInWishlist()
360: {
361: $this->_addDaysInWishlist = true;
362:
363: $adapter = $this->getConnection();
364: $dateModel = Mage::getSingleton('core/date');
365: $resHelper = Mage::getResourceHelper('core');
366:
367: $offsetFromDb = (int) $dateModel->getGmtOffset();
368: $startDate = $adapter->getDateAddSql('added_at', $offsetFromDb, Varien_Db_Adapter_Interface::INTERVAL_SECOND);
369:
370: $nowDate = $dateModel->date();
371: $dateDiff = $resHelper->getDateDiff($startDate, $adapter->formatDate($nowDate));
372:
373: $this->getSelect()->columns(array('days_in_wishlist' => $dateDiff));
374: return $this;
375: }
376:
377: 378: 379: 380: 381: 382: 383: 384:
385: public function addDaysFilter($constraints)
386: {
387: if (!is_array($constraints)) {
388: return $this;
389: }
390:
391: $filter = array();
392:
393: $now = Mage::getSingleton('core/date')->date();
394: $gmtOffset = (int) Mage::getSingleton('core/date')->getGmtOffset();
395: if (isset($constraints['from'])) {
396: $lastDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
397: $lastDay->subSecond($gmtOffset)
398: ->subDay($constraints['from'] - 1);
399: $filter['to'] = $lastDay;
400: }
401:
402: if (isset($constraints['to'])) {
403: $firstDay = new Zend_Date($now, Varien_Date::DATETIME_INTERNAL_FORMAT);
404: $firstDay->subSecond($gmtOffset)
405: ->subDay($constraints['to']);
406: $filter['from'] = $firstDay;
407: }
408:
409: if ($filter) {
410: $filter['datetime'] = true;
411: $this->addFieldToFilter('added_at', $filter);
412: }
413:
414: return $this;
415: }
416:
417: 418: 419: 420: 421:
422: protected function _joinProductNameTable()
423: {
424: if (!$this->_isProductNameJoined) {
425: $entityTypeId = Mage::getResourceModel('catalog/config')
426: ->getEntityTypeId();
427: $attribute = Mage::getModel('catalog/entity_attribute')
428: ->loadByCode($entityTypeId, 'name');
429:
430: $storeId = Mage::app()->getStore()->getId();
431:
432: $this->getSelect()
433: ->join(
434: array('product_name_table' => $attribute->getBackendTable()),
435: 'product_name_table.entity_id=main_table.product_id' .
436: ' AND product_name_table.store_id=' . $storeId .
437: ' AND product_name_table.attribute_id=' . $attribute->getId().
438: ' AND product_name_table.entity_type_id=' . $entityTypeId,
439: array()
440: );
441:
442: $this->_isProductNameJoined = true;
443: }
444: return $this;
445: }
446:
447: 448: 449: 450: 451: 452:
453: public function addProductNameFilter($productName)
454: {
455: $this->_joinProductNameTable();
456: $this->getSelect()
457: ->where('INSTR(product_name_table.value, ?)', $productName);
458:
459: return $this;
460: }
461:
462: 463: 464: 465: 466: 467:
468: public function setOrderByProductName($dir)
469: {
470: $this->_joinProductNameTable();
471: $this->getSelect()->order('product_name_table.value ' . $dir);
472: return $this;
473: }
474:
475: 476: 477: 478: 479:
480: public function getItemsQty(){
481: if (is_null($this->_itemsQty)) {
482: $this->_itemsQty = 0;
483: foreach ($this as $wishlistItem) {
484: $qty = $wishlistItem->getQty();
485: $this->_itemsQty += ($qty === 0) ? 1 : $qty;
486: }
487: }
488:
489: return (int)$this->_itemsQty;
490: }
491: }
492: