Exemplo n.º 1
0
 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()));
     }
 }
Exemplo n.º 2
0
 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()));
     }
 }