/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = array()) { if ($data === null || $data === '') { return null; } $context = $context + ['use_relative_media_path' => false]; return $this->manager->createFromFilename($data, $context['use_relative_media_path']); }
/** * {@inheritdoc} */ protected function doNormalize($object, $format = null, array $context = array()) { $context = $this->resolveContext($context); if ($context['versioning']) { return $object->getFilename(); } return $this->manager->getExportPath($object); }
/** * @param ProductMediaInterface $media */ protected function copyMedia(ProductMediaInterface $media) { $result = $this->mediaManager->copy($media, dirname($this->getPath())); if ($result === true) { $exportPath = $this->mediaManager->getExportPath($media); $this->writtenFiles[sprintf('%s/%s', dirname($this->getPath()), $exportPath)] = $exportPath; } }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { if (empty($data)) { return null; } $media = $this->mediaManager->createFromFilePath($data['filePath']); $media->setOriginalFilename($data['originalFilename']); return $media; }
/** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = []) { $file = $object->getFile(); if (null !== $file && $file instanceof UploadedFile) { // happens in case of mass edition return ['originalFilename' => $file->getClientOriginalName(), 'filePath' => $file->getPathname()]; } return ['originalFilename' => $object->getOriginalFilename(), 'filePath' => $this->manager->getFilePath($object, false)]; }
/** * {@inheritdoc} * * Expected data input format : * { * "originalFilename": "original_filename.extension", * "filePath": "/current/file/path/original_filename.extension" * } */ public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $options = $this->resolver->resolve($options); $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'media'); $this->checkData($attribute, $data); $file = $this->getFileData($attribute, $data); $this->setMedia($product, $attribute, $file, $data['originalFilename'], $options['locale'], $options['scope']); $this->mediaManager->handleProductMedias($product); }
/** * {@inheritdoc} */ public function write(array $items) { $this->versionManager->setRealTimeVersioning($this->realTimeVersioning); foreach ($items as $item) { $this->incrementCount($item); } $this->mediaManager->handleAllProductsMedias($items); $this->productSaver->saveAll($items, ['recalculate' => false]); $this->detacher->detachAll($items); }
/** * {@inheritdoc} */ public function setValue(array $products, AttributeInterface $attribute, $data, $locale = null, $scope = null) { $this->checkLocaleAndScope($attribute, $locale, $scope, 'media'); $this->checkData($attribute, $data); $file = $this->getFileData($attribute, $data); foreach ($products as $product) { $this->setMedia($attribute, $product, $file, $locale, $scope); } $this->mediaManager->handleAllProductsMedias($products); }
/** * Handles the media of the given product template * * @param ProductTemplateInterface $template */ public function handleProductTemplateMedia(ProductTemplateInterface $template) { $mediaHandled = false; foreach ($template->getValues() as $value) { if (null !== ($media = $value->getMedia())) { $mediaHandled = true; $filenamePrefix = $media->getFile() ? $this->generateFilenamePrefix($value) : null; $this->mediaManager->handle($media, $filenamePrefix); } } if ($mediaHandled) { $this->updateNormalizedValues($template); } }
/** * Update product * * @param Request $request * @param integer $id * * @Template("PimEnrichBundle:Product:edit.html.twig") * @AclAncestor("pim_enrich_product_index") * @return RedirectResponse */ public function updateAction(Request $request, $id) { $product = $this->findProductOr404($id); $this->productManager->ensureAllAssociationTypes($product); $form = $this->createForm('pim_product_edit', $product, $this->getEditFormOptions($product)); $form->submit($request, false); if ($form->isValid()) { try { $this->mediaManager->handleProductMedias($product); $this->productSaver->save($product); $this->addFlash('success', 'flash.product.updated'); } catch (MediaManagementException $e) { $this->addFlash('error', $e->getMessage()); } $params = ['id' => $product->getId(), 'dataLocale' => $this->getDataLocaleCode()]; if ($comparisonLocale = $this->getComparisonLocale()) { $params['compareWith'] = $comparisonLocale; } return $this->redirectAfterEdit($params); } else { $this->addFlash('error', 'flash.product.invalid'); } $channels = $this->getRepository('PimCatalogBundle:Channel')->findAll(); $trees = $this->productCatManager->getProductCountByTree($product); return $this->getProductEditTemplateParams($form, $product, $channels, $trees); }
/** * {@inheritdoc} */ public function write(array $products) { $this->collection = $this->documentManager->getDocumentCollection($this->productClass); $productsToInsert = array(); $productsToUpdate = array(); foreach ($products as $product) { if (null === $product->getId()) { $productsToInsert[] = $product; $product->setId($this->mongoFactory->createMongoId()); } else { $productsToUpdate[] = $product; } } $this->mediaManager->handleAllProductsMedias($products); $this->eventDispatcher->dispatch(self::PRE_INSERT, new GenericEvent($productsToInsert)); $this->eventDispatcher->dispatch(self::PRE_UPDATE, new GenericEvent($productsToUpdate)); $insertDocs = $this->getDocsFromProducts($productsToInsert); $updateDocs = $this->getDocsFromProducts($productsToUpdate); if (count($insertDocs) > 0) { $this->insertDocuments($insertDocs); } if (count($updateDocs) > 0) { $this->updateDocuments($updateDocs); } $this->pendingPersister->persistPendingVersions($products); $this->eventDispatcher->dispatch(self::POST_INSERT, new GenericEvent($productsToInsert)); $this->eventDispatcher->dispatch(self::POST_UPDATE, new GenericEvent($productsToUpdate)); $this->documentManager->clear(); $this->cacheClearer->clear(); }
/** * Persists an object * * @param object $object */ protected function persistObject($object) { if ($object instanceof \Pim\Bundle\CatalogBundle\Model\ProductInterface) { $this->mediaManager->handleProductMedias($object); } $this->objectManager->persist($object); $this->doctrineCache->setReference($object); }
/** * TODO: remove this method after the refactoring of the product media manager * * @param ProductInterface $product * @param ProductValueInterface $fromValue * @param ProductValueInterface $toValue */ protected function duplicateMedia(ProductInterface $product, ProductValueInterface $fromValue, ProductValueInterface $toValue) { if (null === $toValue->getMedia()) { $media = $this->mediaFactory->createMedia(); $toValue->setMedia($media); } $this->mediaManager->duplicate($fromValue->getMedia(), $toValue->getMedia(), $this->mediaManager->generateFilenamePrefix($product, $fromValue)); }
/** * @param string $locale * @param string $scope * @param string $exportPath * * @dataProvider getExportPathData */ public function testGetExportPath($locale, $scope, $exportPath) { $media = $this->getMediaMock(); $entity = $this->getProductMock('sku000'); $attribute = $this->getAttributeMock('mockFile', $locale !== '', $scope !== ''); $value = $this->getValueMock($entity, $attribute, $locale, $scope); $media->expects($this->any())->method('getValue')->will($this->returnValue($value)); $media->expects($this->any())->method('getOriginalFilename')->will($this->returnValue('phpunit-file.txt')); $media->expects($this->any())->method('getFilePath')->will($this->returnValue('filePath')); $this->assertEquals($exportPath, $this->manager->getExportPath($media)); }
/** * @param ProductInterface $product */ public function handleMedia(ProductInterface $product) { foreach ($product->getValues() as $value) { if ($media = $value->getMedia()) { if ($id = $media->getCopyFrom()) { $source = $this->objectManager->getRepository('Pim\\Bundle\\CatalogBundle\\Model\\ProductMedia')->find($id); if (!$source) { throw new \Exception(sprintf('Could not find media with id %d', $id)); } $this->mediaManager->duplicate($source, $media, $this->mediaManager->generateFilenamePrefix($product, $value)); } else { $filenamePrefix = $media->getFile() ? $this->mediaManager->generateFilenamePrefix($product, $value) : null; $this->mediaManager->handle($media, $filenamePrefix); } } } }
/** * Get all images of a product normalized. * * @param ProductInterface $product * @param string $sku * @param string $smallImageAttribute * @param string $baseImageAttribute * @param string $thumbnailAttribute * * @return array */ public function getNormalizedImages(ProductInterface $product, $sku = '', $smallImageAttribute = '', $baseImageAttribute = '', $thumbnailAttribute = '') { $imageValues = $product->getValues()->filter(function ($value) { return $value->getData() instanceof AbstractProductMedia && in_array($value->getData()->getMimeType(), array('image/jpeg', 'image/png', 'image/gif')); }); if ($sku === '') { $sku = $product->getIdentifier(); } $images = []; foreach ($imageValues as $imageValue) { $data = $imageValue->getData(); if ($imageData = $this->mediaManager->getBase64($data)) { $imageTypes = array_merge($imageValue->getAttribute()->getCode() == $smallImageAttribute ? [Webservice::SMALL_IMAGE] : [], $imageValue->getAttribute()->getCode() == $baseImageAttribute ? [Webservice::BASE_IMAGE] : [], $imageValue->getAttribute()->getCode() == $thumbnailAttribute ? [Webservice::THUMBNAIL] : []); $images[] = [(string) $sku, ['file' => ['name' => $data->getFilename(), 'content' => $imageData, 'mime' => $data->getMimeType()], 'label' => $data->getFilename(), 'position' => 0, 'types' => $imageTypes, 'exclude' => 0], 0, 'sku']; } } return $images; }
/** * @param ProductInterface[] $products * * @return null * * @deprecated will be removed in 1.4, replaced by MediaManager::handleAllProductsMedias */ public function handleAllMedia(array $products) { return $this->mediaManager->handleAllProductsMedias($products); }