forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThesaurus.php
More file actions
479 lines (420 loc) · 13.2 KB
/
Thesaurus.php
File metadata and controls
479 lines (420 loc) · 13.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteThesaurus\Model;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteThesaurus\Api\Data\ThesaurusInterface;
use Smile\ElasticsuiteThesaurus\Model\Indexer\Thesaurus as ThesaurusIndexer;
/**
* Thesaurus Model
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class Thesaurus extends \Magento\Framework\Model\AbstractModel implements ThesaurusInterface
{
/**
* Name of the Thesaurus Expanded Terms Mysql Table
*/
const THESAURUS_EXPANDED_TERMS_TABLE_NAME = 'smile_elasticsuite_thesaurus_expanded_terms';
/**
* Name of the Thesaurus Reference Terms Mysql Table
*/
const THESAURUS_REFERENCE_TERMS_TABLE_NAME = 'smile_elasticsuite_thesaurus_reference_terms';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'smile_elasticsuite_thesaurus';
/**
* Parameter name in event
* In observer method you can use $observer->getEvent()->getThesaurus() in this case
*
* @var string
*/
protected $_eventObject = 'thesaurus';
/**
* @var IndexerRegistry
*/
protected $indexerRegistry;
/**
* Thesaurus Factory
*
* @var ThesaurusFactory
*/
protected $thesaurusFactory;
/**
* Resource Connection
*
* @var ResourceConnection
*/
protected $resourceConnection;
/**
* @var array The store ids of this thesaurus
*/
private $storeIds = [];
/**
* @var array The term data of this thesaurus
*/
private $termsData;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* Message manager
*
* @var ManagerInterface
*/
private $messageManager;
/**
* PHP constructor
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param \Magento\Framework\Model\Context $context Magento Context
* @param \Magento\Framework\Registry $registry Magento Registry
* @param IndexerRegistry $indexerRegistry Indexers registry
* @param ThesaurusFactory $thesaurusFactory Thesaurus Factory
* @param ResourceConnection $resourceConnection Resource Connection
* @param \Magento\Store\Model\StoreManagerInterface $storeManager Store Manager
* @param ManagerInterface $messageManager Message Manager
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource Magento Resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection Magento Collection
* @param array $data Magento Data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
IndexerRegistry $indexerRegistry,
ThesaurusFactory $thesaurusFactory,
ResourceConnection $resourceConnection,
StoreManagerInterface $storeManager,
ManagerInterface $messageManager,
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->indexerRegistry = $indexerRegistry;
$this->thesaurusFactory = $thesaurusFactory;
$this->resourceConnection = $resourceConnection;
$this->storeManager = $storeManager;
$this->messageManager = $messageManager;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Process after save operations
*
* @return $this
*/
public function afterSave()
{
parent::afterSave();
$this->checkThesaurusTerms();
$this->invalidateIndex();
return $this;
}
/**
* Process after delete operations
*
* @return $this
*/
public function afterDeleteCommit()
{
parent::afterDeleteCommit();
$this->invalidateIndex();
return $this;
}
/**
* Retrieve thesaurus name
*
* @return string
*/
public function getName()
{
return (string) $this->getData(self::NAME);
}
/**
* Set name
*
* @param string $name the value to save
*
* @return Thesaurus
*/
public function setName($name)
{
return $this->setData(self::NAME, (string) $name);
}
/**
* Get Thesaurus ID
*
* @return int|null
*/
public function getThesaurusId()
{
return $this->getId();
}
/**
* Set Thesaurus ID
*
* @param int $identifier the value to save
*
* @return ThesaurusInterface
*/
public function setThesaurusId($identifier)
{
return $this->setId($identifier);
}
/**
* Retrieve thesaurus type
*
* @return string
*/
public function getType()
{
return (string) $this->getData(self::TYPE);
}
/**
* Set type
*
* @param string $type the type of thesaurus to save
*
* @return ThesaurusInterface
*/
public function setType($type)
{
return $this->setData(self::TYPE, (string) $type);
}
/**
* Get store ids
*
* @return int[]
*/
public function getStoreIds()
{
if ($this->storeManager->isSingleStoreMode()) {
$this->storeIds = [$this->storeManager->getStore(true)->getId()];
}
if (empty($this->storeIds)) {
$this->storeIds = $this->getResource()->getStoreIdsFromThesaurusId($this->getThesaurusId());
}
return $this->storeIds;
}
/**
* Set store ids
*
* @param int[] $storeIds the store ids
*
* @return ThesaurusInterface
*/
public function setStoreIds($storeIds)
{
$this->setData('store_id', $storeIds);
$this->storeIds = $storeIds;
return $this;
}
/**
* Get terms data
*
* @return array
*/
public function getTermsData()
{
if (empty($this->termsData)) {
$this->termsData = $this->getResource()->getTermsDataFromThesaurus($this);
}
return $this->termsData;
}
/**
* Get Thesaurus status
*
* @return bool
*/
public function isActive()
{
return (bool) $this->getData(self::IS_ACTIVE);
}
/**
* Set Thesaurus status
*
* @param bool $status The thesaurus status
*
* @return ThesaurusInterface
*/
public function setIsActive($status)
{
return $this->setData(self::IS_ACTIVE, (bool) $status);
}
/**
* Check for existing terms in other thesaurus.
*
* @return void
*/
public function checkThesaurusTerms(): void
{
$termsData = $this->getTermsData();
$currentType = $this->getType();
$lhs = [];
$rhs = [];
foreach ($termsData as $row) {
if (!empty($row['reference_term'])) {
$lhs[] = trim($row['reference_term']);
}
if (!empty($row['values'])) {
foreach (explode(',', $row['values']) as $v) {
$value = trim($v);
if ($value !== '') {
$rhs[] = $value;
}
}
}
}
$lhs = array_values(array_unique($lhs));
$rhs = array_values(array_unique($rhs));
if (empty($lhs) && empty($rhs)) {
return;
}
// Logic for thesaurus with type 'expansion'.
if ($currentType === self::TYPE_EXPANSION) {
$this->checkExpansionTerms($lhs, (int) $this->getId());
return;
}
// Logic for thesaurus with type 'synonym'.
$allTerms = array_unique(array_merge($lhs, $rhs));
$this->checkSynonymTerms($allTerms, (int) $this->getId());
}
/**
* Check duplicate reference terms (LHS) for thesaurus with type 'expansion'.
*
* For thesaurus with type 'expansion' we only show a warning about duplicate when the reference term (LHS)
* exists as a reference term in another thesaurus (duplicate LHS).
* Duplicate values (RHS) are ignored entirely.
*
* @param string[] $lhsTerms List of reference (LHS) terms.
* @param int $currentThesaurusId Identifier of the currently saved thesaurus.
*
* @return void
*/
protected function checkExpansionTerms(array $lhsTerms, int $currentThesaurusId): void
{
if (empty($lhsTerms)) {
return;
}
$connection = $this->resourceConnection->getConnection();
$referenceTable = $this->resourceConnection->getTableName(
self::THESAURUS_REFERENCE_TERMS_TABLE_NAME
);
$select = $connection->select()
->from(['r' => $referenceTable], ['term', 'thesaurus_id', 'count' => 'COUNT(*)'])
->where('r.term IN (?)', $lhsTerms)
->where('r.thesaurus_id != ?', $currentThesaurusId)
->group(['term', 'thesaurus_id']);
$rows = $connection->fetchAll($select);
foreach ($rows as $row) {
if ((int) $row['count'] > 0) {
$name = $this->getThesaurusNameById((int) $row['thesaurus_id']);
$this->messageManager->addWarning(__(
'The reference term "<strong>%1</strong>" is already defined as a ' .
'reference term in the <strong>%2</strong> thesaurus.',
$row['term'],
$name
));
}
}
}
/**
* Check duplicate synonym terms across all thesaurus tables.
*
* For thesaurus with type 'synonym' all synonym terms are checked on duplicate across all tables.
* If duplicates exist, a warning about duplicate will be displayed.
*
* @param string[] $allTerms List of all terms.
* @param int $currentThesaurusId Identifier of the currently saved thesaurus.
*
* @return void
*/
protected function checkSynonymTerms(array $allTerms, int $currentThesaurusId): void
{
if (empty($allTerms)) {
return;
}
$connection = $this->resourceConnection->getConnection();
$referenceTable = $this->resourceConnection->getTableName(
self::THESAURUS_REFERENCE_TERMS_TABLE_NAME
);
$expandedTable = $this->resourceConnection->getTableName(
self::THESAURUS_EXPANDED_TERMS_TABLE_NAME
);
$selectReference = $connection->select()
->from(['r' => $referenceTable], ['term', 'thesaurus_id', 'count' => 'COUNT(*)'])
->where('r.term IN (?)', $allTerms)
->where('r.thesaurus_id != ?', $currentThesaurusId)
->group(['term', 'thesaurus_id']);
$selectExpanded = $connection->select()
->from(['e' => $expandedTable], ['term', 'thesaurus_id', 'count' => 'COUNT(*)'])
->where('e.term IN (?)', $allTerms)
->where('e.thesaurus_id != ?', $currentThesaurusId)
->group(['term', 'thesaurus_id']);
$rows = array_merge(
$connection->fetchAll($selectReference),
$connection->fetchAll($selectExpanded)
);
foreach ($rows as $row) {
if ((int) $row['count'] > 0) {
$name = $this->getThesaurusNameById((int) $row['thesaurus_id']);
$this->messageManager->addWarning(__(
'The term "<strong>%1</strong>" is already existing in the <strong>%2</strong> thesaurus.',
$row['term'],
$name
));
}
}
}
/**
* Internal Constructor
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _construct()
{
$this->_init('Smile\ElasticsuiteThesaurus\Model\ResourceModel\Thesaurus');
}
/**
* Invalidate Thesaurus index
*
* @return $this
*/
private function invalidateIndex()
{
$this->indexerRegistry->get(ThesaurusIndexer::INDEXER_ID)->invalidate();
return $this;
}
/**
* Get the name of the thesaurus by ID
*
* @param int $thesaurusId Thesaurus ID
* @return string
*/
private function getThesaurusNameById($thesaurusId)
{
$thesaurus = $this->thesaurusFactory->create()->load($thesaurusId);
return $thesaurus->getName();
}
}