/vendor/magento/module-catalog/Controller/Adminhtml/Product/Save.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 188 lines · 129 code · 16 blank · 43 comment · 15 complexity · 6616a2264be4823ccebf4af90534fde6 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Catalog\Controller\Adminhtml\Product;
  8. use Magento\Backend\App\Action;
  9. use Magento\Catalog\Controller\Adminhtml\Product;
  10. class Save extends \Magento\Catalog\Controller\Adminhtml\Product
  11. {
  12. /**
  13. * @var Initialization\Helper
  14. */
  15. protected $initializationHelper;
  16. /**
  17. * @var \Magento\Catalog\Model\Product\Copier
  18. */
  19. protected $productCopier;
  20. /**
  21. * @var \Magento\Catalog\Model\Product\TypeTransitionManager
  22. */
  23. protected $productTypeManager;
  24. /**
  25. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  26. */
  27. protected $productRepository;
  28. /**
  29. * @param Action\Context $context
  30. * @param Builder $productBuilder
  31. * @param Initialization\Helper $initializationHelper
  32. * @param \Magento\Catalog\Model\Product\Copier $productCopier
  33. * @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
  34. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  35. */
  36. public function __construct(
  37. \Magento\Backend\App\Action\Context $context,
  38. Product\Builder $productBuilder,
  39. Initialization\Helper $initializationHelper,
  40. \Magento\Catalog\Model\Product\Copier $productCopier,
  41. \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager,
  42. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  43. ) {
  44. $this->initializationHelper = $initializationHelper;
  45. $this->productCopier = $productCopier;
  46. $this->productTypeManager = $productTypeManager;
  47. $this->productRepository = $productRepository;
  48. parent::__construct($context, $productBuilder);
  49. }
  50. /**
  51. * Save product action
  52. *
  53. * @return \Magento\Backend\Model\View\Result\Redirect
  54. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  55. * @SuppressWarnings(PHPMD.NPathComplexity)
  56. */
  57. public function execute()
  58. {
  59. $storeId = $this->getRequest()->getParam('store');
  60. $redirectBack = $this->getRequest()->getParam('back', false);
  61. $productId = $this->getRequest()->getParam('id');
  62. $resultRedirect = $this->resultRedirectFactory->create();
  63. $data = $this->getRequest()->getPostValue();
  64. $productAttributeSetId = $this->getRequest()->getParam('set');
  65. $productTypeId = $this->getRequest()->getParam('type');
  66. if ($data) {
  67. try {
  68. $product = $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
  69. $this->productTypeManager->processProduct($product);
  70. if (isset($data['product'][$product->getIdFieldName()])) {
  71. throw new \Magento\Framework\Exception\LocalizedException(__('Unable to save product'));
  72. }
  73. $originalSku = $product->getSku();
  74. $product->save();
  75. $this->handleImageRemoveError($data, $product->getId());
  76. $productId = $product->getId();
  77. $productAttributeSetId = $product->getAttributeSetId();
  78. $productTypeId = $product->getTypeId();
  79. /**
  80. * Do copying data to stores
  81. */
  82. if (isset($data['copy_to_stores'])) {
  83. foreach ($data['copy_to_stores'] as $storeTo => $storeFrom) {
  84. $this->_objectManager->create('Magento\Catalog\Model\Product')
  85. ->setStoreId($storeFrom)
  86. ->load($productId)
  87. ->setStoreId($storeTo)
  88. ->save();
  89. }
  90. }
  91. $this->messageManager->addSuccess(__('You saved the product.'));
  92. if ($product->getSku() != $originalSku) {
  93. $this->messageManager->addNotice(
  94. __(
  95. 'SKU for product %1 has been changed to %2.',
  96. $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getName()),
  97. $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getSku())
  98. )
  99. );
  100. }
  101. $this->_eventManager->dispatch(
  102. 'controller_action_catalog_product_save_entity_after',
  103. ['controller' => $this]
  104. );
  105. if ($redirectBack === 'duplicate') {
  106. $newProduct = $this->productCopier->copy($product);
  107. $this->messageManager->addSuccess(__('You duplicated the product.'));
  108. }
  109. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  110. $this->messageManager->addError($e->getMessage());
  111. $this->_session->setProductData($data);
  112. $redirectBack = $productId ? true : 'new';
  113. } catch (\Exception $e) {
  114. $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
  115. $this->messageManager->addError($e->getMessage());
  116. $this->_session->setProductData($data);
  117. $redirectBack = $productId ? true : 'new';
  118. }
  119. } else {
  120. $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
  121. $this->messageManager->addError('No data to save');
  122. return $resultRedirect;
  123. }
  124. if ($redirectBack === 'new') {
  125. $resultRedirect->setPath(
  126. 'catalog/*/new',
  127. ['set' => $productAttributeSetId, 'type' => $productTypeId]
  128. );
  129. } elseif ($redirectBack === 'duplicate' && isset($newProduct)) {
  130. $resultRedirect->setPath(
  131. 'catalog/*/edit',
  132. ['id' => $newProduct->getId(), 'back' => null, '_current' => true]
  133. );
  134. } elseif ($redirectBack) {
  135. $resultRedirect->setPath(
  136. 'catalog/*/edit',
  137. ['id' => $productId, '_current' => true, 'set' => $productAttributeSetId]
  138. );
  139. } else {
  140. $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
  141. }
  142. return $resultRedirect;
  143. }
  144. /**
  145. * Notify customer when image was not deleted in specific case.
  146. * TODO: temporary workaround must be eliminated in MAGETWO-45306
  147. *
  148. * @param array $postData
  149. * @param int $productId
  150. * @return void
  151. */
  152. private function handleImageRemoveError($postData, $productId)
  153. {
  154. if (isset($postData['product']['media_gallery']['images'])) {
  155. $removedImagesAmount = 0;
  156. foreach ($postData['product']['media_gallery']['images'] as $image) {
  157. if (!empty($image['removed'])) {
  158. $removedImagesAmount++;
  159. }
  160. }
  161. if ($removedImagesAmount) {
  162. $expectedImagesAmount = count($postData['product']['media_gallery']['images']) - $removedImagesAmount;
  163. $product = $this->productRepository->getById($productId);
  164. if ($expectedImagesAmount != count($product->getMediaGallery('images'))) {
  165. $this->messageManager->addNotice(
  166. __('The image cannot be removed as it has been assigned to the other image role')
  167. );
  168. }
  169. }
  170. }
  171. }
  172. }