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: abstract class Mage_CatalogIndex_Model_Indexer_Abstract
34: extends Mage_Core_Model_Abstract
35: implements Mage_CatalogIndex_Model_Indexer_Interface
36: {
37: protected $_processChildren = true;
38: protected $_processChildrenForConfigurable = true;
39: protected $_runOnce = false;
40:
41: public function processAfterSave(Mage_Catalog_Model_Product $object, $forceId = null)
42: {
43: $associated = array();
44: switch ($object->getTypeId()) {
45: case Mage_Catalog_Model_Product_Type::TYPE_GROUPED:
46: $associated = $object->getTypeInstance(true)->getAssociatedProducts($object);
47: break;
48:
49: case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE:
50: $associated = $object->getTypeInstance(true)->getUsedProducts(null, $object);
51: break;
52: }
53:
54: if (!$this->_isObjectIndexable($object) && is_null($forceId)) {
55: return;
56: }
57:
58: $data = array();
59:
60: if ($this->_runOnce) {
61: $data = $this->createIndexData($object);
62: } else {
63: $attributes = $object->getAttributes();
64: foreach ($attributes as $attribute) {
65: if ($this->_isAttributeIndexable($attribute) && $object->getData($attribute->getAttributeCode()) != null) {
66: $row = $this->createIndexData($object, $attribute);
67: if ($row && is_array($row)) {
68: if (isset($row[0]) && is_array($row[0])) {
69: $data = array_merge($data, $row);
70: } else {
71: $data[] = $row;
72: }
73: }
74: }
75: }
76: }
77: $function = 'saveIndex';
78: if ($data && is_array($data)) {
79: if (isset($data[0]) && is_array($data[0]))
80: $function = 'saveIndices';
81:
82: $this->$function($data, $object->getStoreId(), ($forceId != null ? $forceId : $object->getId()));
83: }
84:
85: if (!$this->_processChildrenForConfigurable && $object->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
86: return;
87:
88: if ($associated && $this->_processChildren) {
89:
90: foreach ($associated as $child) {
91: $child
92: ->setStoreId($object->getStoreId())
93: ->setWebsiteId($object->getWebsiteId());
94: $this->processAfterSave($child, $object->getId());
95: }
96: }
97: }
98:
99: public function saveIndex($data, $storeId, $productId)
100: {
101: $this->_getResource()->saveIndex($data, $storeId, $productId);
102: }
103:
104: public function saveIndices(array $data, $storeId, $productId)
105: {
106: $this->_getResource()->saveIndices($data, $storeId, $productId);
107: }
108:
109: protected function _isObjectIndexable(Mage_Catalog_Model_Product $object)
110: {
111: if ($object->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
112: return false;
113: }
114:
115: if ($object->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG &&
116: $object->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) {
117: return false;
118: }
119:
120: return true;
121: }
122:
123: public function isAttributeIndexable(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
124: {
125: return $this->_isAttributeIndexable($attribute);
126: }
127:
128: protected function _isAttributeIndexable(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
129: {
130: return true;
131: }
132:
133: public function getIndexableAttributeCodes()
134: {
135: return $this->_getResource()->loadAttributeCodesByCondition($this->_getIndexableAttributeConditions());
136: }
137:
138: protected function _getIndexableAttributeConditions()
139: {
140: return array();
141: }
142:
143: public function cleanup($productId, $storeId = null)
144: {
145: $this->_getResource()->cleanup($productId, $storeId);
146: }
147:
148: public function isAttributeIdUsed()
149: {
150: return true;
151: }
152: }
153: