123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Search\Controller\Adminhtml\Term;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Backend\App\Action\Context;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Search\Model\QueryFactory;
- use Magento\Search\Controller\Adminhtml\Term as TermController;
- use Magento\Framework\Exception\LocalizedException;
- class Save extends TermController implements HttpPostActionInterface
- {
- /**
- * @var QueryFactory
- */
- private $queryFactory;
- /**
- * @param Context $context
- * @param QueryFactory $queryFactory
- */
- public function __construct(
- Context $context,
- QueryFactory $queryFactory
- ) {
- parent::__construct($context);
- $this->queryFactory = $queryFactory;
- }
- /**
- * Save search query
- *
- * @return \Magento\Backend\Model\View\Result\Redirect
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function execute()
- {
- $data = $this->getRequest()->getPostValue();
- if ($this->getRequest()->isPost() && $data) {
- try {
- $model = $this->loadQuery();
- $model->addData($data);
- $model->setIsProcessed(0);
- $model->save();
- $this->messageManager->addSuccessMessage(__('You saved the search term.'));
- } catch (LocalizedException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- return $this->proceedToEdit($data);
- } catch (\Exception $e) {
- $this->messageManager->addExceptionMessage(
- $e,
- __('Something went wrong while saving the search query.')
- );
- return $this->proceedToEdit($data);
- }
- }
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- return $redirectResult->setPath('search/*');
- }
- /**
- * Create\Load Query model instance
- *
- * @return \Magento\Search\Model\Query
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- private function loadQuery()
- {
- //validate query
- $queryText = $this->getRequest()->getPost('query_text', false);
- $queryId = $this->getRequest()->getPost('query_id', null);
- /* @var $model \Magento\Search\Model\Query */
- $model = $this->queryFactory->create();
- if ($queryText) {
- $storeId = $this->getRequest()->getPost('store_id', false);
- $model->setStoreId($storeId);
- $model->loadByQueryText($queryText);
- if ($model->getId() && $model->getId() != $queryId) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('You already have an identical search term query.')
- );
- }
- }
- if ($queryId && !$model->getId()) {
- $model->load($queryId);
- }
- return $model;
- }
- /**
- * Redirect to Edit page
- *
- * @param array $data
- * @return \Magento\Backend\Model\View\Result\Redirect
- */
- private function proceedToEdit($data)
- {
- $this->_getSession()->setPageData($data);
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- return $redirectResult->setPath('search/*/edit', ['id' => $this->getRequest()->getPost('query_id', null)]);
- }
- }
|