Example #1
0
 public function create(Product $product, FileUpload $fileUpload)
 {
     switch ($fileUpload->getContentType()) {
         case 'image/jpeg':
             $suffix = 'jpg';
             break;
         case 'image/png':
             $suffix = 'png';
             break;
         case 'image/gif':
             $suffix = 'gif';
             break;
         default:
             throw new EntityInvalidArgumentException(sprintf('File is of an unknown type %s.', $fileUpload->getContentType()));
     }
     $baseName = sprintf('%s-%%s.%s', Strings::webalize($product->getName()), $suffix);
     do {
         $fileName = sprintf($baseName, Random::generate(5, '0-9a-zA-Z'));
         $path = sprintf('%s/%s', $this->imagesDir, $fileName);
     } while (file_exists($path));
     $fileUpload->move($path);
     $image = new ProductImage($product, $fileName);
     $this->createEntity($image);
     $product->addImage($image);
     return $image;
 }
Example #2
0
 private function checkDuplicity(Product $product)
 {
     foreach ($product->getCategories() as $category) {
         foreach ($category->getProducts() as $duplicateCandidate) {
             $path = $product->getPath($category);
             if ($duplicateCandidate !== $product && $duplicateCandidate->getPath($category) === $path) {
                 throw new EntityDuplicateException(sprintf('Product with path %s already exists.', $path));
             }
         }
     }
 }
Example #3
0
 private function addProductToCart(BuyForm $form)
 {
     $values = $form->getValues();
     $item = new CartItem($this->product, $values->amount);
     $this->currentCartService->getCurrentCart()->addItem($item);
     if (!$form->hasErrors()) {
         $this->currentCartService->saveCurrentCart();
         if ($item->getAmount() > 1) {
             $this->flashMessage(sprintf('%dx %s was added to cart.', $item->getAmount(), $this->product->getName()));
         } else {
             $this->flashMessage(sprintf('%s was added to cart.', $this->product->getName()));
         }
         $this->redirect('this');
     }
 }
Example #4
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()));
     }
 }
Example #5
0
 private function addImageControl()
 {
     $this->addUpload('imagesUpload', 'Image', true)->addRule(self::IMAGE, 'File must be an image.');
     if ($this->editedProduct !== null && $this->editedProduct->hasImages()) {
         $container = $this->addContainer('images');
         foreach ($this->editedProduct->getImages() as $image) {
             $imageContainer = $container->addContainer($image->getId());
             $imageContainer->addText('description', 'Image description')->setDefaultValue($image->getDescription());
             $imageContainer->addHidden('order', $image->getOrder());
             $imageContainer->addHidden('remove', false);
         }
     }
 }
Example #6
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()));
     }
 }
Example #7
0
 public function __construct(Product $product, $path)
 {
     $this->order = count($product->getImages()) + 1;
     $this->path = $path;
     $this->product = $product;
 }