Skip to content

Commit

Permalink
MM-29: Add functionality to map relation between website and sales_ch…
Browse files Browse the repository at this point in the history
…annel on the integration form (#54)
  • Loading branch information
alexandr-parkhomenko authored Jun 30, 2020
1 parent 9c76576 commit c9a5ad9
Show file tree
Hide file tree
Showing 42 changed files with 1,938 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Marello\Bundle\Magento2Bundle\Autocomplete;

use Marello\Bundle\SalesBundle\Entity\Repository\SalesChannelRepository;
use Oro\Bundle\FormBundle\Autocomplete\SearchHandler;

/**
* @todo Cover with functional test
*/
class SalesChannelInGroupHandler extends SearchHandler
{
private const DELIMITER = ';';

/**
* {@inheritdoc}
*/
protected function checkAllDependenciesInjected()
{
if (!$this->entityRepository || !$this->idFieldName) {
throw new \RuntimeException('Search handler is not fully configured');
}
}

/**
* {@inheritdoc}
*/
protected function searchEntities($search, $firstResult, $maxResults)
{
$parts = explode(self::DELIMITER, $search);
if (3 !== count($parts)) {
return [];
}

$searchTerm = $parts[0];
$salesChannelGroupId = (int) $parts[1];
$skippedSalesChannelIds = '' !== $parts[2] ? explode(',', $parts[2]) : [];

$resultEntities = [];
if (0 !== $salesChannelGroupId) {
/** @var SalesChannelRepository $repository */
$repository = $this->entityRepository;
$queryBuilder = $repository->getActiveSalesChannelBySearchTermLimitedWithGroupIdQB(
$searchTerm,
$salesChannelGroupId,
$skippedSalesChannelIds
);

$queryBuilder
->setFirstResult($firstResult)
->setMaxResults($maxResults);

$resultEntities = $this->aclHelper->apply($queryBuilder->getQuery())->getResult();
}

return $resultEntities;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function checkAction(
$transportEntity ?? new Magento2Transport()
);

$response = $this->getUpdateSuccessResponse($response);
if ($response['success']) {
$response = $this->getUpdateSuccessResponse($response);
}
} catch (\Exception $e) {
$response = $this->logErrorAndGetResponse($e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Marello\Bundle\Magento2Bundle\DTO;

class WebsiteToSalesChannelMappingItemDTO implements \JsonSerializable
{
/** @var string[] */
public const REQUIRED_KEYS = [
'originWebsiteId',
'websiteName',
'salesChannelId',
'salesChannelName'
];

/**
* @var array
*/
protected $originData = [];

/**
* @var array
*/
protected $data = [];

/**
* @param array $data
*/
public function __construct(array $data)
{
$notExistedKeys = \array_diff_key(\array_flip(self::REQUIRED_KEYS), $data);
if (!empty($notExistedKeys)) {
throw new \LogicException(
'The website to sales channel mapping item must contains all required keys.',
[
'missedKeys' => $notExistedKeys,
'originalData' => $data
]
);
}

$this->originData = $data;
$this->data = $data;
}

/**
* @return int
*/
public function getOriginWebsiteId(): int
{
return $this->data['originWebsiteId'];
}

/**
* @return string
*/
public function getWebsiteName(): string
{
return $this->data['websiteName'];
}

/**
* @param string $websiteName
*/
public function setWebsiteName(string $websiteName): void
{
$this->data['websiteName'] = $websiteName;
}

/**
* @return int
*/
public function getSalesChannelId(): int
{
return $this->data['salesChannelId'];
}

/**
* @return string
*/
public function getSalesChannelName(): string
{
return $this->data['salesChannelName'];
}

/**
* @param string $salesChannelName
*/
public function setSalesChannelName(string $salesChannelName): void
{
$this->data['salesChannelName'] = $salesChannelName;
}

/**
* @return array
*/
public function getOriginData(): array
{
return $this->originData;
}

/**
* @return array
*/
public function getData(): array
{
return $this->data;
}

/**
* @inheritDoc
*/
public function jsonSerialize()
{
return $this->data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ class Magento2Transport extends Transport

/**
* @var array
* [
* [
* 'website_code' => string <website_code>,
* 'sales_chanel_code' => string <sales_channel_code>
* ],
* ...
* ]
*
* The structure described in constant REQUIRED_KEYS of @see WebsiteToSalesChannelMappingItemDTO
*
* @ORM\Column(name="m2_websites_sales_channel_map", type="json", nullable=true)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
use Oro\Component\MessageQueue\Client\MessageProducerInterface;
use Oro\Component\MessageQueue\Transport\Exception\Exception;

/**
* @todo Block changes currency and block changes in sales channel code
*/
class SalesChannelReverseSyncListener
{
private const ACTIVE_PROPERTY_NAME = 'active';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected function loadCreatedWebsites(UnitOfWork $unitOfWork)
protected function loadUpdatedWebsites(UnitOfWork $unitOfWork)
{
foreach ($unitOfWork->getScheduledEntityUpdates() as $entityUpdate) {
if ($entityUpdate instanceof Website && $this->isApplicableWebsite($entityUpdate)) {
if ($entityUpdate instanceof Website) {
$changeSet = $unitOfWork->getEntityChangeSet($entityUpdate);

if (!\array_key_exists(self::SALES_CHANNEL_FIELD_NAME, $changeSet)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Marello\Bundle\Magento2Bundle\Form\Type;

use Oro\Bundle\FormBundle\Form\Type\OroJquerySelect2HiddenType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SalesChannelInGroupSelectType extends AbstractType
{
private const BLOCK_PREFIX = 'marello_magento2_sales_channel_in_group_select';

/**
* {@inheritDoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'componentName' => 'salesChannelInGroupSelectComponent',
'autocomplete_alias' => 'magento2_saleschannels_in_group',
'configs' => [
'allowClear' => false,
'component' => 'autocomplete-magento2-sales-channel-in-group',
'placeholder' => 'marello.sales.saleschannel.form.select_saleschannel',
'result_template_twig' => 'MarelloMagento2Bundle:SalesChannel:Autocomplete/result.html.twig',
'selection_template_twig' => 'MarelloMagento2Bundle:SalesChannel:Autocomplete/selection.html.twig'
],
'attr' => [
'data-role' => 'sales-channel-in-group-select'
]
]
);
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);

$vars = [
'attr' => [
'data-page-component-name' => $options['componentName']
]
];

$view->vars = array_replace_recursive($view->vars, $vars);
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return OroJquerySelect2HiddenType::class;
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return self::BLOCK_PREFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@
namespace Marello\Bundle\Magento2Bundle\Form\Type;

use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TransportCheckButtonType extends ButtonType
{
public const NAME = 'marello_magento2_transport_check_button';
private const BLOCK_PREFIX = 'marello_magento2_transport_check_button';

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getName()
public function getBlockPrefix()
{
return $this->getBlockPrefix();
return self::BLOCK_PREFIX;
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getBlockPrefix()
public function configureOptions(OptionsResolver $resolver)
{
return self::NAME;
parent::configureOptions($resolver);

$resolver->setRequired(['selectorForFieldsRequiredReCheckConnection']);
$resolver->setDefaults(['attr' => ['class' => 'btn btn-primary']]);
}

/**
* {@inheritDoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::configureOptions($resolver);
parent::buildView($view, $form, $options);

$resolver->setRequired(['websiteToSalesChannelMappingSelector']);
$resolver->setRequired(['salesGroupSelector']);
$resolver->setDefaults(['attr' => ['class' => 'btn btn-primary']]);
$view->vars = \array_replace_recursive($view->vars, [
'component_options' => [
'selectorForFieldsRequiredReCheckConnection' => $options['selectorForFieldsRequiredReCheckConnection']
]
]);
}
}
Loading

0 comments on commit c9a5ad9

Please sign in to comment.