Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/1.9' into 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrebenchuk committed Mar 31, 2016
2 parents 66be263 + dff3882 commit 4821371
Show file tree
Hide file tree
Showing 15 changed files with 779 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ orocrm:
organization.label: Organization
direction_label.label: Direction

calldirection:
entity_label: Direction
entity_plural_label: Directions
label.label: Label
name.label: Name

block:
additional: Additional
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Mapping\ClassMetadata;

use Oro\Bundle\EntityBundle\ORM\QueryUtils;
Expand Down Expand Up @@ -169,7 +168,7 @@ protected function getCustomerListQueryBuilder(SearchResult $searchResult)
}
}

$rsm = new ResultSetMapping();
$rsm = QueryUtils::createResultSetMapping($em->getConnection()->getDatabasePlatform());
$rsm
->addScalarResult('channelId', 'channelId', 'integer')
->addScalarResult('entityId', 'id', 'integer')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace OroCRM\Bundle\ChannelBundle\EventListener;

use Akeneo\Bundle\BatchBundle\Event\EventInterface;
use Akeneo\Bundle\BatchBundle\Event\JobExecutionEvent;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\SecurityBundle\Authentication\Token\ConsoleToken;

class JobExecutionSubscriber implements EventSubscriberInterface
{
const CHANNEL_CONFIG = 'channel';

/** @var DoctrineHelper */
protected $doctrineHelper;

/** @var TokenStorageInterface */
protected $tokenStorage;

/**
* @param DoctrineHelper $doctrineHelper
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(DoctrineHelper $doctrineHelper, TokenStorageInterface $tokenStorage)
{
$this->doctrineHelper = $doctrineHelper;
$this->tokenStorage = $tokenStorage;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
EventInterface::BEFORE_JOB_EXECUTION => 'beforeJobExecution',
];
}

/**
* @param JobExecutionEvent $event
*/
public function beforeJobExecution(JobExecutionEvent $event)
{
$config = $event->getJobExecution()->getJobInstance()->getRawConfiguration();
if (!isset($config[static::CHANNEL_CONFIG])) {
return;
}
$channelId = $config[static::CHANNEL_CONFIG];

$integration = $this->getIntegration($channelId);
if ($integration) {
$this->updateToken($integration);
}
}


/**
* @param int|null $channelId
*
* @return Integration
*/
protected function getIntegration($channelId = null)
{
if (!$channelId) {
return;
}

return $this->doctrineHelper->getEntityRepositoryForClass('OroIntegrationBundle:Channel')
->find($channelId);
}

/**
* @param Integration $integration
*/
protected function updateToken(Integration $integration)
{
$token = $this->tokenStorage->getToken();
if (!$token) {
$token = new ConsoleToken();
$this->tokenStorage->setToken($token);
}

$token->setOrganizationContext($integration->getOrganization());
}
}
9 changes: 9 additions & 0 deletions src/OroCRM/Bundle/ChannelBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parameters:
orocrm_channel.event_listener.embedded_form.class: OroCRM\Bundle\ChannelBundle\EventListener\EmbeddedFormListener
orocrm_channel.event_listener.doctrine.class: OroCRM\Bundle\ChannelBundle\EventListener\ChannelDoctrineListener
orocrm_channel.event_listener.timezone_change.class: OroCRM\Bundle\ChannelBundle\EventListener\TimezoneChangeListener
orocrm_channel.event_listener.job_execution.class: OroCRM\Bundle\ChannelBundle\EventListener\JobExecutionSubscriber

#twig
orocrm_channel.twig.metadata_extension.class: OroCRM\Bundle\ChannelBundle\Twig\MetadataExtension
Expand Down Expand Up @@ -314,6 +315,14 @@ services:
tags:
- { name: kernel.event_listener, event: oro_config.update_after, method: onConfigUpdate }

orocrm_channel.event_listener.job_execution:
class: %orocrm_channel.event_listener.job_execution.class%
arguments:
- @oro_entity.doctrine_helper
- @security.token_storage
tags:
- { name: kernel.event_subscriber }

