Save.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Search\Controller\Adminhtml\Synonyms;
  8. use Magento\Search\Model\Synonym\MergeConflictException;
  9. class Save extends \Magento\Backend\App\Action
  10. {
  11. /**
  12. * Authorization level of a basic admin session
  13. *
  14. * @see _isAllowed()
  15. */
  16. const ADMIN_RESOURCE = 'Magento_Search::synonyms';
  17. /**
  18. * @var \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  19. */
  20. private $synGroupRepository;
  21. /**
  22. * MassDelete constructor.
  23. *
  24. * @param \Magento\Backend\App\Action\Context $context
  25. * @param \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  26. */
  27. public function __construct(
  28. \Magento\Backend\App\Action\Context $context,
  29. \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  30. ) {
  31. $this->synGroupRepository = $synGroupRepository;
  32. parent::__construct($context);
  33. }
  34. /**
  35. * Save action
  36. *
  37. * @return \Magento\Framework\Controller\ResultInterface
  38. */
  39. public function execute()
  40. {
  41. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  42. $resultRedirect = $this->resultRedirectFactory->create();
  43. // check if data sent
  44. $data = $this->getRequest()->getPostValue();
  45. if ($data) {
  46. $synGroupId = $this->getRequest()->getParam('group_id');
  47. if (empty($data['group_id'])) {
  48. $data['group_id'] = null;
  49. }
  50. // Create model and load any existing data
  51. $synGroup = $this->synGroupRepository->get($synGroupId);
  52. if (!$synGroup->getGroupId() && $synGroupId) {
  53. $this->messageManager->addErrorMessage(__('This synonym group no longer exists.'));
  54. return $resultRedirect->setPath('*/*/');
  55. }
  56. // Pre-process data and save it to model
  57. // Extract website_id and store_id out of scope_id
  58. // scope_id = website_id:store_id
  59. $tokens = explode(':', $data['scope_id']);
  60. $data['website_id'] = $tokens[0];
  61. $data['store_id'] = $tokens[1];
  62. // Remove unnecessary white spaces and convert synonyms to lower case
  63. $words = explode(',', $data['synonyms']);
  64. $words = array_map('trim', $words);
  65. $data['synonyms'] = strtolower(implode(',', $words));
  66. $synGroup->setGroupId($data['group_id']);
  67. $synGroup->setStoreId($data['store_id']);
  68. $synGroup->setWebsiteId($data['website_id']);
  69. $synGroup->setSynonymGroup($data['synonyms']);
  70. // save the data
  71. if (isset($data['mergeOnConflict']) && $data['mergeOnConflict'] === 'true') {
  72. $this->synGroupRepository->save($synGroup);
  73. $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
  74. } else {
  75. try {
  76. $this->synGroupRepository->save($synGroup, true);
  77. $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
  78. } catch (MergeConflictException $exception) {
  79. $this->getMessageManager()->addErrorMessage($this->getErrorMessage($exception));
  80. $this->_getSession()->setFormData($data);
  81. return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
  82. }
  83. }
  84. // check if 'Save and Continue'
  85. if ($this->getRequest()->getParam('back')) {
  86. return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
  87. }
  88. }
  89. return $resultRedirect->setPath('*/*/');
  90. }
  91. /**
  92. * Constructs the error message from the Merge conflict exception
  93. *
  94. * @param MergeConflictException $exception
  95. * @return \Magento\Framework\Phrase
  96. */
  97. private function getErrorMessage(MergeConflictException $exception)
  98. {
  99. $data = $this->getRequest()->getPostValue();
  100. $conflictingSynonyms = $exception->getConflictingSynonyms();
  101. foreach ($conflictingSynonyms as $key => $conflictingSynonym) {
  102. $conflictingSynonyms[$key] = '(' . implode(',', $conflictingSynonym) . ')';
  103. }
  104. if (count($conflictingSynonyms) == 1) {
  105. $conflictingSynonymsMessage = __(
  106. 'The terms you entered, (%1), ' .
  107. 'belong to 1 existing synonym group, %2. ' .
  108. 'Select the "Merge existing synonyms" checkbox so the terms can be merged.',
  109. $data['synonyms'],
  110. $conflictingSynonyms[0]
  111. );
  112. } else {
  113. $lastConflict = array_pop($conflictingSynonyms);
  114. $conflictingInfo = implode(', ', $conflictingSynonyms);
  115. $conflictingSynonymsMessage = __(
  116. 'The terms you entered, (%1), ' .
  117. 'belong to %2 existing synonym groups, %3 and %4. ' .
  118. 'Select the "Merge existing synonyms" checkbox so the terms can be merged.',
  119. $data['synonyms'],
  120. count($conflictingSynonyms) + 1,
  121. $conflictingInfo,
  122. $lastConflict
  123. );
  124. }
  125. return $conflictingSynonymsMessage;
  126. }
  127. }