@@ -30,21 +30,16 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
30
30
private $ analyticsParams = [];
31
31
32
32
/**
33
- * Index constructor.
34
- *
35
33
* @param BackendView $backendView
36
34
* @param AnalyticsHelper $analyticsHelper
37
35
* @param IndexEntityDataProvider $indexEntityDataProvider
38
36
*/
39
37
public function __construct (
40
- BackendView $ backendView ,
41
- AnalyticsHelper $ analyticsHelper ,
42
- IndexEntityDataProvider $ indexEntityDataProvider
43
- ) {
44
- $ this ->backendView = $ backendView ;
45
- $ this ->analyticsHelper = $ analyticsHelper ;
46
- $ this ->indexEntityDataProvider = $ indexEntityDataProvider ;
47
- }
38
+ protected BackendView $ backendView ,
39
+ protected AnalyticsHelper $ analyticsHelper ,
40
+ protected IndexEntityDataProvider $ indexEntityDataProvider ,
41
+ protected ResolverInterface $ localeResolver
42
+ ) { }
48
43
49
44
/**
50
45
* @return BackendView
@@ -86,13 +81,11 @@ public function getAnalyticsParams($additional = [])
86
81
if (empty ($ this ->analyticsParams )) {
87
82
$ params = ['index ' => $ this ->getIndexName ()];
88
83
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 ' );
84
+ if (!empty ($ formData ['from ' ])) {
85
+ $ params ['startDate ' ] = $ this ->formatFormSubmittedDate ($ formData ['from ' ]);
93
86
}
94
- if (isset ($ formData ['to ' ]) && $ formData [ ' to ' ] !== '' ) {
95
- $ params ['endDate ' ] = $ dateTime -> date ($ formData ['to ' ], $ timeZone , true , false )-> format ( ' Y-m-d ' );
87
+ if (! empty ($ formData ['to ' ])) {
88
+ $ params ['endDate ' ] = $ this -> formatFormSubmittedDate ($ formData ['to ' ]);
96
89
}
97
90
}
98
91
@@ -102,6 +95,39 @@ public function getAnalyticsParams($additional = [])
102
95
return array_merge ($ this ->analyticsParams , $ additional );
103
96
}
104
97
98
+ /**
99
+ * @param string $dateString
100
+ * @return string
101
+ * @throws NoSuchEntityException
102
+ */
103
+ protected function formatFormSubmittedDate (string $ dateString ): string
104
+ {
105
+ $ timezone = $ this ->getTimeZone ();
106
+ $ dateTime = $ this ->parseFormSubmittedDate ($ dateString , $ timezone );
107
+ return $ dateTime ->format (AnalyticsHelper::DATE_FORMAT_API );
108
+ }
109
+
110
+ /**
111
+ * @param string|null $dateString
112
+ * @param string|null $timezone
113
+ * @return \DateTime
114
+ * @throws NoSuchEntityException
115
+ */
116
+ protected function parseFormSubmittedDate (string $ dateString = null , string $ timezone = null ): \DateTime
117
+ {
118
+ if (empty ($ timezone )) {
119
+ $ timezone = $ this ->getTimeZone ();
120
+ }
121
+
122
+ if (empty ($ dateString )) {
123
+ return new \DateTime ('now ' , new \DateTimeZone ($ timezone ));
124
+ }
125
+
126
+ $ dateFormatter = $ this ->analyticsHelper ->getAnalyticsDatePickerFormatter ($ timezone );
127
+ $ parsedDate = $ dateFormatter ->parse ($ dateString );
128
+ return (new \DateTime ('now ' , new \DateTimeZone ($ timezone )))->setTimestamp ($ parsedDate );
129
+ }
130
+
105
131
public function getTotalCountOfSearches ()
106
132
{
107
133
return $ this ->analyticsHelper ->getTotalCountOfSearches ($ this ->getAnalyticsParams ());
@@ -309,11 +335,12 @@ public function getNoResultSearches()
309
335
public function checkIsValidDateRange ()
310
336
{
311
337
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 ));
338
+ if (!empty ($ formData ['from ' ])) {
339
+ $ timezone = $ this ->getTimeZone ();
340
+
341
+ $ startDate = $ this ->parseFormSubmittedDate ($ formData ['from ' ], $ timezone );
342
+ $ now = $ this ->parseFormSubmittedDate (null , $ timezone );
343
+ $ diff = date_diff ($ startDate , $ now );
317
344
318
345
if ($ diff ->days > $ this ->getAnalyticRetentionDays ()) {
319
346
return false ;
0 commit comments