/** * {@inheritdoc} */ public function convert(array $item, array $options = []) { $standardizedItem = $this->converter->convert($item, $options); $delocalizedItem = $this->delocalizer->convertToDefaultFormats($standardizedItem, $options); $violations = $this->delocalizer->getViolations(); if ($violations->count() > 0) { throw new DataArrayConversionException('An error occurred during the delocalization of the product.', 0, null, $violations); } return $delocalizedItem; }
/** * Apply current values to a fake product and test its integrity with the product validator. * If violations are raised, values are not valid. * * Errors are stored in json format to be useable by the Product Edit Form. * * @return bool */ public function hasValidValues() { $data = json_decode($this->values, true); $locale = $this->userContext->getUiLocale()->getCode(); $data = $this->localizedConverter->convertToDefaultFormats($data, ['locale' => $locale]); $product = $this->productBuilder->createProduct('FAKE_SKU_FOR_MASS_EDIT_VALIDATION_' . microtime()); $this->productUpdater->update($product, $data); $violations = $this->productValidator->validate($product); $violations->addAll($this->localizedConverter->getViolations()); $errors = ['values' => $this->internalNormalizer->normalize($violations, 'internal_api', ['product' => $product])]; $this->errors = json_encode($errors); return 0 === $violations->count(); }
/** * @param Request $request * @param string $code * * @throws NotFoundHttpException If product is not found or the user cannot see it * @throws AccessDeniedHttpException If the user does not have right to edit the product * * @return JsonResponse */ public function postAction(Request $request, $code) { $variantGroup = $this->repository->findOneByIdentifier($code); if (null === $variantGroup) { throw new NotFoundHttpException(sprintf('Variant group with code "%s" not found', $code)); } $data = json_decode($request->getContent(), true); $data = $this->convertLocalizedAttributes($data); $data = $this->variantGroupDataFilter->filterCollection($data, null); $this->updater->update($variantGroup, $data); $violations = $this->validator->validate($variantGroup); $violations->addAll($this->validator->validate($variantGroup->getProductTemplate())); $violations->addAll($this->attributeConverter->getViolations()); if (0 < $violations->count()) { $errors = $this->violationNormalizer->normalize($violations, 'internal_api', $this->userContext->toArray() + ['product' => $variantGroup->getProductTemplate()]); return new JsonResponse($errors, 400); } $this->saver->save($variantGroup, ['copy_values_to_products' => true]); return new JsonResponse($this->normalizer->normalize($variantGroup, 'internal_api', $this->userContext->toArray() + ['with_variant_group_values' => true])); }
/** * @param Request $request * @param string $id * * @throws NotFoundHttpException If product is not found or the user cannot see it * @throws AccessDeniedHttpException If the user does not have right to edit the product * * @return JsonResponse */ public function postAction(Request $request, $id) { $product = $this->findProductOr404($id); if ($this->objectFilter->filterObject($product, 'pim.internal_api.product.edit')) { throw new AccessDeniedHttpException(); } $data = json_decode($request->getContent(), true); try { $data = $this->productEditDataFilter->filterCollection($data, null, ['product' => $product]); } catch (ObjectNotFoundException $e) { throw new BadRequestHttpException(); } $this->updateProduct($product, $data); $violations = $this->validator->validate($product); $violations->addAll($this->localizedConverter->getViolations()); if (0 === $violations->count()) { $this->productSaver->save($product); $normalizationContext = $this->userContext->toArray() + ['filter_types' => ['pim.internal_api.product_value.view'], 'disable_grouping_separator' => true]; return new JsonResponse($this->normalizer->normalize($product, 'internal_api', $normalizationContext)); } $errors = ['values' => $this->normalizer->normalize($violations, 'internal_api', ['product' => $product])]; return new JsonResponse($errors, 400); }