Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Wishlist_Block_Abstract
  • Mage_Wishlist_Block_Customer_Sharing
  • Mage_Wishlist_Block_Customer_Sidebar
  • Mage_Wishlist_Block_Customer_Wishlist
  • Mage_Wishlist_Block_Customer_Wishlist_Button
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Column
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Column_Cart
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Column_Comment
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Column_Image
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Column_Remove
  • Mage_Wishlist_Block_Customer_Wishlist_Item_Options
  • Mage_Wishlist_Block_Customer_Wishlist_Items
  • Mage_Wishlist_Block_Item_Configure
  • Mage_Wishlist_Block_Links
  • Mage_Wishlist_Block_Render_Item_Price
  • Mage_Wishlist_Block_Share_Email_Items
  • Mage_Wishlist_Block_Share_Email_Rss
  • Mage_Wishlist_Block_Share_Wishlist
  • Mage_Wishlist_Controller_Abstract
  • Mage_Wishlist_Helper_Data
  • Mage_Wishlist_IndexController
  • Mage_Wishlist_Model_Config
  • Mage_Wishlist_Model_Config_Source_Summary
  • Mage_Wishlist_Model_Item
  • Mage_Wishlist_Model_Item_Option
  • Mage_Wishlist_Model_Mysql4_Item
  • Mage_Wishlist_Model_Mysql4_Item_Collection
  • Mage_Wishlist_Model_Mysql4_Item_Option
  • Mage_Wishlist_Model_Mysql4_Item_Option_Collection
  • Mage_Wishlist_Model_Mysql4_Product_Collection
  • Mage_Wishlist_Model_Mysql4_Wishlist
  • Mage_Wishlist_Model_Mysql4_Wishlist_Collection
  • Mage_Wishlist_Model_Observer
  • Mage_Wishlist_Model_Resource_Item
  • Mage_Wishlist_Model_Resource_Item_Collection
  • Mage_Wishlist_Model_Resource_Item_Option
  • Mage_Wishlist_Model_Resource_Item_Option_Collection
  • Mage_Wishlist_Model_Resource_Wishlist
  • Mage_Wishlist_Model_Resource_Wishlist_Collection
  • Mage_Wishlist_Model_Session
  • Mage_Wishlist_Model_Wishlist
  • Mage_Wishlist_SharedController
  • Overview
  • Package
  • Class
  • Tree
  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) 2012 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: 
 28: /**
 29:  * Wishlist item collection
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Wishlist
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Wishlist_Model_Resource_Item_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
 36: {
 37:     /**
 38:      * Product Visibility Filter to product collection flag
 39:      *
 40:      * @var bool
 41:      */
 42:     protected $_productVisible = false;
 43: 
 44:     /**
 45:      * Product Salable Filter to product collection flag
 46:      *
 47:      * @var bool
 48:      */
 49:     protected $_productSalable = false;
 50: 
 51:     /**
 52:      * If product out of stock, its item will be removed after load
 53:      *
 54:      * @var bool
 55:       */
 56:     protected $_productInStock = false;
 57: 
 58:     /**
 59:      * Product Ids array
 60:      *
 61:      * @var array
 62:      */
 63:     protected $_productIds = array();
 64: 
 65:     /**
 66:      * Store Ids array
 67:      *
 68:      * @var array
 69:      */
 70:     protected $_storeIds = array();
 71: 
 72:     /**
 73:      * Add days in whishlist filter of product collection
 74:      *
 75:      * @var boolean
 76:      */
 77:     protected $_addDaysInWishlist = false;
 78: 
 79:     /**
 80:      * Sum of items collection qty
 81:      *
 82:      * @var int
 83:      */
 84:     protected $_itemsQty;
 85: 
 86:     /**
 87:      * Whether product name attribute value table is joined in select
 88:      *
 89:      * @var boolean
 90:      */
 91:     protected $_isProductNameJoined = false;
 92: 
 93:     /**
 94:      * Initialize resource model for collection
 95:      *
 96:      * @return void
 97:      */
 98:     public function _construct()
 99:     {
100:         $this->_init('wishlist/item');
101:         $this->addFilterToMap('store_id', 'main_table.store_id');
102:     }
103: 
104:     /**
105:      * After load processing
106:      *
107:      * @return Mage_Wishlist_Model_Resource_Item_Collection
108:      */
109:     protected function _afterLoad()
110:     {
111:         parent::_afterLoad();
112: 
113:         /**
114:          * Assign products
115:          */
116:         $this->_assignOptions();
117:         $this->_assignProducts();
118:         $this->resetItemsDataChanged();
119: 
120:         $this->getPageSize();
121: 
122:         return $this;
123:     }
124: 
125:     /**
126:      * Add options to items
127:      *
128:      * @return Mage_Wishlist_Model_Resource_Item_Collection
129:      */
130:     protected function _assignOptions()
131:     {
132:         $itemIds = array_keys($this->_items);
133:         /* @var $optionCollection Mage_Wishlist_Model_Resource_Item_Option_Collection */
134:         $optionCollection = Mage::getModel('wishlist/item_option')->getCollection();
135:         $optionCollection->addItemFilter($itemIds);
136: 
137:         /* @var $item Mage_Wishlist_Model_Item */
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:      * Add products to items and item options
149:      *
150:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Add filter by wishlist object
222:      *
223:      * @param Mage_Wishlist_Model_Wishlist $wishlist
224:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Add filtration by customer id
234:      *
235:      * @param int $customerId
236:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Add filter by shared stores
252:      *
253:      * @param array $storeIds
254:      *
255:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Add items store data to collection
270:      *
271:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Add wishlist sort order
285:      *
286:      * @deprecated after 1.6.0.0-rc2
287:      * @see Varien_Data_Collection_Db::setOrder() is used instead
288:      *
289:      * @param string $attribute
290:      * @param string $dir
291:      * @return Mage_Wishlist_Model_Resource_Item_Collection
292:      */
293:     public function addWishListSortOrder($attribute = 'added_at', $dir = 'desc')
294:     {
295:         $this->setOrder($attribute, $dir);
296:         return $this;
297:     }
298: 
299:     /**
300:      * Reset sort order
301:      *
302:      * @return Mage_Wishlist_Model_Resource_Item_Collection
303:      */
304:     public function resetSortOrder()
305:     {
306:         $this->getSelect()->reset(Zend_Db_Select::ORDER);
307:         return $this;
308:     }
309: 
310:     /**
311:      * Set product Visibility Filter to product collection flag
312:      *
313:      * @param bool $flag
314:      * @return Mage_Wishlist_Model_Resource_Item_Collection
315:      */
316:     public function setVisibilityFilter($flag = true)
317:     {
318:         $this->_productVisible = (bool)$flag;
319:         return $this;
320:     }
321: 
322:     /**
323:      * Set Salable Filter.
324:      * This filter apply Salable Product Types Filter to product collection.
325:      *
326:      * @param bool $flag
327:      * @return Mage_Wishlist_Model_Resource_Item_Collection
328:      */
329:     public function setSalableFilter($flag = true)
330:     {
331:         $this->_productSalable = (bool)$flag;
332:         return $this;
333:     }
334: 
335:     /**
336:      * Set In Stock Filter.
337:      * This filter remove items with no salable product.
338:      *
339:      * @param bool $flag
340:      * @return Mage_Wishlist_Model_Resource_Item_Collection
341:      */
342:     public function setInStockFilter($flag = true)
343:     {
344:         $this->_productInStock = (bool)$flag;
345:         return $this;
346:     }
347: 
348:     /**
349:      * Set add days in whishlist
350:      *
351:      * This method appears in 1.5.0.0 in deprecated state, because:
352:      * - we need it to make wishlist item collection interface as much as possible compatible with old
353:      *   wishlist product collection
354:      * - this method is useless because we can calculate days in php, and don't use MySQL for it
355:      *
356:      * @deprecated after 1.4.2.0
357:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Adds filter on days in wishlist
379:      *
380:      * $constraints may contain 'from' and 'to' indexes with number of days to look for items
381:      *
382:      * @param array $constraints
383:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Joins product name attribute value to use it in WHERE and ORDER clauses
419:      *
420:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Adds filter on product name
449:      *
450:      * @param string $productName
451:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Sets ordering by product name
464:      *
465:      * @param string $dir
466:      * @return Mage_Wishlist_Model_Resource_Item_Collection
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:      * Get sum of items collection qty
477:      *
478:      * @return int
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0