|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of the Magebit_StaticContentProcessor package |
| 4 | + * |
| 5 | + * DISCLAIMER |
| 6 | + * |
| 7 | + * Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor |
| 8 | + * to newer versions in the future. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) 2021 Magebit (https://magebit.com/) |
| 11 | + * @author Liene Tunne <[email protected]> |
| 12 | + * @license GNU General Public License ("GPL") v3.0 |
| 13 | + * |
| 14 | + * For the full copyright and license information, please view the LICENSE |
| 15 | + * file that was distributed with this source code. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category; |
| 19 | + |
| 20 | +use Magento\Framework\UrlInterface; |
| 21 | +use Magento\Store\Model\ScopeInterface; |
| 22 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 23 | +use Divante\VsbridgeIndexerCore\Api\DataProviderInterface; |
| 24 | +use Magento\Catalog\Model\Config; |
| 25 | +use Magento\Framework\Exception\LocalizedException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class WysiwygBlockData |
| 29 | + */ |
| 30 | +class ImageUrlRewrite implements DataProviderInterface |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Entity code |
| 34 | + */ |
| 35 | + const ENTITY = 'catalog_category'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ScopeConfigInterface |
| 39 | + */ |
| 40 | + private $scopeConfig; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Config |
| 44 | + */ |
| 45 | + private $catalogConfig; |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * ImageUrlRewrite constructor. |
| 50 | + * @param ScopeConfigInterface $scopeConfig |
| 51 | + * @param Config $catalogConfig |
| 52 | + */ |
| 53 | + public function __construct( |
| 54 | + ScopeConfigInterface $scopeConfig, |
| 55 | + Config $catalogConfig |
| 56 | + ) { |
| 57 | + $this->scopeConfig = $scopeConfig; |
| 58 | + $this->catalogConfig = $catalogConfig; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array $indexData |
| 63 | + * @param int $storeId |
| 64 | + * @return array |
| 65 | + * @throws LocalizedException |
| 66 | + */ |
| 67 | + public function addData(array $indexData, $storeId): array |
| 68 | + { |
| 69 | + $processable = []; |
| 70 | + |
| 71 | + $rewriteCatMediaUrl = $this->scopeConfig->getValue( |
| 72 | + 'vsbridge_indexer_settings/url_rewrites/parse_category_media', |
| 73 | + ScopeInterface::SCOPE_STORE |
| 74 | + ); |
| 75 | + |
| 76 | + if ($rewriteCatMediaUrl) { |
| 77 | + foreach ($indexData as $categoryId => $categoryData) { |
| 78 | + foreach ($categoryData as $attributeCode => $attribute) { |
| 79 | + |
| 80 | + if ($this->checkAttributeIsImage($attributeCode)) { |
| 81 | + $processable[] = [ |
| 82 | + 'categoryId' => $categoryId, |
| 83 | + 'attributeId' => $attributeCode, |
| 84 | + 'content' => $attribute |
| 85 | + ]; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + $filteredIndexData = $this->convertMediaUrl($processable); |
| 91 | + |
| 92 | + foreach ($filteredIndexData as $data) { |
| 93 | + $indexData[$data['categoryId']][$data['attributeId']] = $data['content']; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $indexData; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param string|null $attributeCode |
| 102 | + * @return bool |
| 103 | + * @throws LocalizedException |
| 104 | + */ |
| 105 | + protected function checkAttributeIsImage(?string $attributeCode): bool |
| 106 | + { |
| 107 | + $attributeData = $this->catalogConfig->getAttribute(self::ENTITY, $attributeCode); |
| 108 | + |
| 109 | + return $attributeData->getFrontendInput() == 'image' ? true : false; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param array $data |
| 114 | + * @return array |
| 115 | + */ |
| 116 | + protected function convertMediaUrl(array $data): array |
| 117 | + { |
| 118 | + $urlRewritesMedia = $this->scopeConfig->getValue( |
| 119 | + 'vsbridge_indexer_settings/url_rewrites/media_url', |
| 120 | + ScopeInterface::SCOPE_STORE |
| 121 | + ); |
| 122 | + |
| 123 | + foreach ($data as &$attribute) { |
| 124 | + // Remove store Url |
| 125 | + $cleanVSFMediaPath = substr($urlRewritesMedia, strpos($urlRewritesMedia, '/media')); |
| 126 | + $attribute['content'] = str_replace('/media', $cleanVSFMediaPath, $attribute['content']); |
| 127 | + } |
| 128 | + |
| 129 | + return $data; |
| 130 | + } |
| 131 | +} |
0 commit comments