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 class Mage_CatalogIndex_Model_Mysql4_Data_Abstract extends Mage_Core_Model_Mysql4_Abstract
00028 {
00029
00030
00031
00032
00033
00034 protected $_attributeCodeIds = array();
00035
00036
00037
00038
00039
00040
00041 protected $_linkSelect = null;
00042
00043
00044
00045
00046
00047
00048 protected function _setLinkSelect($select)
00049 {
00050 $this->_linkSelect = $select;
00051 return $this;
00052 }
00053
00054
00055
00056
00057
00058
00059 protected function _getLinkSelect()
00060 {
00061 return $this->_linkSelect;
00062 }
00063
00064 protected function _construct()
00065 {
00066 $this->_init('catalog/product', 'entity_id');
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076 public function getAttributeData($products, $attributes, $store)
00077 {
00078 $suffixes = array('decimal', 'varchar', 'int', 'text', 'datetime');
00079 if (!is_array($products)) {
00080 $products = new Zend_Db_Expr($products);
00081 }
00082 $result = array();
00083 foreach ($suffixes as $suffix) {
00084 $tableName = "{$this->getTable('catalog/product')}_{$suffix}";
00085 $condition = "product.entity_id = c.entity_id AND c.store_id = {$store} AND c.attribute_id = d.attribute_id";
00086 $defaultCondition = "product.entity_id = d.entity_id AND d.store_id = 0";
00087 $fields = array('entity_id', 'type_id', 'attribute_id'=>'IFNULL(c.attribute_id, d.attribute_id)', 'value'=>'IFNULL(c.value, d.value)');
00088
00089 $select = $this->_getReadAdapter()->select()
00090 ->from(array('product'=>$this->getTable('catalog/product')), $fields)
00091 ->where('product.entity_id in (?)', $products)
00092 ->joinRight(array('d'=>$tableName), $defaultCondition, array())
00093 ->joinLeft(array('c'=>$tableName), $condition, array())
00094 ->where('c.attribute_id IN (?) OR d.attribute_id IN (?)', $attributes);
00095 $part = $this->_getReadAdapter()->fetchAll($select);
00096
00097 if (is_array($part)) {
00098 $result = array_merge($result, $part);
00099 }
00100 }
00101
00102 return $result;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 public function fetchLinkInformation($store, $table, $idField, $whereField, $id, $additionalWheres = array())
00117 {
00118 $idsConditionSymbol = "= ?";
00119 if (is_array($id)) {
00120 $idsConditionSymbol = "in (?)";
00121 }
00122
00123 $select = $this->_getReadAdapter()->select();
00124 $select->from(array('l'=>$this->getTable($table)), array("l.{$idField}"))
00125 ->where("l.{$whereField} {$idsConditionSymbol}", $id);
00126 foreach ($additionalWheres as $field=>$condition) {
00127 $select->where("l.$field = ?", $condition);
00128 }
00129
00130
00131 $this->_addAttributeFilter($select, 'status', 'l', $idField, $store,
00132 Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
00133
00134 if ($websiteId = Mage::app()->getStore($store)->getWebsiteId()) {
00135 $select->join(
00136 array('w' => $this->getTable('catalog/product_website')),
00137 "l.{$idField}=w.product_id AND w.website_id={$websiteId}",
00138 array()
00139 );
00140 }
00141
00142 $this->_setLinkSelect($select);
00143 $this->_prepareLinkFetchSelect($store, $table, $idField, $whereField, $id, $additionalWheres);
00144
00145 return $this->_getWriteAdapter()->fetchCol($this->_getLinkSelect());
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 protected function _prepareLinkFetchSelect($store, $table, $idField, $whereField, $id, $additionalWheres = array()) {}
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 public function getMinimalPrice($products, $priceAttributes, $store)
00170 {
00171 $website = Mage::app()->getStore($store)->getWebsiteId();
00172
00173 $fields = array('customer_group_id', 'minimal_value'=>'MIN(value)');
00174 $select = $this->_getReadAdapter()->select()
00175 ->from(array('base'=>$this->getTable('catalogindex/price')), $fields)
00176 ->where('base.entity_id in (?)', $products)
00177 ->where('base.attribute_id in (?)', $priceAttributes)
00178 ->where('base.website_id = ?', $website)
00179 ->group('base.customer_group_id');
00180 return $this->_getReadAdapter()->fetchAll($select);
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190 public function getTierPrices($products, $website)
00191 {
00192 $fields = array(
00193 'entity_id',
00194 'type_id',
00195 'c.customer_group_id',
00196 'c.qty',
00197 'c.value',
00198 'c.all_groups',
00199 );
00200 $condition = "product.entity_id = c.entity_id";
00201
00202 $select = $this->_getReadAdapter()->select()
00203 ->from(array('product'=>$this->getTable('catalog/product')), $fields)
00204 ->joinLeft(array('c'=>"{$this->getTable('catalog/product')}_tier_price"), $condition, array())
00205 ->where('product.entity_id in (?)', $products);
00206 if (Mage::helper('catalog')->isPriceGlobal())
00207 {
00208 $select->where('c.website_id=?', 0);
00209 }
00210 elseif (Mage::app()->getWebsite($website)->getBaseCurrencyCode() != Mage::app()->getBaseCurrencyCode()) {
00211 $select->where('c.website_id=?', $website);
00212 }
00213 else {
00214 $select->where('c.website_id IN(?)', array(0, $website));
00215 }
00216
00217 return $this->_getReadAdapter()->fetchAll($select);
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 protected function _addAttributeFilter(Varien_Db_Select $select, $attributeCode, $table, $field, $store, $value)
00232 {
00233 $adapter = $this->_getReadAdapter();
00234 $attribute = Mage::getSingleton('eav/config')
00235 ->getAttribute('catalog_product', $attributeCode);
00236
00237 $attributeTable = $attribute->getBackend()->getTable();
00238 if ($attribute->getBackendType() == 'static') {
00239 $tableAlias = sprintf('t_%s', $attribute->getAttributeCode());
00240 $joinCond = join(' AND ', array(
00241 sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableAlias)
00242 ));
00243 $select
00244 ->join(
00245 array($tableAlias => $attributeTable),
00246 $joinCond,
00247 array())
00248 ->where(sprintf('%s.%s IN(?)', $tableAlias, $attribute->getAttributeCode()), $value);
00249 }
00250 elseif ($attribute->isScopeGlobal()) {
00251 $tableAlias = sprintf('t_%s', $attribute->getAttributeCode());
00252 $joinCond = join(' AND ', array(
00253 sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableAlias),
00254 $adapter->quoteInto(sprintf('`%s`.`attribute_id`=?', $tableAlias), $attribute->getAttributeId()),
00255 $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableAlias), 0)
00256 ));
00257 $select
00258 ->join(
00259 array($tableAlias => $attributeTable),
00260 $joinCond,
00261 array())
00262 ->where(sprintf('%s.value IN(?)', $tableAlias), $value);
00263 }
00264 else {
00265 $tableGlobal = sprintf('t_global_%s', $attribute->getAttributeCode());
00266 $tableStore = sprintf('t_store_%s', $attribute->getAttributeCode());
00267 $joinCondGlobal = join(' AND ', array(
00268 sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableGlobal),
00269 $adapter->quoteInto(sprintf('`%s`.`attribute_id`=?', $tableGlobal), $attribute->getAttributeId()),
00270 $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableGlobal), 0)
00271 ));
00272 $joinCondStore = join(' AND ', array(
00273 sprintf('`%s`.`entity_id`=`%s`.`entity_id`', $tableGlobal, $tableStore),
00274 sprintf('`%s`.`attribute_id`=`%s`.`attribute_id`', $tableGlobal, $tableStore),
00275 $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableStore), $store)
00276 ));
00277 $whereCond = sprintf('IFNULL(`%s`.`value`, `%s`.`value`) IN(?)', $tableStore, $tableGlobal);
00278
00279 $select
00280 ->join(
00281 array($tableGlobal => $attributeTable),
00282 $joinCondGlobal,
00283 array())
00284 ->joinLeft(
00285 array($tableStore => $attributeTable),
00286 $joinCondStore,
00287 array())
00288 ->where($whereCond, $value);
00289 }
00290
00291 return $this;
00292 }
00293 }