Collection.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Entity\Attribute\Option;
  7. /**
  8. * Entity attribute option collection
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  13. {
  14. /**
  15. * Option value table
  16. *
  17. * @var string
  18. */
  19. protected $_optionValueTable;
  20. /**
  21. * @var \Magento\Framework\App\ResourceConnection
  22. */
  23. protected $_coreResource;
  24. /**
  25. * @var \Magento\Store\Model\StoreManagerInterface
  26. */
  27. protected $_storeManager;
  28. /**
  29. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  30. * @param \Psr\Log\LoggerInterface $logger
  31. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  32. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  33. * @param \Magento\Framework\App\ResourceConnection $coreResource
  34. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  35. * @param mixed $connection
  36. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  37. * @codeCoverageIgnore
  38. */
  39. public function __construct(
  40. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  41. \Psr\Log\LoggerInterface $logger,
  42. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  43. \Magento\Framework\Event\ManagerInterface $eventManager,
  44. \Magento\Framework\App\ResourceConnection $coreResource,
  45. \Magento\Store\Model\StoreManagerInterface $storeManager,
  46. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  47. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  48. ) {
  49. $this->_storeManager = $storeManager;
  50. $this->_coreResource = $coreResource;
  51. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  52. }
  53. /**
  54. * Resource initialization
  55. *
  56. * @return void
  57. */
  58. protected function _construct()
  59. {
  60. $this->_init(
  61. \Magento\Eav\Model\Entity\Attribute\Option::class,
  62. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option::class
  63. );
  64. $this->_optionValueTable = $this->_coreResource->getTableName('eav_attribute_option_value');
  65. }
  66. /**
  67. * Set attribute filter
  68. *
  69. * @param int $setId
  70. * @return $this
  71. * @codeCoverageIgnore
  72. */
  73. public function setAttributeFilter($setId)
  74. {
  75. return $this->addFieldToFilter('main_table.attribute_id', $setId);
  76. }
  77. /**
  78. * Add store filter to collection
  79. *
  80. * @param int $storeId
  81. * @param bool $useDefaultValue
  82. * @return $this
  83. */
  84. public function setStoreFilter($storeId = null, $useDefaultValue = true)
  85. {
  86. if ($storeId === null) {
  87. $storeId = $this->_storeManager->getStore()->getId();
  88. }
  89. $connection = $this->getConnection();
  90. $joinCondition = $connection->quoteInto('tsv.option_id = main_table.option_id AND tsv.store_id = ?', $storeId);
  91. if ($useDefaultValue) {
  92. $this->getSelect()->join(
  93. ['tdv' => $this->_optionValueTable],
  94. 'tdv.option_id = main_table.option_id',
  95. ['default_value' => 'value']
  96. )->joinLeft(
  97. ['tsv' => $this->_optionValueTable],
  98. $joinCondition,
  99. [
  100. 'store_default_value' => 'value',
  101. 'value' => $connection->getCheckSql('tsv.value_id > 0', 'tsv.value', 'tdv.value')
  102. ]
  103. )->where(
  104. 'tdv.store_id = ?',
  105. 0
  106. );
  107. } else {
  108. $this->getSelect()->joinLeft(
  109. ['tsv' => $this->_optionValueTable],
  110. $joinCondition,
  111. 'value'
  112. )->where(
  113. 'tsv.store_id = ?',
  114. $storeId
  115. );
  116. }
  117. $this->setOrder('value', self::SORT_ORDER_ASC);
  118. return $this;
  119. }
  120. /**
  121. * Add option id(s) filter to collection
  122. *
  123. * @param int|array $optionId
  124. * @return $this
  125. * @codeCoverageIgnore
  126. */
  127. public function setIdFilter($optionId)
  128. {
  129. return $this->addFieldToFilter('main_table.option_id', ['in' => $optionId]);
  130. }
  131. /**
  132. * Convert collection items to select options array
  133. *
  134. * @param string $valueKey
  135. * @return array
  136. */
  137. public function toOptionArray($valueKey = 'value')
  138. {
  139. return $this->_toOptionArray('option_id', $valueKey);
  140. }
  141. /**
  142. * Set order by position or alphabetically by values in admin
  143. *
  144. * @param string $dir direction
  145. * @param bool $sortAlpha sort alphabetically by values in admin
  146. * @return $this
  147. */
  148. public function setPositionOrder($dir = self::SORT_ORDER_ASC, $sortAlpha = false)
  149. {
  150. $this->setOrder('main_table.sort_order', $dir);
  151. // sort alphabetically by values in admin
  152. if ($sortAlpha) {
  153. $this->getSelect()->joinLeft(
  154. ['sort_alpha_value' => $this->_optionValueTable],
  155. 'sort_alpha_value.option_id = main_table.option_id AND sort_alpha_value.store_id = 0',
  156. ['value']
  157. );
  158. $this->setOrder('sort_alpha_value.value', $dir);
  159. }
  160. return $this;
  161. }
  162. }