Skip to content

Commit

Permalink
Merge pull request #4789 from laboro/ticket/CRM-6389_1.12
Browse files Browse the repository at this point in the history
CRM-6389: JS error is occurred on Dashboard
  • Loading branch information
Pavel Usachev authored Nov 30, 2016
2 parents 1033e69 + b64518f commit 10a9fa5
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 18 deletions.
68 changes: 50 additions & 18 deletions src/Oro/Bundle/DashboardBundle/Helper/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Oro\Bundle\FilterBundle\Form\Type\Filter\AbstractDateFilterType;
use Oro\Bundle\LocaleBundle\Model\LocaleSettings;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class DateHelper
{
const YEAR_TYPE_DAYS = 1460;
Expand Down Expand Up @@ -169,48 +172,78 @@ public function getDatePeriod(\DateTime $start, \DateTime $end)
* @param \DateTime $start
* @param \DateTime $end
* @param QueryBuilder $qb
* @param $entityField
* @param string $entityField
* @param bool $useCurrentTimeZone
*/
public function addDatePartsSelect(\DateTime $start, \DateTime $end, QueryBuilder $qb, $entityField)
{
public function addDatePartsSelect(
\DateTime $start,
\DateTime $end,
QueryBuilder $qb,
$entityField,
$useCurrentTimeZone = true
) {
switch ($this->getFormatStrings($start, $end)['viewType']) {
case 'year':
$qb->addSelect(sprintf('%s as yearCreated', $this->getEnforcedTimezoneFunction('YEAR', $entityField)));
$qb->addSelect(sprintf(
'%s as yearCreated',
$this->getEnforcedTimezoneFunction('YEAR', $entityField, $useCurrentTimeZone)
));
$qb->addGroupBy('yearCreated');
break;
case 'month':
$qb->addSelect(sprintf('%s as yearCreated', $this->getEnforcedTimezoneFunction('YEAR', $entityField)));
$qb->addSelect(sprintf(
'%s as yearCreated',
$this->getEnforcedTimezoneFunction('YEAR', $entityField, $useCurrentTimeZone)
));
$qb->addSelect(
sprintf(
'%s as monthCreated',
$this->getEnforcedTimezoneFunction('MONTH', $entityField)
$this->getEnforcedTimezoneFunction('MONTH', $entityField, $useCurrentTimeZone)
)
);
$qb->addGroupBy('yearCreated');
$qb->addGroupBy('monthCreated');
break;
case 'date':
$qb->addSelect(sprintf("%s as yearCreated", $this->getEnforcedTimezoneFunction('YEAR', $entityField)));
$qb->addSelect(sprintf('%s as weekCreated', $this->getEnforcedTimezoneFunction('WEEK', $entityField)));
$qb->addSelect(sprintf(
"%s as yearCreated",
$this->getEnforcedTimezoneFunction('YEAR', $entityField, $useCurrentTimeZone)
));
$qb->addSelect(sprintf(
'%s as weekCreated',
$this->getEnforcedTimezoneFunction('WEEK', $entityField, $useCurrentTimeZone)
));
$qb->addGroupBy('yearCreated');
$qb->addGroupBy('weekCreated');
break;
case 'day':
$qb->addSelect(sprintf("%s as yearCreated", $this->getEnforcedTimezoneFunction('YEAR', $entityField)));
$qb->addSelect(sprintf(
"%s as yearCreated",
$this->getEnforcedTimezoneFunction('YEAR', $entityField, $useCurrentTimeZone)
));
$qb->addSelect(
sprintf(
"%s as monthCreated",
$this->getEnforcedTimezoneFunction('MONTH', $entityField)
$this->getEnforcedTimezoneFunction('MONTH', $entityField, $useCurrentTimeZone)
)
);
$qb->addSelect(sprintf("%s as dayCreated", $this->getEnforcedTimezoneFunction('DAY', $entityField)));
$qb->addSelect(sprintf(
"%s as dayCreated",
$this->getEnforcedTimezoneFunction('DAY', $entityField, $useCurrentTimeZone)
));
$qb->addGroupBy('yearCreated');
$qb->addGroupBy('monthCreated');
$qb->addGroupBy('dayCreated');
break;
case 'time':
$qb->addSelect(sprintf('%s as dateCreated', $this->getEnforcedTimezoneFunction('DATE', $entityField)));
$qb->addSelect(sprintf('%s as hourCreated', $this->getEnforcedTimezoneFunction('HOUR', $entityField)));
$qb->addSelect(sprintf(
'%s as dateCreated',
$this->getEnforcedTimezoneFunction('DATE', $entityField, $useCurrentTimeZone)
));
$qb->addSelect(sprintf(
'%s as hourCreated',
$this->getEnforcedTimezoneFunction('HOUR', $entityField, $useCurrentTimeZone)
));
$qb->addGroupBy('dateCreated');
$qb->addGroupBy('hourCreated');
break;
Expand All @@ -233,17 +266,15 @@ public function getKey(\DateTime $start, \DateTime $end, $row)
break;
case 'year':
return $row['yearCreated'];
break;
case 'day':
$time = strtotime(sprintf('%s-%s-%s', $row['yearCreated'], $row['monthCreated'], $row['dayCreated']));
break;
case 'date':
$week = $row['weekCreated'] < 10 ? '0' . $row['weekCreated'] : $row['weekCreated'];

return $row['yearCreated'] . '-' . $week;
break;
case 'time':
return $row['dateCreated'] . '-' . $row['hourCreated'];
return $row['dateCreated'] . '-' . str_pad($row['hourCreated'], 2, '0', STR_PAD_LEFT);
}

return date($config['valueStringFormat'], $time);
Expand Down Expand Up @@ -402,12 +433,13 @@ protected function getFormattedLabel($config, \DateTime $date, $increment)
*
* @param string $functionName
* @param string $fieldName
* @param bool $useCurrentTimeZone
*
* @return string
*/
protected function getEnforcedTimezoneFunction($functionName, $fieldName)
protected function getEnforcedTimezoneFunction($functionName, $fieldName, $useCurrentTimeZone = true)
{
if ('UTC' !== $this->localeSettings->getTimeZone()) {
if ($useCurrentTimeZone && 'UTC' !== $this->localeSettings->getTimeZone()) {
$fieldName = sprintf("CONVERT_TZ(%s, '+00:00', '%s')", $fieldName, $this->getTimeZoneOffset());
}
$result = sprintf('%s(%s)', $functionName, $fieldName);
Expand Down
113 changes: 113 additions & 0 deletions src/Oro/Bundle/DashboardBundle/Tests/Unit/Helper/DateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,117 @@ public function getPreviousDateTimeIntervalDataProvider()
]
];
}

/**
* @dataProvider getFormatStringsProvider
*/
public function testGetFormatStrings(\DateTime $start, \DateTime $end, array $expectedValue)
{
$this->assertEquals($expectedValue, $this->helper->getFormatStrings($start, $end));
}

public function getFormatStringsProvider()
{
return [
'year' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2015-01-01', new \DateTimeZone('UTC')),
[
'intervalString' => 'P1Y',
'valueStringFormat' => 'Y',
'viewType' => 'year',
]
],
'month' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-05-01', new \DateTimeZone('UTC')),
[
'intervalString' => 'P1M',
'valueStringFormat' => 'Y-m',
'viewType' => 'month',
]
],
'date' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-03-15', new \DateTimeZone('UTC')),
[
'intervalString' => 'P1W',
'valueStringFormat' => 'Y-W',
'viewType' => 'date',
]
],
'day' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-01-15', new \DateTimeZone('UTC')),
[
'intervalString' => 'P1D',
'valueStringFormat' => 'Y-m-d',
'viewType' => 'day',
]
],
'time' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-01-02', new \DateTimeZone('UTC')),
[
'intervalString' => 'PT1H',
'valueStringFormat' => 'Y-m-d-H',
'viewType' => 'time',
]
],
];
}

