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_Catalog_Model_Resource_Product_Status extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_productAttributes = array();
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: $this->_init('catalog/product_enabled_index', 'product_id');
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function getProductAttribute($attributeCode)
60: {
61: return $this->_getProductAttribute($attributeCode);
62: }
63:
64: 65: 66: 67: 68: 69:
70: protected function _getProductAttribute($attribute)
71: {
72: if (empty($this->_productAttributes[$attribute])) {
73: $this->_productAttributes[$attribute] = Mage::getSingleton('catalog/product')->getResource()->getAttribute($attribute);
74: }
75: return $this->_productAttributes[$attribute];
76: }
77:
78: 79: 80: 81: 82: 83: 84:
85: public function refreshEnabledIndex($productId, $storeId)
86: {
87: if ($storeId == Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID) {
88: foreach (Mage::app()->getStores() as $store) {
89: $this->refreshEnabledIndex($productId, $store->getId());
90: }
91:
92: return $this;
93: }
94:
95: Mage::getResourceSingleton('catalog/product')->refreshEnabledIndex($storeId, $productId);
96:
97: return $this;
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107:
108: public function updateProductStatus($productId, $storeId, $value)
109: {
110: $statusAttributeId = $this->_getProductAttribute('status')->getId();
111: $statusEntityTypeId = $this->_getProductAttribute('status')->getEntityTypeId();
112: $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable();
113: $refreshIndex = true;
114: $adapter = $this->_getWriteAdapter();
115:
116: $data = new Varien_Object(array(
117: 'entity_type_id' => $statusEntityTypeId,
118: 'attribute_id' => $statusAttributeId,
119: 'store_id' => $storeId,
120: 'entity_id' => $productId,
121: 'value' => $value
122: ));
123:
124: $data = $this->_prepareDataForTable($data, $statusTable);
125:
126: $select = $adapter->select()
127: ->from($statusTable)
128: ->where('attribute_id = :attribute_id')
129: ->where('store_id = :store_id')
130: ->where('entity_id = :product_id');
131:
132: $binds = array(
133: 'attribute_id' => $statusAttributeId,
134: 'store_id' => $storeId,
135: 'product_id' => $productId
136: );
137:
138: $row = $adapter->fetchRow($select);
139:
140: if ($row) {
141: if ($row['value'] == $value) {
142: $refreshIndex = false;
143: } else {
144: $condition = array('value_id = ?' => $row['value_id']);
145: $adapter->update($statusTable, $data, $condition);
146: }
147: } else {
148: $adapter->insert($statusTable, $data);
149: }
150:
151: if ($refreshIndex) {
152: $this->refreshEnabledIndex($productId, $storeId);
153: }
154:
155: return $this;
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165:
166: public function getProductStatus($productIds, $storeId = null)
167: {
168: $statuses = array();
169:
170: $attribute = $this->_getProductAttribute('status');
171: $attributeTable = $attribute->getBackend()->getTable();
172: $adapter = $this->_getReadAdapter();
173:
174: if (!is_array($productIds)) {
175: $productIds = array($productIds);
176: }
177:
178: if ($storeId === null || $storeId == Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID) {
179: $select = $adapter->select()
180: ->from($attributeTable, array('entity_id', 'value'))
181: ->where('entity_id IN (?)', $productIds)
182: ->where('attribute_id = ?', $attribute->getAttributeId())
183: ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
184:
185: $rows = $adapter->fetchPairs($select);
186: } else {
187: $valueCheckSql = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
188:
189: $select = $adapter->select()
190: ->from(
191: array('t1' => $attributeTable),
192: array('value' => $valueCheckSql))
193: ->joinLeft(
194: array('t2' => $attributeTable),
195: 't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id = ' . (int)$storeId,
196: array('t1.entity_id')
197: )
198: ->where('t1.store_id = ?', Mage_Core_Model_App::ADMIN_STORE_ID)
199: ->where('t1.attribute_id = ?', $attribute->getAttributeId())
200: ->where('t1.entity_id IN(?)', $productIds);
201: $rows = $adapter->fetchPairs($select);
202: }
203:
204: foreach ($productIds as $productId) {
205: if (isset($rows[$productId])) {
206: $statuses[$productId] = $rows[$productId];
207: } else {
208: $statuses[$productId] = -1;
209: }
210: }
211:
212: return $statuses;
213: }
214: }
215: