Config.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel;
  7. /**
  8. * Catalog Config Resource Model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Config extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * catalog_product entity type id
  16. *
  17. * @var int
  18. */
  19. protected $_entityTypeId;
  20. /**
  21. * Store id
  22. *
  23. * @var int
  24. */
  25. protected $_storeId = null;
  26. /**
  27. * Eav config
  28. *
  29. * @var \Magento\Eav\Model\Config
  30. */
  31. protected $_eavConfig;
  32. /**
  33. * Store manager
  34. *
  35. * @var \Magento\Store\Model\StoreManagerInterface
  36. */
  37. protected $_storeManager;
  38. /**
  39. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  40. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  41. * @param \Magento\Eav\Model\Config $eavConfig
  42. * @param string $connectionName
  43. */
  44. public function __construct(
  45. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  46. \Magento\Store\Model\StoreManagerInterface $storeManager,
  47. \Magento\Eav\Model\Config $eavConfig,
  48. $connectionName = null
  49. ) {
  50. $this->_storeManager = $storeManager;
  51. $this->_eavConfig = $eavConfig;
  52. parent::__construct($context, $connectionName);
  53. }
  54. /**
  55. * Initialize connection
  56. *
  57. * @return void
  58. */
  59. protected function _construct()
  60. {
  61. $this->_init('eav_attribute', 'attribute_id');
  62. }
  63. /**
  64. * Set store id
  65. *
  66. * @param integer $storeId
  67. * @return $this
  68. */
  69. public function setStoreId($storeId)
  70. {
  71. $this->_storeId = (int)$storeId;
  72. return $this;
  73. }
  74. /**
  75. * Return store id.
  76. * If is not set return current app store
  77. *
  78. * @return integer
  79. */
  80. public function getStoreId()
  81. {
  82. if ($this->_storeId === null) {
  83. $this->_storeId = (int)$this->_storeManager->getStore()->getId();
  84. }
  85. return $this->_storeId;
  86. }
  87. /**
  88. * Retrieve catalog_product entity type id
  89. *
  90. * @return int
  91. */
  92. public function getEntityTypeId()
  93. {
  94. if ($this->_entityTypeId === null) {
  95. $this->_entityTypeId = (int)$this->_eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)
  96. ->getId();
  97. }
  98. return $this->_entityTypeId;
  99. }
  100. /**
  101. * Retrieve Product Attributes Used in Catalog Product listing
  102. *
  103. * @return array
  104. */
  105. public function getAttributesUsedInListing()
  106. {
  107. $connection = $this->getConnection();
  108. $storeLabelExpr = $connection->getCheckSql('al.value IS NOT NULL', 'al.value', 'main_table.frontend_label');
  109. $select = $connection->select()->from(
  110. ['main_table' => $this->getTable('eav_attribute')]
  111. )->join(
  112. ['additional_table' => $this->getTable('catalog_eav_attribute')],
  113. 'main_table.attribute_id = additional_table.attribute_id'
  114. )->joinLeft(
  115. ['al' => $this->getTable('eav_attribute_label')],
  116. 'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
  117. ['store_label' => $storeLabelExpr]
  118. )->where(
  119. 'main_table.entity_type_id = ?',
  120. $this->getEntityTypeId()
  121. )->where(
  122. 'additional_table.used_in_product_listing = ?',
  123. 1
  124. );
  125. return $connection->fetchAll($select);
  126. }
  127. /**
  128. * Retrieve Used Product Attributes for Catalog Product Listing Sort By
  129. *
  130. * @return array
  131. */
  132. public function getAttributesUsedForSortBy()
  133. {
  134. $connection = $this->getConnection();
  135. $storeLabelExpr = $connection->getCheckSql('al.value IS NULL', 'main_table.frontend_label', 'al.value');
  136. $select = $connection->select()->from(
  137. ['main_table' => $this->getTable('eav_attribute')]
  138. )->join(
  139. ['additional_table' => $this->getTable('catalog_eav_attribute')],
  140. 'main_table.attribute_id = additional_table.attribute_id'
  141. )->joinLeft(
  142. ['al' => $this->getTable('eav_attribute_label')],
  143. 'al.attribute_id = main_table.attribute_id AND al.store_id = ' . $this->getStoreId(),
  144. ['store_label' => $storeLabelExpr]
  145. )->where(
  146. 'main_table.entity_type_id = ?',
  147. $this->getEntityTypeId()
  148. )->where(
  149. 'additional_table.used_for_sort_by = ?',
  150. 1
  151. );
  152. return $connection->fetchAll($select);
  153. }
  154. }