/** * Returns a JSON response containing options * * @param Request $request * * @return JsonResponse */ public function listAction(Request $request) { $query = $request->query; $search = $query->get('search'); $referenceDataName = $query->get('referenceDataName'); $class = $query->get('class'); if (null !== $referenceDataName) { $class = $this->registry->get($referenceDataName)->getClass(); } $repository = $this->doctrine->getRepository($class); if ($repository instanceof OptionRepositoryInterface) { $choices = $repository->getOptions($query->get('dataLocale'), $query->get('collectionId'), $search, $query->get('options', [])); } elseif ($repository instanceof ReferenceDataRepositoryInterface) { $choices['results'] = $repository->findBySearch($search, $query->get('options', [])); } elseif ($repository instanceof SearchableRepositoryInterface) { $choices['results'] = $repository->findBySearch($search, $query->get('options', [])); } elseif (method_exists($repository, 'getOptions')) { $choices = $repository->getOptions($query->get('dataLocale'), $query->get('collectionId'), $search, $query->get('options', [])); } else { throw new \LogicException(sprintf('The repository of the class "%s" can not retrieve options via Ajax.', $query->get('class'))); } if ($query->get('isCreatable') && 0 === count($choices['results'])) { $choices['results'] = [['id' => $search, 'text' => $search]]; } return new JsonResponse($choices); }
/** * {@inheritdoc} */ public function validate($attribute, Constraint $constraint) { $referenceDataName = $attribute->getProperty('reference_data_name'); if (null !== $this->registry && in_array($attribute->getAttributeType(), $this->referenceDataType) && !$this->registry->has($referenceDataName)) { $references = array_keys($this->registry->all()); $this->context->buildViolation($constraint->message)->setParameter('%reference_data_name%', $referenceDataName)->setParameter('%references%', implode(', ', $references))->addViolation(); } }
/** * @param AttributeInterface $attribute * * @return array */ protected function getChoiceUrlParams(AttributeInterface $attribute) { $referenceDataName = $attribute->getReferenceDataName(); $referenceData = $this->registry->get($referenceDataName); if (null === $referenceData) { throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist', $referenceDataName)); } return ['class' => $referenceData->getClass(), 'dataLocale' => $this->userContext->getCurrentLocaleCode(), 'collectionId' => $attribute->getId()]; }
/** * @return array */ protected function getReferenceDataTypeChoices() { $choices = []; foreach ($this->referenceDataRegistry->all() as $configuration) { if (ConfigurationInterface::TYPE_SIMPLE === $configuration->getType()) { $choices[$configuration->getName()] = $configuration->getName(); } } return $choices; }
/** * {@inheritdoc} */ protected function getFormOptions() { $attribute = $this->getAttribute(); $referenceDataName = $attribute->getReferenceDataName(); $referenceData = $this->registry->get($referenceDataName); if (null === $referenceData) { throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist', $referenceDataName)); } return array_merge(parent::getFormOptions(), ['choice_url_params' => ['class' => $referenceData->getClass(), 'dataLocale' => $this->userContext->getCurrentLocaleCode(), 'collectionId' => $attribute->getId()]]); }
/** * {@inheritdoc} */ public function transform($value, array $options = []) { if (null === $this->registry) { return null; } $value = trim($value); if (empty($value)) { return null; } if (!$this->registry->has($value)) { $references = array_keys($this->registry->all()); throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist. Allowed values are: %s', $value, implode(', ', $references))); } return $value; }
/** * {@inheritdoc} */ public function getEligibleProductIdsForVariantGroup($variantGroupId) { $sql = 'SELECT v.entity_id AS product_id, gp.group_id, ga.group_id ' . 'FROM pim_catalog_group g ' . 'INNER JOIN pim_catalog_group_attribute ga ON ga.group_id = g.id ' . 'INNER JOIN %product_value_table% v ON v.attribute_id = ga.attribute_id ' . 'LEFT JOIN pim_catalog_group_product gp ON (v.entity_id = gp.product_id) ' . 'INNER JOIN pim_catalog_group_type gt on gt.id = g.type_id ' . 'WHERE ga.group_id = :groupId AND gt.code = "VARIANT" ' . 'AND (v.option_id IS NOT NULL'; if (null !== $this->referenceDataRegistry) { $references = $this->referenceDataRegistry->all(); if (!empty($references)) { $valueMetadata = QueryBuilderUtility::getProductValueMetadata($this->_em, $this->_entityName); foreach ($references as $code => $referenceData) { if (ConfigurationInterface::TYPE_SIMPLE === $referenceData->getType()) { if ($valueMetadata->isAssociationWithSingleJoinColumn($code)) { $sql .= sprintf(' OR v.%s IS NOT NULL', $valueMetadata->getSingleAssociationJoinColumnName($code)); } } } } } $sql .= ') ' . 'GROUP BY v.entity_id ' . 'HAVING (gp.group_id IS NULL OR gp.group_id = ga.group_id) ' . 'AND COUNT(ga.attribute_id) = (SELECT COUNT(*) FROM pim_catalog_group_attribute WHERE group_id = :groupId)'; $sql = QueryBuilderUtility::prepareDBALQuery($this->_em, $this->_entityName, $sql); $stmt = $this->getEntityManager()->getConnection()->prepare($sql); $stmt->bindValue('groupId', $variantGroupId); $stmt->execute(); $results = $stmt->fetchAll(); $productIds = array_map(function ($row) { return $row['product_id']; }, $results); return $productIds; }
/** * @param string $value * * @throws \InvalidArgumentException */ protected function checkIfReferenceDataExists($value) { if (in_array($value['attributeType'], $this->referenceDataType)) { if (!$this->registry->has($value['reference_data_name'])) { $references = array_keys($this->registry->all()); throw new \InvalidArgumentException(sprintf('Reference data "%s" does not exist. Allowed values are: %s', $value['reference_data_name'], implode(', ', $references))); } } }
/** * {@inheritdoc} */ public function supportsAttribute(AttributeInterface $attribute) { $referenceDataName = $attribute->getReferenceDataName(); return null !== $referenceDataName && null !== $this->registry->get($referenceDataName) ? true : false; }
/** * Get the reference data configuration * * @return JsonResponse */ public function indexAction() { $referenceConfig = $this->registry->all(); return new JsonResponse($this->normalizer->normalize($referenceConfig, 'internal_api')); }
/** * Get the class of a reference data type * * @param string $referenceDataType * * @return string */ protected function getReferenceDataClass($referenceDataType) { $configuration = $this->referenceDataRegistry->get($referenceDataType); return $configuration->getClass(); }
/** * {@inheritdoc} */ public function resolve($referenceDataType) { $referenceDataConf = $this->configurationRegistry->get($referenceDataType); $referenceDataClass = $referenceDataConf->getClass(); return $this->doctrineRegistry->getRepository($referenceDataClass); }