/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Status.php

https://github.com/guiohm/magento-french · PHP · 231 lines · 131 code · 24 blank · 76 comment · 16 complexity · 040f191b07174b5552d18efb279bad1e MD5 · raw file

  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_Catalog
  23. * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product website resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Status extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Product atrribute cache
  37. *
  38. * @var array
  39. */
  40. protected $_productAttributes = array();
  41. /**
  42. * Initialize connection
  43. *
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init('catalog/product_enabled_index', 'product_id');
  48. }
  49. /**
  50. * Retrieve product attribute (public method for status model)
  51. *
  52. * @param string $attributeCode
  53. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  54. */
  55. public function getProductAttribute($attributeCode)
  56. {
  57. return $this->_getProductAttribute($attributeCode);
  58. }
  59. /**
  60. * Retrieve product attribute
  61. *
  62. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  63. */
  64. protected function _getProductAttribute($attribute)
  65. {
  66. if (empty($this->_productAttributes[$attribute])) {
  67. $this->_productAttributes[$attribute] = Mage::getSingleton('catalog/product')->getResource()->getAttribute($attribute);
  68. }
  69. return $this->_productAttributes[$attribute];
  70. }
  71. /**
  72. * Refresh enabled index cache
  73. *
  74. * @param int $productId
  75. * @param int $storeId
  76. *
  77. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Status
  78. */
  79. public function refreshEnabledIndex($productId, $storeId)
  80. {
  81. $statusAttributeId = $this->_getProductAttribute('status')->getId();
  82. $visibilityAttributeId = $this->_getProductAttribute('visibility')->getId();
  83. $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable();
  84. $visibilityTable = $this->_getProductAttribute('visibility')->getBackend()->getTable();
  85. $indexTable = $this->getTable('catalog/product_enabled_index');
  86. if ($storeId == 0) {
  87. foreach (Mage::app()->getStores() as $store) {
  88. $this->refreshEnabledIndex($productId, $store->getId());
  89. }
  90. return $this;
  91. }
  92. $this->_getWriteAdapter()->delete($indexTable, array(
  93. $this->_getWriteAdapter()->quoteInto('product_id=?', $productId),
  94. $this->_getWriteAdapter()->quoteInto('store_id=?', $storeId)
  95. ));
  96. $query = "INSERT INTO $indexTable
  97. SELECT
  98. {$productId}, {$storeId}, IF(t_v.value_id>0, t_v.value, t_v_default.value)
  99. FROM
  100. {$visibilityTable} AS t_v_default
  101. LEFT JOIN {$visibilityTable} AS `t_v`
  102. ON (t_v.entity_id = t_v_default.entity_id) AND (t_v.attribute_id='{$visibilityAttributeId}') AND (t_v.store_id='{$storeId}')
  103. INNER JOIN {$statusTable} AS `t_s_default`
  104. 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
  105. LEFT JOIN {$statusTable} AS `t_s`
  106. ON (t_s.entity_id = t_v_default.entity_id) AND (t_s.attribute_id='{$statusAttributeId}') AND (t_s.store_id='{$storeId}')
  107. WHERE
  108. t_v_default.entity_id={$productId}
  109. AND t_v_default.attribute_id='{$visibilityAttributeId}' AND t_v_default.store_id=0
  110. AND (IF(t_s.value_id>0, t_s.value, t_s_default.value)=".Mage_Catalog_Model_Product_Status::STATUS_ENABLED.")";
  111. $this->_getWriteAdapter()->query($query);
  112. return $this;
  113. }
  114. /**
  115. * Update product status for store
  116. *
  117. * @param int $productId
  118. * @param int $storId
  119. * @param int $value
  120. *
  121. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Status
  122. */
  123. public function updateProductStatus($productId, $storId, $value)
  124. {
  125. $statusAttributeId = $this->_getProductAttribute('status')->getId();
  126. $statusEntityTypeId = $this->_getProductAttribute('status')->getEntityTypeId();
  127. $statusTable = $this->_getProductAttribute('status')->getBackend()->getTable();
  128. $refreshIndex = true;
  129. $prop = array(
  130. 'entity_type_id' => $statusEntityTypeId,
  131. 'attribute_id' => $statusAttributeId,
  132. 'store_id' => $storId,
  133. 'entity_id' => $productId,
  134. 'value' => $value
  135. );
  136. $select = $this->_getWriteAdapter()->select()
  137. ->from($statusTable)
  138. ->where('attribute_id=?', $statusAttributeId)
  139. ->where('store_id=?', $storId)
  140. ->where('entity_id=?', $productId);
  141. $row = $this->_getWriteAdapter()->fetchRow($select);
  142. if ($row) {
  143. if ($row['value'] == $value) {
  144. $refreshIndex = false;
  145. }
  146. else {
  147. $this->_getWriteAdapter()->update($statusTable, $prop, $this->_getWriteAdapter()->quoteInto('value_id=?', $row['value_id']));
  148. }
  149. }
  150. else {
  151. $this->_getWriteAdapter()->insert($statusTable, $prop);
  152. }
  153. if ($refreshIndex) {
  154. $this->refreshEnabledIndex($productId, $storId);
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Retrieve Product(s) status for store
  160. * Return array where key is a product_id, value - status
  161. *
  162. * @param array|int $productIds
  163. * @param int $storeId
  164. * @return array
  165. */
  166. public function getProductStatus($productIds, $storeId = null)
  167. {
  168. $statuses = array();
  169. $attribute = $this->_getProductAttribute('status');
  170. $attributeTable = $attribute->getBackend()->getTable();
  171. if (!is_array($productIds)) {
  172. $productIds = array($productIds);
  173. }
  174. if (is_null($storeId) || $storeId == 0) {
  175. $select = $this->_getReadAdapter()->select()
  176. ->from($attributeTable, array('entity_id', 'value'))
  177. ->where('entity_id IN(?)', $productIds)
  178. ->where('attribute_id=?', $attribute->getAttributeId())
  179. ->where('store_id=?', 0);
  180. $rows = $this->_getWriteAdapter()->fetchPairs($select);
  181. }
  182. else {
  183. $select = $this->_getReadAdapter()->select()
  184. ->from(
  185. array('t1' => $attributeTable),
  186. array('entity_id', 'IF(t2.value_id>0, t2.value, t1.value) as value'))
  187. ->joinLeft(
  188. array('t2' => $attributeTable),
  189. $this->_getReadAdapter()->quoteInto('t1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id=?', $storeId),
  190. array()
  191. )
  192. ->where('t1.store_id = ?', 0)
  193. ->where('t1.attribute_id = ?', $attribute->getAttributeId())
  194. ->where('t1.entity_id IN(?)', $productIds);
  195. $rows = $this->_getWriteAdapter()->fetchPairs($select);
  196. }
  197. foreach ($productIds as $productId) {
  198. if (isset($rows[$productId])) {
  199. $statuses[$productId] = $rows[$productId];
  200. }
  201. else {
  202. $statuses[$productId] = -1;
  203. }
  204. }
  205. return $statuses;
  206. }
  207. }