23 private $synGroupRepository;
33 \
Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
35 $this->synGroupRepository = $synGroupRepository;
36 parent::__construct($context);
47 $resultRedirect = $this->resultRedirectFactory->create();
52 $synGroupId = $this->
getRequest()->getParam(
'group_id');
54 if (empty(
$data[
'group_id'])) {
55 $data[
'group_id'] =
null;
59 $synGroup = $this->synGroupRepository->get($synGroupId);
61 if (!$synGroup->getGroupId() && $synGroupId) {
62 $this->messageManager->addErrorMessage(
__(
'This synonym group no longer exists.'));
63 return $resultRedirect->setPath(
'*/*/');
74 $words = explode(
',',
$data[
'synonyms']);
75 $words = array_map(
'trim', $words);
76 $data[
'synonyms'] = strtolower(implode(
',', $words));
78 $synGroup->setGroupId(
$data[
'group_id']);
79 $synGroup->setStoreId(
$data[
'store_id']);
80 $synGroup->setWebsiteId(
$data[
'website_id']);
81 $synGroup->setSynonymGroup(
$data[
'synonyms']);
84 if (isset(
$data[
'mergeOnConflict']) &&
$data[
'mergeOnConflict'] ===
'true') {
85 $this->synGroupRepository->save($synGroup);
89 $this->synGroupRepository->save($synGroup,
true);
91 }
catch (MergeConflictException $exception) {
94 return $resultRedirect->setPath(
'*/*/edit', [
'group_id' => $synGroup->getGroupId()]);
100 return $resultRedirect->setPath(
'*/*/edit', [
'group_id' => $synGroup->getGroupId()]);
103 return $resultRedirect->setPath(
'*/*/');
112 private function getErrorMessage(MergeConflictException $exception)
115 $conflictingSynonyms = $exception->getConflictingSynonyms();
117 foreach ($conflictingSynonyms as $key => $conflictingSynonym) {
118 $conflictingSynonyms[$key] =
'(' . implode(
',', $conflictingSynonym) .
')';
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.',
127 $conflictingSynonyms[0]
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.',
137 count($conflictingSynonyms) + 1,
143 return $conflictingSynonymsMessage;
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository)