Commit 4d5874c3 authored by Piotr Borek's avatar Piotr Borek
Browse files

filtering attributes in drop-down

parent 7674bb77
Pipeline #257695709 passed with stages
in 13 minutes and 51 seconds
......@@ -90,4 +90,35 @@ class AttributeController
return new JsonResponse($normalizedAttributes);
}
public function getForF2FMappingAction(Request $request): Response
{
$sourceFamilyCode = $request->query->get('sourceFamily');
$destinationFamilyCode = $request->query->get('destinationFamily');
[$sourceAttributeList, $destinationAttributeList] = $this->ruleAttributeProvider->getForF2FAttributeMapping(
$sourceFamilyCode ? $this->familyRepository->findOneBy(['code' => $sourceFamilyCode]) : null,
$destinationFamilyCode ? $this->familyRepository->findOneBy(['code' => $destinationFamilyCode]) : null
);
$normalizedSourceAttributes = array_map(function ($attribute) {
return $this->lightAttributeNormalizer->normalize(
$attribute,
'internal_api',
['locale' => $this->userContext->getUiLocale()->getCode()]
);
}, $sourceAttributeList);
$normalizedDestinationAttributes = array_map(function ($attribute) {
return $this->lightAttributeNormalizer->normalize(
$attribute,
'internal_api',
['locale' => $this->userContext->getUiLocale()->getCode()]
);
}, $destinationAttributeList);
return new JsonResponse([
'sourceAttributeList' => $normalizedSourceAttributes,
'destinationAttributeList' => $normalizedDestinationAttributes,
]);
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ config:
pcmt/rules/fetcher/key-attribute-for-rules: pcmtrules/js/fetcher/key-attribute-for-rules-fetcher
pcmt/rules/fetcher/attributes-for-rules-job: pcmtrules/js/fetcher/attributes-for-rules-job-fetcher
pcmt/rules/fetcher/attributes-for-f2f-mapping: pcmtrules/js/fetcher/attributes-for-f2f-mapping
pcmt/rules/job/field/select: pcmtrules/js/job/field/select
pcmt/rules/job/field/attribute-select: pcmtrules/js/job/field/attribute-select
......@@ -69,6 +70,11 @@ config:
urls:
list: pcmt_rules_attributes_for_options
get: pim_enrich_attribute_rest_get
attributes-for-f2f-mapping:
module: pcmt/rules/fetcher/attributes-for-f2f-mapping
options:
urls:
list: pcmt_rules_attributes_for_f2f_mapping
job-instance-rules:
module: pim/base-fetcher
options:
......
......@@ -8,6 +8,11 @@ pcmt_rules_attributes_for_options:
defaults: { _controller: pcmt.controller.rest.attributes:getForOptionsAction }
methods: [GET]
pcmt_rules_attributes_for_f2f_mapping:
path: /rest/attributes-for-f2f-mapping/
defaults: { _controller: pcmt.controller.rest.attributes:getForF2FMappingAction }
methods: [GET]
pcmt_rules_index_new:
resource: '@PcmtRulesBundle/Resources/config/routing/rules_profile.yml'
prefix: /rules
......
'use strict';
define(['jquery', 'underscore', 'pim/base-fetcher', 'routing'], function($, _, BaseFetcher, Routing) {
return BaseFetcher.extend({
/**
* Fetch all elements of the collection
*
* @return {Promise}
*/
fetchForOptions: function (options) {
let searchOptions = {};
let possibleOptions = ['sourceFamily', 'destinationFamily'];
_.each(possibleOptions, function(option) {
if (options[option]) {
searchOptions[option] = options[option];
}
});
console.log(searchOptions);
return this.search(searchOptions);
},
});
});
......@@ -65,34 +65,29 @@ define(
}
},
postUpdate: function(data) {
var changed = false;
if (data.configuration.sourceFamily !== this.sourceFamily) {
this.sourceFamily = data.configuration.sourceFamily;
this.fetchSourceAttributes();
changed = true;
}
if (data.configuration.destinationFamily !== this.destinationFamily) {
this.destinationFamily = data.configuration.destinationFamily;
this.fetchDestinationAttributes();
changed = true;
}
if (changed) {
this.fetchAttributes();
}
},
fetchSourceAttributes: function() {
let options = {
family: this.sourceFamily
};
FetcherRegistry.getFetcher('attributes-for-rules-job').fetchForOptions(options).then(
function (options) {
this.sourceAttributeList = options;
this.render();
}.bind(this)
);
},
fetchDestinationAttributes: function() {
fetchAttributes: function() {
let options = {
family: this.destinationFamily
sourceFamily: this.sourceFamily,
destinationFamily: this.destinationFamily
};
FetcherRegistry.getFetcher('attributes-for-rules-job').fetchForOptions(options).then(
function (options) {
this.destinationAttributeList = options;
FetcherRegistry.getFetcher('attributes-for-f2f-mapping').fetchForOptions(options).then(
function (result) {
this.sourceAttributeList = result.sourceAttributeList;
this.destinationAttributeList = result.destinationAttributeList;
this.render();
}.bind(this)
);
......
......@@ -109,4 +109,21 @@ class RuleAttributeProvider
{
return $this->attributeRepository->findOneBy(['code' => $code]);
}
public function getForF2FAttributeMapping(?FamilyInterface $sourceFamily, ?FamilyInterface $destinationFamily): array
{
$sourceAttributeList = $sourceFamily ? $sourceFamily->getAttributes()->getValues() : [];
$destinationAttributeList = $destinationFamily ? $destinationFamily->getAttributes()->getValues() : [];
// removing those attributes that are in both families
foreach ($sourceAttributeList as $sKey => $sourceAttribute) {
foreach ($destinationAttributeList as $dKey => $destinationAttribute) {
if ($sourceAttribute->getCode() === $destinationAttribute->getCode()) {
unset($sourceAttributeList[$sKey], $destinationAttributeList[$dKey]);
}
}
}
return [array_values($sourceAttributeList), array_values($destinationAttributeList)];
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ namespace PcmtRulesBundle\Tests\Service;
use Akeneo\Pim\Structure\Component\AttributeTypes;
use Akeneo\Pim\Structure\Component\Model\Family;
use Akeneo\Pim\Structure\Component\Model\FamilyInterface;
use Akeneo\Pim\Structure\Component\Repository\AttributeRepositoryInterface;
use PcmtRulesBundle\Service\RuleAttributeProvider;
use PcmtRulesBundle\Tests\TestDataBuilder\AttributeBuilder;
......@@ -168,6 +169,39 @@ class RuleAttributeProviderTest extends TestCase
$this->assertEquals($attribute->getCode(), $result->getCode());
}
public function dataGetForF2FAttributeMapping(): array
{
$attribute1 = (new AttributeBuilder())->withCode('a1')->build();
$attribute2 = (new AttributeBuilder())->withCode('a2')->build();
$attribute3 = (new AttributeBuilder())->withCode('a3')->build();
$attribute4 = (new AttributeBuilder())->withCode('a4')->build();
$sourceFamily = (new FamilyBuilder())->build();
$sourceFamily->addAttribute($attribute1);
$sourceFamily->addAttribute($attribute2);
$destinationFamily = (new FamilyBuilder())->build();
$destinationFamily->addAttribute($attribute2);
$destinationFamily->addAttribute($attribute3);
$destinationFamily->addAttribute($attribute4);
$result = [[$attribute1], [$attribute3, $attribute4]];
return [
[$sourceFamily, $destinationFamily, $result],
];
}
/** @dataProvider dataGetForF2FAttributeMapping */
public function testGetForF2FAttributeMapping(
?FamilyInterface $sourceFamily,
?FamilyInterface $destinationFamily,
array $expectedResult
): void {
$provider = $this->getRuleAttributeProviderInstance();
$result = $provider->getForF2FAttributeMapping($sourceFamily, $destinationFamily);
$this->assertEquals($expectedResult, $result);
}
private function getRuleAttributeProviderInstance(): RuleAttributeProvider
{
return new RuleAttributeProvider($this->attributeRepositoryMock);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment