private function createProduct(ProductForm $form) { $values = $form->getValues(); $product = new Product($values->name, $values->price); $product->setDescription($values->description); if ($values->discountType === ProductForm::DISCOUNT_PERCENT) { $product->setDiscountPercent($values->discountPercent); } else { $product->setNominalDiscount($values->nominalDiscount); } $product->setCategories($this->categoryService->getByIds($values->categories)); /** @var FileUpload $fileUpload */ foreach ($values->imagesUpload as $fileUpload) { $this->productImageService->create($product, $fileUpload); } try { if (!$form->hasErrors()) { $this->productService->create($product); $this->flashMessage(sprintf('Product %s has been created.', $product->getName())); $this->redirect(':Admin:Product:List:'); } } catch (EntityDuplicateException $e) { $form->addError(sprintf('Product with name %s already exists.', $product->getName())); } }
/** * @param string $path */ public function actionDefault($path) { $pathsSeparatorPosition = strrpos($path, '/'); if ($pathsSeparatorPosition !== false) { $categoryPath = substr($path, 0, $pathsSeparatorPosition); $productPath = substr($path, $pathsSeparatorPosition + 1); $productCandidates = $this->productService->getByPath($productPath); foreach ($productCandidates as $productCandidate) { foreach ($productCandidate->getCategories() as $category) { if ($category->getPath() === $categoryPath) { $this->product = $productCandidate; break 2; } } } } if ($this->product === null) { throw new BadRequestException(sprintf('Product with path %s not found.', $path)); } $this->setCurrentCategory($category); }
private function updateProduct(ProductForm $form) { $values = $form->getValues(); $this->product->setName($values->name); $this->product->setOriginalPrice($values->price); $this->product->setDescription($values->description); if ($values->discountType === ProductForm::DISCOUNT_PERCENT) { $this->product->setDiscountPercent($values->discountPercent); } else { $this->product->setNominalDiscount($values->nominalDiscount); } $this->product->setCategories($this->categoryService->getByIds($values->categories)); if ($this->product->hasImages()) { $fixedOrders = $this->fixOrders($values->images); foreach ($values->images as $imageId => $imageData) { $imageId = (int) $imageId; $image = $this->productImageService->getById($imageId); if ((bool) $imageData->remove) { $this->productImageService->delete($image); } else { $image->setDescription($imageData->description === '' ? null : $imageData->description); $image->setOrder($fixedOrders[$imageId]); } } } /** @var FileUpload $fileUpload */ foreach ($values->imagesUpload as $fileUpload) { $this->productImageService->create($this->product, $fileUpload); } try { if (!$form->hasErrors()) { $this->productService->update($this->product); $this->flashMessage(sprintf('Product %s has been updated.', $this->product->getName())); $this->redirect(':Admin:Product:List:'); } } catch (EntityDuplicateException $e) { $form->addError(sprintf('Product with name %s already exists.', $this->product->getName())); } }