/** * {@inheritdoc} */ public function findByFilters($filters, $page, $pageSize, $limit, $locale, $options = []) { if (!array_key_exists('dataSource', $filters) || $filters['dataSource'] === '' || $limit !== null && $limit < 1) { return []; } if ($filters['dataSource'] === 'root') { // if root collection is selected remove filter for data-source $filters['dataSource'] = null; } $entities = $this->parentFindByFilters($filters, $page, $pageSize, $limit, $locale, $options); return array_map(function (Media $media) use($locale) { return $this->mediaManager->addFormatsAndUrl(new MediaApi($media, $locale)); }, $entities); }
/** * Returns the url for the image. * * @param $data * @param $locale * * @throws \RuntimeException * @throws \InvalidArgumentException */ private function getImageUrl($data, $locale) { // new structures will container an instance of MediaSelectionContainer if ($data instanceof MediaSelectionContainer) { $medias = $data->getData('de'); // old ones an array ... } else { if (!isset($data['ids'])) { throw new \RuntimeException(sprintf('Was expecting media value to contain array key "ids", got: "%s"', print_r($data, true))); } $medias = $this->mediaManager->get($locale, ['ids' => $data['ids']]); } // no media, no thumbnail URL if (!$medias) { return; } $media = current($medias); if (!$media) { return; } $formats = $media->getThumbnails(); if (!isset($formats[$this->searchImageFormat])) { throw new \InvalidArgumentException(sprintf('Search image format "%s" is not known', $this->searchImageFormat)); } return $formats[$this->searchImageFormat]; }
/** * @param string $locale * * @return Media[] */ private function loadData($locale) { if (!empty($this->ids)) { return $this->mediaManager->getByIds($this->ids, $locale); } else { return []; } }
private function resolveMediaObject($media, $locale) { if ($media instanceof MediaEntity) { return $this->mediaManager->addFormatsAndUrl(new MediaApi($media, $locale)); } elseif ($media instanceof MediaApi) { return $this->mediaManager->addFormatsAndUrl($media); } return; }
/** * Return the image URL for the given media. * * TODO: The media API needs to be improved here. */ private function getImageUrl($media, $locale) { $mediaApi = new Media($media, $locale); $this->mediaManager->addFormatsAndUrl($mediaApi); $formats = $mediaApi->getThumbnails(); if (!isset($formats[$this->searchImageFormat])) { $this->logger->warning(sprintf('Media with ID "%s" does not have thumbnail format "%s". This thumbnail would be used by the search results.', $media->getId(), $this->searchImageFormat)); return; } return $formats[$this->searchImageFormat]; }
/** * 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 delete($mediaId, $formatKey) { if (!isset($this->formats[$formatKey])) { throw new FormatNotFoundException($formatKey); } $media = $this->mediaManager->getEntityById($mediaId); $fileVersion = $this->getFileVersionForMedia($media); $formatOptions = $fileVersion->getFormatOptions()->get($formatKey); if (isset($formatOptions)) { $fileVersion->getFormatOptions()->remove($formatKey); $fileVersion->increaseSubVersion(); $this->em->remove($formatOptions); $this->em->persist($fileVersion); $this->purgeMedia($mediaId, $fileVersion); } }