Skip to content

Commit 2f2c150

Browse files
authored
Merge pull request #1515 from algolia/feature/MAGE-866
Feature/mage 866
2 parents 5ad5942 + 7b089a1 commit 2f2c150

File tree

2 files changed

+83
-27
lines changed

2 files changed

+83
-27
lines changed

Helper/AnalyticsHelper.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Config\AnalyticsConfig;
77
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
88
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
9+
use Magento\Framework\Locale\ResolverInterface;
910

1011
class AnalyticsHelper
1112
{
@@ -14,9 +15,6 @@ class AnalyticsHelper
1415
public const ANALYTICS_FILTER_PATH = '/2/filters';
1516
public const ANALYTICS_CLICKS_PATH = '/2/clicks';
1617

17-
/** @var AlgoliaHelper */
18-
private $algoliaHelper;
19-
2018
/** @var ConfigHelper */
2119
private $configHelper;
2220

@@ -26,6 +24,12 @@ class AnalyticsHelper
2624
/** @var Logger */
2725
private $logger;
2826

27+
/** @var ResolverInterface */
28+
private $localeResolver;
29+
30+
public const DATE_FORMAT_PICKER = 'dd MMM yyyy';
31+
public const DATE_FORMAT_API = 'Y-m-d';
32+
2933
private $searches;
3034
private $users;
3135
private $rateOfNoResults;
@@ -58,25 +62,21 @@ class AnalyticsHelper
5862
protected $region;
5963

6064
/**
61-
* @param AlgoliaHelper $algoliaHelper
6265
* @param ConfigHelper $configHelper
6366
* @param IndexEntityDataProvider $entityHelper
6467
* @param Logger $logger
65-
* @param string $region
68+
* @param ResolverInterface $localeResolver
6669
*/
6770
public function __construct(
68-
AlgoliaHelper $algoliaHelper,
6971
ConfigHelper $configHelper,
7072
IndexEntityDataProvider $entityHelper,
7173
Logger $logger,
72-
string $region = 'us'
74+
ResolverInterface $localeResolver
7375
) {
74-
$this->algoliaHelper = $algoliaHelper;
7576
$this->configHelper = $configHelper;
76-
7777
$this->entityHelper = $entityHelper;
78-
7978
$this->logger = $logger;
79+
$this->localeResolver = $localeResolver;
8080
$this->region = $this->configHelper->getAnalyticsRegion();
8181
}
8282

@@ -371,4 +371,17 @@ public function getErrors()
371371
{
372372
return $this->errors;
373373
}
374+
375+
/**
376+
* @param string $timezone
377+
* @return \IntlDateFormatter
378+
*/
379+
public function getAnalyticsDatePickerFormatter(string $timezone): \IntlDateFormatter
380+
{
381+
$locale = $this->localeResolver->getLocale();
382+
$dateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, $timezone);
383+
$dateFormatter->setPattern(self::DATE_FORMAT_PICKER);
384+
return $dateFormatter;
385+
}
386+
374387
}

ViewModel/Adminhtml/Analytics/Overview.php

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Algolia\AlgoliaSearch\ViewModel\Adminhtml\BackendView;
88
use Magento\Framework\Exception\LocalizedException;
99
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Locale\ResolverInterface;
1011
use Magento\Store\Api\Data\StoreInterface;
1112

1213
class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterface
@@ -26,24 +27,30 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
2627
/** @var IndexEntityDataProvider */
2728
private $indexEntityDataProvider;
2829

30+
/** @var ResolverInterface */
31+
private $localeResolver;
32+
2933
/** @var array */
3034
private $analyticsParams = [];
3135

3236
/**
33-
* Index constructor.
37+
* Index constructor.
3438
*
3539
* @param BackendView $backendView
3640
* @param AnalyticsHelper $analyticsHelper
3741
* @param IndexEntityDataProvider $indexEntityDataProvider
42+
* @param ResolverInterface $localeResolver
3843
*/
3944
public function __construct(
4045
BackendView $backendView,
4146
AnalyticsHelper $analyticsHelper,
42-
IndexEntityDataProvider $indexEntityDataProvider
47+
IndexEntityDataProvider $indexEntityDataProvider,
48+
ResolverInterface $localeResolver
4349
) {
4450
$this->backendView = $backendView;
4551
$this->analyticsHelper = $analyticsHelper;
4652
$this->indexEntityDataProvider = $indexEntityDataProvider;
53+
$this->localeResolver = $localeResolver;
4754
}
4855

4956
/**
@@ -54,9 +61,13 @@ public function getBackendView()
5461
return $this->backendView;
5562
}
5663

57-
public function getTimeZone()
64+
/**
65+
* @return string
66+
* @throws NoSuchEntityException
67+
*/
68+
public function getTimeZone(): string
5869
{
59-
return $this->backendView->getDateTime()->getConfigTimezone(
70+
return (string) $this->backendView->getDateTime()->getConfigTimezone(
6071
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
6172
$this->getStore()->getId()
6273
);
@@ -81,18 +92,16 @@ public function getIndexName()
8192
*
8293
* @return array
8394
*/
84-
public function getAnalyticsParams($additional = [])
95+
public function getAnalyticsParams(array $additional = []): array
8596
{
8697
if (empty($this->analyticsParams)) {
8798
$params = ['index' => $this->getIndexName()];
8899
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']);
93102
}
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']);
96105
}
97106
}
98107

@@ -102,6 +111,39 @@ public function getAnalyticsParams($additional = [])
102111
return array_merge($this->analyticsParams, $additional);
103112
}
104113

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+
105147
public function getTotalCountOfSearches()
106148
{
107149
return $this->analyticsHelper->getTotalCountOfSearches($this->getAnalyticsParams());
@@ -306,14 +348,15 @@ public function getNoResultSearches()
306348
*
307349
* @return bool
308350
*/
309-
public function checkIsValidDateRange()
351+
public function checkIsValidDateRange(): bool
310352
{
311353
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);
317360

318361
if ($diff->days > $this->getAnalyticRetentionDays()) {
319362
return false;

0 commit comments

Comments
 (0)