Collection.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\ResourceModel\Option;
  7. /**
  8. * Bundle Options Resource Collection
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  13. {
  14. /**
  15. * All item ids cache
  16. *
  17. * @var array
  18. */
  19. protected $_itemIds;
  20. /**
  21. * True when selections appended
  22. *
  23. * @var bool
  24. */
  25. protected $_selectionsAppended = false;
  26. /**
  27. * Init model and resource model
  28. *
  29. * @return void
  30. */
  31. protected function _construct()
  32. {
  33. $this->_init(\Magento\Bundle\Model\Option::class, \Magento\Bundle\Model\ResourceModel\Option::class);
  34. }
  35. /**
  36. * Joins values to options
  37. *
  38. * @param int $storeId
  39. * @return $this
  40. */
  41. public function joinValues($storeId)
  42. {
  43. $this->getSelect()->joinLeft(
  44. ['option_value_default' => $this->getTable('catalog_product_bundle_option_value')],
  45. implode(
  46. ' AND ',
  47. [
  48. 'main_table.option_id = option_value_default.option_id',
  49. 'main_table.parent_id = option_value_default.parent_product_id',
  50. 'option_value_default.store_id = 0'
  51. ]
  52. ),
  53. []
  54. )->columns(
  55. ['default_title' => 'option_value_default.title']
  56. );
  57. $title = $this->getConnection()->getCheckSql(
  58. 'option_value.title IS NOT NULL',
  59. 'option_value.title',
  60. 'option_value_default.title'
  61. );
  62. if ($storeId !== null) {
  63. $this->getSelect()->columns(
  64. ['title' => $title]
  65. )->joinLeft(
  66. ['option_value' => $this->getTable('catalog_product_bundle_option_value')],
  67. $this->getConnection()->quoteInto(
  68. implode(
  69. ' AND ',
  70. [
  71. 'main_table.option_id = option_value.option_id',
  72. 'main_table.parent_id = option_value.parent_product_id',
  73. 'option_value.store_id = ?'
  74. ]
  75. ),
  76. $storeId
  77. ),
  78. []
  79. );
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Sets product id filter
  85. *
  86. * @param int $productId
  87. * @return $this
  88. */
  89. public function setProductIdFilter($productId)
  90. {
  91. $productTable = $this->getTable('catalog_product_entity');
  92. $linkField = $this->getConnection()->getAutoIncrementField($productTable);
  93. $this->getSelect()->join(
  94. ['cpe' => $productTable],
  95. 'cpe.'.$linkField.' = main_table.parent_id',
  96. []
  97. )->where(
  98. "cpe.entity_id = ?",
  99. $productId
  100. );
  101. return $this;
  102. }
  103. /**
  104. * Set product link filter
  105. *
  106. * @param int $productLinkFieldValue
  107. *
  108. * @return $this
  109. * @since 100.1.0
  110. */
  111. public function setProductLinkFilter($productLinkFieldValue)
  112. {
  113. $this->getSelect()->where(
  114. 'main_table.parent_id = ?',
  115. $productLinkFieldValue
  116. );
  117. return $this;
  118. }
  119. /**
  120. * Sets order by position
  121. *
  122. * @return $this
  123. */
  124. public function setPositionOrder()
  125. {
  126. $this->getSelect()->order('main_table.position asc')->order('main_table.option_id asc');
  127. return $this;
  128. }
  129. /**
  130. * Append selection to options
  131. * stripBefore - indicates to reload
  132. * appendAll - indicates do we need to filter by saleable and required custom options
  133. *
  134. * @param \Magento\Bundle\Model\ResourceModel\Selection\Collection $selectionsCollection
  135. * @param bool $stripBefore
  136. * @param bool $appendAll
  137. * @return \Magento\Framework\DataObject[]
  138. */
  139. public function appendSelections($selectionsCollection, $stripBefore = false, $appendAll = true)
  140. {
  141. if ($stripBefore) {
  142. $this->_stripSelections();
  143. }
  144. if (!$this->_selectionsAppended) {
  145. foreach ($selectionsCollection->getItems() as $key => $selection) {
  146. $option = $this->getItemById($selection->getOptionId());
  147. if ($option) {
  148. if ($appendAll || $selection->isSalable() && !$selection->getRequiredOptions()) {
  149. $selection->setOption($option);
  150. $option->addSelection($selection);
  151. } else {
  152. $selectionsCollection->removeItemByKey($key);
  153. }
  154. }
  155. }
  156. $this->_selectionsAppended = true;
  157. }
  158. return $this->getItems();
  159. }
  160. /**
  161. * Removes appended selections before
  162. *
  163. * @return $this
  164. */
  165. protected function _stripSelections()
  166. {
  167. foreach ($this->getItems() as $option) {
  168. $option->setSelections([]);
  169. }
  170. $this->_selectionsAppended = false;
  171. return $this;
  172. }
  173. /**
  174. * Sets filter by option id
  175. *
  176. * @param array|int $ids
  177. * @return $this
  178. */
  179. public function setIdFilter($ids)
  180. {
  181. if (is_array($ids)) {
  182. $this->addFieldToFilter('main_table.option_id', ['in' => $ids]);
  183. } elseif ($ids != '') {
  184. $this->addFieldToFilter('main_table.option_id', $ids);
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Reset all item ids cache
  190. *
  191. * @return $this
  192. */
  193. public function resetAllIds()
  194. {
  195. $this->_itemIds = null;
  196. return $this;
  197. }
  198. /**
  199. * Retrieve all ids for collection
  200. *
  201. * @return array
  202. */
  203. public function getAllIds()
  204. {
  205. if ($this->_itemIds === null) {
  206. $this->_itemIds = parent::getAllIds();
  207. }
  208. return $this->_itemIds;
  209. }
  210. }