Beispiel #1
0
 /**
  * Edit Synonym Group
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     // 1. Get ID and create model
     $groupId = $this->getRequest()->getParam('group_id');
     /** @var \Magento\Search\Api\Data\SynonymGroupInterface $synGroup */
     $synGroup = $this->synGroupRepository->get($groupId);
     // 2. Initial checking
     if ($groupId && !$synGroup->getGroupId()) {
         $this->messageManager->addError(__('This synonyms group no longer exists.'));
         /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
         $resultRedirect = $this->resultRedirectFactory->create();
         return $resultRedirect->setPath('*/*/');
     }
     // 3. Set entered data if was error when we do save
     $data = $this->_session->getFormData(true);
     if (!empty($data)) {
         $synGroup->setGroupId($data['group_id']);
         $synGroup->setStoreId($data['store_id']);
         $synGroup->setWebsiteId($data['website_id']);
         $synGroup->setSynonymGroup($data['synonyms']);
     }
     // 4. Register model to use later in save
     $this->registry->register(\Magento\Search\Controller\RegistryConstants::SEARCH_SYNONYMS, $synGroup);
     // 5. Build edit synonyms group form
     $resultPage = $this->pageBuilder->build();
     $resultPage->addBreadcrumb($groupId ? __('Edit Synonym Group') : __('New Synonym Group'), $groupId ? __('Edit Synonym Group') : __('New Synonym Group'));
     $resultPage->getConfig()->getTitle()->prepend(__('Synonym Group'));
     $resultPage->getConfig()->getTitle()->prepend($synGroup->getGroupId() ? $synGroup->getSynonymGroup() : __('New Synonym Group'));
     return $resultPage;
 }
Beispiel #2
0
 /**
  * Save action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     // check if data sent
     $data = $this->getRequest()->getPostValue();
     if ($data) {
         $synGroupId = $this->getRequest()->getParam('group_id');
         if (empty($data['group_id'])) {
             $data['group_id'] = null;
         }
         // Create model and load any existing data
         $synGroup = $this->synGroupRepository->get($synGroupId);
         if (!$synGroup->getGroupId() && $synGroupId) {
             $this->messageManager->addError(__('This synonym group no longer exists.'));
             return $resultRedirect->setPath('*/*/');
         }
         // Pre-process data and save it to model
         // Extract website_id and store_id out of scope_id
         // scope_id = website_id:store_id
         $tokens = explode(':', $data['scope_id']);
         $data['website_id'] = $tokens[0];
         $data['store_id'] = $tokens[1];
         // Remove unnecessary white spaces and convert synonyms to lower case
         $words = explode(',', $data['synonyms']);
         $words = array_map('trim', $words);
         $data['synonyms'] = strtolower(implode(',', $words));
         $synGroup->setGroupId($data['group_id']);
         $synGroup->setStoreId($data['store_id']);
         $synGroup->setWebsiteId($data['website_id']);
         $synGroup->setSynonymGroup($data['synonyms']);
         // save the data
         if (isset($data['mergeOnConflict']) && $data['mergeOnConflict'] === 'true') {
             $this->synGroupRepository->save($synGroup);
             $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
         } else {
             try {
                 $this->synGroupRepository->save($synGroup, true);
                 $this->getMessageManager()->addSuccessMessage(__('You saved the synonym group.'));
             } catch (MergeConflictException $exception) {
                 $this->getMessageManager()->addErrorMessage($this->getErrorMessage($exception));
                 $this->_getSession()->setFormData($data);
                 return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
             }
         }
         // check if 'Save and Continue'
         if ($this->getRequest()->getParam('back')) {
             return $resultRedirect->setPath('*/*/edit', ['group_id' => $synGroup->getGroupId()]);
         }
     }
     return $resultRedirect->setPath('*/*/');
 }
Beispiel #3
0
 /**
  * Delete action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $id = $this->getRequest()->getParam('group_id');
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($id) {
         try {
             /** @var \Magento\Search\Model\SynonymGroup $synGroupModel */
             $synGroupModel = $this->synGroupRepository->get($id);
             $this->synGroupRepository->delete($synGroupModel);
             $this->messageManager->addSuccess(__('The synonym group has been deleted.'));
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
             $this->logger->error($e);
         } catch (\Exception $e) {
             $this->messageManager->addError(__('An error was encountered while performing delete operation.'));
             $this->logger->error($e);
         }
     } else {
         $this->messageManager->addError(__('We can\'t find a synonym group to delete.'));
     }
     return $resultRedirect->setPath('*/*/');
 }
Beispiel #4
0
 /**
  * Execute action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  * @throws \Magento\Framework\Exception\LocalizedException|\Exception
  */
 public function execute()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $collectionSize = $collection->getSize();
     $deletedItems = 0;
     foreach ($collection as $synonymGroup) {
         try {
             $this->synGroupRepository->delete($synonymGroup);
             $deletedItems++;
         } catch (\Exception $e) {
             $this->messageManager->addError($e->getMessage());
         }
     }
     if ($deletedItems != 0) {
         if ($collectionSize != $deletedItems) {
             $this->messageManager->addError(__('Failed to delete %1 synonym group(s).', $collectionSize - $deletedItems));
         }
         $this->messageManager->addSuccess(__('A total of %1 synonym group(s) have been deleted.', $deletedItems));
     }
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
     return $resultRedirect->setPath('*/*/');
 }