/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Decimal.php

https://github.com/rgranadino/magento-mirror · PHP · 145 lines · 75 code · 13 blank · 57 comment · 5 complexity · 7bc3f2c6677c6da37346167c587f5e7e 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) 2011 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. * Catalog Product Eav Decimal Attributes Indexer 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_Product_Indexer_Eav_Decimal
  34. extends Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  35. {
  36. /**
  37. * Initialize connection and define main index table
  38. *
  39. */
  40. protected function _construct()
  41. {
  42. $this->_init('catalog/product_index_eav_decimal', 'entity_id');
  43. }
  44. /**
  45. * Prepare data index for indexable attributes
  46. *
  47. * @param array $entityIds the entity ids limitation
  48. * @param int $attributeId the attribute id limitation
  49. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Decimal
  50. */
  51. protected function _prepareIndex($entityIds = null, $attributeId = null)
  52. {
  53. $write = $this->_getWriteAdapter();
  54. $idxTable = $this->getIdxTable();
  55. // prepare select attributes
  56. if (is_null($attributeId)) {
  57. $attrIds = $this->_getIndexableAttributes();
  58. } else {
  59. $attrIds = array($attributeId);
  60. }
  61. if (!$attrIds) {
  62. return $this;
  63. }
  64. $productValueExpression = $write->getCheckSql('pds.value_id > 0', 'pds.value', 'pdd.value');
  65. $select = $write->select()
  66. ->from(
  67. array('pdd' => $this->getValueTable('catalog/product', 'decimal')),
  68. array('entity_id', 'attribute_id'))
  69. ->join(
  70. array('cs' => $this->getTable('core/store')),
  71. '',
  72. array('store_id'))
  73. ->joinLeft(
  74. array('pds' => $this->getValueTable('catalog/product', 'decimal')),
  75. 'pds.entity_id = pdd.entity_id AND pds.attribute_id = pdd.attribute_id'
  76. . ' AND pds.store_id=cs.store_id',
  77. array('value' => $productValueExpression))
  78. ->where('pdd.store_id=?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)
  79. ->where('cs.store_id!=?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)
  80. ->where('pdd.attribute_id IN(?)', $attrIds)
  81. ->where("{$productValueExpression} IS NOT NULL");
  82. $statusCond = $write->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
  83. $this->_addAttributeToSelect($select, 'status', 'pdd.entity_id', 'cs.store_id', $statusCond);
  84. if (!is_null($entityIds)) {
  85. $select->where('pdd.entity_id IN(?)', $entityIds);
  86. }
  87. /**
  88. * Add additional external limitation
  89. */
  90. Mage::dispatchEvent('prepare_catalog_product_index_select', array(
  91. 'select' => $select,
  92. 'entity_field' => new Zend_Db_Expr('pdd.entity_id'),
  93. 'website_field' => new Zend_Db_Expr('cs.website_id'),
  94. 'store_field' => new Zend_Db_Expr('cs.store_id')
  95. ));
  96. $query = $select->insertFromSelect($idxTable);
  97. $write->query($query);
  98. return $this;
  99. }
  100. /**
  101. * Retrieve decimal indexable attributes
  102. *
  103. * @return array
  104. */
  105. protected function _getIndexableAttributes()
  106. {
  107. $adapter = $this->_getReadAdapter();
  108. $select = $adapter->select()
  109. ->from(array('ca' => $this->getTable('catalog/eav_attribute')), 'attribute_id')
  110. ->join(
  111. array('ea' => $this->getTable('eav/attribute')),
  112. 'ca.attribute_id = ea.attribute_id',
  113. array())
  114. ->where('ea.attribute_code != ?', 'price')
  115. ->where($this->_getIndexableAttributesCondition())
  116. ->where('ea.backend_type=?', 'decimal');
  117. return $adapter->fetchCol($select);
  118. }
  119. /**
  120. * Retrieve temporary decimal index table name
  121. *
  122. * @param string $table
  123. * @return string
  124. */
  125. public function getIdxTable($table = null)
  126. {
  127. if ($this->useIdxTable()) {
  128. return $this->getTable('catalog/product_eav_decimal_indexer_idx');
  129. }
  130. return $this->getTable('catalog/product_eav_decimal_indexer_tmp');
  131. }
  132. }