Collection.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\Attribute;
  7. /**
  8. * EAV Form Attribute Resource Collection
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  15. {
  16. /**
  17. * Current module pathname
  18. *
  19. * @var string
  20. */
  21. protected $_moduleName = '';
  22. /**
  23. * Current EAV entity type code
  24. *
  25. * @var string
  26. */
  27. protected $_entityTypeCode = '';
  28. /**
  29. * Current store instance
  30. *
  31. * @var \Magento\Store\Model\Store
  32. */
  33. protected $_store;
  34. /**
  35. * Eav Entity Type instance
  36. *
  37. * @var \Magento\Eav\Model\Entity\Type
  38. */
  39. protected $_entityType;
  40. /**
  41. * @var \Magento\Store\Model\StoreManagerInterface
  42. */
  43. protected $_storeManager;
  44. /**
  45. * @var \Magento\Eav\Model\Config
  46. */
  47. protected $_eavConfig;
  48. /**
  49. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  50. * @param \Psr\Log\LoggerInterface $logger
  51. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  52. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  53. * @param \Magento\Eav\Model\Config $eavConfig
  54. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  55. * @param mixed $connection
  56. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  57. * @codeCoverageIgnore
  58. */
  59. public function __construct(
  60. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  61. \Psr\Log\LoggerInterface $logger,
  62. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  63. \Magento\Framework\Event\ManagerInterface $eventManager,
  64. \Magento\Eav\Model\Config $eavConfig,
  65. \Magento\Store\Model\StoreManagerInterface $storeManager,
  66. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  67. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  68. ) {
  69. $this->_storeManager = $storeManager;
  70. $this->_eavConfig = $eavConfig;
  71. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  72. }
  73. /**
  74. * Resource initialization
  75. *
  76. * @return void
  77. * @throws \Magento\Framework\Exception\LocalizedException
  78. */
  79. protected function _construct()
  80. {
  81. if (empty($this->_moduleName)) {
  82. throw new \Magento\Framework\Exception\LocalizedException(__('The current module pathname is undefined.'));
  83. }
  84. if (empty($this->_entityTypeCode)) {
  85. throw new \Magento\Framework\Exception\LocalizedException(
  86. __('The current module EAV entity is undefined.')
  87. );
  88. }
  89. }
  90. /**
  91. * Get EAV website table
  92. *
  93. * Get table, where website-dependent attribute parameters are stored
  94. * If realization doesn't demand this functionality, let this function just return null
  95. *
  96. * @return string|null
  97. * @codeCoverageIgnore
  98. */
  99. protected function _getEavWebsiteTable()
  100. {
  101. return null;
  102. }
  103. /**
  104. * Set current store to collection
  105. *
  106. * @param \Magento\Store\Model\Store|string|int $store
  107. * @return $this
  108. * @codeCoverageIgnore
  109. */
  110. public function setStore($store)
  111. {
  112. $this->_store = $this->_storeManager->getStore($store);
  113. return $this;
  114. }
  115. /**
  116. * Return current store instance
  117. *
  118. * @return \Magento\Store\Model\Store
  119. */
  120. public function getStore()
  121. {
  122. if ($this->_store === null) {
  123. $this->_store = $this->_storeManager->getStore();
  124. }
  125. return $this->_store;
  126. }
  127. /**
  128. * Set entity type instance to collection
  129. *
  130. * @param \Magento\Eav\Model\Entity\Type|string|int $entityType
  131. * @return $this
  132. * @codeCoverageIgnore
  133. */
  134. public function setEntityType($entityType)
  135. {
  136. $this->_entityType = $this->_eavConfig->getEntityType($entityType);
  137. return $this;
  138. }
  139. /**
  140. * Return current entity type instance
  141. *
  142. * @return \Magento\Eav\Model\Entity\Type
  143. */
  144. public function getEntityType()
  145. {
  146. if ($this->_entityType === null) {
  147. $this->setEntityType($this->_entityTypeCode);
  148. }
  149. return $this->_entityType;
  150. }
  151. /**
  152. * Add Form Code filter to collection
  153. *
  154. * @param string $code
  155. * @return $this
  156. * @codeCoverageIgnore
  157. */
  158. public function addFormCodeFilter($code)
  159. {
  160. return $this->addFieldToFilter('main_table.form_code', $code);
  161. }
  162. /**
  163. * Set order by attribute sort order
  164. *
  165. * @param string $direction
  166. * @return $this
  167. */
  168. public function setSortOrder($direction = self::SORT_ORDER_ASC)
  169. {
  170. $this->setOrder('ea.is_user_defined', self::SORT_ORDER_ASC);
  171. return $this->setOrder('ca.sort_order', $direction);
  172. }
  173. /**
  174. * Add joins to select
  175. *
  176. * @return $this
  177. */
  178. protected function _beforeLoad()
  179. {
  180. $select = $this->getSelect();
  181. $connection = $this->getConnection();
  182. $entityType = $this->getEntityType();
  183. $this->setItemObjectClass($entityType->getAttributeModel());
  184. $eaColumns = [];
  185. $caColumns = [];
  186. $saColumns = [];
  187. $eaDescribe = $connection->describeTable($this->getTable('eav_attribute'));
  188. unset($eaDescribe['attribute_id']);
  189. foreach (array_keys($eaDescribe) as $columnName) {
  190. $eaColumns[$columnName] = $columnName;
  191. }
  192. $select->join(
  193. ['ea' => $this->getTable('eav_attribute')],
  194. 'main_table.attribute_id = ea.attribute_id',
  195. $eaColumns
  196. );
  197. // join additional attribute data table
  198. $additionalTable = $entityType->getAdditionalAttributeTable();
  199. if ($additionalTable) {
  200. $caDescribe = $connection->describeTable($this->getTable($additionalTable));
  201. unset($caDescribe['attribute_id']);
  202. foreach (array_keys($caDescribe) as $columnName) {
  203. $caColumns[$columnName] = $columnName;
  204. }
  205. $select->join(
  206. ['ca' => $this->getTable($additionalTable)],
  207. 'main_table.attribute_id = ca.attribute_id',
  208. $caColumns
  209. );
  210. }
  211. // add scope values
  212. if ($this->_getEavWebsiteTable()) {
  213. $saDescribe = $connection->describeTable($this->_getEavWebsiteTable());
  214. unset($saDescribe['attribute_id']);
  215. foreach (array_keys($saDescribe) as $columnName) {
  216. if ($columnName == 'website_id') {
  217. $saColumns['scope_website_id'] = $columnName;
  218. } else {
  219. if (isset($eaColumns[$columnName])) {
  220. $code = sprintf('scope_%s', $columnName);
  221. $expression = $connection->getCheckSql('sa.%s IS NULL', 'ea.%s', 'sa.%s');
  222. $saColumns[$code] = new \Zend_Db_Expr(
  223. sprintf($expression, $columnName, $columnName, $columnName)
  224. );
  225. } elseif (isset($caColumns[$columnName])) {
  226. $code = sprintf('scope_%s', $columnName);
  227. $expression = $connection->getCheckSql('sa.%s IS NULL', 'ca.%s', 'sa.%s');
  228. $saColumns[$code] = new \Zend_Db_Expr(
  229. sprintf($expression, $columnName, $columnName, $columnName)
  230. );
  231. }
  232. }
  233. }
  234. $store = $this->getStore();
  235. $joinWebsiteExpression = $connection->quoteInto(
  236. 'sa.attribute_id = main_table.attribute_id AND sa.website_id = ?',
  237. (int)$store->getWebsiteId()
  238. );
  239. $select->joinLeft(['sa' => $this->_getEavWebsiteTable()], $joinWebsiteExpression, $saColumns);
  240. }
  241. // add store attribute label
  242. $storeLabelExpr = $connection->getCheckSql('al.value IS NULL', 'ea.frontend_label', 'al.value');
  243. $joinExpression = $connection->quoteInto(
  244. 'al.attribute_id = main_table.attribute_id AND al.store_id = ?',
  245. (int)$store->getId()
  246. );
  247. $select->joinLeft(
  248. ['al' => $this->getTable('eav_attribute_label')],
  249. $joinExpression,
  250. ['store_label' => $storeLabelExpr]
  251. );
  252. // add entity type filter
  253. $select->where('ea.entity_type_id = ?', (int)$entityType->getId());
  254. return parent::_beforeLoad();
  255. }
  256. }