/**
  * @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);
         $normalizationContext = $this->userContext->toArray() + ['filter_type' => 'pim.internal_api.product_value.view', 'disable_grouping_separator' => true];
         return new JsonResponse($this->normalizer->normalize($product, 'internal_api', $normalizationContext));
     } else {
         $errors = ['values' => $this->normalizer->normalize($violations, 'internal_api', ['product' => $product])];
         return new JsonResponse($errors, 400);
     }
 }
Ejemplo n.º 2
0
 /**
  * @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 permissions to edit the product
  *
  * @return JsonResponse
  */
 public function postAction(Request $request, $code)
 {
     $group = $this->groupRepository->findOneByIdentifier($code);
     if (null === $group) {
         throw new NotFoundHttpException(sprintf('Group with code "%s" not found', $code));
     }
     $data = json_decode($request->getContent(), true);
     $this->updater->update($group, $data);
     $violations = $this->validator->validate($group);
     if (0 < $violations->count()) {
         $errors = $this->violationNormalizer->normalize($violations, 'internal_api', $this->userContext->toArray());
         return new JsonResponse($errors, 400);
     }
     $this->saver->save($group);
     return new JsonResponse($this->normalizer->normalize($group, 'internal_api', $this->userContext->toArray()));
 }
 /**
  * @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);
 }