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
00035 class Mage_CatalogIndex_Model_Data_Abstract extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_typeInstance;
00043
00044
00045
00046
00047
00048
00049 protected $_haveChildren = array(
00050 Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_TIERS=>true,
00051 Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_PRICES=>true,
00052 Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_ATTRIBUTES=>true,
00053 );
00054
00055
00056
00057
00058
00059
00060 protected $_haveParents = true;
00061
00062 const LINK_GET_CHILDREN = 1;
00063 const LINK_GET_PARENTS = 1;
00064
00065
00066
00067
00068
00069 protected function _construct()
00070 {
00071 $this->_init('catalogindex/data_abstract');
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081 public function getChildProductIds($store, $parentIds)
00082 {
00083 if (!$this->_haveChildren) {
00084 return false;
00085 }
00086
00087 if (!$this->_getLinkSettings()) {
00088 return false;
00089 }
00090
00091 return $this->fetchLinkInformation($store, $this->_getLinkSettings(), self::LINK_GET_CHILDREN, $parentIds);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 public function getParentProductIds($store, $childIds)
00102 {
00103 if (!$this->_haveParents) {
00104 return false;
00105 }
00106
00107 if (!$this->_getLinkSettings()) {
00108 return false;
00109 }
00110
00111 return $this->fetchLinkInformation($store, $this->_getLinkSettings(), self::LINK_GET_PARENTS, $childIds);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 protected function fetchLinkInformation($store, $settings, $type, $suppliedId)
00123 {
00124 switch ($type) {
00125 case self::LINK_GET_CHILDREN:
00126 $whereField = $settings['parent_field'];
00127 $idField = $settings['child_field'];
00128 break;
00129
00130 case self::LINK_GET_PARENTS:
00131 $idField = $settings['parent_field'];
00132 $whereField = $settings['child_field'];
00133 break;
00134 }
00135
00136 $additional = array();
00137 if (isset($settings['additional']) && is_array($settings['additional'])) {
00138 $additional = $settings['additional'];
00139 }
00140
00141 return $this->getResource()->fetchLinkInformation($store->getId(), $settings['table'], $idField, $whereField, $suppliedId, $additional);
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 public function getFinalPrice($product, $store, $group)
00153 {
00154 $basePrice = $specialPrice = $specialPriceFrom = $specialPriceTo = null;
00155 $priceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'price');
00156 $specialPriceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'special_price');
00157 $specialPriceFromId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'special_from_date');
00158 $specialPriceToId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'special_to_date');
00159
00160 $attributes = array($priceId, $specialPriceId, $specialPriceFromId, $specialPriceToId);
00161
00162 $productData = $this->getAttributeData($product, $attributes, $store);
00163 foreach ($productData as $row) {
00164 switch ($row['attribute_id']) {
00165 case $priceId:
00166 $basePrice = $row['value'];
00167 break;
00168 case $specialPriceId:
00169 $specialPrice = $row['value'];
00170 break;
00171 case $specialPriceFromId:
00172 $specialPriceFrom = $row['value'];
00173 break;
00174 case $specialPriceToId:
00175 $specialPriceTo = $row['value'];
00176 break;
00177 }
00178 }
00179
00180 $finalPrice = Mage::getSingleton('catalog/product_type')
00181 ->priceFactory($this->getTypeCode())
00182 ->calculatePrice($basePrice, $specialPrice, $specialPriceFrom, $specialPriceTo, false, $store, $group, $product);
00183
00184 return $finalPrice;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 public function getMinimalPrice($products, $store)
00195 {
00196 $priceAttributes = array(
00197 Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'tier_price'),
00198 Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'price'));
00199
00200 $data = $this->getResource()->getMinimalPrice($products, $priceAttributes, $store->getId());
00201
00202 $this->setMinimalPriceData($data);
00203 $eventData = array('indexer'=>$this, 'product_ids'=>$products, 'store'=>$store);
00204 Mage::dispatchEvent('catalogindex_get_minimal_price', $eventData);
00205 $data = $this->getMinimalPriceData();
00206
00207 return $data;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217 public function getTaxClassId($productId, $store)
00218 {
00219 $attributeId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'tax_class_id');
00220 $taxClassId = $this->getResource()->getAttributeData(array($productId), array($attributeId), $store->getId());
00221 if (is_array($taxClassId) && isset($taxClassId[0]['value'])) {
00222 $taxClassId = $taxClassId[0]['value'];
00223 }
00224 else {
00225 $taxClassId = 0;
00226 }
00227 return $taxClassId;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 public function getTierPrices($products, $store)
00238 {
00239 return $this->getResource()->getTierPrices($products, $store->getWebsiteId());
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249 public function getAttributeData($products, $attributes, $store)
00250 {
00251 return $this->getResource()->getAttributeData($products, $attributes, $store->getId());
00252 }
00253
00254
00255
00256
00257
00258
00259 public function getTypeCode()
00260 {
00261 Mage::throwException('Define custom data retreiver with getTypeCode function');
00262 }
00263
00264
00265
00266
00267
00268
00269 protected function _getLinkSettings()
00270 {
00271 return false;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 public function areChildrenIndexable($type)
00281 {
00282 if (!$this->_haveChildren || !isset($this->_haveChildren[$type]) || !$this->_haveChildren[$type]) {
00283 return false;
00284 }
00285 return true;
00286 }
00287
00288
00289
00290
00291
00292
00293 public function getTypeInstance()
00294 {
00295 if (is_null($this->_typeInstance)) {
00296 $product = new Varien_Object();
00297 $product->setTypeId($this->getTypeCode());
00298 $this->_typeInstance = Mage::getSingleton('catalog/product_type')
00299 ->factory($product, true);
00300 }
00301 return $this->_typeInstance;
00302 }
00303 }