/**
* @dataProvider getKeyGeneratesKeysFromGetDatePeriod
*/
public function testGetKeyGeneratesKeysFromGetDatePeriod(
\DateTime $start,
\DateTime $end,
array $row,
$expectedViewType
) {
$formatStrings = $this->helper->getFormatStrings($start, $end);
$this->assertEquals($expectedViewType, $formatStrings['viewType']);

$this->assertArrayHasKey(
$this->helper->getKey($start, $end, $row),
$this->helper->getDatePeriod($start, $end)
);
}

public function getKeyGeneratesKeysFromGetDatePeriod()
{
return [
'year' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2015-01-01', new \DateTimeZone('UTC')),
['yearCreated' => '2010'],
'year',
],
'month' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-05-01', new \DateTimeZone('UTC')),
['yearCreated' => '2010', 'monthCreated' => '4'],
'month',
],
'date' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-03-15', new \DateTimeZone('UTC')),
['yearCreated' => '2010', 'weekCreated' => '1'],
'date',
],
'time with hour having 1 digit' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-01-02', new \DateTimeZone('UTC')),
['dateCreated' => '2010-01-01', 'hourCreated' => '5'],
'time',
],
'time with hour having 2 digits' => [
new \DateTime('2010-01-01', new \DateTimeZone('UTC')),
new \DateTime('2010-01-02', new \DateTimeZone('UTC')),
['dateCreated' => '2010-01-01', 'hourCreated' => '11'],
'time',
],
];
}
}

0 comments on commit 10a9fa5

Please sign in to comment.