5
5
use Algolia \AlgoliaSearch \Exception \DiagnosticsException ;
6
6
use Algolia \AlgoliaSearch \Exceptions \AlgoliaException ;
7
7
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 ;
8
12
use Magento \Backend \App \Action ;
9
13
use Magento \Backend \App \Action \Context ;
10
14
use Magento \Backend \Model \View \Result \Redirect ;
@@ -20,6 +24,9 @@ class Reindex extends Action
20
24
public function __construct (
21
25
Context $ context ,
22
26
protected StoreManagerInterface $ storeManager ,
27
+ protected StoreNameFetcher $ storeNameFetcher ,
28
+ protected IndexNameFetcher $ indexNameFetcher ,
29
+ protected ConfigHelper $ configHelper ,
23
30
protected ProductBatchQueueProcessor $ productBatchQueueProcessor ,
24
31
protected CategoryBatchQueueProcessor $ categoryBatchQueueProcessor ,
25
32
protected PageBatchQueueProcessor $ pageBatchQueueProcessor ,
@@ -30,34 +37,102 @@ public function __construct(
30
37
/**
31
38
* @throws ExceededRetriesException
32
39
* @throws AlgoliaException
33
- * @throws NoSuchEntityException
40
+ * @throws NoSuchEntityException|DiagnosticsException
34
41
*/
35
42
public function execute ()
36
43
{
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 ?
38
46
array_keys ($ this ->storeManager ->getStores ()) :
39
- [(int ) $ this -> getRequest ()-> getParam ( "store_id " ) ];
47
+ [(int ) $ params [ "store_id " ] ];
40
48
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 ;
44
51
45
- $ this ->reindexEntities ($ entities , $ storeIds );
52
+ $ this ->reindexEntities ($ entities , $ storeIds, $ entityIds );
46
53
47
54
/** @var Redirect $resultRedirect */
48
55
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
49
56
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 " ]);
51
124
}
52
125
53
126
/**
54
127
* @param array $entities
55
128
* @param array|null $storeIds
129
+ * @param array|null $entityIds
56
130
* @return void
57
131
* @throws AlgoliaException
58
- * @throws ExceededRetriesException|NoSuchEntityException|DiagnosticsException
132
+ * @throws DiagnosticsException
133
+ * @throws NoSuchEntityException
59
134
*/
60
- protected function reindexEntities (array $ entities , array $ storeIds = null ): void
135
+ protected function reindexEntities (array $ entities , array $ storeIds = null , array $ entityIds = null ): void
61
136
{
62
137
foreach ($ entities as $ entity ) {
63
138
$ processor = match ($ entity ) {
@@ -68,10 +143,25 @@ protected function reindexEntities(array $entities, array $storeIds = null): voi
68
143
};
69
144
70
145
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 )));
73
164
}
74
165
}
75
166
}
76
167
}
77
-
0 commit comments