AbstractCollection.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model\ResourceModel;
  7. use Magento\Store\Model\Store;
  8. /**
  9. * Abstract collection of CMS pages and blocks
  10. */
  11. abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  12. {
  13. /**
  14. * Store manager
  15. *
  16. * @var \Magento\Store\Model\StoreManagerInterface
  17. */
  18. protected $storeManager;
  19. /**
  20. * @var \Magento\Framework\EntityManager\MetadataPool
  21. */
  22. protected $metadataPool;
  23. /**
  24. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  25. * @param \Psr\Log\LoggerInterface $logger
  26. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  27. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  28. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  29. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  30. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  31. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  32. */
  33. public function __construct(
  34. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  35. \Psr\Log\LoggerInterface $logger,
  36. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  37. \Magento\Framework\Event\ManagerInterface $eventManager,
  38. \Magento\Store\Model\StoreManagerInterface $storeManager,
  39. \Magento\Framework\EntityManager\MetadataPool $metadataPool,
  40. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  41. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  42. ) {
  43. $this->storeManager = $storeManager;
  44. $this->metadataPool = $metadataPool;
  45. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  46. }
  47. /**
  48. * Perform operations after collection load
  49. *
  50. * @param string $tableName
  51. * @param string|null $linkField
  52. * @return void
  53. */
  54. protected function performAfterLoad($tableName, $linkField)
  55. {
  56. $linkedIds = $this->getColumnValues($linkField);
  57. if (count($linkedIds)) {
  58. $connection = $this->getConnection();
  59. $select = $connection->select()->from(['cms_entity_store' => $this->getTable($tableName)])
  60. ->where('cms_entity_store.' . $linkField . ' IN (?)', $linkedIds);
  61. $result = $connection->fetchAll($select);
  62. if ($result) {
  63. $storesData = [];
  64. foreach ($result as $storeData) {
  65. $storesData[$storeData[$linkField]][] = $storeData['store_id'];
  66. }
  67. foreach ($this as $item) {
  68. $linkedId = $item->getData($linkField);
  69. if (!isset($storesData[$linkedId])) {
  70. continue;
  71. }
  72. $storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true);
  73. if ($storeIdKey !== false) {
  74. $stores = $this->storeManager->getStores(false, true);
  75. $storeId = current($stores)->getId();
  76. $storeCode = key($stores);
  77. } else {
  78. $storeId = current($storesData[$linkedId]);
  79. $storeCode = $this->storeManager->getStore($storeId)->getCode();
  80. }
  81. $item->setData('_first_store_id', $storeId);
  82. $item->setData('store_code', $storeCode);
  83. $item->setData('store_id', $storesData[$linkedId]);
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * Add field filter to collection
  90. *
  91. * @param array|string $field
  92. * @param string|int|array|null $condition
  93. * @return $this
  94. */
  95. public function addFieldToFilter($field, $condition = null)
  96. {
  97. if ($field === 'store_id') {
  98. return $this->addStoreFilter($condition, false);
  99. }
  100. return parent::addFieldToFilter($field, $condition);
  101. }
  102. /**
  103. * Add filter by store
  104. *
  105. * @param int|array|Store $store
  106. * @param bool $withAdmin
  107. * @return $this
  108. */
  109. abstract public function addStoreFilter($store, $withAdmin = true);
  110. /**
  111. * Perform adding filter by store
  112. *
  113. * @param int|array|Store $store
  114. * @param bool $withAdmin
  115. * @return void
  116. */
  117. protected function performAddStoreFilter($store, $withAdmin = true)
  118. {
  119. if ($store instanceof Store) {
  120. $store = [$store->getId()];
  121. }
  122. if (!is_array($store)) {
  123. $store = [$store];
  124. }
  125. if ($withAdmin) {
  126. $store[] = Store::DEFAULT_STORE_ID;
  127. }
  128. $this->addFilter('store', ['in' => $store], 'public');
  129. }
  130. /**
  131. * Join store relation table if there is store filter
  132. *
  133. * @param string $tableName
  134. * @param string|null $linkField
  135. * @return void
  136. */
  137. protected function joinStoreRelationTable($tableName, $linkField)
  138. {
  139. if ($this->getFilter('store')) {
  140. $this->getSelect()->join(
  141. ['store_table' => $this->getTable($tableName)],
  142. 'main_table.' . $linkField . ' = store_table.' . $linkField,
  143. []
  144. )->group(
  145. 'main_table.' . $linkField
  146. );
  147. }
  148. parent::_renderFiltersBefore();
  149. }
  150. /**
  151. * Get SQL for get record count
  152. *
  153. * Extra GROUP BY strip added.
  154. *
  155. * @return \Magento\Framework\DB\Select
  156. */
  157. public function getSelectCountSql()
  158. {
  159. $countSelect = parent::getSelectCountSql();
  160. $countSelect->reset(\Magento\Framework\DB\Select::GROUP);
  161. return $countSelect;
  162. }
  163. /**
  164. * Returns pairs identifier - title for unique identifiers
  165. * and pairs identifier|entity_id - title for non-unique after first
  166. *
  167. * @return array
  168. */
  169. public function toOptionIdArray()
  170. {
  171. $res = [];
  172. $existingIdentifiers = [];
  173. foreach ($this as $item) {
  174. $identifier = $item->getData('identifier');
  175. $data['value'] = $identifier;
  176. $data['label'] = $item->getData('title');
  177. if (in_array($identifier, $existingIdentifiers)) {
  178. $data['value'] .= '|' . $item->getData($this->getIdFieldName());
  179. } else {
  180. $existingIdentifiers[] = $identifier;
  181. }
  182. $res[] = $data;
  183. }
  184. return $res;
  185. }
  186. }