#twig
orocrm_channel.twig.metadata_extension:
class: %orocrm_channel.twig.metadata_extension.class%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,37 @@ protected function assertExportResults(array $expected, array $actual)
$this->assertCollectionData($expected, $actual, ['Phones 2 Phone', 'Phones 3 Phone']);
$this->assertCollectionData($expected, $actual, ['Addresses 2 Street', 'Addresses 3 Street']);
$this->assertCollectionData($expected, $actual, ['Addresses 2 Zip/postal code', 'Addresses 3 Zip/postal code']);
$this->assertArrayData($expected, $actual, 'Tags');

$this->assertEquals($expected, $actual);
}

/**
* @param array $expected
* @param array $actual
* @param string $key
*/
protected function assertArrayData(array &$expected, array &$actual, $key)
{
$this->assertArrayHasKey($key, $expected);
$this->assertArrayHasKey($key, $actual);

$e = $this->stringToArray($expected[$key]);
$a = $this->stringToArray($actual[$key]);
sort($e);
sort($a);

$this->assertEquals($e, $a);

unset($expected[$key]);
unset($actual[$key]);
}

protected function stringToArray($string)
{
return explode(', ', $string);
}

/**
* Order of elements in collection is not important, except the first (primary) element
*
Expand Down
212 changes: 212 additions & 0 deletions src/OroCRM/Bundle/MagentoBundle/EventListener/OrderGridListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

namespace OroCRM\Bundle\MagentoBundle\EventListener;

use Oro\Bundle\AddressBundle\Datagrid\CountryDatagridHelper;

use Oro\Bundle\DataGridBundle\Datagrid\DatagridInterface;
use Oro\Bundle\DataGridBundle\Datagrid\ParameterBag;

use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
use Oro\Bundle\DataGridBundle\Event\BuildBefore;
use Oro\Bundle\FilterBundle\Grid\Extension\OrmFilterExtension;

/**
* The aim of this listener is to replace sub-selects of country and region of order's billing addresses
* with corresponding joins in case when address or region filters is activated.
* Because of slow performance of the "magento-order-grid" grid query with joins without filtering by these columns.
*/
class OrderGridListener
{
/** @var CountryDatagridHelper */
protected $datagridHelper;

/**
* @param CountryDatagridHelper $datagridHelper
*/
public function __construct(CountryDatagridHelper $datagridHelper)
{
$this->datagridHelper = $datagridHelper;
}

const COUNTRY_NAME_COLUMN = 'countryName';
const REGION_NAME_COLUMN = 'regionName';

const SELECT_PATH = '[source][query][select]';
const FILTERS_PATH = '[filters][columns]';

/**
* @param BuildBefore $event
*/
public function onBuildBefore(BuildBefore $event)
{
$datagrid = $event->getDatagrid();
$config = $event->getConfig();
$columns = $config->offsetGetByPath('[columns]', []);

$countryPos = array_search(self::COUNTRY_NAME_COLUMN, array_keys($columns));
$regionPos = array_search(self::REGION_NAME_COLUMN, array_keys($columns));
$processFilters = [];
if (false !== $countryPos) {
$processFilters[$countryPos] = [
'name' => self::COUNTRY_NAME_COLUMN,
'definition' => [
self::COUNTRY_NAME_COLUMN => $this->getCountryFilterDefinition(
$this->isFilterEnabled($datagrid->getParameters(), self::COUNTRY_NAME_COLUMN)
)
]
];
}
if (false !== $regionPos) {
$processFilters[$regionPos] = [
'name' => self::REGION_NAME_COLUMN,
'definition' => [
self::REGION_NAME_COLUMN => $this->getRegionFilterDefinition(
$this->isFilterEnabled($datagrid->getParameters(), self::REGION_NAME_COLUMN)
)
]
];
}
ksort($processFilters);

foreach ($processFilters as $columnPos => $data) {
$this->addToArrayByPos($config, self::FILTERS_PATH, $columnPos, $data['definition']);
}

$activeFilters = $this->getActiveFilters($datagrid);
if (isset($activeFilters[self::COUNTRY_NAME_COLUMN]) || isset($activeFilters[self::REGION_NAME_COLUMN])) {
$this->replaceAddressSelects($config);
}
}

/**
* @param DatagridInterface $datagrid
*
* @return array
*/
protected function getActiveFilters(DatagridInterface $datagrid)
{
$parameters = $datagrid->getParameters();

if ($parameters->has(ParameterBag::MINIFIED_PARAMETERS)) {
$minifiedParameters = $parameters->get(ParameterBag::MINIFIED_PARAMETERS);
$filters = [];

if (array_key_exists('f', $minifiedParameters)) {
$filters = $minifiedParameters['f'];
}

return $filters;
}

return $parameters->get(OrmFilterExtension::FILTER_ROOT_PARAM, []);
}

/**
* @param boolean $enabled
*
* @return array
*/
protected function getCountryFilterDefinition($enabled)
{
return [
'type' => 'entity',
'data_name' => 'address.country',
'enabled' => $enabled,
'options' => [
'field_options' => [
'class' => 'OroAddressBundle:Country',
'property' => 'name',
'query_builder' => $this->datagridHelper->getCountryFilterQueryBuilder(),
'translatable_options' => false
]
]
];
}

/**
* @param boolean $enabled
*
* @return array
*/
protected function getRegionFilterDefinition($enabled)
{
return [
'type' => 'string',
'data_name' => 'regionName',
'enabled' => $enabled
];
}

/**
* @param ParameterBag $datagridParameters
* @param string $fieldName
*
* @return bool
*/
protected function isFilterEnabled(ParameterBag $datagridParameters, $fieldName)
{
$enabled = false;
$minified = $datagridParameters->get(ParameterBag::MINIFIED_PARAMETERS);
if (isset($minified['f']['__' . $fieldName])) {
$enabled = (bool)$minified['f']['__' . $fieldName];
}

return $enabled;
}

/**
* @param DatagridConfiguration $config
* @param string $path
* @param int $pos
* @param array $value
*/
protected function addToArrayByPos(DatagridConfiguration $config, $path, $pos, $value)
{
$array = $config->offsetGetByPath($path, []);
$slice1 = array_slice($array, 0, $pos, true);
$slice2 = array_slice($array, $pos, count($array) - 1, true);
$config->offsetSetByPath($path, $slice1 + $value + $slice2);
}

/**
* @param DatagridConfiguration $config
*/
protected function replaceAddressSelects(DatagridConfiguration $config)
{
$select = $config->offsetGetByPath(self::SELECT_PATH, []);
$countryColumn = array_filter($select, function ($val) {
return strpos($val, ' as countryName') !== false;
});
$regionColumn = array_filter($select, function ($val) {
return strpos($val, ' as regionName') !== false;
});
$countryColumnPos = key($countryColumn);
$regionColumnPos = key($regionColumn);
$addJoins = false;
if ($countryColumnPos) {
$addJoins = true;
$select[$countryColumnPos] = 'country.name as countryName';
}
if ($regionColumnPos) {
$addJoins = true;
$select[$regionColumnPos] = 'CONCAT(CASE WHEN address.regionText IS NOT NULL ' .
'THEN address.regionText ELSE region.name END, \'\') as regionName';
}
if ($addJoins) {
$addressJoins = [
[
'join' => 'o.addresses',
'alias' => 'address',
'conditionType' => 'WITH',
'condition' => 'address.id IN (SELECT oa.id FROM OroCRMMagentoBundle:OrderAddress oa '
. 'LEFT JOIN oa.types type WHERE type.name = \'billing\' OR type.name IS NULL)'
],
['join' => 'address.country', 'alias' => 'country'],
['join' => 'address.region', 'alias' => 'region']
];
$config->offsetAddToArrayByPath('[source][query][join][left]', $addressJoins);
}
$config->offsetSetByPath(self::SELECT_PATH, $select);
}
}
Loading

0 comments on commit 4821371

Please sign in to comment.