Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #3115, add a warning message for existing terms in other thesaurus #3128

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 118 additions & 5 deletions src/module-elasticsuite-thesaurus/Model/Thesaurus.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
*/
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;
use Magento\Framework\Indexer\IndexerRegistry;

/**
* Thesaurus Model
Expand All @@ -29,6 +31,16 @@
*/
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
*
Expand All @@ -49,6 +61,20 @@ class Thesaurus extends \Magento\Framework\Model\AbstractModel implements Thesau
*/
protected $indexerRegistry;

/**
* Thesaurus Factory
*
* @var ThesaurusFactory
*/
protected $thesaurusFactory;

/**
* Resource Connection
*
* @var ResourceConnection
*/
protected $resourceConnection;

/**
* @var array The store ids of this thesaurus
*/
Expand All @@ -64,13 +90,25 @@ class Thesaurus extends \Magento\Framework\Model\AbstractModel implements Thesau
*/
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 \Magento\Store\Model\StoreManagerInterface $storeManager Store Manager.
* @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
Expand All @@ -79,13 +117,19 @@ 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->storeManager = $storeManager;
$this->indexerRegistry = $indexerRegistry;
$this->thesaurusFactory = $thesaurusFactory;
$this->resourceConnection = $resourceConnection;
$this->storeManager = $storeManager;
$this->messageManager = $messageManager;

parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
Expand All @@ -99,6 +143,7 @@ public function afterSave()
{
parent::afterSave();

$this->checkThesaurusTerms();
$this->invalidateIndex();

return $this;
Expand Down Expand Up @@ -253,6 +298,61 @@ public function setIsActive($status)
return $this->setData(self::IS_ACTIVE, (bool) $status);
}

/**
* Check for existing terms in other thesaurus
*
* @return void
*/
public function checkThesaurusTerms()
{
$termsData = $this->getTermsData();

$terms = [];
foreach ($termsData as $termData) {
if (isset($termData['reference_term']) && !empty($termData['reference_term'])) {
$terms[] = $termData['reference_term'];
}

$terms = array_merge($terms, explode(',', $termData['values']));
}

$connection = $this->resourceConnection->getConnection();
$expandedTableName = $this->resourceConnection->getTableName(self::THESAURUS_EXPANDED_TERMS_TABLE_NAME);
$referenceTableName = $this->resourceConnection->getTableName(self::THESAURUS_REFERENCE_TERMS_TABLE_NAME);

$selectExpanded = $connection->select()
->from(['expanded' => $expandedTableName], ['term', 'thesaurus_id', 'count' => 'COUNT(*)'])
->where('expanded.term IN (?)', $terms)
->where('expanded.thesaurus_id != ?', $this->getId())
->group(['term', 'thesaurus_id'])
->order('expanded.term ASC');

$selectReference = $connection->select()
->from(['reference' => $referenceTableName], ['term', 'thesaurus_id', 'count' => 'COUNT(*)'])
->where('reference.term IN (?)', $terms)
->where('reference.thesaurus_id != ?', $this->getId())
->group(['term', 'thesaurus_id']);

$resultExpanded = $connection->fetchAll($selectExpanded);
$resultReference = $connection->fetchAll($selectReference);

$result = array_merge($resultReference, $resultExpanded);

foreach ($result as $row) {
if ($row['count'] > 0) {
$existingThesaurusId = $row['thesaurus_id'];
$existingThesaurusName = $this->getThesaurusNameById($existingThesaurusId);

$message = __(
'The term "<strong>%1</strong>" is already existing in the <strong>%2</strong> thesaurus.',
$row['term'],
$existingThesaurusName
);
$this->messageManager->addWarning($message);
}
}
}

/**
* Internal Constructor
*
Expand All @@ -274,4 +374,17 @@ private function invalidateIndex()

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();
}
}
Loading