/** * Return assets by id for given attributes. * * @param array $attributesByTag * @param string $locale * * @return array */ private function preloadMedias($attributesByTag, $locale) { $ids = array_unique(array_values(array_map(function ($attributes) { return $attributes['id']; }, $attributesByTag))); $medias = $this->mediaRepository->findMediaDisplayInfo($ids, $locale); $result = []; foreach ($medias as $media) { $media['url'] = $this->mediaManager->getUrl($media['id'], $media['name'], $media['version']); $result[$media['id']] = $media; } return $result; }
/** * {@inheritdoc} */ public function move($id, $locale, $destCollection) { try { $mediaEntity = $this->mediaRepository->findMediaById($id); if ($mediaEntity === null) { throw new MediaNotFoundException($id); } $mediaEntity->setCollection($this->em->getReference(self::ENTITY_NAME_COLLECTION, $destCollection)); $this->em->flush(); return $this->addFormatsAndUrl(new Media($mediaEntity, $locale, null)); } catch (DBALException $ex) { throw new CollectionNotFoundException($destCollection); } }
/** * Sets the medias of the given contact to the given medias. * Currently associated medias are replaced. * * @param Contact $contact * @param $medias * * @throws EntityNotFoundException */ private function setMedias(Contact $contact, $medias) { $mediaIds = array_map(function ($media) { return $media['id']; }, $medias); $foundMedias = $this->mediaRepository->findById($mediaIds); $foundMediaIds = array_map(function ($mediaEntity) { return $mediaEntity->getId(); }, $foundMedias); if ($missingMediaIds = array_diff($mediaIds, $foundMediaIds)) { throw new EntityNotFoundException($this->mediaRepository->getClassName(), reset($missingMediaIds)); } $contact->getMedias()->clear(); foreach ($foundMedias as $media) { $contact->addMedia($media); } }
/** * @param int $id * @param string $locale * * @return array */ protected function getPreview($id, $locale) { $medias = $this->mediaRepository->findMedia(['collection' => $id, 'paginator' => false], 1); if (count($medias) > 0) { $media = $medias[0]; foreach ($media->getFiles() as $file) { foreach ($file->getFileVersions() as $fileVersion) { if ($fileVersion->getVersion() == $file->getVersion()) { $format = $this->getPreviewsFromFileVersion($media->getId(), $fileVersion, $locale); if (!empty($format)) { return $format; } } } } } return; }