init
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Plugin\Wiki;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\WikiTranslationApplier;
|
||||
use Esun\Wiki\Model\ResourceModel\Category\Collection;
|
||||
|
||||
class ApplyWikiCategoryCollectionTranslation
|
||||
{
|
||||
private bool $applying = false;
|
||||
|
||||
public function __construct(private readonly WikiTranslationApplier $translationApplier)
|
||||
{
|
||||
}
|
||||
|
||||
public function afterLoad(Collection $subject, Collection $result): Collection
|
||||
{
|
||||
if ($this->applying) {
|
||||
return $result;
|
||||
}
|
||||
$this->applying = true;
|
||||
try {
|
||||
$this->translationApplier->applyMany($result->getItems());
|
||||
} finally {
|
||||
$this->applying = false;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Plugin\Wiki;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\WikiTranslationApplier;
|
||||
use Esun\Wiki\Model\ResourceModel\Homepage\Collection;
|
||||
|
||||
class ApplyWikiHomepageCollectionTranslation
|
||||
{
|
||||
private bool $applying = false;
|
||||
|
||||
public function __construct(private readonly WikiTranslationApplier $translationApplier)
|
||||
{
|
||||
}
|
||||
|
||||
public function afterLoad(Collection $subject, Collection $result): Collection
|
||||
{
|
||||
if ($this->applying) {
|
||||
return $result;
|
||||
}
|
||||
$this->applying = true;
|
||||
try {
|
||||
$this->translationApplier->applyMany($result->getItems(), true);
|
||||
} finally {
|
||||
$this->applying = false;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Plugin\Wiki;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\WikiTranslationApplier;
|
||||
use Esun\Wiki\Model\Category;
|
||||
use Esun\Wiki\Model\Homepage;
|
||||
|
||||
class ApplyWikiTranslation
|
||||
{
|
||||
public function __construct(
|
||||
private readonly WikiTranslationApplier $translationApplier,
|
||||
private readonly CmsTranslationRepository $translationRepository
|
||||
) {
|
||||
}
|
||||
|
||||
public function afterLoad(Category $subject, Category $result): Category
|
||||
{
|
||||
$this->translationApplier->apply($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function afterDelete(Category $subject, Category $result): Category
|
||||
{
|
||||
$this->translationRepository->clearEntityTranslations(
|
||||
(int)$subject->getId(),
|
||||
$subject instanceof Homepage
|
||||
? CmsTranslationRepository::ENTITY_WIKI_HOMEPAGE
|
||||
: CmsTranslationRepository::ENTITY_WIKI_CATEGORY
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Plugin\Wiki;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\Wiki\Block\Wiki\View;
|
||||
use Esun\Wiki\Model\ResourceModel\Category\CollectionFactory;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class SearchTranslatedCategories
|
||||
{
|
||||
/** @var array<int, array> */
|
||||
private array $results = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly StoreManagerInterface $storeManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function aroundGetSearchResults(View $subject, callable $proceed): array
|
||||
{
|
||||
$query = $subject->getSearchQuery();
|
||||
$storeId = (int)$this->storeManager->getStore()->getId();
|
||||
if ($query === '' || $storeId <= 0) {
|
||||
return $proceed();
|
||||
}
|
||||
|
||||
$cacheKey = spl_object_id($subject);
|
||||
if (isset($this->results[$cacheKey])) {
|
||||
return $this->results[$cacheKey];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$pattern = '%' . $query . '%';
|
||||
$translatedIds = array_map('intval', $connection->fetchCol(
|
||||
$connection->select()
|
||||
->distinct(true)
|
||||
->from($this->resourceConnection->getTableName('esun_localetranslation_cms'), ['entity_id'])
|
||||
->where('entity_type = ?', CmsTranslationRepository::ENTITY_WIKI_CATEGORY)
|
||||
->where('store_id = ?', $storeId)
|
||||
->where('field_name IN (?)', ['title', 'summary', 'content'])
|
||||
->where('translated_value LIKE ?', $pattern)
|
||||
));
|
||||
|
||||
if ($translatedIds === []) {
|
||||
return $this->results[$cacheKey] = $proceed();
|
||||
}
|
||||
|
||||
$collection = $this->collectionFactory->create()
|
||||
->addActiveFilter()
|
||||
->addStoreFilter($storeId)
|
||||
->addTreeOrder();
|
||||
$adapter = $collection->getConnection();
|
||||
$collection->getSelect()->where(implode(' OR ', [
|
||||
$adapter->quoteInto('main_table.title LIKE ?', $pattern),
|
||||
$adapter->quoteInto('main_table.slug LIKE ?', $pattern),
|
||||
$adapter->quoteInto('main_table.summary LIKE ?', $pattern),
|
||||
$adapter->quoteInto('main_table.content LIKE ?', $pattern),
|
||||
$adapter->quoteInto('main_table.category_id IN (?)', $translatedIds),
|
||||
]));
|
||||
|
||||
$results = [];
|
||||
foreach ($collection as $category) {
|
||||
if (!$subject->isCategoryClickable($category)) {
|
||||
continue;
|
||||
}
|
||||
$results[] = $category;
|
||||
if (count($results) >= 20) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->results[$cacheKey] = $results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user