/**
  * Customize the attribute form
  *
  * @param Form              $form
  * @param AbstractAttribute $attribute
  */
 protected function customizeForm(Form $form, AbstractAttribute $attribute)
 {
     $attTypeClass = $this->attTypeFactory->get($attribute->getAttributeType());
     $fields = $attTypeClass->buildAttributeFormTypes($this->factory, $attribute);
     foreach ($fields as $field) {
         $form->add($field);
     }
 }
 /**
  * Don't allow creating an identifier attribute if one already exists
  *
  * @param AbstractAttribute $attribute
  * @param Constraint        $constraint
  */
 public function validate($attribute, Constraint $constraint)
 {
     if ($attribute->getAttributeType() === 'pim_catalog_identifier') {
         $identifier = $this->manager->getIdentifierAttribute();
         if ($identifier && $identifier->getId() !== $attribute->getId()) {
             $this->context->addViolationAt('attributeType', $constraint->message);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AbstractAttribute $attribute)
 {
     $constraints = array();
     if ('pim_catalog_date' === $attribute->getAttributeType()) {
         $min = $attribute->getDateMin();
         $max = $attribute->getDateMax();
     } else {
         $min = $attribute->getNumberMin();
         $max = $attribute->getNumberMax();
         if (false === $attribute->isNegativeAllowed() && ($min === null || $min < 0)) {
             $min = 0;
         }
     }
     if (null !== $min || null !== $max) {
         $constraints[] = new Range(array('min' => $min, 'max' => $max));
     }
     return $constraints;
 }
 /**
  * Check if an attribute can be removed from the product
  *
  * @param AbstractAttribute $attribute
  *
  * @return boolean
  */
 public function isAttributeRemovable(AbstractAttribute $attribute)
 {
     if ('pim_catalog_identifier' === $attribute->getAttributeType()) {
         return false;
     }
     if (null !== $this->family && $this->family->getAttributes()->contains($attribute)) {
         return false;
     }
     foreach ($this->groups as $group) {
         if ($group->getType()->isVariant()) {
             if ($group->getAttributes()->contains($attribute)) {
                 return false;
             }
         }
     }
     return true;
 }
Пример #5
0
 function it_normalizes_an_attribute(AbstractAttribute $attribute)
 {
     $attribute->getCode()->willReturn('foo');
     $attribute->getAttributeType()->willReturn('bar');
     $this->normalize($attribute, 'json')->shouldReturn(['code' => 'foo', 'type' => 'bar']);
 }
 /**
  * Get normalized configurable for attribute
  * @param AbstractAttribute $attribute
  * @param array             $axisAttributes
  *
  * @return string
  */
 protected function getNormalizedConfigurable(AbstractAttribute $attribute, array $axisAttributes)
 {
     return $attribute->getAttributeType() === 'pim_catalog_simpleselect' && in_array($attribute->getCode(), $axisAttributes) ? '1' : '0';
 }
 /**
  * Is the given attribute ignored ?
  *
  * @param AbstractAttribute $attribute
  * @param MappingCollection $attributeMapping
  *
  * @return boolean
  */
 protected function isAttributeIgnored(AbstractAttribute $attribute, MappingCollection $attributeMapping)
 {
     return in_array(strtolower($attributeMapping->getTarget($attribute->getCode())), $this->getIgnoredAttributes()) || $attribute->getAttributeType() === self::IMAGE_ATTRIBUTE_TYPE;
 }
 /**
  * {@inheritdoc}
  */
 public function supportAttribute(AbstractAttribute $attribute)
 {
     return in_array($attribute->getAttributeType(), array('pim_catalog_text', 'pim_catalog_identifier'));
 }
 /**
  * Ensure indexes from attribute if needed
  *
  * @param AbstractAttribute $attribute
  */
 public function ensureIndexesFromAttribute(AbstractAttribute $attribute)
 {
     if ($attribute->isUseableAsGridFilter() || AbstractProduct::IDENTIFIER_TYPE === $attribute->getAttributeType() || $attribute->isUnique()) {
         $this->indexCreator->ensureIndexesFromAttribute($attribute);
     }
 }
 /**
  * Check if the attribute is removable, otherwise throw an exception or redirect
  *
  * @param AbstractAttribute $attribute
  *
  * @throws DeleteException For ajax requests if the attribute is not removable
  *
  * @return RedirectResponse|null
  */
 protected function validateRemoval(AbstractAttribute $attribute)
 {
     if ($attribute->getAttributeType() === 'pim_catalog_identifier') {
         $errorMessage = 'flash.attribute.identifier not removable';
         $messageParameters = array();
     } else {
         $groupCount = $this->getRepository('Pim\\Bundle\\CatalogBundle\\Entity\\Group')->countVariantGroupAxis($attribute);
         if ($groupCount > 0) {
             $errorMessage = 'flash.attribute.used by groups';
             $messageParameters = array('%count%' => $groupCount);
         }
     }
     if (isset($errorMessage)) {
         if ($this->getRequest()->isXmlHttpRequest()) {
             throw new DeleteException($this->getTranslator()->trans($errorMessage, $messageParameters));
         } else {
             $this->addFlash($errorMessage, $messageParameters);
             return $this->redirectToRoute('pim_enrich_attribute_index');
         }
     }
 }
 function it_generates_attribute_indexes_when_saving_filterable_scopable_and_localizable_attribute($collection, $namingUtility, AbstractAttribute $description)
 {
     $description->getCode()->willReturn('description');
     $description->getBackendType()->willReturn('varchar');
     $description->isLocalizable()->willReturn(true);
     $description->isScopable()->willReturn(true);
     $description->isUseableAsGridFilter()->willReturn(true);
     $description->getAttributeType()->willReturn('pim_catalog_simpleselect');
     $namingUtility->getAttributeNormFields($description)->willReturn(['normalizedData.description-en_US-ecommerce', 'normalizedData.description-de_DE-ecommerce', 'normalizedData.description-en_US-mobile']);
     $this->ensureIndexesFromAttribute($description);
 }
 function it_normalizes_product_with_a_multiselect_value($filter, $serializer, ProductInterface $product, AbstractAttribute $skuAttribute, AbstractAttribute $colorsAttribute, AbstractProductValue $sku, AbstractProductValue $colors, AttributeOption $red, AttributeOption $blue, Collection $values, Family $family)
 {
     $family->getCode()->willReturn('shoes');
     $skuAttribute->getCode()->willReturn('sku');
     $skuAttribute->getAttributeType()->willReturn('pim_catalog_identifier');
     $skuAttribute->isLocalizable()->willReturn(false);
     $skuAttribute->isScopable()->willReturn(false);
     $sku->getAttribute()->willReturn($skuAttribute);
     $sku->getData()->willReturn('sku-001');
     $colorsAttribute->getCode()->willReturn('colors');
     $colorsAttribute->isLocalizable()->willReturn(false);
     $colorsAttribute->isScopable()->willReturn(false);
     $colors->getAttribute()->willReturn($colorsAttribute);
     $colors->getData()->willReturn([$red, $blue]);
     $product->getIdentifier()->willReturn($sku);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroupCodes()->willReturn('');
     $product->getCategoryCodes()->willReturn('');
     $product->getAssociations()->willReturn([]);
     $product->getValues()->willReturn($values);
     $filter->filter($values, ['identifier' => $sku, 'scopeCode' => null, 'localeCodes' => []])->willReturn([$sku, $colors]);
     $serializer->normalize($sku, 'flat', Argument::any())->willReturn(['sku' => 'sku-001']);
     $serializer->normalize($colors, 'flat', Argument::any())->willReturn(['colors' => 'red, blue']);
     $this->normalize($product, 'flat', [])->shouldReturn(['sku' => 'sku-001', 'family' => 'shoes', 'groups' => '', 'categories' => '', 'colors' => 'red, blue', 'enabled' => 1]);
 }
 /**
  * {@inheritdoc}
  */
 public function supportAttribute(AbstractAttribute $attribute)
 {
     return 'pim_catalog_metric' === $attribute->getAttributeType();
 }
 /**
  * Get allowed operators for related attribute
  *
  * @param AbstractAttribute $attribute
  *
  * @throws ProductQueryException
  *
  * @return array
  */
 protected function getAllowedOperators($attribute)
 {
     $operators = ['pim_catalog_identifier' => ['=', 'NOT LIKE', 'LIKE', 'IN'], 'pim_catalog_text' => ['=', 'NOT LIKE', 'LIKE', 'EMPTY'], 'pim_catalog_textarea' => ['=', 'NOT LIKE', 'LIKE', 'EMPTY'], 'pim_catalog_simpleselect' => ['IN', 'NOT IN'], 'pim_catalog_multiselect' => ['IN', 'NOT IN'], 'pim_catalog_number' => ['=', '<', '<=', '>', '>=', 'EMPTY'], 'pim_catalog_boolean' => ['='], 'pim_catalog_date' => ['=', '<', '<=', '>', '>=', 'BETWEEN', 'EMPTY'], 'pim_catalog_price_collection' => ['=', '<', '<=', '>', '>=', 'EMPTY'], 'pim_catalog_metric' => ['=', '<', '<=', '>', '>=', 'EMPTY']];
     $attributeType = $attribute->getAttributeType();
     if (!isset($operators[$attributeType])) {
         throw new \LogicException(sprintf('Attribute type %s is not configured for attribute %s', $attributeType, $attribute->getCode()));
     }
     return $operators[$attributeType];
 }
Пример #15
0
 /**
  * Remove attribute
  *
  * @param AbstractAttribute $attribute
  *
  * @return Family
  *
  * @throws InvalidArgumentException
  */
 public function removeAttribute(AbstractAttribute $attribute)
 {
     if ('pim_catalog_identifier' === $attribute->getAttributeType()) {
         throw new \InvalidArgumentException('Identifier cannot be removed from a family.');
     }
     $this->attributes->removeElement($attribute);
     return $this;
 }
 function it_removes_indexes_for_attribute_when_removing_it($indexPurger, AbstractAttribute $name, LifecycleEventArgs $args)
 {
     $name->isUseableAsGridFilter()->willReturn(true);
     $name->getAttributeType()->willReturn('pim_catalog_text');
     $name->isUnique()->willReturn(false);
     $args->getEntity()->willReturn($name);
     $indexPurger->purgeIndexesFromAttribute($name)->shouldBeCalled();
     $this->postRemove($args);
 }
Пример #17
0
 function it_allows_defining_an_attribute_to_use_as_label(AbstractAttribute $name)
 {
     $name->getAttributeType()->willReturn('pim_catalog_text');
     $this->getAttributeAsLabel()->shouldReturn(null);
     $this->setAttributeAsLabel($name)->shouldReturn($this);
 }
 /**
  * Ensure at least one option for the attribute
  *
  * @param AbstractAttribute $entity
  */
 protected function ensureOneOption(AbstractAttribute $entity)
 {
     $selectTypes = array('pim_catalog_simpleselect', 'pim_catalog_multiselect');
     if (in_array($entity->getAttributeType(), $selectTypes) && count($entity->getOptions()) < 1) {
         $option = $this->attributeManager->createAttributeOption();
         $option->setTranslatable(true);
         $entity->addOption($option);
     }
 }
 /**
  * Prepare attribute view
  *
  * @param AbstractAttribute     $attribute
  * @param ProductValueInterface $value
  * @param FormView              $view
  *
  * @return array
  */
 protected function prepareAttributeView(AbstractAttribute $attribute, ProductValueInterface $value, FormView $view)
 {
     $attributeView = array('id' => $attribute->getId(), 'isRemovable' => $value->isRemovable(), 'code' => $attribute->getCode(), 'label' => $attribute->getLabel(), 'sortOrder' => $attribute->getSortOrder(), 'allowValueCreation' => in_array($attribute->getAttributeType(), $this->choiceAttributeTypes), 'locale' => $value->getLocale());
     if ($attribute->isScopable()) {
         $attributeView['values'] = array_merge($this->getAttributeValues($attribute, $value->getLocale()), array($value->getScope() => $view));
         ksort($attributeView['values']);
     } else {
         $attributeView['value'] = $view;
     }
     $classes = $this->getAttributeClasses($attribute);
     if (!empty($classes)) {
         $attributeView['classes'] = $classes;
     }
     return $attributeView;
 }
Пример #20
0
 /**
  * {@inheritdoc}
  */
 public function supportAttribute(AbstractAttribute $attribute)
 {
     return in_array($attribute->getAttributeType(), array('pim_catalog_date'));
 }
 /**
  * {@inheritdoc}
  */
 public function addAttributeSorter(AbstractAttribute $attribute, $direction)
 {
     $attributeType = $attribute->getAttributeType();
     if (isset($this->attributeSorters[$attributeType])) {
         $sorterClass = $this->attributeSorters[$attributeType];
     } else {
         $sorterClass = 'Pim\\Bundle\\CatalogBundle\\Doctrine\\MongoDBODM\\Sorter\\BaseSorter';
     }
     $sorter = new $sorterClass($this->getQueryBuilder(), $this->context);
     $sorter->addAttributeSorter($attribute, $direction);
     return $this;
 }