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