00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00035 extends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_flatEnabled = array();
00043
00044
00045
00046
00047
00048
00049 protected $_productWebsiteTable;
00050
00051
00052
00053
00054
00055
00056 protected $_productCategoryTable;
00057
00058
00059
00060
00061
00062
00063 protected $_addUrlRewrite = false;
00064
00065
00066
00067
00068
00069
00070 protected $_urlRewriteCategory = '';
00071
00072
00073
00074
00075
00076
00077 protected $_addMinimalPrice = false;
00078
00079
00080
00081
00082
00083
00084 protected $_addFinalPrice = false;
00085
00086
00087
00088
00089
00090
00091 protected $_allIdsCache = null;
00092
00093
00094
00095
00096
00097
00098 protected $_addTaxPercents = false;
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 protected $_productLimitationFilters = array();
00113
00114
00115
00116
00117
00118
00119 protected $_productCountSelect = null;
00120
00121
00122
00123
00124 protected $_isWebsiteFilter = false;
00125
00126
00127
00128
00129
00130
00131 public function getFlatHelper()
00132 {
00133 return Mage::helper('catalog/product_flat');
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 public function isEnabledFlat()
00143 {
00144 if (Mage::app()->getStore()->isAdmin()) {
00145 return false;
00146 }
00147 if (!isset($this->_flatEnabled[$this->getStoreId()])) {
00148 $this->_flatEnabled[$this->getStoreId()] = $this->getFlatHelper()
00149 ->isEnabled($this->getStoreId());
00150 }
00151 return $this->_flatEnabled[$this->getStoreId()];
00152 }
00153
00154
00155
00156
00157
00158 protected function _construct()
00159 {
00160 if ($this->isEnabledFlat()) {
00161 $this->_init('catalog/product', 'catalog/product_flat');
00162 }
00163 else {
00164 $this->_init('catalog/product');
00165 }
00166
00167 $this->_productWebsiteTable = $this->getResource()->getTable('catalog/product_website');
00168 $this->_productCategoryTable= $this->getResource()->getTable('catalog/category_product');
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 protected function _init($model, $entityModel=null)
00178 {
00179 if ($this->isEnabledFlat()) {
00180 $entityModel = 'catalog/product_flat';
00181 }
00182
00183 return parent::_init($model, $entityModel);
00184 }
00185
00186
00187
00188
00189
00190
00191 protected function _prepareStaticFields()
00192 {
00193 if ($this->isEnabledFlat()) {
00194 return $this;
00195 }
00196 return parent::_prepareStaticFields();
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 public function getNewEmptyItem()
00206 {
00207 $object = parent::getNewEmptyItem();
00208 if ($this->isEnabledFlat()) {
00209 $object->setIdFieldName($this->getEntity()->getIdFieldName());
00210 }
00211 return $object;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 public function setEntity($entity)
00221 {
00222 if ($this->isEnabledFlat() && ($entity instanceof Mage_Core_Model_Mysql4_Abstract)) {
00223 $this->_entity = $entity;
00224 return $this;
00225 }
00226 return parent::setEntity($entity);
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 public function setStore($store)
00236 {
00237 parent::setStore($store);
00238 if ($this->isEnabledFlat()) {
00239 $this->getEntity()->setStoreId($this->getStoreId());
00240 }
00241 return $this;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251 protected function _initSelect()
00252 {
00253 if ($this->isEnabledFlat()) {
00254 $this->getSelect()
00255 ->from(array('e' => $this->getEntity()->getFlatTableName()), null)
00256 ->from(null, array('status' => new Zend_Db_Expr(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)));
00257 $this->addAttributeToSelect(array('entity_id', 'type_id', 'attribute_set_id'));
00258 if ($this->getFlatHelper()->isAddChildData()) {
00259 $this->getSelect()
00260 ->where('e.is_child=?', 0);
00261 $this->addAttributeToSelect(array('child_id', 'is_child'));
00262 }
00263 }
00264 else {
00265 $this->getSelect()->from(array('e'=>$this->getEntity()->getEntityTable()));
00266 }
00267 return $this;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277 public function _loadAttributes($printQuery = false, $logQuery = false)
00278 {
00279 if ($this->isEnabledFlat()) {
00280 return $this;
00281 }
00282 return parent::_loadAttributes($printQuery, $logQuery);
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 public function addAttributeToSelect($attribute, $joinType = false)
00294 {
00295 if ($this->isEnabledFlat()) {
00296 if (!is_array($attribute)) {
00297 $attribute = array($attribute);
00298 }
00299 foreach ($attribute as $attributeCode) {
00300 if ($attributeCode == '*') {
00301 foreach ($this->getEntity()->getAllTableColumns() as $column) {
00302 $this->getSelect()->from(null, 'e.'.$column);
00303 $this->_selectAttributes[$column] = $column;
00304 $this->_staticFields[$column] = $column;
00305 }
00306 }
00307 else {
00308 if ($columns = $this->getEntity()->getAttributeForSelect($attributeCode)) {
00309 foreach ($columns as $alias => $column) {
00310 $this->getSelect()->from(null, array($alias => 'e.'.$column));
00311 $this->_selectAttributes[$column] = $column;
00312 $this->_staticFields[$column] = $column;
00313 }
00314 }
00315 }
00316 }
00317 return $this;
00318 }
00319 return parent::addAttributeToSelect($attribute, $joinType);
00320 }
00321
00322
00323
00324
00325
00326
00327 protected function _beforeLoad()
00328 {
00329 if ($this->_addFinalPrice) {
00330 $this->_joinPriceRules();
00331 }
00332 Mage::dispatchEvent('catalog_product_collection_load_before', array('collection'=>$this));
00333
00334 return parent::_beforeLoad();
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 protected function _afterLoad()
00344 {
00345 if ($this->_addUrlRewrite) {
00346 $this->_addUrlRewrite($this->_urlRewriteCategory);
00347 }
00348 if ($this->_addMinimalPrice) {
00349 $this->_addMinimalPrice();
00350 }
00351 if ($this->_addFinalPrice) {
00352 $this->_addFinalPrice();
00353 }
00354 if (count($this) > 0) {
00355 Mage::dispatchEvent('catalog_product_collection_load_after', array('collection'=>$this));
00356 }
00357 return $this;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366 public function addIdFilter($productId, $exclude = false)
00367 {
00368 if (empty($productId)) {
00369 $this->_setIsLoaded(true);
00370 return $this;
00371 }
00372 if (is_array($productId)) {
00373 if (!empty($productId)) {
00374 if ($exclude) {
00375 $condition = array('nin'=>$productId);
00376 } else {
00377 $condition = array('in'=>$productId);
00378 }
00379 }
00380 else {
00381 $condition = '';
00382 }
00383 }
00384 else {
00385 if ($exclude) {
00386 $condition = array('neq'=>$productId);
00387 } else {
00388 $condition = $productId;
00389 }
00390 }
00391 $this->addFieldToFilter('entity_id', $condition);
00392 return $this;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 public function addWebsiteNamesToResult()
00402 {
00403 $productStores = array();
00404 foreach ($this as $product) {
00405 $productWebsites[$product->getId()] = array();
00406 }
00407
00408 if (!empty($productWebsites)) {
00409 $select = $this->getConnection()->select()
00410 ->from(array('product_website'=>$this->_productWebsiteTable))
00411 ->join(
00412 array('website'=>$this->getResource()->getTable('core/website')),
00413 'website.website_id=product_website.website_id',
00414 array('name'))
00415 ->where($this->getConnection()->quoteInto(
00416 'product_website.product_id IN (?)',
00417 array_keys($productWebsites))
00418 )
00419 ->where('website.website_id>0');
00420
00421 $data = $this->getConnection()->fetchAll($select);
00422 foreach ($data as $row) {
00423 $productWebsites[$row['product_id']][] = $row['website_id'];
00424 }
00425 }
00426
00427 foreach ($this as $product) {
00428 if (isset($productWebsites[$product->getId()])) {
00429 $product->setData('websites', $productWebsites[$product->getId()]);
00430 }
00431 }
00432 return $this;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442 public function addStoreFilter($store=null)
00443 {
00444 if (is_null($store)) {
00445 $store = $this->getStoreId();
00446 }
00447 $store = Mage::app()->getStore($store);
00448
00449 if (!$store->isAdmin()) {
00450 $this->setStoreId($store);
00451 $this->_productLimitationFilters['store_id'] = $store->getId();
00452 $this->_applyProductLimitations();
00453 }
00454
00455 return $this;
00456 }
00457
00458
00459
00460
00461
00462
00463
00464 public function addWebsiteFilter($websites = null)
00465 {
00466 if (!is_array($websites)) {
00467 $websites = array(Mage::app()->getWebsite($websites)->getId());
00468 }
00469
00470 $this->_productLimitationFilters['website_ids'] = $websites;
00471 $this->_applyProductLimitations();
00472
00473 return $this;
00474 }
00475
00476
00477
00478
00479
00480
00481
00482 public function addCategoryFilter(Mage_Catalog_Model_Category $category)
00483 {
00484 $this->_productLimitationFilters['category_id'] = $category->getId();
00485 if ($category->getIsAnchor()) {
00486 unset($this->_productLimitationFilters['category_is_anchor']);
00487 }
00488 else {
00489 $this->_productLimitationFilters['category_is_anchor'] = 1;
00490 }
00491
00492 $this->_applyProductLimitations();
00493
00494 return $this;
00495 }
00496
00497
00498
00499
00500
00501
00502 public function joinMinimalPrice()
00503 {
00504 $this->addAttributeToSelect('price')
00505 ->addAttributeToSelect('minimal_price');
00506 return $this;
00507 }
00508
00509
00510
00511
00512
00513
00514
00515 public function getMaxAttributeValue($attribute)
00516 {
00517 $select = clone $this->getSelect();
00518 $attribute = $this->getEntity()->getAttribute($attribute);
00519 $attributeCode = $attribute->getAttributeCode();
00520 $tableAlias = $attributeCode.'_max_value';
00521
00522 $condition = 'e.entity_id='.$tableAlias.'.entity_id
00523 AND '.$this->_getConditionSql($tableAlias.'.attribute_id', $attribute->getId())
00524
00525 ;
00526
00527 $select->join(
00528 array($tableAlias => $attribute->getBackend()->getTable()),
00529 $condition,
00530 array('max_'.$attributeCode=>new Zend_Db_Expr('MAX('.$tableAlias.'.value)'))
00531 )
00532 ->group('e.entity_type_id');
00533
00534 $data = $this->getConnection()->fetchRow($select);
00535 if (isset($data['max_'.$attributeCode])) {
00536 return $data['max_'.$attributeCode];
00537 }
00538 return null;
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548 public function getAttributeValueCountByRange($attribute, $range)
00549 {
00550 $select = clone $this->getSelect();
00551 $attribute = $this->getEntity()->getAttribute($attribute);
00552 $attributeCode = $attribute->getAttributeCode();
00553 $tableAlias = $attributeCode.'_range_count_value';
00554
00555 $condition = 'e.entity_id='.$tableAlias.'.entity_id
00556 AND '.$this->_getConditionSql($tableAlias.'.attribute_id', $attribute->getId())
00557
00558 ;
00559
00560 $select->reset(Zend_Db_Select::GROUP);
00561 $select->join(
00562 array($tableAlias => $attribute->getBackend()->getTable()),
00563 $condition,
00564 array(
00565 'count_'.$attributeCode=>new Zend_Db_Expr('COUNT(DISTINCT e.entity_id)'),
00566 'range_'.$attributeCode=>new Zend_Db_Expr('CEIL(('.$tableAlias.'.value+0.01)/'.$range.')')
00567 )
00568 )
00569 ->group('range_'.$attributeCode);
00570
00571 $data = $this->getConnection()->fetchAll($select);
00572 $res = array();
00573
00574 foreach ($data as $row) {
00575 $res[$row['range_'.$attributeCode]] = $row['count_'.$attributeCode];
00576 }
00577 return $res;
00578 }
00579
00580
00581
00582
00583
00584
00585
00586 public function getAttributeValueCount($attribute)
00587 {
00588 $select = clone $this->getSelect();
00589 $attribute = $this->getEntity()->getAttribute($attribute);
00590 $attributeCode = $attribute->getAttributeCode();
00591 $tableAlias = $attributeCode.'_value_count';
00592
00593 $select->reset(Zend_Db_Select::GROUP);
00594 $condition = 'e.entity_id='.$tableAlias.'.entity_id
00595 AND '.$this->_getConditionSql($tableAlias.'.attribute_id', $attribute->getId())
00596
00597 ;
00598
00599 $select->join(
00600 array($tableAlias => $attribute->getBackend()->getTable()),
00601 $condition,
00602 array(
00603 'count_'.$attributeCode=>new Zend_Db_Expr('COUNT(DISTINCT e.entity_id)'),
00604 'value_'.$attributeCode=>new Zend_Db_Expr($tableAlias.'.value')
00605 )
00606 )
00607 ->group('value_'.$attributeCode);
00608
00609 $data = $this->getConnection()->fetchAll($select);
00610 $res = array();
00611
00612 foreach ($data as $row) {
00613 $res[$row['value_'.$attributeCode]] = $row['count_'.$attributeCode];
00614 }
00615 return $res;
00616 }
00617
00618
00619
00620
00621
00622
00623 public function getSelectCountSql()
00624 {
00625 $this->_renderFilters();
00626
00627 $countSelect = clone $this->getSelect();
00628 $countSelect->reset(Zend_Db_Select::ORDER);
00629 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00630 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00631 $countSelect->reset(Zend_Db_Select::COLUMNS);
00632
00633 $countSelect->from('', 'COUNT(DISTINCT e.entity_id)');
00634 $countSelect->resetJoinLeft();
00635
00636 return $countSelect;
00637 }
00638
00639
00640
00641
00642
00643
00644 public function getAllIds($limit=null, $offset=null)
00645 {
00646 $idsSelect = clone $this->getSelect();
00647 $idsSelect->reset(Zend_Db_Select::ORDER);
00648 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00649 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00650 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00651 $idsSelect->from(null, 'e.'.$this->getEntity()->getIdFieldName());
00652 $idsSelect->limit($limit, $offset);
00653 $idsSelect->resetJoinLeft();
00654
00655 return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
00656 }
00657
00658
00659
00660
00661
00662
00663 public function getProductCountSelect()
00664 {
00665 if ($this->_productCountSelect === null) {
00666 $this->_productCountSelect = clone $this->getSelect();
00667 $this->_productCountSelect->reset(Zend_Db_Select::COLUMNS)
00668 ->reset(Zend_Db_Select::GROUP)
00669 ->reset(Zend_Db_Select::ORDER)
00670 ->distinct(false)
00671 ->join(array('count_table' => $this->getTable('catalog/category_product_index')),
00672 'count_table.product_id = e.entity_id',
00673 array('count_table.category_id', 'product_count' => new Zend_Db_Expr('COUNT(DISTINCT count_table.product_id)'))
00674 )
00675 ->where('count_table.store_id = ?', $this->getStoreId())
00676 ->group('count_table.category_id');
00677 }
00678
00679 return $this->_productCountSelect;
00680 }
00681
00682
00683
00684
00685
00686
00687 public function unsProductCountSelect()
00688 {
00689 $this->_productCountSelect = null;
00690 return $this;
00691 }
00692
00693
00694
00695
00696
00697
00698
00699 public function addCountToCategories($categoryCollection)
00700 {
00701 $isAnchor = array();
00702 $isNotAnchor = array();
00703 foreach ($categoryCollection as $category) {
00704 if ($category->getIsAnchor()) {
00705 $isAnchor[] = $category->getId();
00706 } else {
00707 $isNotAnchor[] = $category->getId();
00708 }
00709 }
00710 $productCounts = array();
00711 if ($isAnchor || $isNotAnchor) {
00712 $select = $this->getProductCountSelect();
00713
00714 Mage::dispatchEvent('catalog_product_collection_before_add_count_to_categories', array('collection'=>$this));
00715
00716 if ($isAnchor) {
00717 $anchorStmt = clone $select;
00718 $anchorStmt->where('count_table.category_id in (?)', $isAnchor);
00719 $productCounts += $this->getConnection()->fetchPairs($anchorStmt, array('category_id'=>'product_count'));
00720 $anchorStmt = null;
00721 }
00722 if ($isNotAnchor) {
00723 $notAnchorStmt = clone $select;
00724 $notAnchorStmt->where('count_table.category_id in (?)', $isNotAnchor);
00725 $notAnchorStmt->where('count_table.is_parent=1');
00726 $productCounts += $this->getConnection()->fetchPairs($notAnchorStmt, array('category_id'=>'product_count'));
00727 $notAnchorStmt = null;
00728 }
00729 $select = null;
00730 $this->unsProductCountSelect();
00731 }
00732
00733 foreach ($categoryCollection as $category) {
00734 $_count = 0;
00735 if (isset($productCounts[$category->getId()])) {
00736 $_count = $productCounts[$category->getId()];
00737 }
00738 $category->setProductCount($_count);
00739 }
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766 return $this;
00767 }
00768
00769 public function getSetIds()
00770 {
00771 $select = clone $this->getSelect();
00772
00773 $select->reset(Zend_Db_Select::COLUMNS);
00774 $select->distinct(true);
00775 $select->from('', 'attribute_set_id');
00776 return $this->getConnection()->fetchCol($select);
00777 }
00778
00779
00780
00781
00782
00783
00784 public function joinUrlRewrite()
00785 {
00786 $this->joinTable(
00787 'core/url_rewrite',
00788 'entity_id=entity_id',
00789 array('request_path'),
00790 '{{table}}.type='.Mage_Core_Model_Url_Rewrite::TYPE_PRODUCT,
00791 'left'
00792 );
00793
00794 return $this;
00795 }
00796
00797
00798 public function addUrlRewrite($categoryId = '')
00799 {
00800 $this->_addUrlRewrite = true;
00801 $this->_urlRewriteCategory = Mage::getStoreConfig('catalog/seo/product_use_categories') ? $categoryId : 0;
00802 return $this;
00803 }
00804
00805 protected function _addUrlRewrite()
00806 {
00807 $urlRewrites = null;
00808 if ($this->_cacheConf) {
00809 if (!($urlRewrites = Mage::app()->loadCache($this->_cacheConf['prefix'].'urlrewrite'))) {
00810 $urlRewrites = null;
00811 } else {
00812 $urlRewrites = unserialize($urlRewrites);
00813 }
00814 }
00815
00816 if (!$urlRewrites) {
00817 $productIds = array();
00818 foreach($this->getItems() as $item) {
00819 $productIds[] = $item->getEntityId();
00820 }
00821 if (!count($productIds)) {
00822 return;
00823 }
00824
00825 $select = $this->getConnection()->select()
00826 ->from($this->getTable('core/url_rewrite'), array('product_id', 'request_path'))
00827 ->where('store_id=?', Mage::app()->getStore()->getId())
00828 ->where('is_system=?', 1)
00829 ->where('category_id=? OR category_id is NULL', $this->_urlRewriteCategory)
00830 ->where('product_id IN(?)', $productIds)
00831 ->order('category_id DESC');
00832 $urlRewrites = array();
00833
00834 foreach ($this->getConnection()->fetchAll($select) as $row) {
00835 if (!isset($urlRewrites[$row['product_id']])) {
00836 $urlRewrites[$row['product_id']] = $row['request_path'];
00837 }
00838 }
00839
00840 if ($this->_cacheConf) {
00841 Mage::app()->saveCache(
00842 serialize($urlRewrites),
00843 $this->_cacheConf['prefix'].'urlrewrite',
00844 array_merge($this->_cacheConf['tags'], array(Mage_Catalog_Model_Product_Url::CACHE_TAG)),
00845 $this->_cacheLifetime
00846 );
00847 }
00848 }
00849
00850 foreach($this->getItems() as $item) {
00851 if (isset($urlRewrites[$item->getEntityId()])) {
00852 $item->setData('request_path', $urlRewrites[$item->getEntityId()]);
00853 }
00854 }
00855 }
00856
00857 public function addMinimalPrice()
00858 {
00859 $this->_addMinimalPrice = true;
00860 return $this;
00861 }
00862
00863 protected function _addMinimalPrice()
00864 {
00865 Mage::getSingleton('catalogindex/price')->addMinimalPrices($this);
00866 return $this;
00867 }
00868
00869 public function addFinalPrice()
00870 {
00871 $this->_addFinalPrice = true;
00872 $this->addAttributeToSelect('price')
00873 ->addAttributeToSelect('special_price')
00874 ->addAttributeToSelect('special_from_date')
00875 ->addAttributeToSelect('special_to_date');
00876
00877 return $this;
00878 }
00879
00880
00881
00882
00883
00884
00885 protected function _joinPriceRules()
00886 {
00887 if ($this->isEnabledFlat()) {
00888 $customerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId();
00889 $priceColumn = 'e.display_price_group_' . $customerGroup;
00890 $this->getSelect()->from(null, array('_rule_price' => $priceColumn));
00891
00892 return $this;
00893 }
00894 $wId = Mage::app()->getWebsite()->getId();
00895 $gId = Mage::getSingleton('customer/session')->getCustomerGroupId();
00896
00897 $storeDate = Mage::app()->getLocale()->storeTimeStamp($this->getStoreId());
00898 $conditions = "_price_rule.product_id = e.entity_id AND ";
00899 $conditions .= "_price_rule.rule_date = '".$this->getResource()->formatDate($storeDate, false)."' AND ";
00900 $conditions .= "_price_rule.website_id = '{$wId}' AND ";
00901 $conditions .= "_price_rule.customer_group_id = '{$gId}'";
00902
00903 $this->getSelect()->joinLeft(
00904 array('_price_rule'=>$this->getTable('catalogrule/rule_product_price')),
00905 $conditions,
00906 array('_rule_price'=>'rule_price')
00907 );
00908 return $this;
00909 }
00910
00911 protected function _addFinalPrice()
00912 {
00913 foreach ($this->_items as $product) {
00914 $basePrice = $product->getPrice();
00915 $specialPrice = $product->getSpecialPrice();
00916 $specialPriceFrom = $product->getSpecialFromDate();
00917 $specialPriceTo = $product->getSpecialToDate();
00918 if ($this->isEnabledFlat()) {
00919 $rulePrice = null;
00920 if ($product->getData('_rule_price') != $basePrice) {
00921 $rulePrice = $product->getData('_rule_price');
00922 }
00923 }
00924 else {
00925 $rulePrice = $product->getData('_rule_price');
00926 }
00927
00928 $finalPrice = $product->getPriceModel()->calculatePrice(
00929 $basePrice,
00930 $specialPrice,
00931 $specialPriceFrom,
00932 $specialPriceTo,
00933 $rulePrice,
00934 null,
00935 null,
00936 $product->getId()
00937 );
00938
00939 $product->setCalculatedFinalPrice($finalPrice);
00940 }
00941 }
00942
00943 public function getAllIdsCache($resetCache = false)
00944 {
00945 $ids = null;
00946 if (!$resetCache) {
00947 $ids = $this->_allIdsCache;
00948 }
00949
00950 if (is_null($ids)) {
00951 $ids = $this->getAllIds();
00952 $this->setAllIdsCache($ids);
00953 }
00954
00955 return $ids;
00956 }
00957
00958 public function setAllIdsCache($value)
00959 {
00960 $this->_allIdsCache = $value;
00961 return $this;
00962 }
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 public function addAttributeToFilter($attribute, $condition=null, $joinType='inner')
00973 {
00974 if ($this->isEnabledFlat()) {
00975 if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
00976 $attribute = $attribute->getAttributeCode();
00977 }
00978
00979 if (is_array($attribute)) {
00980 $sqlArr = array();
00981 foreach ($attribute as $condition) {
00982 $sqlArr[] = $this->_getAttributeConditionSql($condition['attribute'], $condition, $joinType);
00983 }
00984 $conditionSql = '('.join(') OR (', $sqlArr).')';
00985 $this->getSelect()->where($conditionSql);
00986 return $this;
00987 }
00988
00989 if (!isset($this->_selectAttributes[$attribute])) {
00990 $this->addAttributeToSelect($attribute);
00991 }
00992
00993 if (isset($this->_selectAttributes[$attribute])) {
00994 $this->getSelect()->where($this->_getConditionSql('e.'.$attribute, $condition));
00995 }
00996
00997 return $this;
00998 }
00999
01000 $this->_allIdsCache = null;
01001 if (is_string($attribute) && $attribute == 'is_saleable') {
01002 return $this->getSelect()->where($this->_getConditionSql('(IF(manage_stock, is_in_stock, 1))', $condition));
01003 }
01004 else {
01005 return parent::addAttributeToFilter($attribute, $condition, $joinType);
01006 }
01007 }
01008
01009
01010
01011
01012
01013
01014 public function addTaxPercents()
01015 {
01016 $this->_addTaxPercents = true;
01017 return $this;
01018 }
01019
01020
01021
01022
01023
01024
01025 public function requireTaxPercent()
01026 {
01027 return $this->_addTaxPercents;
01028 }
01029
01030
01031
01032
01033 protected function _addTaxPercents()
01034 {
01035 $classToRate = array();
01036 $request = Mage::getSingleton('tax/calculation')->getRateRequest();
01037 foreach ($this as &$item) {
01038 if (null === $item->getTaxClassId()) {
01039 $item->setTaxClassId($item->getMinimalTaxClassId());
01040 }
01041 if (!isset($classToRate[$item->getTaxClassId()])) {
01042 $request->setProductClassId($item->getTaxClassId());
01043 $classToRate[$item->getTaxClassId()] = Mage::getSingleton('tax/calculation')->getRate($request);
01044 }
01045 $item->setTaxPercent($classToRate[$item->getTaxClassId()]);
01046 }
01047 }
01048
01049
01050
01051
01052
01053
01054 public function addOptionsToResult()
01055 {
01056 $productIds = array();
01057 foreach ($this as $product) {
01058 $productIds[] = $product->getId();
01059 }
01060 if (!empty($productIds)) {
01061 $options = Mage::getModel('catalog/product_option')
01062 ->getCollection()
01063 ->addTitleToResult(Mage::app()->getStore()->getId())
01064 ->addPriceToResult(Mage::app()->getStore()->getId())
01065 ->addProductToFilter($productIds)
01066 ->addValuesToResult();
01067
01068 foreach ($options as $option) {
01069 if($this->getItemById($option->getProductId())) {
01070 $this->getItemById($option->getProductId())->addOption($option);
01071 }
01072 }
01073 }
01074
01075 return $this;
01076 }
01077
01078
01079
01080
01081
01082
01083 public function addFilterByRequiredOptions()
01084 {
01085 $this->addAttributeToFilter('required_options', array(array('neq'=>'1'), array('null'=>true)), 'left');
01086 return $this;
01087 }
01088
01089
01090
01091
01092
01093
01094
01095 public function setVisibility($visibility)
01096 {
01097 $this->_productLimitationFilters['visibility'] = $visibility;
01098 $this->_applyProductLimitations();
01099
01100 return $this;
01101 }
01102
01103
01104
01105
01106
01107
01108
01109
01110 public function addAttributeToSort($attribute, $dir='asc')
01111 {
01112 if ($attribute == 'position') {
01113 if (isset($this->_joinFields[$attribute])) {
01114 $this->getSelect()->order("{$attribute} {$dir}");
01115 return $this;
01116 }
01117 $this->getSelect()->order("cat_index_position {$dir}");
01118
01119 $filters = $this->_productLimitationFilters;
01120 if (isset($filters['category_id']) || isset($filters['visibility'])) {
01121 $this->getSelect()->order('cat_index.product_id ' . $dir);
01122 }
01123 else {
01124 $this->getSelect()->order('e.entity_id ' . $dir);
01125 }
01126
01127 return $this;
01128 }
01129
01130 $storeId = Mage::app()->getStore()->getId();
01131 if ($attribute == 'price' && $storeId != 0) {
01132 $websiteId = Mage::app()->getStore()->getWebsiteId();
01133 $customerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId();
01134
01135 if ($this->isEnabledFlat()) {
01136 $priceColumn = 'e.display_price_group_' . $customerGroup;
01137 $this->getSelect()->order("{$priceColumn} {$dir}");
01138 }
01139 else {
01140 $priceAttributeId = $this->getAttribute('price')->getId();
01141
01142 $entityCondition = '_price_order_table.entity_id = e.entity_id';
01143 $storeCondition = $this->getConnection()->quoteInto(
01144 '_price_order_table.website_id = ?',
01145 $websiteId
01146 );
01147 $groupCondition = $this->getConnection()->quoteInto(
01148 '_price_order_table.customer_group_id = ?',
01149 $customerGroup
01150 );
01151 $attributeCondition = $this->getConnection()->quoteInto(
01152 '_price_order_table.attribute_id = ?',
01153 $priceAttributeId
01154 );
01155
01156 $this->getSelect()->joinLeft(
01157 array('_price_order_table'=>$this->getTable('catalogindex/price')),
01158 "{$entityCondition} AND {$storeCondition} AND {$groupCondition} AND {$attributeCondition}",
01159 array()
01160 );
01161 $this->getSelect()->order('_price_order_table.value ' . $dir);
01162
01163
01164
01165
01166
01167 $this->getSelect()->distinct(true);
01168 }
01169
01170 return $this;
01171 }
01172
01173 if ($this->isEnabledFlat()) {
01174 $column = $this->getEntity()->getAttributeSortColumn($attribute);
01175
01176 if ($column) {
01177 $this->getSelect()->order("e.{$column} {$dir}");
01178 }
01179
01180 return $this;
01181 } else {
01182 $attrInstance = $this->getEntity()->getAttribute($attribute);
01183 if ($attrInstance && $attrInstance->usesSource()) {
01184 $attrInstance->getSource()
01185 ->addValueSortToCollection($this, $dir);
01186 return $this;
01187 }
01188 }
01189
01190 return parent::addAttributeToSort($attribute, $dir);
01191 }
01192
01193
01194
01195
01196
01197
01198 protected function _prepareProductLimitationFilters()
01199 {
01200 if (isset($this->_productLimitationFilters['visibility'])
01201 && !isset($this->_productLimitationFilters['store_id'])
01202 ) {
01203 $this->_productLimitationFilters['store_id'] = $this->getStoreId();
01204 }
01205 if (isset($this->_productLimitationFilters['category_id'])
01206 && !isset($this->_productLimitationFilters['store_id'])
01207 ) {
01208 $this->_productLimitationFilters['store_id'] = $this->getStoreId();
01209 }
01210 if (isset($this->_productLimitationFilters['store_id'])
01211 && isset($this->_productLimitationFilters['visibility'])
01212 && !isset($this->_productLimitationFilters['category_id'])
01213 ) {
01214 $this->_productLimitationFilters['category_id'] = Mage::app()
01215 ->getStore($this->_productLimitationFilters['store_id'])
01216 ->getRootCategoryId();
01217 }
01218
01219 return $this;
01220 }
01221
01222
01223
01224
01225
01226
01227 protected function _productLimitationJoinWebsite()
01228 {
01229 $joinWebsite = false;
01230 $filters = $this->_productLimitationFilters;
01231 $conditions = array(
01232 'product_website.product_id=e.entity_id'
01233 );
01234 if (isset($filters['website_ids'])) {
01235 $joinWebsite = true;
01236 if (count($filters['website_ids']) > 1) {
01237 $this->getSelect()->distinct(true);
01238 }
01239 $conditions[] = $this->getConnection()
01240 ->quoteInto('product_website.website_id IN(?)', $filters['website_ids']);
01241 }
01242 elseif (isset($filters['store_id'])
01243 && (!isset($filters['visibility']) && !isset($filters['category_id']))
01244 && !$this->isEnabledFlat()
01245 ) {
01246 $joinWebsite = true;
01247 $websiteId = Mage::app()->getStore($filters['store_id'])->getWebsiteId();
01248 $conditions[] = $this->getConnection()
01249 ->quoteInto('product_website.website_id=?', $websiteId);
01250 }
01251
01252 $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM);
01253 if (isset($fromPart['product_website'])) {
01254 if (!$joinWebsite) {
01255 unset($fromPart['product_website']);
01256 }
01257 else {
01258 $fromPart['product_website']['joinCondition'] = join(' AND ', $conditions);
01259 }
01260 $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart);
01261 }
01262 elseif ($joinWebsite) {
01263 $this->getSelect()->join(
01264 array('product_website' => $this->getTable('catalog/product_website')),
01265 join(' AND ', $conditions),
01266 array()
01267 );
01268 }
01269
01270 return $this;
01271 }
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283 protected function _applyProductLimitations()
01284 {
01285 $this->_prepareProductLimitationFilters();
01286 $this->_productLimitationJoinWebsite();
01287 $filters = $this->_productLimitationFilters;
01288
01289 if (!isset($filters['category_id']) && !isset($filters['visibility'])) {
01290 return $this;
01291 }
01292
01293 $conditions = array(
01294 'cat_index.product_id=e.entity_id',
01295 $this->getConnection()->quoteInto('cat_index.store_id=?', $filters['store_id'])
01296 );
01297 if (isset($filters['visibility'])) {
01298 $conditions[] = $this->getConnection()
01299 ->quoteInto('cat_index.visibility IN(?)', $filters['visibility']);
01300 }
01301 $conditions[] = $this->getConnection()
01302 ->quoteInto('cat_index.category_id=?', $filters['category_id']);
01303 if (isset($filters['category_is_anchor'])) {
01304 $conditions[] = $this->getConnection()
01305 ->quoteInto('cat_index.is_parent=?', $filters['category_is_anchor']);
01306 }
01307
01308 $joinCond = join(' AND ', $conditions);
01309 $fromPart = $this->getSelect()->getPart(Zend_Db_Select::FROM);
01310 if (isset($fromPart['cat_index'])) {
01311 $fromPart['cat_index']['joinCondition'] = $joinCond;
01312 $this->getSelect()->setPart(Zend_Db_Select::FROM, $fromPart);
01313 }
01314 else {
01315 $this->getSelect()->join(
01316 array('cat_index' => $this->getTable('catalog/category_product_index')),
01317 $joinCond,
01318 array('cat_index_position' => 'position')
01319 );
01320 }
01321
01322 Mage::dispatchEvent('catalog_product_collection_apply_limitations_after', array(
01323 'collection' => $this
01324 ));
01325
01326 return $this;
01327 }
01328 }