Skip to content

Commit 2131827

Browse files
authored
Merge pull request #164 from TheDMSGroup/ENG-657
[ENG-657] fix query and tz shifting on client stats chart on client view page
2 parents d9ca56b + 71dc7ab commit 2131827

File tree

1 file changed

+82
-31
lines changed

1 file changed

+82
-31
lines changed

Model/ContactClientModel.php

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ public function getEventRepository()
322322
}
323323

324324
/**
325-
* @param ContactClient $contactClient
326-
* @param $unit
325+
* @param ContactClient $contactClient
326+
* @param $unit
327327
* @param \DateTime|null $dateFrom
328328
* @param \DateTime|null $dateTo
329329
* @param null $campaignId
@@ -341,17 +341,9 @@ public function getStats(
341341
$dateFormat = null,
342342
$canViewOthers = true
343343
) {
344-
$localDateFrom = date_modify($dateFrom, 'midnight');
345-
$utcDateFrom = clone $localDateFrom;
346-
$utcDateFrom->setTimezone(new \DateTimeZone('UTC'));
347-
348-
$localDateTo = date_modify($dateTo, 'midnight +1 day -1 sec');
349-
$utcDateTo = clone $localDateTo;
350-
$utcDateTo->setTimezone(new \DateTimeZone('UTC'));
351-
352-
$unit = (null === $unit) ? $this->getTimeUnitFromDateRange($utcDateFrom, $utcDateTo) : $unit;
353-
$chart = new LineChart($unit, $localDateFrom, $localDateTo, $dateFormat);
354-
$query = new ChartQuery($this->em->getConnection(), $utcDateFrom, $utcDateTo, $unit);
344+
$query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo, $unit);
345+
$unit = (null === $unit) ? $this->getTimeUnitFromDateRange($dateFrom, $dateTo) : $unit;
346+
$chart = new LineChart($unit, $dateFrom, $dateTo, $dateFormat);
355347
$stat = new Stat();
356348

357349
$params = ['contactclient_id' => $contactClient->getId()];
@@ -368,6 +360,40 @@ public function getStats(
368360
$params
369361
);
370362

363+
if (!in_array($unit, ['H', 'i', 's'])) {
364+
// For some reason, Mautic only sets UTC in Query Date builder
365+
// if its an intra-day date range ¯\_(ツ)_/¯
366+
// so we have to do it here.
367+
$userTZ = new \DateTime('now');
368+
$userTzName = $userTZ->getTimezone()->getName();
369+
$paramDateTo = $q->getParameter('dateTo');
370+
$paramDateFrom = $q->getParameter('dateFrom');
371+
$paramDateTo = new \DateTime($paramDateTo);
372+
$paramDateTo->setTimeZone(new \DateTimeZone('UTC'));
373+
$q->setParameter('dateTo', $paramDateTo->format('Y-m-d H:i:s'));
374+
$paramDateFrom = new \DateTime($paramDateFrom);
375+
$paramDateFrom->setTimeZone(new \DateTimeZone('UTC'));
376+
$q->setParameter('dateFrom', $paramDateFrom->format('Y-m-d H:i:s'));
377+
$select = $q->getQueryPart('select')[0];
378+
$newSelect = str_replace(
379+
't.date_added,',
380+
"CONVERT_TZ(t.date_added, @@global.time_zone, '$userTzName'),",
381+
$select
382+
);
383+
$q->resetQueryPart('select');
384+
$q->select($newSelect);
385+
386+
// AND adjust the group By, since its using db timezone Date values
387+
$groupBy = $q->getQueryPart('groupBy')[0];
388+
$newGroupBy = str_replace(
389+
't.date_added,',
390+
"CONVERT_TZ(t.date_added, @@global.time_zone, '$userTzName'),",
391+
$groupBy
392+
);
393+
$q->resetQueryPart('groupBy');
394+
$q->groupBy($newGroupBy);
395+
}
396+
371397
if (!$canViewOthers) {
372398
$this->limitQueryToCreator($q);
373399
}
@@ -437,9 +463,9 @@ public function limitQueryToCreator(QueryBuilder $q)
437463
}
438464

439465
/**
440-
* @param ContactClient $contactClient
441-
* @param $unit
442-
* @param $type
466+
* @param ContactClient $contactClient
467+
* @param $unit
468+
* @param $type
443469
* @param \DateTime|null $dateFrom
444470
* @param \DateTime|null $dateTo
445471
* @param null $campaignId
@@ -460,22 +486,28 @@ public function getStatsBySource(
460486
) {
461487
$unit = (null === $unit) ? $this->getTimeUnitFromDateRange($dateFrom, $dateTo) : $unit;
462488
$dateToAdjusted = clone $dateTo;
463-
if (in_array($unit, ['H', 'i', 's'])) {
464-
// draw the chart with the correct intervals for intra-day
465-
$dateToAdjusted->setTime(23, 59, 59);
466-
}
489+
$dateToAdjusted->setTime(23, 59, 59);
467490
$chart = new LineChart($unit, $dateFrom, $dateToAdjusted, $dateFormat);
468491
$query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateToAdjusted, $unit);
469-
$utmSources = $this->getStatRepository()->getSourcesByClient($contactClient->getId(), $dateFrom, $dateToAdjusted);
492+
$utmSources = $this->getStatRepository()->getSourcesByClient(
493+
$contactClient->getId(),
494+
$dateFrom,
495+
$dateToAdjusted
496+
);
470497

471-
if (isset($campaignId)) {
498+
//if (isset($campaignId)) {
499+
if (!empty($campaignId)) {
472500
$params['campaign_id'] = (int) $campaignId;
473501
}
502+
$params['contactclient_id'] = $contactClient->getId();
503+
504+
$userTZ = new \DateTime('now');
505+
$userTzName = $userTZ->getTimezone()->getName();
474506

475507
if ('revenue' != $type) {
476508
$params['type'] = $type;
477509
foreach ($utmSources as $utmSource) {
478-
$params['utm_source'] = $utmSource;
510+
$params['utm_source'] = empty($utmSource) ? ['expression' => 'isNull'] : $utmSource;
479511
$q = $query->prepareTimeDataQuery(
480512
'contactclient_stats',
481513
'date_added',
@@ -494,14 +526,20 @@ public function getStatsBySource(
494526
$paramDateFrom = new \DateTime($paramDateFrom);
495527
$paramDateFrom->setTimeZone(new \DateTimeZone('UTC'));
496528
$q->setParameter('dateFrom', $paramDateFrom->format('Y-m-d H:i:s'));
529+
$select = $q->getQueryPart('select')[0];
530+
$newSelect = str_replace(
531+
't.date_added,',
532+
"CONVERT_TZ(t.date_added, @@global.time_zone, '$userTzName'),",
533+
$select
534+
);
535+
$q->resetQueryPart('select');
536+
$q->select($newSelect);
497537

498538
// AND adjust the group By, since its using db timezone Date values
499-
$userTZ = new \DateTime('now');
500-
$interval = abs($userTZ->getOffset() / 3600);
501539
$groupBy = $q->getQueryPart('groupBy')[0];
502540
$newGroupBy = str_replace(
503-
'DATE_FORMAT(t.date_added,',
504-
"DATE_FORMAT(DATE_SUB(t.date_added, INTERVAL $interval HOUR),",
541+
't.date_added,',
542+
"CONVERT_TZ(t.date_added, @@global.time_zone, '$userTzName'),",
505543
$groupBy
506544
);
507545
$q->resetQueryPart('groupBy');
@@ -535,12 +573,17 @@ public function getStatsBySource(
535573
}
536574
$dbUnit = $query->getTimeUnitFromDateRange($dateFrom, $dateTo);
537575
$dbUnit = $query->translateTimeUnit($dbUnit);
538-
$dateConstruct = 'DATE_FORMAT(t.date_added, \''.$dbUnit.'\')';
576+
$dateConstruct = "DATE_FORMAT(CONVERT_TZ(t.date_added, @@global.time_zone, '$userTzName'), '$dbUnit.')";
539577
foreach ($utmSources as $utmSource) {
540578
$q->select($dateConstruct.' AS date, ROUND(SUM(t.attribution), 2) AS count')
541-
->where('utm_source = :utmSource')
542-
->setParameter('utmSource', $utmSource)
543579
->groupBy($dateConstruct);
580+
if (empty($utmSource)) { // utmSource can be a NULL value
581+
$q->andWhere('utm_source IS NULL');
582+
} else {
583+
$q->andWhere('utm_source = :utmSource')
584+
->setParameter('utmSource', $utmSource);
585+
}
586+
544587
$data = $query->loadAndBuildTimeData($q);
545588
foreach ($data as $val) {
546589
if (0 !== $val) {
@@ -664,6 +707,14 @@ public function getCampaigns($contactclientId)
664707
/** @var CampaignRepository $campaignRepo */
665708
$campaignRepo = $this->em->getRepository('MauticCampaignBundle:Campaign');
666709

667-
return $campaignRepo->getEntities(['filter'=>['column' => 'canvasSettings', 'expr' => 'like', 'value' => "%'contactclient': $contactclientId,%"]]);
710+
return $campaignRepo->getEntities(
711+
[
712+
'filter' => [
713+
'column' => 'canvasSettings',
714+
'expr' => 'like',
715+
'value' => "%'contactclient': $contactclientId,%",
716+
],
717+
]
718+
);
668719
}
669720
}

0 commit comments

Comments
 (0)