1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_CatalogIndex
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Indexer resource model abstraction
30: *
31: * @category Mage
32: * @package Mage_CatalogIndex
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogIndex_Model_Resource_Indexer_Abstract extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * should be defined because abstract
39: *
40: */
41: protected function _construct()
42: {
43:
44: }
45:
46: /**
47: * Enter description here ...
48: *
49: * @param unknown_type $data
50: * @param unknown_type $storeId
51: * @param unknown_type $productId
52: * @return unknown
53: */
54: public function saveIndex($data, $storeId, $productId)
55: {
56: return $this->saveIndices(array($data), $storeId, $productId);
57: }
58:
59: /**
60: * Enter description here ...
61: *
62: * @param array $data
63: * @param unknown_type $storeId
64: * @param unknown_type $productId
65: */
66: public function saveIndices(array $data, $storeId, $productId)
67: {
68: $this->_executeReplace($data, $storeId, $productId);
69: }
70:
71: /**
72: * Enter description here ...
73: *
74: * @param unknown_type $data
75: * @param unknown_type $storeId
76: * @param unknown_type $productId
77: * @return Mage_CatalogIndex_Model_Resource_Indexer_Abstract
78: */
79: protected function _executeReplace($data, $storeId, $productId)
80: {
81: $this->beginTransaction();
82: try {
83: foreach ($data as $row) {
84: $row[$this->_entityIdFieldName] = $productId;
85: $this->_getWriteAdapter()->insert($this->getMainTable(), $row);
86: }
87: $this->commit();
88: } catch (Exception $e) {
89: $this->rollBack();
90: throw $e;
91: }
92:
93: return $this;
94: }
95:
96: /**
97: * Enter description here ...
98: *
99: * @param unknown_type $productId
100: * @param unknown_type $storeId
101: * @param unknown_type $attributeId
102: */
103: public function cleanup($productId, $storeId = null, $attributeId = null)
104: {
105: $conditions[] = $this->_getWriteAdapter()->quoteInto("{$this->_entityIdFieldName} = ?", $productId);
106:
107: if (!is_null($storeId))
108: $conditions[] = $this->_getWriteAdapter()->quoteInto("{$this->_storeIdFieldName} = ?", $storeId);
109:
110: if (!is_null($attributeId))
111: $conditions[] = $this->_getWriteAdapter()->quoteInto("{$this->_attributeIdFieldName} = ?", $attributeId);
112:
113: $conditions = implode (' AND ', $conditions);
114: $this->_getWriteAdapter()->delete($this->getMainTable(), $conditions);
115: }
116:
117: /**
118: * Enter description here ...
119: *
120: * @param unknown_type $conditions
121: * @return unknown
122: */
123: public function loadAttributeCodesByCondition($conditions)
124: {
125: $table = $this->getTable('eav/attribute');
126: $select = $this->_getReadAdapter()->select();
127: $select->from(array('main_table' => $table), 'attribute_id')
128: ->join(array('additional_table' => $this->getTable('catalog/eav_attribute')), 'additional_table.attribute_id=main_table.attribute_id');
129: $select->distinct(true);
130:
131: if (is_array($conditions)) {
132: foreach ($conditions as $k=>$condition) {
133: if (is_array($condition)) {
134: if ($k == 'or') {
135: $function = 'where';
136: foreach ($condition as $field=>$value) {
137: if (is_array($value))
138: $select->$function("{$field} in (?)", $value);
139: else
140: $select->$function("{$field} = ?", $value);
141:
142: $function = 'orWhere';
143: }
144: } else {
145: $select->where("{$k} in (?)", $condition);
146: }
147: } else {
148: $select->where("{$k} = ?", $condition);
149: }
150: }
151: } else {
152: $select->where($conditions);
153: }
154: return $this->_getReadAdapter()->fetchCol($select);
155: }
156: }
157: