/** * @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(); } // TODO: PEF should never update groups, no way to do so from the screen, if a product is added to // another group during the save, this relation will be removed, other issue is that variant groups are never // passed here, so a product is always removed from it's variant group when saved unset($data['groups']); $data = $this->convertLocalizedAttributes($data); $this->updateProduct($product, $data); $violations = $this->validator->validate($product); $violations->addAll($this->localizedConverter->getViolations()); if (0 === $violations->count()) { $this->productSaver->save($product); return new JsonResponse($this->normalizer->normalize($product, 'internal_api', $this->buildContext())); } else { $errors = $this->transformViolations($violations, $product); return new JsonResponse($errors, 400); } }
/** * 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->convertLocalizedToDefaultValues($data, ['locale' => $locale]); $product = $this->productBuilder->createProduct('0'); $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(); }
/** * {@inheritdoc} */ public function process($item) { $convertedItem = $this->convertItemData($item); $convertedItem = $this->convertLocalizedAttributes($convertedItem); $violations = $this->localizedConverter->getViolations(); if ($violations->count() > 0) { $this->skipItemWithConstraintViolations($item, $violations); } $identifier = $this->getIdentifier($convertedItem); if (null === $identifier) { $this->skipItemWithMessage($item, 'The identifier must be filled'); } $familyCode = $this->getFamilyCode($convertedItem); $filteredItem = $this->filterItemData($convertedItem); $product = $this->findOrCreateProduct($identifier, $familyCode); if (false === $this->itemHasStatus && null !== $product->getId()) { unset($filteredItem['enabled']); } if ($this->enabledComparison) { $filteredItem = $this->filterIdenticalData($product, $filteredItem); if (empty($filteredItem) && null !== $product->getId()) { $this->detachProduct($product); $this->stepExecution->incrementSummaryInfo('product_skipped_no_diff'); return null; } } try { $this->updateProduct($product, $filteredItem); } catch (\InvalidArgumentException $exception) { $this->detachProduct($product); $this->skipItemWithMessage($item, $exception->getMessage(), $exception); } $violations = $this->validateProduct($product); if ($violations->count() > 0) { $this->detachProduct($product); $this->skipItemWithConstraintViolations($item, $violations); } return $product; }