Skip to content

Commit cb986d0

Browse files
authored
Merge pull request #1708 from algolia/feat/MAGE-1224-reindex-via-grids
MAGE-1224: Add reindexing features in products and pages grid
2 parents 40c0c3b + f305317 commit cb986d0

File tree

6 files changed

+234
-13
lines changed

6 files changed

+234
-13
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\Adminhtml\Reindex;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Backend\Block\Widget\Context;
7+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
8+
9+
abstract class AbstractReindexAllButton implements ButtonProviderInterface
10+
{
11+
protected string $entity;
12+
13+
protected string $redirectPath;
14+
15+
public function __construct(
16+
protected Context $context,
17+
protected ConfigHelper $configHelper
18+
) {}
19+
20+
/**
21+
* @return string
22+
*/
23+
protected function getEntity(): string
24+
{
25+
return $this->entity;
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
protected function getRedirectPath(): string
32+
{
33+
return $this->redirectPath;
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public function getButtonData(): array
40+
{
41+
$entity = $this->getEntity();
42+
$redirectPath = $this->getRedirectPath();
43+
44+
$message = "Are you sure you want to reindex all $entity to Algolia ?";
45+
46+
if (!$this->configHelper->isQueueActive() && $entity === 'products') {
47+
$message .= ' Warning : Your Indexing Queue is not activated. Depending on the size of the data you want to index, it may takes a lot of time and resources.';
48+
$message .= 'We highly suggest to turn it on if you\'re performing a full product reindexing with a large catalog.';
49+
}
50+
51+
$message = htmlentities(__($message));
52+
$url = $this->context->getUrlBuilder()->getUrl('algolia_algoliasearch/indexingmanager/reindex');
53+
54+
return [
55+
'label' => __('Reindex All ' . ucfirst($this->getEntity()) . ' to Algolia'),
56+
'class' => 'algolia_reindex_all',
57+
'on_click' => "deleteConfirm('{$message}', '{$url}', {data:{'entity':'{$entity}', 'redirect': '{$redirectPath}'}})",
58+
'sort_order' => 5,
59+
];
60+
}
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\ReindexAll;
4+
5+
use Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\AbstractReindexAllButton;
6+
7+
class Page extends AbstractReindexAllButton
8+
{
9+
protected string $entity = "pages";
10+
11+
protected string $redirectPath = "cms/page/index";
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\ReindexAll;
4+
5+
use Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\AbstractReindexAllButton;
6+
7+
class Product extends AbstractReindexAllButton
8+
{
9+
protected string $entity = "products";
10+
11+
protected string $redirectPath = "catalog/product/index";
12+
}

Controller/Adminhtml/IndexingManager/Reindex.php

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
66
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
77
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
8+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
10+
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
11+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
812
use Magento\Backend\App\Action;
913
use Magento\Backend\App\Action\Context;
1014
use Magento\Backend\Model\View\Result\Redirect;
@@ -20,6 +24,9 @@ class Reindex extends Action
2024
public function __construct(
2125
Context $context,
2226
protected StoreManagerInterface $storeManager,
27+
protected StoreNameFetcher $storeNameFetcher,
28+
protected IndexNameFetcher $indexNameFetcher,
29+
protected ConfigHelper $configHelper,
2330
protected ProductBatchQueueProcessor $productBatchQueueProcessor,
2431
protected CategoryBatchQueueProcessor $categoryBatchQueueProcessor,
2532
protected PageBatchQueueProcessor $pageBatchQueueProcessor,
@@ -30,34 +37,102 @@ public function __construct(
3037
/**
3138
* @throws ExceededRetriesException
3239
* @throws AlgoliaException
33-
* @throws NoSuchEntityException
40+
* @throws NoSuchEntityException|DiagnosticsException
3441
*/
3542
public function execute()
3643
{
37-
$storeIds = $this->getRequest()->getParam("store_id") === '0' ?
44+
$params = $this->getRequest()->getParams();
45+
$storeIds = !isset($params["store_id"]) || $params["store_id"] === (string) AlgoliaConnector::ALGOLIA_DEFAULT_SCOPE ?
3846
array_keys($this->storeManager->getStores()) :
39-
[(int) $this->getRequest()->getParam("store_id")];
47+
[(int) $params["store_id"]];
4048

41-
$entities = $this->getRequest()->getParam("entity") === 'all' ?
42-
['products', 'categories', 'pages'] :
43-
[$this->getRequest()->getParam("entity")];
49+
$entities = $this->defineEntitiesToIndex($params);
50+
$entityIds = $params['selected'] ?? null;
4451

45-
$this->reindexEntities($entities, $storeIds);
52+
$this->reindexEntities($entities, $storeIds, $entityIds);
4653

4754
/** @var Redirect $resultRedirect */
4855
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
4956

50-
return $resultRedirect->setPath('*/*/');
57+
return $resultRedirect->setPath($this->defineRedirectPath($params));
58+
}
59+
60+
/**
61+
* @return array|string[]
62+
*/
63+
protected function defineEntitiesToIndex(array $params): array
64+
{
65+
$entities = [];
66+
if (isset($params["entity"])) {
67+
$entities = $this->isFullIndex($params) ?
68+
['products', 'categories', 'pages'] :
69+
[$params["entity"]];
70+
} else if ($this->isMassAction($params)) {
71+
$entities = match ($params["namespace"]) {
72+
'product_listing' => ['products'],
73+
'cms_page_listing' => ['pages'],
74+
default => []
75+
};
76+
}
77+
78+
return $entities;
79+
}
80+
81+
/**
82+
* @param array $params
83+
* @return string
84+
*/
85+
protected function defineRedirectPath(array $params): string
86+
{
87+
$redirect = '*/*/';
88+
89+
if (isset($params["redirect"])) {
90+
return $params["redirect"];
91+
}
92+
93+
if ($this->isMassAction($params)) {
94+
$redirect = match ($params["namespace"]) {
95+
'product_listing' => 'catalog/product/index',
96+
'cms_page_listing' => 'cms/page/index',
97+
default => '*/*/'
98+
};
99+
}
100+
101+
return $redirect;
102+
}
103+
104+
/**
105+
* Defines if all entities need to be reindex
106+
*
107+
* @param array $params
108+
* @return bool
109+
*/
110+
protected function isFullIndex(array $params): bool
111+
{
112+
return isset($params["entity"]) && $params["entity"] === 'all';
113+
}
114+
115+
/**
116+
* Check if the request is coming from a grid (products or pages)
117+
*
118+
* @param array $params
119+
* @return bool
120+
*/
121+
protected function isMassAction(array $params): bool
122+
{
123+
return isset($params["namespace"]);
51124
}
52125

53126
/**
54127
* @param array $entities
55128
* @param array|null $storeIds
129+
* @param array|null $entityIds
56130
* @return void
57131
* @throws AlgoliaException
58-
* @throws ExceededRetriesException|NoSuchEntityException|DiagnosticsException
132+
* @throws DiagnosticsException
133+
* @throws NoSuchEntityException
59134
*/
60-
protected function reindexEntities(array $entities, array $storeIds = null): void
135+
protected function reindexEntities(array $entities, array $storeIds = null, array $entityIds = null): void
61136
{
62137
foreach ($entities as $entity) {
63138
$processor = match ($entity) {
@@ -68,10 +143,25 @@ protected function reindexEntities(array $entities, array $storeIds = null): voi
68143
};
69144

70145
foreach ($storeIds as $storeId) {
71-
$processor->processBatch($storeId);
72-
$this->messageManager->addSuccessMessage("Reindex successful (Store: $storeId, entities: $entity)");
146+
$processor->processBatch($storeId, $entityIds);
147+
$message = $this->storeNameFetcher->getStoreName($storeId) . " ";
148+
$message .= "(" . $this->indexNameFetcher->getIndexName('_' . $entity, $storeId);
149+
150+
if (!is_null($entityIds)) {
151+
$recordLabel = count($entityIds) > 1 ? "records" : "record";
152+
$message .= " - " . count($entityIds) . " " . $recordLabel;
153+
} else {
154+
$message .= " - full reindexing job";
155+
}
156+
157+
if (!$this->configHelper->isQueueActive($storeId)) {
158+
$message .= " successfully processed)";
159+
} else {
160+
$message .= " successfully added to the Algolia indexing queue)";
161+
}
162+
163+
$this->messageManager->addSuccessMessage(htmlentities(__($message)));
73164
}
74165
}
75166
}
76167
}
77-
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
3+
<settings>
4+
<buttons>
5+
<button class="Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\ReindexAll\Page" name="algolia_reindex_all" />
6+
</buttons>
7+
</settings>
8+
<listingToolbar name="listing_top">
9+
<massaction name="listing_massaction">
10+
<action name="algolia_reindex">
11+
<settings>
12+
<confirm>
13+
<message translate="true">Are you sure you want to reindex the selected items to Algolia ?</message>
14+
<title translate="true">Reindex items</title>
15+
</confirm>
16+
<url path="algolia_algoliasearch/indexingmanager/reindex"/>
17+
<type>algolia_reindex</type>
18+
<label translate="true">Reindex to Algolia</label>
19+
</settings>
20+
</action>
21+
</massaction>
22+
</listingToolbar>
23+
</listing>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
3+
<settings>
4+
<buttons>
5+
<button class="Algolia\AlgoliaSearch\Block\Adminhtml\Reindex\ReindexAll\Product" name="algolia_reindex_all" />
6+
</buttons>
7+
</settings>
8+
<listingToolbar name="listing_top">
9+
<massaction name="listing_massaction">
10+
<action name="algolia_reindex">
11+
<settings>
12+
<confirm>
13+
<message translate="true">Are you sure you want to reindex the selected items to Algolia ?</message>
14+
<title translate="true">Reindex items</title>
15+
</confirm>
16+
<url path="algolia_algoliasearch/indexingmanager/reindex"/>
17+
<type>algolia_reindex</type>
18+
<label translate="true">Reindex to Algolia</label>
19+
</settings>
20+
</action>
21+
</massaction>
22+
</listingToolbar>
23+
</listing>

0 commit comments

Comments
 (0)