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_Catalog_Model_Resource_Eav_Mysql4_Product_Status extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_productAttributes = array();
00043
00044
00045
00046
00047
00048 protected function _construct()
00049 {
00050 $this->_init('catalog/product_enabled_index', 'product_id');
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 public function getProductAttribute($attributeCode)
00060 {
00061 return $this->_getProductAttribute($attributeCode);
00062 }
00063
00064
00065
00066
00067
00068
00069 protected function _getProductAttribute($attribute)
00070 {
00071 if (empty($this->_productAttributes[$attribute])) {
00072 $this->_productAttributes[$attribute] = Mage::getSingleton('catalog/product')->getResource()->getAttribute($attribute);
00073 }
00074 return $this->_productAttributes[$attribute];
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 public function refreshEnabledIndex($productId, $storeId)
00086 {
00087 $statusAttributeId = $this->_getProductAttribute('status')->getId();
00088 $visibilityAttributeId = $this->_getProductAttribute('visibility')->getId();
00089 $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable();
00090 $visibilityTable = $this->_getProductAttribute('visibility')->getBackend()->getTable();
00091
00092 $indexTable = $this->getTable('catalog/product_enabled_index');
00093
00094 if ($storeId == 0) {
00095 foreach (Mage::app()->getStores() as $store) {
00096 $this->refreshEnabledIndex($productId, $store->getId());
00097 }
00098
00099 return $this;
00100 }
00101
00102 $this->_getWriteAdapter()->delete($indexTable, array(
00103 $this->_getWriteAdapter()->quoteInto('product_id=?', $productId),
00104 $this->_getWriteAdapter()->quoteInto('store_id=?', $storeId)
00105 ));
00106
00107 $query = "INSERT INTO $indexTable
00108 SELECT
00109 {$productId}, {$storeId}, IFNULL(t_v.value, t_v_default.value)
00110 FROM
00111 {$visibilityTable} AS t_v_default
00112 LEFT JOIN {$visibilityTable} AS `t_v`
00113 ON (t_v.entity_id = t_v_default.entity_id) AND (t_v.attribute_id='{$visibilityAttributeId}') AND (t_v.store_id='{$storeId}')
00114 INNER JOIN {$statusTable} AS `t_s_default`
00115 ON (t_s_default.entity_id = t_v_default.entity_id) AND (t_s_default.attribute_id='{$statusAttributeId}') AND t_s_default.store_id=0
00116 LEFT JOIN {$statusTable} AS `t_s`
00117 ON (t_s.entity_id = t_v_default.entity_id) AND (t_s.attribute_id='{$statusAttributeId}') AND (t_s.store_id='{$storeId}')
00118 WHERE
00119 t_v_default.entity_id={$productId}
00120 AND t_v_default.attribute_id='{$visibilityAttributeId}' AND t_v_default.store_id=0
00121 AND (IFNULL(t_s.value, t_s_default.value)=".Mage_Catalog_Model_Product_Status::STATUS_ENABLED.")";
00122 $this->_getWriteAdapter()->query($query);
00123
00124 return $this;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 public function updateProductStatus($productId, $storId, $value)
00137 {
00138 $statusAttributeId = $this->_getProductAttribute('status')->getId();
00139 $statusEntityTypeId = $this->_getProductAttribute('status')->getEntityTypeId();
00140 $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable();
00141 $refreshIndex = true;
00142
00143 $prop = array(
00144 'entity_type_id' => $statusEntityTypeId,
00145 'attribute_id' => $statusAttributeId,
00146 'store_id' => $storId,
00147 'entity_id' => $productId,
00148 'value' => $value
00149 );
00150
00151 $select = $this->_getWriteAdapter()->select()
00152 ->from($statusTable)
00153 ->where('attribute_id=?', $statusAttributeId)
00154 ->where('store_id=?', $storId)
00155 ->where('entity_id=?', $productId);
00156 $row = $this->_getWriteAdapter()->fetchRow($select);
00157
00158 if ($row) {
00159 if ($row['value'] == $value) {
00160 $refreshIndex = false;
00161 }
00162 else {
00163 $this->_getWriteAdapter()->update($statusTable, $prop, $this->_getWriteAdapter()->quoteInto('value_id=?', $row['value_id']));
00164 }
00165 }
00166 else {
00167 $this->_getWriteAdapter()->insert($statusTable, $prop);
00168 }
00169
00170 if ($refreshIndex) {
00171 $this->refreshEnabledIndex($productId, $storId);
00172 }
00173
00174 return $this;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 public function getProductStatus($productIds, $storeId = null)
00186 {
00187 $statuses = array();
00188
00189 $attribute = $this->_getProductAttribute('status');
00190 $attributeTable = $attribute->getBackend()->getTable();
00191
00192 if (!is_array($productIds)) {
00193 $productIds = array($productIds);
00194 }
00195
00196 if (is_null($storeId) || $storeId == 0) {
00197 $select = $this->_getReadAdapter()->select()
00198 ->from($attributeTable, array('entity_id', 'value'))
00199 ->where('entity_id IN(?)', $productIds)
00200 ->where('attribute_id=?', $attribute->getAttributeId())
00201 ->where('store_id=?', 0);
00202 $rows = $this->_getWriteAdapter()->fetchPairs($select);
00203 }
00204 else {
00205 $select = $this->_getReadAdapter()->select()
00206 ->from(
00207 array('t1' => $attributeTable),
00208 array('entity_id', 'IFNULL(t2.value, t1.value) as value'))
00209 ->joinLeft(
00210 array('t2' => $attributeTable),
00211 $this->_getReadAdapter()->quoteInto('t1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id=?', $storeId),
00212 array()
00213 )
00214 ->where('t1.store_id = ?', 0)
00215 ->where('t1.attribute_id = ?', $attribute->getAttributeId())
00216 ->where('t1.entity_id IN(?)', $productIds);
00217 $rows = $this->_getWriteAdapter()->fetchPairs($select);
00218 }
00219
00220 foreach ($productIds as $productId) {
00221 if (isset($rows[$productId])) {
00222 $statuses[$productId] = $rows[$productId];
00223 }
00224 else {
00225 $statuses[$productId] = -1;
00226 }
00227 }
00228
00229 return $statuses;
00230 }
00231 }