7
7
use Algolia \AlgoliaSearch \ViewModel \Adminhtml \BackendView ;
8
8
use Magento \Framework \Exception \LocalizedException ;
9
9
use Magento \Framework \Exception \NoSuchEntityException ;
10
+ use Magento \Framework \Locale \ResolverInterface ;
10
11
use Magento \Store \Api \Data \StoreInterface ;
11
12
12
13
class Overview implements \Magento \Framework \View \Element \Block \ArgumentInterface
@@ -26,24 +27,30 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
26
27
/** @var IndexEntityDataProvider */
27
28
private $ indexEntityDataProvider ;
28
29
30
+ /** @var ResolverInterface */
31
+ private $ localeResolver ;
32
+
29
33
/** @var array */
30
34
private $ analyticsParams = [];
31
35
32
36
/**
33
- * Index constructor.
37
+ * Index constructor.
34
38
*
35
39
* @param BackendView $backendView
36
40
* @param AnalyticsHelper $analyticsHelper
37
41
* @param IndexEntityDataProvider $indexEntityDataProvider
42
+ * @param ResolverInterface $localeResolver
38
43
*/
39
44
public function __construct (
40
45
BackendView $ backendView ,
41
46
AnalyticsHelper $ analyticsHelper ,
42
- IndexEntityDataProvider $ indexEntityDataProvider
47
+ IndexEntityDataProvider $ indexEntityDataProvider ,
48
+ ResolverInterface $ localeResolver
43
49
) {
44
50
$ this ->backendView = $ backendView ;
45
51
$ this ->analyticsHelper = $ analyticsHelper ;
46
52
$ this ->indexEntityDataProvider = $ indexEntityDataProvider ;
53
+ $ this ->localeResolver = $ localeResolver ;
47
54
}
48
55
49
56
/**
@@ -54,9 +61,13 @@ public function getBackendView()
54
61
return $ this ->backendView ;
55
62
}
56
63
57
- public function getTimeZone ()
64
+ /**
65
+ * @return string
66
+ * @throws NoSuchEntityException
67
+ */
68
+ public function getTimeZone (): string
58
69
{
59
- return $ this ->backendView ->getDateTime ()->getConfigTimezone (
70
+ return ( string ) $ this ->backendView ->getDateTime ()->getConfigTimezone (
60
71
\Magento \Store \Model \ScopeInterface::SCOPE_STORE ,
61
72
$ this ->getStore ()->getId ()
62
73
);
@@ -81,18 +92,16 @@ public function getIndexName()
81
92
*
82
93
* @return array
83
94
*/
84
- public function getAnalyticsParams ($ additional = [])
95
+ public function getAnalyticsParams (array $ additional = []): array
85
96
{
86
97
if (empty ($ this ->analyticsParams )) {
87
98
$ params = ['index ' => $ this ->getIndexName ()];
88
99
if ($ formData = $ this ->getBackendView ()->getBackendSession ()->getAlgoliaAnalyticsFormData ()) {
89
- $ dateTime = $ this ->getBackendView ()->getDateTime ();
90
- $ timeZone = $ this ->getTimeZone ();
91
- if (isset ($ formData ['from ' ]) && $ formData ['from ' ] !== '' ) {
92
- $ params ['startDate ' ] = $ dateTime ->date ($ formData ['from ' ], $ timeZone , true , false )->format ('Y-m-d ' );
100
+ if (!empty ($ formData ['from ' ])) {
101
+ $ params ['startDate ' ] = $ this ->formatFormSubmittedDate ($ formData ['from ' ]);
93
102
}
94
- if (isset ($ formData ['to ' ]) && $ formData [ ' to ' ] !== '' ) {
95
- $ params ['endDate ' ] = $ dateTime -> date ($ formData ['to ' ], $ timeZone , true , false )-> format ( ' Y-m-d ' );
103
+ if (! empty ($ formData ['to ' ])) {
104
+ $ params ['endDate ' ] = $ this -> formatFormSubmittedDate ($ formData ['to ' ]);
96
105
}
97
106
}
98
107
@@ -102,6 +111,39 @@ public function getAnalyticsParams($additional = [])
102
111
return array_merge ($ this ->analyticsParams , $ additional );
103
112
}
104
113
114
+ /**
115
+ * @param string $dateString
116
+ * @return string
117
+ * @throws NoSuchEntityException
118
+ */
119
+ protected function formatFormSubmittedDate (string $ dateString ): string
120
+ {
121
+ $ timezone = $ this ->getTimeZone ();
122
+ $ dateTime = $ this ->parseFormSubmittedDate ($ dateString , $ timezone );
123
+ return $ dateTime ->format (AnalyticsHelper::DATE_FORMAT_API );
124
+ }
125
+
126
+ /**
127
+ * @param string|null $dateString
128
+ * @param string|null $timezone
129
+ * @return \DateTime
130
+ * @throws NoSuchEntityException
131
+ */
132
+ protected function parseFormSubmittedDate (string $ dateString = null , string $ timezone = null ): \DateTime
133
+ {
134
+ if (empty ($ timezone )) {
135
+ $ timezone = $ this ->getTimeZone ();
136
+ }
137
+
138
+ if (empty ($ dateString )) {
139
+ return new \DateTime ('now ' , new \DateTimeZone ($ timezone ));
140
+ }
141
+
142
+ $ dateFormatter = $ this ->analyticsHelper ->getAnalyticsDatePickerFormatter ($ timezone );
143
+ $ parsedDate = $ dateFormatter ->parse ($ dateString );
144
+ return (new \DateTime ('now ' , new \DateTimeZone ($ timezone )))->setTimestamp ($ parsedDate );
145
+ }
146
+
105
147
public function getTotalCountOfSearches ()
106
148
{
107
149
return $ this ->analyticsHelper ->getTotalCountOfSearches ($ this ->getAnalyticsParams ());
@@ -306,14 +348,15 @@ public function getNoResultSearches()
306
348
*
307
349
* @return bool
308
350
*/
309
- public function checkIsValidDateRange ()
351
+ public function checkIsValidDateRange (): bool
310
352
{
311
353
if ($ formData = $ this ->getBackendView ()->getBackendSession ()->getAlgoliaAnalyticsFormData ()) {
312
- if (isset ($ formData ['from ' ]) && !empty ($ formData ['from ' ])) {
313
- $ dateTime = $ this ->getBackendView ()->getDateTime ();
314
- $ timeZone = $ this ->getTimeZone ();
315
- $ startDate = $ dateTime ->date ($ formData ['from ' ], $ timeZone , true , false );
316
- $ diff = date_diff ($ startDate , $ dateTime ->date (null , $ timeZone , true , null ));
354
+ if (!empty ($ formData ['from ' ])) {
355
+ $ timezone = $ this ->getTimeZone ();
356
+
357
+ $ startDate = $ this ->parseFormSubmittedDate ($ formData ['from ' ], $ timezone );
358
+ $ now = $ this ->parseFormSubmittedDate (null , $ timezone );
359
+ $ diff = date_diff ($ startDate , $ now );
317
360
318
361
if ($ diff ->days > $ this ->getAnalyticRetentionDays ()) {
319
362
return false ;
0 commit comments