Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/1.10' into 1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrebenchuk committed Dec 1, 2016
2 parents 0de1b0a + b8f144c commit 647160b
Show file tree
Hide file tree
Showing 22 changed files with 517 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ define(function(require) {
},

_onContentChange: function() {
this.disposePageComponents();
this.$(this.options.infoBlock).html(this.model.get('contentHTML'));
this.initLayout().done(_.bind(function() {
// if the activity has an EmailTreadView -- handle comment count change in own way
Expand Down
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',
],
];
}
}
42 changes: 42 additions & 0 deletions src/Oro/Bundle/EmailBundle/Command/Manager/AssociationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Oro\Bundle\BatchBundle\ORM\Query\BufferedQueryResultIterator;
use Oro\Bundle\EmailBundle\Command\AddAssociationCommand;
use Oro\Bundle\EmailBundle\Command\UpdateEmailOwnerAssociationsCommand;
use Oro\Bundle\EmailBundle\Entity\Email;
use Oro\Bundle\EmailBundle\Entity\Manager\EmailManager;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
Expand All @@ -18,6 +19,7 @@
class AssociationManager
{
const EMAIL_BUFFER_SIZE = 100;
const OWNER_IDS_BUFFER_SIZE = 100;

/** @var DoctrineHelper */
protected $doctrineHelper;
Expand Down Expand Up @@ -70,6 +72,46 @@ public function processAddAssociation($ids, $targetClass, $targetId)
return $countNewAssociations;
}

/**
* Makes sure that all email owners have assigned their emails
*/
public function processUpdateAllEmailOwners()
{
$jobManager = $this->doctrineHelper->getEntityManagerForClass(Job::class);

$emailOwnerClasseNames = $this->emailOwnersProvider->getSupportedEmailOwnerClassNames();
foreach ($emailOwnerClasseNames as $emailOwnerClassName) {
$ownerColumnName = $this->emailOwnersProvider->getOwnerColumnName($emailOwnerClassName);
if (!$ownerColumnName) {
continue;
}

$ownerIdsQb = $this->doctrineHelper
->getEntityRepository(Email::class)
->getOwnerIdsWithEmailsQb(
$emailOwnerClassName,
$this->doctrineHelper->getSingleEntityIdentifierFieldName($emailOwnerClassName),
$ownerColumnName
);

$ownerIds = (new BufferedQueryResultIterator($ownerIdsQb))
->setBufferSize(self::OWNER_IDS_BUFFER_SIZE)
->setPageLoadedCallback(function (array $rows) use ($emailOwnerClassName, $jobManager) {
$job = new Job(
UpdateEmailOwnerAssociationsCommand::COMMAND_NAME,
array_merge([$emailOwnerClassName], array_map('current', $rows))
);
$jobManager->persist($job);
$jobManager->flush();
$jobManager->clear();
});

// iterate through ownerIds to call pageLoadedCallback
foreach ($ownerIds as $ownerId) {
}
}
}

/**
* Process of command oro:email:update-email-owner-associations
*
Expand Down
69 changes: 69 additions & 0 deletions src/Oro/Bundle/EmailBundle/Command/UpdateAssociationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Oro\Bundle\EmailBundle\Command;

use Doctrine\ORM\EntityManager;
use JMS\JobQueueBundle\Entity\Job;
use Oro\Bundle\EmailBundle\Command\Manager\AssociationManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateAssociationsCommand extends ContainerAwareCommand
{
const NAME = 'oro:email:update-associations';
const OPTION_FOREGROUND = 'foreground';

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('oro:email:update-associations')
->setDescription('Update associations to emails')
->addOption(
static::OPTION_FOREGROUND,
'f',
InputOption::VALUE_NONE,
'Schedule jobs in backround and exits immediately'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption(static::OPTION_FOREGROUND)) {
$this
->getCommandAssociationManager()
->processUpdateAllEmailOwners();
} else {
$job = new Job(static::NAME, ['--foreground']);

$em = $this->getJobManager();
$em->persist($job);
$em->flush();
}

$output->writeln('<info>Update of associations has been scheduled.</info>');
}

/**
* @return EntityManager
*/
public function getJobManager()
{
return $this->getContainer()->get('doctrine')->getManagerForClass(Job::class);
}

/**
* @return AssociationManager
*/
protected function getCommandAssociationManager()
{
return $this->getContainer()->get('oro_email.command.association_manager');
}
}
Loading

0 comments on commit 647160b

Please sign in to comment.