Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
8 
10 
12 {
18  const ADMIN_RESOURCE = 'Magento_Search::synonyms';
19 
23  private $synGroupRepository;
24 
31  public function __construct(
32  \Magento\Backend\App\Action\Context $context,
33  \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
34  ) {
35  $this->synGroupRepository = $synGroupRepository;
36  parent::__construct($context);
37  }
38 
44  public function execute()
45  {
47  $resultRedirect = $this->resultRedirectFactory->create();
48 
49  // check if data sent
50  $data = $this->getRequest()->getPostValue();
51  if ($data) {
52  $synGroupId = $this->getRequest()->getParam('group_id');
53 
54  if (empty($data['group_id'])) {
55  $data['group_id'] = null;
56  }
57 
58  // Create model and load any existing data
59  $synGroup = $this->synGroupRepository->get($synGroupId);
60 
61  if (!$synGroup->getGroupId() && $synGroupId) {
62  $this->messageManager->addErrorMessage(__('This synonym group no longer exists.'));
63  return $resultRedirect->setPath('*/*/');
64  }
65 
66  // Pre-process data and save it to model
67  // Extract website_id and store_id out of scope_id
68  // scope_id = website_id:store_id
69  $tokens = explode(':', $data['scope_id']);
70  $data['website_id'] = $tokens[0];
71  $data['store_id'] = $tokens[1];
72 
73  // Remove unnecessary white spaces and convert synonyms to lower case
74  $words = explode(',', $data['synonyms']);
75  $words = array_map('trim', $words);
76  $data['synonyms'] = strtolower(implode(',', $words));
77 
78  $synGroup->setGroupId($data['group_id']);
79  $synGroup->setStoreId($data['store_id']);
80  $synGroup->setWebsiteId($data['website_id']);
81  $synGroup->setSynonymGroup($data['synonyms']);
82 
83  // save the data
84  if (isset($data['mergeOnConflict']) && $data['mergeOnConflict'] === 'true') {
85  $this->synGroupRepository->save($synGroup);
86  $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
87  } else {
88  try {
89  $this->synGroupRepository->save($synGroup, true);
90  $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
91  } catch (MergeConflictException $exception) {
92  $this->getMessageManager()->addErrorMessage($this->getErrorMessage($exception));
93  $this->_getSession()->setFormData($data);
94  return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
95  }
96  }
97 
98  // check if 'Save and Continue'
99  if ($this->getRequest()->getParam('back')) {
100  return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
101  }
102  }
103  return $resultRedirect->setPath('*/*/');
104  }
105 
112  private function getErrorMessage(MergeConflictException $exception)
113  {
114  $data = $this->getRequest()->getPostValue();
115  $conflictingSynonyms = $exception->getConflictingSynonyms();
116 
117  foreach ($conflictingSynonyms as $key => $conflictingSynonym) {
118  $conflictingSynonyms[$key] = '(' . implode(',', $conflictingSynonym) . ')';
119  }
120 
121  if (count($conflictingSynonyms) == 1) {
122  $conflictingSynonymsMessage = __(
123  'The terms you entered, (%1), ' .
124  'belong to 1 existing synonym group, %2. ' .
125  'Select the "Merge existing synonyms" checkbox so the terms can be merged.',
126  $data['synonyms'],
127  $conflictingSynonyms[0]
128  );
129  } else {
130  $lastConflict = array_pop($conflictingSynonyms);
131  $conflictingInfo = implode(', ', $conflictingSynonyms);
132  $conflictingSynonymsMessage = __(
133  'The terms you entered, (%1), ' .
134  'belong to %2 existing synonym groups, %3 and %4. ' .
135  'Select the "Merge existing synonyms" checkbox so the terms can be merged.',
136  $data['synonyms'],
137  count($conflictingSynonyms) + 1,
138  $conflictingInfo,
139  $lastConflict
140  );
141  }
142 
143  return $conflictingSynonymsMessage;
144  }
145 }
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository)
Definition: Save.php:31
__()
Definition: __.php:13
$tokens
Definition: cards_list.phtml:9