-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathModuleAjaxTrait.php
363 lines (327 loc) · 12.7 KB
/
ModuleAjaxTrait.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Copyright (C) 2017-2024 thirty bees
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.md
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author thirty bees <[email protected]>
* @copyright 2017-2024 thirty bees
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
namespace ElasticsearchModule;
use Adapter_Exception;
use Configuration;
use Context;
use Db;
use Elasticsearch;
use Elasticsearch\Client;
use Exception;
use HTMLPurifier_Exception;
use Logger;
use PrestaShopDatabaseException;
use PrestaShopException;
use ReflectionClass;
use ReflectionException;
use SmartyException;
use Tools;
if (!defined('_TB_VERSION_')) {
return;
}
/**
* Trait ModuleAjaxTrait
*
* @package ElasticsearchModule
*/
trait ModuleAjaxTrait
{
/**
* Ajax process save module settings
* @throws HTMLPurifier_Exception
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws ReflectionException
*/
public function ajaxProcessSaveSettings()
{
header('Content-Type: application/json; charset=utf-8');
$settings = json_decode(file_get_contents('php://input'), true);
// Figure out which setting keys are available (constants from the main class)
/** @var ReflectionClass $reflect */
$reflect = new ReflectionClass($this);
$consts = $reflect->getConstants();
foreach ($settings as $setting => $value) {
if (in_array($setting, $consts)) {
if ($setting === static::METAS) {
Meta::saveMetas($value);
continue;
} elseif ($setting == static::STOP_WORDS) {
try {
Configuration::updateValue($setting, $value);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
continue;
} elseif ($setting == static::SERVERS) {
if ($settings[static::PROXY]) {
foreach ($value as &$server) {
$server['read'] = 1;
$server['write'] = 1;
}
}
$value = json_encode($value);
} elseif (is_array($value)) {
$value = json_encode($value);
}
try {
Configuration::updateValue($setting, $value);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
}
}
try {
Configuration::updateValue(Elasticsearch::CONFIG_UPDATED, true);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
// Response status
die(json_encode([
'success' => true,
'indexed' => 0,
'total' => (int)IndexStatus::countProducts(null, $this->context->shop->id),
]));
}
/**
* Index remaining products
*
* @throws PrestaShopException
* @throws Adapter_Exception
* @throws SmartyException
*/
public function ajaxProcessIndexRemaining()
{
header('Content-Type: application/json; charset=utf-8');
/** @var Client $client */
$client = static::getClient();
if (!$client) {
die(json_encode([
'success' => false,
]));
}
$version = static::getElasticVersion();
$extra = [];
if (version_compare($version, '7.0', '<')) {
$extra = [
'_type' => 'product'
];
}
$input = json_decode(file_get_contents('php://input'), true);
try {
$amount = (int)(isset($input['amount'])
? (int)$input['amount']
: Configuration::get(static::INDEX_CHUNK_SIZE));
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
$amount = false;
}
if (!$amount) {
$amount = 100;
}
try {
$index = Configuration::get(Elasticsearch::INDEX_PREFIX);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
return;
}
$idShop = Context::getContext()->shop->id;
$idLang = Context::getContext()->language->id;
$dateUpdAlias = Elasticsearch::getAlias('date_upd');
$priceTaxExclAlias = Elasticsearch::getAlias('price_tax_excl');
$metas = Meta::getAllMetas([$idLang]);
if (isset($metas[$idLang])) {
$metas = $metas[$idLang];
}
// Check which products are available for indexing
$products = IndexStatus::getProductsToIndex($amount, 0, null, $this->context->shop->id);
if (empty($products)) {
// Nothing to index
die(json_encode([
'success' => true,
'indexed' => IndexStatus::getIndexed(null, $this->context->shop->id),
'total' => (int)IndexStatus::countProducts(null, $this->context->shop->id),
'nbErrors' => 0,
'errors' => [],
]));
}
$params = [
'body' => [],
];
foreach ($products as &$product) {
$params['body'][] = [
'index' => array_merge($extra, [
'_index' => "{$index}_{$idShop}_{$product->elastic_id_lang}",
'_id' => $product->id,
]),
];
// Process prices for customer groups
foreach ($product->{$priceTaxExclAlias} as $group => $value) {
$product->{"{$priceTaxExclAlias}_{$group}"} = $value;
}
unset($product->{$priceTaxExclAlias});
// Make aggregatable copies of the properties
// These need to be `link_rewrite`d to make sure they can fit a the friendly URL
foreach (get_object_vars($product) as $name => $var) {
// Do not create an aggregatable copy for color codes
// Color codes are meta data for aggregations
if (substr($name, -11) === '_color_code' || substr($name, -11) === '_color_id_attribute') {
continue;
}
if (isset($metas[$name]) && in_array($metas[$name]['elastic_type'], ['string', 'text'])) {
if (is_array($var)) {
foreach ($var as &$item) {
try {
$item = Tools::link_rewrite($item);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
continue;
}
}
} else {
try {
$var = Tools::link_rewrite($var);
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
continue;
}
}
}
$product->{$name . '_agg'} = $var;
}
$params['body'][] = $product;
}
// Push to Elasticsearch
try {
$results = $client->bulk($params);
} catch (Exception $exception) {
die(json_encode([
'success' => false,
]));
}
$failed = [];
foreach ($results['items'] as $result) {
if ((int)substr($result['index']['status'], 0, 1) !== 2) {
preg_match(
'/(?P<index>[a-zA-Z]+)\_(?P<id_shop>\d+)\_(?P<id_lang>\d+)/',
$result['index']['_index'],
$details
);
$failed[] = [
'id_lang' => (int)$details['id_lang'],
'id_shop' => (int)$details['id_shop'],
'id_product' => (int)$result['index']['_id'],
'error' => isset($result['index']['error']['reason'])
? $result['index']['error']['reason'] . (isset($result['index']['error']['caused_by']['reason'])
? ' ' . $result['index']['error']['caused_by']['reason']
: '')
: 'Unknown error',
];
}
}
if (!empty($failed)) {
foreach ($failed as $failure) {
foreach ($products as $index => $product) {
if ((int)$product->id === (int)$failure['id_product']
&& (int)$product->elastic_id_shop === (int)$failure['id_shop']
&& (int)$product->elastic_id_lang === (int)$failure['id_lang']
) {
try {
Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . "elasticsearch_index_status` (`id_product`,`id_lang`,`id_shop`, `error`) VALUES ('{$failed['id_product']}', '{$failed['id_lang']}', '{$failed['id_shop']}', '{$failed['error']}') ON DUPLICATE KEY UPDATE `error` = VALUES(`error`)");
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
unset($products[$index]);
}
}
}
}
// Insert index status into database
$values = '';
foreach ($products as &$product) {
$values .= "('{$product->id}', '{$product->elastic_id_lang}', '{$this->context->shop->id}', '{$product->{$dateUpdAlias}}', ''),";
}
$values = rtrim($values, ',');
if ($values) {
try {
Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . "elasticsearch_index_status` (`id_product`,`id_lang`,`id_shop`, `date_upd`, `error`) VALUES $values ON DUPLICATE KEY UPDATE `date_upd` = VALUES(`date_upd`), `error` = ''");
} catch (PrestaShopException $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
}
// Response status
die(json_encode([
'success' => true,
'indexed' => IndexStatus::getIndexed(null, $this->context->shop->id),
'total' => (int)IndexStatus::countProducts(null, $this->context->shop->id),
'nbErrors' => count($failed),
'errors' => $failed,
]));
}
/**
* Ajax process erase index
*/
public function ajaxProcessEraseIndex()
{
header('Content-Type: application/json; charset=utf-8');
$idShop = Context::getContext()->shop->id;
try {
// Delete the indices first
Indexer::eraseIndices(null, [$idShop]);
// Reset the mappings
Indexer::createMappings(null, [$idShop]);
// Erase the index status for the current store
IndexStatus::erase($idShop);
} catch (Exception $e) {
}
try {
Configuration::updateValue(Elasticsearch::CONFIG_UPDATED, false);
} catch (Exception $e) {
Logger::addLog("Elasticsearch module error: {$e->getMessage()}");
}
// Response status
die(json_encode([
'success' => true,
'indexed' => IndexStatus::getIndexed(null, $idShop),
'total' => (int)IndexStatus::countProducts(null, $idShop),
]));
}
/**
* @return void
*/
public function ajaxProcessGetElasticsearchVersion()
{
header('Content-Type: application/json; charset=utf-8');
try {
die(json_encode([
'version' => static::getElasticVersion()
]));
} catch (Exception $e) {
die(json_encode([
'version' => $this->l('Unknown'),
'errors' => [
sprintf(
$this->l('Unable to initialize Elasticsearch: %s'),
strip_tags($e->getMessage())
)
]
]));
}
}
}