Collection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Form\Fieldset;
  7. use Magento\Eav\Model\Form\Type;
  8. use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
  9. use Magento\Framework\Data\Collection\EntityFactory;
  10. use Magento\Framework\Event\ManagerInterface;
  11. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Psr\Log\LoggerInterface as Logger;
  14. /**
  15. * Eav Form Fieldset Resource Collection
  16. *
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  21. {
  22. /**
  23. * @var StoreManagerInterface
  24. */
  25. protected $_storeManager;
  26. /**
  27. * Store scope ID
  28. *
  29. * @var int
  30. */
  31. protected $_storeId;
  32. /**
  33. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  34. * @param Logger $logger
  35. * @param FetchStrategyInterface $fetchStrategy
  36. * @param ManagerInterface $eventManager
  37. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  38. * @param mixed $connection
  39. * @param AbstractDb $resource
  40. * @codeCoverageIgnore
  41. */
  42. public function __construct(
  43. EntityFactory $entityFactory,
  44. Logger $logger,
  45. FetchStrategyInterface $fetchStrategy,
  46. ManagerInterface $eventManager,
  47. StoreManagerInterface $storeManager,
  48. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  49. AbstractDb $resource = null
  50. ) {
  51. $this->_storeManager = $storeManager;
  52. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  53. }
  54. /**
  55. * Initialize collection model
  56. *
  57. * @return void
  58. * @codeCoverageIgnore
  59. */
  60. protected function _construct()
  61. {
  62. $this->_init(\Magento\Eav\Model\Form\Fieldset::class, \Magento\Eav\Model\ResourceModel\Form\Fieldset::class);
  63. }
  64. /**
  65. * Add Form Type filter to collection
  66. *
  67. * @param Type|int $type
  68. * @return $this
  69. */
  70. public function addTypeFilter($type)
  71. {
  72. if ($type instanceof Type) {
  73. $type = $type->getId();
  74. }
  75. return $this->addFieldToFilter('type_id', $type);
  76. }
  77. /**
  78. * Set order by fieldset sort order
  79. *
  80. * @return $this
  81. * @codeCoverageIgnore
  82. */
  83. public function setSortOrder()
  84. {
  85. $this->setOrder('sort_order', self::SORT_ORDER_ASC);
  86. return $this;
  87. }
  88. /**
  89. * Retrieve label store scope
  90. *
  91. * @return int
  92. */
  93. public function getStoreId()
  94. {
  95. if ($this->_storeId === null) {
  96. return $this->_storeManager->getStore()->getId();
  97. }
  98. return $this->_storeId;
  99. }
  100. /**
  101. * Set store scope ID
  102. *
  103. * @param int $storeId
  104. * @return $this
  105. * @codeCoverageIgnore
  106. */
  107. public function setStoreId($storeId)
  108. {
  109. $this->_storeId = $storeId;
  110. return $this;
  111. }
  112. /**
  113. * Initialize select object
  114. *
  115. * @return $this
  116. */
  117. protected function _initSelect()
  118. {
  119. parent::_initSelect();
  120. $select = $this->getSelect();
  121. $select->join(
  122. ['default_label' => $this->getTable('eav_form_fieldset_label')],
  123. 'main_table.fieldset_id = default_label.fieldset_id AND default_label.store_id = 0',
  124. []
  125. );
  126. if ($this->getStoreId() == 0) {
  127. $select->columns('label', 'default_label');
  128. } else {
  129. $labelExpr = $select->getConnection()->getIfNullSql('store_label.label', 'default_label.label');
  130. $joinCondition = $this->getConnection()->quoteInto(
  131. 'main_table.fieldset_id = store_label.fieldset_id AND store_label.store_id = ?',
  132. (int)$this->getStoreId()
  133. );
  134. $select->joinLeft(
  135. ['store_label' => $this->getTable('eav_form_fieldset_label')],
  136. $joinCondition,
  137. ['label' => $labelExpr]
  138. );
  139. }
  140. return $this;
  141. }
  142. }