Save.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Controller\Adminhtml\Term;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Search\Model\QueryFactory;
  11. use Magento\Search\Controller\Adminhtml\Term as TermController;
  12. use Magento\Framework\Exception\LocalizedException;
  13. class Save extends TermController implements HttpPostActionInterface
  14. {
  15. /**
  16. * @var QueryFactory
  17. */
  18. private $queryFactory;
  19. /**
  20. * @param Context $context
  21. * @param QueryFactory $queryFactory
  22. */
  23. public function __construct(
  24. Context $context,
  25. QueryFactory $queryFactory
  26. ) {
  27. parent::__construct($context);
  28. $this->queryFactory = $queryFactory;
  29. }
  30. /**
  31. * Save search query
  32. *
  33. * @return \Magento\Backend\Model\View\Result\Redirect
  34. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  35. */
  36. public function execute()
  37. {
  38. $data = $this->getRequest()->getPostValue();
  39. if ($this->getRequest()->isPost() && $data) {
  40. try {
  41. $model = $this->loadQuery();
  42. $model->addData($data);
  43. $model->setIsProcessed(0);
  44. $model->save();
  45. $this->messageManager->addSuccessMessage(__('You saved the search term.'));
  46. } catch (LocalizedException $e) {
  47. $this->messageManager->addErrorMessage($e->getMessage());
  48. return $this->proceedToEdit($data);
  49. } catch (\Exception $e) {
  50. $this->messageManager->addExceptionMessage(
  51. $e,
  52. __('Something went wrong while saving the search query.')
  53. );
  54. return $this->proceedToEdit($data);
  55. }
  56. }
  57. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  58. $redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  59. return $redirectResult->setPath('search/*');
  60. }
  61. /**
  62. * Create\Load Query model instance
  63. *
  64. * @return \Magento\Search\Model\Query
  65. * @throws \Magento\Framework\Exception\LocalizedException
  66. */
  67. private function loadQuery()
  68. {
  69. //validate query
  70. $queryText = $this->getRequest()->getPost('query_text', false);
  71. $queryId = $this->getRequest()->getPost('query_id', null);
  72. /* @var $model \Magento\Search\Model\Query */
  73. $model = $this->queryFactory->create();
  74. if ($queryText) {
  75. $storeId = $this->getRequest()->getPost('store_id', false);
  76. $model->setStoreId($storeId);
  77. $model->loadByQueryText($queryText);
  78. if ($model->getId() && $model->getId() != $queryId) {
  79. throw new \Magento\Framework\Exception\LocalizedException(
  80. __('You already have an identical search term query.')
  81. );
  82. }
  83. }
  84. if ($queryId && !$model->getId()) {
  85. $model->load($queryId);
  86. }
  87. return $model;
  88. }
  89. /**
  90. * Redirect to Edit page
  91. *
  92. * @param array $data
  93. * @return \Magento\Backend\Model\View\Result\Redirect
  94. */
  95. private function proceedToEdit($data)
  96. {
  97. $this->_getSession()->setPageData($data);
  98. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  99. $redirectResult = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  100. return $redirectResult->setPath('search/*/edit', ['id' => $this->getRequest()->getPost('query_id', null)]);
  101. }
  102. }