-
Notifications
You must be signed in to change notification settings - Fork 5
/
admin-global-search-autocomplete.php
150 lines (124 loc) · 4.32 KB
/
admin-global-search-autocomplete.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require_once('vendor/autoload.php');
function checkAccess()
{
$xml = new SimpleXMLElement(file_get_contents('app/etc/local.xml'));
if (!isset($xml->global->elasticsearch->secret)) {
throw new Exception("Missing elasticsearch secret config in local.xml");
}
if (!isset($_GET['key'])) {
throw new Exception("Missing or invalid key");
}
if ($_GET['key'] != (string)$xml->global->elasticsearch->secret) {
throw new Exception("Missing or invalid key");
}
return true;
}
function getElasticaIndex()
{
$xml = new SimpleXMLElement(file_get_contents('app/etc/local.xml'));
if (!isset($xml->global->elasticsearch)) {
throw new Exception("Missing elasticsearch config in local.xml");
}
if (! (string)$xml->global->elasticsearch->index) {
throw new Exception("Missing elasticsearch index name in local.xml");
}
$elasticaClient = new \Elastica\Client(array(
'host' => (string)$xml->global->elasticsearch->host,
'port' => (string)$xml->global->elasticsearch->port,
));
$index = $elasticaClient->getIndex((string)$xml->global->elasticsearch->index);
return $index;
}
/**
* @param $result \Elastica\Result
*/
function getResultUrl($result)
{
$baseUrl = $_GET['base_url'];
$data = $result->getData();
$type = $result->getType();
if ($type == 'customer') {
return $baseUrl . 'customer/edit/id/' . $result->getId();
} elseif ($type == 'order') {
return $baseUrl . 'sales_order/view/order_id/' . $result->getId();
} elseif ($type == 'product') {
return $baseUrl . 'catalog_product/edit/id/' . $result->getId();
} elseif ($type == 'config') {
return $baseUrl . 'system_config/edit/section/' . $data['section_code'];
}
throw new Exception("Can't determine the type of this result");
}
/**
* @param $result \Elastica\Result
*/
function getName($result)
{
$type = $result->getType();
$data = $result->getData();
if (in_array($type, array('order', 'customer'))) {
return $data['fullname'];
} elseif ($type == 'product') {
return $data['name'];
} elseif ($type == 'config') {
return $data['field'];
}
throw new Exception("Can't determine the type of this result");
}
/**
* @param $result \Elastica\Result
*/
function getDescription($result)
{
$data = $result->getData();
if ($result->getType() == 'customer') {
return $data['email'];
} elseif ($result->getType() == 'order') {
return $data['increment_id'] . ' - ' . $data['sku_list'];
} elseif ($result->getType() == 'product') {
return $data['description'];
} elseif ($result->getType() == 'config') {
return $data['section'] . ' > ' . $data['group'];
}
}
checkAccess();
$elasticaIndex = getElasticaIndex();
$query = isset($_REQUEST['query']) ? $_REQUEST['query'] : null;
if (!$query) {
die("Missing ?query");
}
$elasticaQueryString = new \Elastica\Query\MultiMatch();
$elasticaQueryString->setFields(array(
'firstname', 'lastname', 'fullname',
'email', 'increment_id',
'field', 'section', 'group',
'name'
));
$elasticaQueryString->setQuery($query);
$elasticaQueryString->setParam('type', 'phrase_prefix');
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($elasticaQueryString);
$elasticaResultSet = $elasticaIndex->search($elasticaQuery);
$elasticaResults = $elasticaResultSet->getResults();
$totalResults = $elasticaResultSet->getTotalHits();
?>
<ul>
<?php if (! $totalResults): ?>
<li id="error" url="#">
<div style="float:right; color:red; font-weight:bold;">[Error]</div>
<strong>No results</strong><br/>
<span class="informal">No results found for <em><?php echo $query; ?></em></span>
</li>
<?php endif; ?>
<?php foreach ($elasticaResults as $elasticaResult): $data = $elasticaResult->getData(); ?>
<li id="" url="<?php echo getResultUrl($elasticaResult); ?>/">
<div style="float:right; color:red; font-weight:bold;">[
<?php echo $elasticaResult->getType(); ?>
]</div>
<strong><?php echo getName($elasticaResult); ?></strong><br/>
<span class="informal">
<?php echo getDescription($elasticaResult); ?>
</span>
</li>
<?php endforeach; ?>
</ul>