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_CatalogIndex_Model_Data_Abstract extends Mage_Core_Model_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_typeInstance;
43:
44: 45: 46: 47: 48:
49: protected $_haveChildren = array(
50: Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_TIERS=>true,
51: Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_PRICES=>true,
52: Mage_CatalogIndex_Model_Retreiver::CHILDREN_FOR_ATTRIBUTES=>true,
53: );
54:
55: 56: 57: 58: 59:
60: protected $_haveParents = true;
61:
62: const LINK_GET_CHILDREN = 1;
63: const LINK_GET_PARENTS = 1;
64:
65: 66: 67: 68:
69: protected function _construct()
70: {
71: $this->_init('catalogindex/data_abstract');
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function getChildProductIds($store, $parentIds)
82: {
83: if (!$this->_haveChildren) {
84: return false;
85: }
86:
87: if (!$this->_getLinkSettings()) {
88: return false;
89: }
90:
91: return $this->fetchLinkInformation($store, $this->_getLinkSettings(), self::LINK_GET_CHILDREN, $parentIds);
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: public function getParentProductIds($store, $childIds)
102: {
103: if (!$this->_haveParents) {
104: return false;
105: }
106:
107: if (!$this->_getLinkSettings()) {
108: return false;
109: }
110:
111: return $this->fetchLinkInformation($store, $this->_getLinkSettings(), self::LINK_GET_PARENTS, $childIds);
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121:
122: protected function fetchLinkInformation($store, $settings, $type, $suppliedId)
123: {
124: switch ($type) {
125: case self::LINK_GET_CHILDREN:
126: $whereField = $settings['parent_field'];
127: $idField = $settings['child_field'];
128: break;
129:
130: case self::LINK_GET_PARENTS:
131: $idField = $settings['parent_field'];
132: $whereField = $settings['child_field'];
133: break;
134: }
135:
136: $additional = array();
137: if (isset($settings['additional']) && is_array($settings['additional'])) {
138: $additional = $settings['additional'];
139: }
140:
141: return $this->getResource()->fetchLinkInformation($store->getId(), $settings['table'], $idField, $whereField, $suppliedId, $additional);
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151:
152: public function getFinalPrice($product, $store, $group)
153: {
154: $basePrice = $specialPrice = $specialPriceFrom = $specialPriceTo = null;
155: $priceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'price');
156: $specialPriceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'special_price');
157: $specialPriceFromId = Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'special_from_date');
158: $specialPriceToId = Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'special_to_date');
159:
160: $attributes = array($priceId, $specialPriceId, $specialPriceFromId, $specialPriceToId);
161:
162: $productData = $this->getAttributeData($product, $attributes, $store);
163: foreach ($productData as $row) {
164: switch ($row['attribute_id']) {
165: case $priceId:
166: $basePrice = $row['value'];
167: break;
168: case $specialPriceId:
169: $specialPrice = $row['value'];
170: break;
171: case $specialPriceFromId:
172: $specialPriceFrom = $row['value'];
173: break;
174: case $specialPriceToId:
175: $specialPriceTo = $row['value'];
176: break;
177: }
178: }
179:
180: $finalPrice = Mage::getSingleton('catalog/product_type')
181: ->priceFactory($this->getTypeCode())
182: ->calculatePrice($basePrice, $specialPrice, $specialPriceFrom, $specialPriceTo, false, $store, $group, $product);
183:
184: return $finalPrice;
185: }
186:
187: 188: 189: 190: 191: 192: 193:
194: public function getMinimalPrice($products, $store)
195: {
196: $priceAttributes = array(
197: Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'tier_price'),
198: Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'price'));
199:
200: $data = $this->getResource()->getMinimalPrice($products, $priceAttributes, $store->getId());
201:
202: $this->setMinimalPriceData($data);
203: $eventData = array('indexer'=>$this, 'product_ids'=>$products, 'store'=>$store);
204: Mage::dispatchEvent('catalogindex_get_minimal_price', $eventData);
205: $data = $this->getMinimalPriceData();
206:
207: return $data;
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: public function getTaxClassId($productId, $store)
218: {
219: $attributeId = Mage::getSingleton('eav/entity_attribute')->getIdByCode(Mage_Catalog_Model_Product::ENTITY, 'tax_class_id');
220: $taxClassId = $this->getResource()->getAttributeData(array($productId), array($attributeId), $store->getId());
221: if (is_array($taxClassId) && isset($taxClassId[0]['value'])) {
222: $taxClassId = $taxClassId[0]['value'];
223: }
224: else {
225: $taxClassId = 0;
226: }
227: return $taxClassId;
228: }
229:
230: 231: 232: 233: 234: 235: 236:
237: public function getTierPrices($products, $store)
238: {
239: return $this->getResource()->getTierPrices($products, $store->getWebsiteId());
240: }
241:
242: 243: 244: 245: 246: 247: 248:
249: public function getAttributeData($products, $attributes, $store)
250: {
251: return $this->getResource()->getAttributeData($products, $attributes, $store->getId());
252: }
253:
254: 255: 256: 257: 258:
259: public function getTypeCode()
260: {
261: Mage::throwException('Define custom data retreiver with getTypeCode function');
262: }
263:
264: 265: 266: 267: 268:
269: protected function _getLinkSettings()
270: {
271: return false;
272: }
273:
274: 275: 276: 277: 278: 279:
280: public function areChildrenIndexable($type)
281: {
282: if (!$this->_haveChildren || !isset($this->_haveChildren[$type]) || !$this->_haveChildren[$type]) {
283: return false;
284: }
285: return true;
286: }
287:
288: 289: 290: 291: 292:
293: public function getTypeInstance()
294: {
295: if (is_null($this->_typeInstance)) {
296: $product = new Varien_Object();
297: $product->setTypeId($this->getTypeCode());
298: $this->_typeInstance = Mage::getSingleton('catalog/product_type')
299: ->factory($product, true);
300: }
301: return $this->_typeInstance;
302: }
303: }
304: