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())); } }
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); } } }