function it_updates_a_family($attrRequiFactory, $channelRepository, FamilyTranslation $translation, FamilyInterface $family, AttributeRepositoryInterface $attributeRepository, AttributeInterface $attribute1, AttributeInterface $attribute2, AttributeInterface $attribute3, AttributeInterface $attribute4, AttributeRequirementInterface $attributeRequirement1, AttributeRequirementInterface $attributeRequirement2, AttributeRequirementInterface $attributeRequirement3, AttributeRequirementInterface $attributeRequirement4, AttributeRequirementInterface $attributeRequirement5, ChannelInterface $channel1, ChannelInterface $channel2, FamilyInterface $family) { $values = ['code' => 'mycode', 'attributes' => ['sku', 'name', 'description', 'price'], 'attribute_as_label' => 'name', 'requirements' => ['mobile' => ['sku', 'name'], 'print' => ['sku', 'name', 'description']], 'labels' => ['fr_FR' => 'Moniteurs', 'en_US' => 'PC Monitors']]; $attributeRepository->findOneByIdentifier('sku')->willReturn($attribute1); $attributeRepository->findOneByIdentifier('name')->willReturn($attribute2); $attributeRepository->findOneByIdentifier('description')->willReturn($attribute3); $attributeRepository->findOneByIdentifier('price')->willReturn($attribute4); $channelRepository->findOneByIdentifier('mobile')->willReturn($channel1); $channelRepository->findOneByIdentifier('print')->willReturn($channel2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel1, true)->willReturn($attributeRequirement1); $attrRequiFactory->createAttributeRequirement($attribute2, $channel1, true)->willReturn($attributeRequirement2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel2, true)->willReturn($attributeRequirement3); $attrRequiFactory->createAttributeRequirement($attribute2, $channel2, true)->willReturn($attributeRequirement4); $attrRequiFactory->createAttributeRequirement($attribute3, $channel2, true)->willReturn($attributeRequirement5); $family->setAttributeRequirements([$attributeRequirement1, $attributeRequirement2, $attributeRequirement3, $attributeRequirement4, $attributeRequirement5])->shouldBeCalled(); $family->setCode('mycode')->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->setLocale('en_US')->shouldBeCalled(); $family->setLocale('fr_FR')->shouldBeCalled(); $family->getTranslation()->willReturn($translation); $translation->setLabel('label en us'); $translation->setLabel('label fr fr'); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute3)->shouldBeCalled(); $family->addAttribute($attribute4)->shouldBeCalled(); $family->setAttributeAsLabel($attribute2)->shouldBeCalled(); $this->update($family, $values, []); }
/** * @return array */ public function resolveAttributeColumns() { if (empty($this->attributesFields)) { $attributes = $this->attributeRepository->findAll(); $currencyCodes = $this->currencyRepository->getActivatedCurrencyCodes(); $values = $this->valuesResolver->resolveEligibleValues($attributes); foreach ($values as $value) { if (null !== $value['locale'] && null !== $value['scope']) { $field = sprintf('%s-%s-%s', $value['attribute'], $value['locale'], $value['scope']); } elseif (null !== $value['locale']) { $field = sprintf('%s-%s', $value['attribute'], $value['locale']); } elseif (null !== $value['scope']) { $field = sprintf('%s-%s', $value['attribute'], $value['scope']); } else { $field = $value['attribute']; } if (AttributeTypes::PRICE_COLLECTION === $value['type']) { $this->attributesFields[] = $field; foreach ($currencyCodes as $currencyCode) { $currencyField = sprintf('%s-%s', $field, $currencyCode); $this->attributesFields[] = $currencyField; } } elseif (AttributeTypes::METRIC === $value['type']) { $this->attributesFields[] = $field; $metricField = sprintf('%s-%s', $field, 'unit'); $this->attributesFields[] = $metricField; } else { $this->attributesFields[] = $field; } } } return $this->attributesFields; }
/** * Get the media attributes * * @return string[] */ public function getMediaAttributes() { if (null === $this->mediaAttributes) { $this->mediaAttributes = $this->attributeRepository->findMediaAttributeCodes(); } return $this->mediaAttributes; }
/** * {@inheritdoc} */ public function getFilter($code) { $attribute = $this->attributeRepository->findOneBy(['code' => FieldFilterHelper::getCode($code)]); if (null !== $attribute) { return $this->getAttributeFilter($attribute); } return $this->getFieldFilter($code); }
/** * Fetch the attribute by its code * * @param string $code * * @throws \LogicException * * @return AttributeInterface */ protected function getAttribute($code) { $attribute = $this->attributeRepository->findOneBy(['code' => $code]); if ($attribute === null) { throw new \LogicException(sprintf('Unknown attribute "%s".', $code)); } return $attribute; }
/** * Don't allow creating an identifier attribute if one already exists * * @param AttributeInterface $attribute * @param Constraint $constraint */ public function validate($attribute, Constraint $constraint) { if (AttributeTypes::IDENTIFIER === $attribute->getAttributeType()) { $identifier = $this->attributeRepository->getIdentifier(); if ($identifier && $identifier->getId() !== $attribute->getId()) { $this->context->buildViolation($constraint->message)->atPath('attributeType')->addViolation(); } } }
/** * Don't allow creating an identifier attribute if one already exists * * @param AttributeInterface $attribute * @param Constraint $constraint */ public function validate($attribute, Constraint $constraint) { if ($attribute->getAttributeType() === 'pim_catalog_identifier') { $identifier = $this->attributeRepository->getIdentifier(); if ($identifier && $identifier->getId() !== $attribute->getId()) { $this->context->addViolationAt('attributeType', $constraint->message); } } }
/** * Find common attributes * Common attributes are: * - not unique (and not identifier) * - without value AND link to family * - with value * * @param ProductInterface[] $products * * @return AttributeInterface[] */ public function findCommonAttributes(array $products) { $productIds = []; foreach ($products as $product) { $productIds[] = $product->getId(); } $attributeIds = $this->massActionRepository->findCommonAttributeIds($productIds); return $this->attributeRepository->findWithGroups(array_unique($attributeIds), ['conditions' => ['unique' => 0]]); }
/** * Load the attribute for this filter * Required to prepare choice url params and filter configuration * * @throws \LogicException */ protected function getAttribute() { $fieldName = $this->get(ProductFilterUtility::DATA_NAME_KEY); $attribute = $this->attributeRepository->findOneByCode($fieldName); if (!$attribute) { throw new \LogicException(sprintf('There is no attribute with code %s.', $fieldName)); } return $attribute; }
/** * Get fields for products * * @param array $productIds * * @return array */ public function getFieldsList($productIds) { $this->prepareAvailableAttributeIds($productIds); $attributes = $this->getAttributeIds(); if (empty($attributes)) { return []; } $attributes = $this->attributeRepository->findBy(['id' => $this->getAttributeIds()]); return $this->prepareFieldsList($attributes); }
/** * {@inheritdoc} */ public function initialize() { $this->channels = $this->channelRepository->findAll(); foreach ($this->attributeRepository->getNonIdentifierAttributes() as $attribute) { $this->attributes[(string) $attribute->getGroup()][] = $attribute; foreach ($this->channels as $channel) { $this->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, false)); } } }
/** * @return FamilyInterface */ public function createFamily() { $family = new $this->familyClass(); $identifier = $this->attributeRepository->getIdentifier(); $family->addAttribute($identifier); $family->setAttributeAsLabel($identifier); foreach ($this->getChannels() as $channel) { $requirement = $this->factory->createAttributeRequirement($identifier, $channel, true); $family->addAttributeRequirement($requirement); } return $family; }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $values = new ArrayCollection(); foreach ($data as $attributeCode => $valuesData) { $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); foreach ($valuesData as $valueData) { $value = $this->denormalizer->denormalize($valueData, $this->valueClass, 'json', ['attribute' => $attribute] + $context); $values->add($value); } } return $values; }
function it_denormalizes_product_values_from_json($denormalizer, $registry, AttributeRepositoryInterface $attributeRepository, ProductValueInterface $nameValue, ProductValueInterface $colorValue, AttributeInterface $name, AttributeInterface $color) { $data = ['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']], 'color' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'value' => 'red']]]; $attributeRepository->findOneByIdentifier('name')->willReturn($name); $attributeRepository->findOneByIdentifier('color')->willReturn($color); $registry->getRepository('Attribute')->willReturn($attributeRepository); $denormalizer->denormalize($data['name'][0], 'ProductValue', 'json', ['attribute' => $name])->shouldBeCalled()->willReturn($nameValue); $denormalizer->denormalize($data['color'][0], 'ProductValue', 'json', ['attribute' => $color])->shouldBeCalled()->willReturn($colorValue); $values = $this->denormalize($data, 'ProductValue[]', 'json'); $values->shouldHaveCount(2); $values[0]->shouldBe($nameValue); $values[1]->shouldBe($colorValue); }
/** * Get the attribute collection * * @return JsonResponse */ public function indexAction(Request $request) { $criteria = []; if ($request->query->has('identifiers')) { $criteria['code'] = explode(',', $request->query->get('identifiers')); } if ($request->query->has('types')) { $criteria['attributeType'] = explode(',', $request->query->get('types')); } $attributes = $this->attributeRepository->findBy($criteria); $filteredAttributes = $this->collectionFilter->filterCollection($attributes, 'pim.internal_api.attribute.view'); $normalizedAttributes = $this->normalizer->normalize($filteredAttributes, 'internal_api'); return new JsonResponse($normalizedAttributes); }
/** * @param FamilyInterface $family * * @return string[] */ protected function getMissingChannelCodes(FamilyInterface $family) { $requirements = $family->getAttributeRequirements(); $identifierCode = $this->attributeRepository->getIdentifierCode(); $currentChannelCodes = []; foreach ($requirements as $requirement) { if ($requirement->getAttributeCode() === $identifierCode) { $currentChannelCodes[] = $requirement->getChannelCode(); } } $expectedChannelCodes = $this->channelRepository->getChannelCodes(); $missingChannelCodes = array_diff($expectedChannelCodes, $currentChannelCodes); return $missingChannelCodes; }
/** * @param GroupInterface $group * @param array $data */ protected function setAxis(GroupInterface $group, $data) { if (isset($data['axis']) && !empty($data['axis'])) { $axisCodes = explode(',', $data['axis']); $attributes = []; foreach ($axisCodes as $code) { $attribute = $this->attributeRepository->findOneByIdentifier($code); if (!$attribute) { throw new \LogicException(sprintf('Attribute with identifier "%s" not found', $code)); } $attributes[] = $attribute; } $group->setAxisAttributes($attributes); } }
/** * {@inheritdoc} */ public function convert(array $items, array $options = []) { $attributeTypes = $this->attributeRepository->getAttributeTypeByCodes(array_keys($items)); foreach ($items as $code => $item) { if (isset($attributeTypes[$code])) { $localizer = $this->localizerRegistry->getLocalizer($attributeTypes[$code]); if (null !== $localizer) { foreach ($item as $i => $data) { $items[$code][$i] = $this->convertAttribute($localizer, $data, $options, $code); } } } } return $items; }
/** * {@inheritdoc} */ public function process($family) { $configuration = $this->getJobConfiguration(); if (!array_key_exists('actions', $configuration)) { throw new InvalidArgumentException('Missing configuration for \'actions\'.'); } $actions = $configuration['actions']; foreach ($actions as $action) { $attribute = $this->attributeRepository->findOneByIdentifier($action['attribute_code']); $channel = $this->channelRepository->findOneByIdentifier($action['channel_code']); $isRequired = $action['is_required']; $family->addAttribute($attribute); $family->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, $isRequired)); } return $family; }
/** * {@inheritdoc} */ public function convertDefaultToLocalizedValue($code, $data, $options = []) { $attribute = $this->attributeRepository->findOneBy(['code' => $code]); if (null === $attribute) { return $data; } $attributeType = $attribute->getAttributeType(); if (null === $attributeType) { return $data; } $localizer = $this->localizerRegistry->getLocalizer($attributeType); if (null === $localizer) { return $data; } return $localizer->convertDefaultToLocalized($data, $options); }
/** * @return string */ protected function getIdentifierCode() { if (null === $this->identifierCode) { $this->identifierCode = $this->attributeRepository->getIdentifierCode(); } return $this->identifierCode; }
/** * {@inheritdoc} */ public function createProduct($identifier = null, $familyCode = null) { $product = new $this->productClass(); $identifierAttribute = $this->attributeRepository->getIdentifier(); $productValue = $this->createProductValue($identifierAttribute); $product->addValue($productValue); if (null !== $identifier) { $productValue->setData($identifier); } if (null !== $familyCode) { $family = $this->familyRepository->findOneByIdentifier($familyCode); $product->setFamily($family); } $event = new GenericEvent($product); $this->eventDispatcher->dispatch(ProductEvents::CREATE, $event); return $product; }
/** * Find an attribute by its id or return a 404 response * * @param int $id * * @throws NotFoundHttpException * * @return \Pim\Bundle\CatalogBundle\Model\AttributeInterface */ protected function findAttributeOr404($id) { $attribute = $this->attributeRepository->find($id); if (!$attribute) { throw new NotFoundHttpException(sprintf('Attribute with id %s could not be found.', $id)); } return $attribute; }
/** * @param FamilyInterface $family * @param string $data */ protected function setAttributeAsLabel(FamilyInterface $family, $data) { if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($data))) { $family->setAttributeAsLabel($attribute); } else { throw new \InvalidArgumentException(sprintf('Attribute with "%s" code does not exist', $data)); } }
/** * Extract attribute field name information with attribute code, locale code, scope code * and optionally price currency * * Returned array like: * [ * "attribute" => AttributeInterface, * "locale_code" => <locale_code>|null, * "scope_code" => <scope_code>|null, * "price_currency" => <currency_code> // this key is optional * ] * * Return null if the field name does not match an attribute. * * @param string $fieldName * * @return array|null */ public function extractColumnInfo($fieldName) { if (!isset($this->fieldNameInfoCache[$fieldName]) && !in_array($fieldName, $this->excludedFieldNames)) { $explodedFieldName = explode(self::FIELD_SEPARATOR, $fieldName); $attributeCode = $explodedFieldName[0]; $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); if (null !== $attribute) { $this->checkFieldNameTokens($attribute, $fieldName, $explodedFieldName); $attributeInfo = $this->extractAttributeInfo($attribute, $explodedFieldName); $this->checkFieldNameLocaleByChannel($attribute, $fieldName, $attributeInfo); $this->fieldNameInfoCache[$fieldName] = $attributeInfo; } else { $this->excludedFieldNames[] = $fieldName; } } return isset($this->fieldNameInfoCache[$fieldName]) ? $this->fieldNameInfoCache[$fieldName] : null; }
/** * Get non eligible attributes to a product template * * @param GroupInterface $group * * @return AttributeInterface[] */ public function getNonEligibleAttributes(GroupInterface $variantGroup) { $attributes = $variantGroup->getAxisAttributes()->toArray(); $template = $variantGroup->getProductTemplate(); if (null !== $template) { foreach (array_keys($template->getValuesData()) as $attributeCode) { $attributes[] = $this->attributeRepository->findOneByIdentifier($attributeCode); } } $uniqueAttributes = $this->attributeRepository->findBy(['unique' => true]); foreach ($uniqueAttributes as $attribute) { if (!in_array($attribute, $attributes)) { $attributes[] = $attribute; } } return $attributes; }
/** * Get an attribute or throw an exception * * @param int $id * * @throws EntityNotFoundException * * @return AttributeInterface * * @deprecated will be removed in 1.5 please use AttributeRepositoryInterface->find() */ public function getAttribute($id) { $attribute = $this->repository->find($id); if (null === $attribute) { throw new EntityNotFoundException(); } return $attribute; }
/** * {@inheritdoc} */ public function filter(ProductInterface $product, array $newValues) { $originalValues = $this->getOriginalProduct($product); $attributeTypes = $this->attributeRepository->getAttributeTypeByCodes(array_keys($newValues)); $result = []; foreach ($newValues as $code => $value) { if (in_array($code, $this->productFields)) { $data = $this->compareField($originalValues, $value, $code); } elseif (isset($attributeTypes[$code])) { $data = $this->compareAttribute($originalValues, $value, $attributeTypes, $code); } else { throw new \LogicException(sprintf('Cannot filter value of field "%s"', $code)); } if (null !== $data) { $result = $this->mergeValueToResult($result, $data); } } return $result; }
/** * Get attributes configuration for attribute that can be used in grid (as column or filter) * * @return array */ protected function getAttributesConfig() { $attributeIds = $this->getAttributeIds(); if (empty($attributeIds)) { return []; } $currentLocale = $this->getCurrentLocaleCode(); $configuration = $this->attributeRepository->getAttributesAsArray(true, $currentLocale, $attributeIds); return $configuration; }
function it_initializes_attribute_requirements_with_all_channels_and_attributes_in_the_PIM(ChannelRepositoryInterface $channelRepository, ChannelInterface $ecommerceChannel, ChannelInterface $mobileChannel, AttributeRepositoryInterface $attributeRepository, AttributeInterface $nameAttribute, AttributeInterface $descriptionAttribute, AttributeRequirementFactory $factory, AttributeRequirementInterface $nameECommerceRequirement, AttributeRequirementInterface $nameMobileRequirement, AttributeRequirementInterface $descriptionECommerceRequirement, AttributeRequirementInterface $descriptionMobileRequirement) { $channelRepository->findAll()->willReturn([$ecommerceChannel, $mobileChannel]); $attributeRepository->getNonIdentifierAttributes()->willReturn([$nameAttribute, $descriptionAttribute]); $factory->createAttributeRequirement($nameAttribute, $ecommerceChannel, false)->willReturn($nameECommerceRequirement); $nameECommerceRequirement->getAttributeCode()->willReturn('name'); $nameECommerceRequirement->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($nameAttribute, $mobileChannel, false)->willReturn($nameMobileRequirement); $nameMobileRequirement->getAttributeCode()->willReturn('name'); $nameMobileRequirement->getChannelCode()->willReturn('mobile'); $factory->createAttributeRequirement($descriptionAttribute, $ecommerceChannel, false)->willReturn($descriptionECommerceRequirement); $descriptionECommerceRequirement->getAttributeCode()->willReturn('description'); $descriptionECommerceRequirement->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($descriptionAttribute, $mobileChannel, false)->willReturn($descriptionMobileRequirement); $descriptionMobileRequirement->getAttributeCode()->willReturn('description'); $descriptionMobileRequirement->getChannelCode()->willReturn('mobile'); $this->initialize(); $this->getAttributeRequirements()->toArray()->shouldReturn(['name_ecommerce' => $nameECommerceRequirement, 'name_mobile' => $nameMobileRequirement, 'description_ecommerce' => $descriptionECommerceRequirement, 'description_mobile' => $descriptionMobileRequirement]); }