_layout = $layout; $this->_mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA); $this->_storeManager = $storeManager; $this->_productVideos = []; parent::__construct($context); } /** * @param array $attributes * @return string */ public static function prepareAttributes(array $attributes) { $escaper = ObjectManager::getInstance()->get(Escaper::class); $attributes = array_filter($attributes); if (empty($attributes)) { return ''; } $html = ''; foreach ($attributes as $attributeName => $attributeValue) { if (is_bool($attributeValue)) { if ($attributeValue) { $html .= sprintf( ' %s', $attributeName ); } } else { $html .= sprintf( ' %s="%s"', $attributeName, $escaper->escapeHtmlAttr($attributeValue) ); } } return $html; } /** * @param Product $product * @return bool */ public function issetVideo($product) { $files = $this->_getVideo($product); return !empty($files); } /** * @param Product $product * @return string[] */ private function _getVideo($product) { if (!$product) { return []; } $productId = $product->getId(); if (!array_key_exists($productId, $this->_productVideos)) { $this->_productVideos[$productId] = []; } if (empty($this->_productVideos[$productId])) { $videoPath = (string)$this->getProductData($product, 'ox_gallery_video'); $videoPath = ltrim($videoPath, '\\\/'); if (!empty($videoPath)) { $videoAbsolutePath = $this->getAbsolutePath($videoPath); $videoReplaceAbsolutePath = preg_replace('#\.[a-z0-9]{3,}$#i', '', $videoAbsolutePath); $this->_productVideos[$productId] = Glob::glob($videoReplaceAbsolutePath . '\.*'); if (empty($this->_productVideos[$productId])) { $this->_productVideos[$productId] = []; if (file_exists($videoAbsolutePath)) { $this->_productVideos[$productId][] = $videoAbsolutePath; } } } } return $this->_productVideos[$productId]; } /** * @param Product $product * @param string $key * @return mixed|null */ private function getProductData($product, $key) { $productId = $product->getId(); $data = $product->getData($key); if (null === $data) { $product = $product->load($productId); if ($product->getId() == $productId) { $data = $product->getData($key); } } return $data; } /** * @param string $path * @return string */ protected function getAbsolutePath( $path = '' ) { return $this->_mediaDirectory->getAbsolutePath($path); } /** * @param Product $product * @return bool */ public function stopOnClick($product) { return (bool)$this->getProductData($product, 'ox_gallery_video_stop_on_click'); } /** * @param Product $product * @return bool */ public function loopVideo($product) { return (bool)$this->getProductData($product, 'ox_gallery_video_loop'); } /** * @param Product $product * @return bool */ public function videoPosition($product) { return (int)$this->getProductData($product, 'ox_gallery_video_index') ?: 2; } /** * @param Product $product * @return bool */ public function videoOnHover($product) { return (bool)$this->getProductData($product, 'ox_gallery_video_listing_hover'); } /** * @param Product $product * @return bool */ public function autoplayVideo($product) { return (bool)$this->getProductData($product, 'ox_gallery_video_autoplay'); } /** * @param Product $product * @return bool */ public function showControls($product) { return (bool)$this->getProductData($product, 'ox_gallery_video_controls'); } /** * @param Product $product * @param array $config * @return string * @throws NoSuchEntityException */ public function getVideo($product, $config = []) { $files = $this->_getVideo($product); if (!empty($files)) { $mimeFiles = []; foreach ($files as $file) { $fileMime = $this->_detectMimeType($file); $mimeFiles[$fileMime] = $this->getUrlPath($file); } if (!empty($mimeFiles)) { uksort($mimeFiles, [$this, 'sortByMime']); return $this->getLayout() ->createBlock( Template::class, '', [ 'data' => array_replace( [ 'mime_files' => $mimeFiles, 'product' => $product, ], $config ), ] ) ->setTemplate(static::TEMPLATE_VIDEO) ->toHtml(); } } return ''; } /** * Internal method to detect the mime type of a file * * @param string $file File * @return string Mimetype of given file */ protected function _detectMimeType($file) { $result = ''; if (class_exists('finfo', false)) { $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; $mime = @finfo_open($const); if (!empty($mime)) { $result = finfo_file($mime, $file); } unset($mime); } if (empty($result) && (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) { $result = mime_content_type($file); } if (empty($result)) { $result = 'application/octet-stream'; } return $result; } /** * @param string $path * @return string * @throws NoSuchEntityException */ protected function getUrlPath( $path = '' ) { $path = str_replace($this->getAbsolutePath(), '', $path); $path = $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $path; return $path; } /** * @return LayoutInterface */ public function getLayout() { return $this->_layout; } /** * @param string $itemPrev * @param string $itemNext * @return int */ public function sortByMime( $itemPrev, $itemNext ) { $indexPrev = in_array($itemPrev, static::ORDER_MIME) ? array_search($itemPrev, static::ORDER_MIME) : 9999; $indexNext = in_array($itemNext, static::ORDER_MIME) ? array_search($itemNext, static::ORDER_MIME) : 9999; if ($indexPrev == $indexNext) { return 0; } elseif ($indexPrev > $indexNext) { return 1; } return -1; } }