private function addNameControl() { $control = $this->addText('name', 'Name'); $control->setRequired(); if ($this->editedProduct !== null) { $control->setDefaultValue($this->editedProduct->getName()); } }
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'); } }
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; }
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())); } }