1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Controller\SearchTermsLog;
use Magento\Framework\App\Action\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Search\Model\QueryFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\CatalogSearch\Helper\Data as HelperData;
use Magento\Framework\Controller\Result\Json;
/**
* Controller for save search terms
*/
class Save extends \Magento\Framework\App\Action\Action
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* Catalog search helper
*
* @var HelperData
*/
private $catalogSearchHelper;
/**
* @var QueryFactory
*/
private $queryFactory;
/**
* @var JsonFactory
*/
private $resultJsonFactory;
/**
* @param Context $context
* @param HelperData $catalogSearchHelper
* @param StoreManagerInterface $storeManager
* @param QueryFactory $queryFactory
* @param JsonFactory $resultJsonFactory
*/
public function __construct(
Context $context,
HelperData $catalogSearchHelper,
StoreManagerInterface $storeManager,
QueryFactory $queryFactory,
JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->catalogSearchHelper = $catalogSearchHelper;
$this->queryFactory = $queryFactory;
$this->resultJsonFactory = $resultJsonFactory;
}
/**
* Save search term
*
* @return Json
*/
public function execute()
{
/* @var $query \Magento\Search\Model\Query */
$query = $this->queryFactory->get();
$query->setStoreId($this->storeManager->getStore()->getId());
if ($query->getQueryText() != '') {
try {
if ($this->catalogSearchHelper->isMinQueryLength()) {
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
} else {
$query->saveIncrementalPopularity();
}
$responseContent = ['success' => true, 'error_message' => ''];
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$responseContent = ['success' => false, 'error_message' => $e];
}
} else {
$responseContent = ['success' => false, 'error_message' => __('Search term is empty')];
}
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($responseContent);
}
}