Collection.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Attribute;
  7. use Magento\Store\Model\Website;
  8. /**
  9. * EAV additional attribute resource collection (Using Forms)
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. abstract class Collection extends \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  15. {
  16. /**
  17. * code of password hash in customer's EAV tables
  18. */
  19. const EAV_CODE_PASSWORD_HASH = 'password_hash';
  20. /**
  21. * Current website scope instance
  22. *
  23. * @var Website
  24. */
  25. protected $_website;
  26. /**
  27. * Attribute Entity Type Filter
  28. *
  29. * @var \Magento\Eav\Model\Entity\Type
  30. */
  31. protected $_entityType;
  32. /**
  33. * @var \Magento\Store\Model\StoreManagerInterface
  34. */
  35. protected $_storeManager;
  36. /**
  37. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  38. * @param \Psr\Log\LoggerInterface $logger
  39. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  40. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  41. * @param \Magento\Eav\Model\Config $eavConfig
  42. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  43. * @param mixed $connection
  44. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  45. * @codeCoverageIgnore
  46. */
  47. public function __construct(
  48. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  49. \Psr\Log\LoggerInterface $logger,
  50. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  51. \Magento\Framework\Event\ManagerInterface $eventManager,
  52. \Magento\Eav\Model\Config $eavConfig,
  53. \Magento\Store\Model\StoreManagerInterface $storeManager,
  54. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  55. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  56. ) {
  57. $this->_storeManager = $storeManager;
  58. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $connection, $resource);
  59. }
  60. /**
  61. * Default attribute entity type code
  62. *
  63. * @return string
  64. */
  65. abstract protected function _getEntityTypeCode();
  66. /**
  67. * Get EAV website table
  68. *
  69. * Get table, where website-dependent attribute parameters are stored
  70. * If realization doesn't demand this functionality, let this function just return null
  71. *
  72. * @return string|null
  73. */
  74. abstract protected function _getEavWebsiteTable();
  75. /**
  76. * Get default attribute entity type code
  77. *
  78. * @return string
  79. * @codeCoverageIgnore
  80. */
  81. public function getEntityTypeCode()
  82. {
  83. return $this->_getEntityTypeCode();
  84. }
  85. /**
  86. * Return eav entity type instance
  87. *
  88. * @return \Magento\Eav\Model\Entity\Type
  89. */
  90. public function getEntityType()
  91. {
  92. if ($this->_entityType === null) {
  93. $this->_entityType = $this->eavConfig->getEntityType($this->_getEntityTypeCode());
  94. }
  95. return $this->_entityType;
  96. }
  97. /**
  98. * Set Website scope
  99. *
  100. * @param Website|int $website
  101. * @return $this
  102. */
  103. public function setWebsite($website)
  104. {
  105. $this->_website = $this->_storeManager->getWebsite($website);
  106. $this->addBindParam('scope_website_id', $this->_website->getId());
  107. return $this;
  108. }
  109. /**
  110. * Return current website scope instance
  111. *
  112. * @return Website
  113. */
  114. public function getWebsite()
  115. {
  116. if ($this->_website === null) {
  117. $this->_website = $this->_storeManager->getStore()->getWebsite();
  118. }
  119. return $this->_website;
  120. }
  121. /**
  122. * Initialize collection select
  123. *
  124. * @return $this
  125. */
  126. protected function _initSelect()
  127. {
  128. $select = $this->getSelect();
  129. $connection = $this->getConnection();
  130. $entityType = $this->getEntityType();
  131. $extraTable = $entityType->getAdditionalAttributeTable();
  132. $mainDescribe = $this->getConnection()->describeTable($this->getResource()->getMainTable());
  133. $mainColumns = [];
  134. foreach (array_keys($mainDescribe) as $columnName) {
  135. $mainColumns[$columnName] = $columnName;
  136. }
  137. $select->from(['main_table' => $this->getResource()->getMainTable()], $mainColumns);
  138. // additional attribute data table
  139. $extraDescribe = $connection->describeTable($this->getTable($extraTable));
  140. $extraColumns = [];
  141. foreach (array_keys($extraDescribe) as $columnName) {
  142. if (isset($mainColumns[$columnName])) {
  143. continue;
  144. }
  145. $extraColumns[$columnName] = $columnName;
  146. }
  147. $this->addBindParam('mt_entity_type_id', (int)$entityType->getId());
  148. $select->join(
  149. ['additional_table' => $this->getTable($extraTable)],
  150. 'additional_table.attribute_id = main_table.attribute_id',
  151. $extraColumns
  152. )->where(
  153. 'main_table.entity_type_id = :mt_entity_type_id'
  154. );
  155. // scope values
  156. $scopeDescribe = $connection->describeTable($this->_getEavWebsiteTable());
  157. unset($scopeDescribe['attribute_id']);
  158. $scopeColumns = [];
  159. foreach (array_keys($scopeDescribe) as $columnName) {
  160. if ($columnName == 'website_id') {
  161. $scopeColumns['scope_website_id'] = $columnName;
  162. } else {
  163. if (isset($mainColumns[$columnName])) {
  164. $alias = 'scope_' . $columnName;
  165. $condition = 'main_table.' . $columnName . ' IS NULL';
  166. $true = 'scope_table.' . $columnName;
  167. $false = 'main_table.' . $columnName;
  168. $expression = $connection->getCheckSql($condition, $true, $false);
  169. $this->addFilterToMap($columnName, $expression);
  170. $scopeColumns[$alias] = $columnName;
  171. } elseif (isset($extraColumns[$columnName])) {
  172. $alias = 'scope_' . $columnName;
  173. $condition = 'additional_table.' . $columnName . ' IS NULL';
  174. $true = 'scope_table.' . $columnName;
  175. $false = 'additional_table.' . $columnName;
  176. $expression = $connection->getCheckSql($condition, $true, $false);
  177. $this->addFilterToMap($columnName, $expression);
  178. $scopeColumns[$alias] = $columnName;
  179. }
  180. }
  181. }
  182. $select->joinLeft(
  183. ['scope_table' => $this->_getEavWebsiteTable()],
  184. 'scope_table.attribute_id = main_table.attribute_id AND scope_table.website_id = :scope_website_id',
  185. $scopeColumns
  186. );
  187. $websiteId = $this->getWebsite() ? (int)$this->getWebsite()->getId() : 0;
  188. $this->addBindParam('scope_website_id', $websiteId);
  189. return $this;
  190. }
  191. /**
  192. * Specify attribute entity type filter.
  193. * Entity type is defined.
  194. *
  195. * @param int $type
  196. * @return $this
  197. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  198. * @codeCoverageIgnore
  199. */
  200. public function setEntityTypeFilter($type)
  201. {
  202. return $this;
  203. }
  204. /**
  205. * Specify filter by "is_visible" field
  206. *
  207. * @return $this
  208. * @codeCoverageIgnore
  209. */
  210. public function addVisibleFilter()
  211. {
  212. return $this->addFieldToFilter('is_visible', 1);
  213. }
  214. /**
  215. * Exclude system hidden attributes
  216. *
  217. * @return $this
  218. */
  219. public function addSystemHiddenFilter()
  220. {
  221. $connection = $this->getConnection();
  222. $expression = $connection->getCheckSql(
  223. 'additional_table.is_system = 1 AND additional_table.is_visible = 0',
  224. '1',
  225. '0'
  226. );
  227. $this->getSelect()->where($connection->quoteInto($expression . ' = ?', 0));
  228. return $this;
  229. }
  230. /**
  231. * Exclude system hidden attributes but include password hash
  232. *
  233. * @return $this
  234. */
  235. public function addSystemHiddenFilterWithPasswordHash()
  236. {
  237. $connection = $this->getConnection();
  238. $expression = $connection->getCheckSql(
  239. $connection->quoteInto(
  240. 'additional_table.is_system = 1 AND additional_table.is_visible = 0 AND main_table.attribute_code != ?',
  241. self::EAV_CODE_PASSWORD_HASH
  242. ),
  243. '1',
  244. '0'
  245. );
  246. $this->getSelect()->where($connection->quoteInto($expression . ' = ?', 0));
  247. return $this;
  248. }
  249. /**
  250. * Add exclude hidden frontend input attribute filter to collection
  251. *
  252. * @return $this
  253. * @codeCoverageIgnore
  254. */
  255. public function addExcludeHiddenFrontendFilter()
  256. {
  257. return $this->addFieldToFilter('main_table.frontend_input', ['neq' => 'hidden']);
  258. }
  259. }