PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Model/Product/Action.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 179 lines | 84 code | 19 blank | 76 comment | 8 complexity | ec07588c3fd3dae050457c3e8e5c6e98 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Catalog\Model\Product;
  8. /**
  9. * Catalog Product Mass Action processing model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Action extends \Magento\Framework\Model\AbstractModel
  14. {
  15. /**
  16. * Product website factory
  17. *
  18. * @var \Magento\Catalog\Model\Product\WebsiteFactory
  19. */
  20. protected $_productWebsiteFactory;
  21. /** @var \Magento\Framework\Indexer\IndexerRegistry */
  22. protected $indexerRegistry;
  23. /**
  24. * @var \Magento\Eav\Model\Config
  25. */
  26. protected $_eavConfig;
  27. /**
  28. * @var \Magento\Catalog\Model\Indexer\Product\Eav\Processor
  29. */
  30. protected $_productEavIndexerProcessor;
  31. /**
  32. * @param \Magento\Framework\Model\Context $context
  33. * @param \Magento\Framework\Registry $registry
  34. * @param WebsiteFactory $productWebsiteFactory
  35. * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
  36. * @param \Magento\Eav\Model\Config $eavConfig
  37. * @param \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor
  38. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  39. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  40. * @param array $data
  41. */
  42. public function __construct(
  43. \Magento\Framework\Model\Context $context,
  44. \Magento\Framework\Registry $registry,
  45. \Magento\Catalog\Model\Product\WebsiteFactory $productWebsiteFactory,
  46. \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
  47. \Magento\Eav\Model\Config $eavConfig,
  48. \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
  49. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  50. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  51. array $data = []
  52. ) {
  53. $this->_productWebsiteFactory = $productWebsiteFactory;
  54. $this->indexerRegistry = $indexerRegistry;
  55. $this->_eavConfig = $eavConfig;
  56. $this->_productEavIndexerProcessor = $productEavIndexerProcessor;
  57. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  58. }
  59. /**
  60. * Initialize resource model
  61. *
  62. * @return void
  63. */
  64. protected function _construct()
  65. {
  66. $this->_init('Magento\Catalog\Model\ResourceModel\Product\Action');
  67. }
  68. /**
  69. * Retrieve resource instance wrapper
  70. *
  71. * @return \Magento\Catalog\Model\ResourceModel\Product\Action
  72. */
  73. protected function _getResource()
  74. {
  75. return parent::_getResource();
  76. }
  77. /**
  78. * Update attribute values for entity list per store
  79. *
  80. * @param array $productIds
  81. * @param array $attrData
  82. * @param int $storeId
  83. * @return $this
  84. */
  85. public function updateAttributes($productIds, $attrData, $storeId)
  86. {
  87. $this->_eventManager->dispatch(
  88. 'catalog_product_attribute_update_before',
  89. ['attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId]
  90. );
  91. $this->_getResource()->updateAttributes($productIds, $attrData, $storeId);
  92. $this->setData(
  93. ['product_ids' => array_unique($productIds), 'attributes_data' => $attrData, 'store_id' => $storeId]
  94. );
  95. if ($this->_hasIndexableAttributes($attrData)) {
  96. $this->_productEavIndexerProcessor->reindexList(array_unique($productIds));
  97. }
  98. $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
  99. if (!$categoryIndexer->isScheduled()) {
  100. $categoryIndexer->reindexList(array_unique($productIds));
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Attributes array has indexable attributes
  106. *
  107. * @param array $attributesData
  108. * @return bool
  109. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  110. */
  111. protected function _hasIndexableAttributes($attributesData)
  112. {
  113. foreach ($attributesData as $code => $value) {
  114. if ($this->_attributeIsIndexable($code)) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. /**
  121. * Check is attribute indexable in EAV
  122. *
  123. * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute|string $attribute
  124. * @return bool
  125. */
  126. protected function _attributeIsIndexable($attribute)
  127. {
  128. if (!$attribute instanceof \Magento\Catalog\Model\ResourceModel\Eav\Attribute) {
  129. $attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attribute);
  130. }
  131. return $attribute->isIndexable();
  132. }
  133. /**
  134. * Update websites for product action
  135. *
  136. * Allowed types:
  137. * - add
  138. * - remove
  139. *
  140. * @param array $productIds
  141. * @param array $websiteIds
  142. * @param string $type
  143. * @return void
  144. */
  145. public function updateWebsites($productIds, $websiteIds, $type)
  146. {
  147. if ($type == 'add') {
  148. $this->_productWebsiteFactory->create()->addProducts($websiteIds, $productIds);
  149. } elseif ($type == 'remove') {
  150. $this->_productWebsiteFactory->create()->removeProducts($websiteIds, $productIds);
  151. }
  152. $this->setData(
  153. ['product_ids' => array_unique($productIds), 'website_ids' => $websiteIds, 'action_type' => $type]
  154. );
  155. $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
  156. if (!$categoryIndexer->isScheduled()) {
  157. $categoryIndexer->reindexList(array_unique($productIds));
  158. }
  159. }
  160. }