-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v1.20.3a' of https://github.com/GiritInteractive/magent…
- Loading branch information
Showing
9 changed files
with
399 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
namespace Cloudinary\Cloudinary\Model\Config\Backend; | ||
|
||
|
||
use Cloudinary\Cloudinary\Core\Image; | ||
use Magento\Framework\App\Config\Value; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\ValidatorException; | ||
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Swatches\Model\Swatch; | ||
use Cloudinary\Cloudinary\Model\Configuration; | ||
use Cloudinary\Cloudinary\Core\CloudinaryImageManager; | ||
|
||
|
||
class SwatchUpload extends Value | ||
{ | ||
const ERROR_DEFAULT = 'Error uploading swatches to Cloudinary Media Library.'; | ||
const SWATCH_MEDIA_PATH = 'attribute/swatch'; | ||
|
||
/** | ||
* @var SwatchCollectionFactory | ||
*/ | ||
protected $swatchCollectionFactory; | ||
|
||
protected $_configuration; | ||
|
||
protected $_cldImageManager; | ||
|
||
/** | ||
* @var DirectoryList | ||
*/ | ||
protected $directoryList; | ||
|
||
|
||
/** | ||
* @param SwatchCollectionFactory $swatchCollectionFactory | ||
* @param DirectoryList $directoryList | ||
* @param Configuration $configuration | ||
* @param CloudinaryImageManager $cldImageManager | ||
* @param \Magento\Framework\Model\Context $context | ||
* @param \Magento\Framework\Registry $registry | ||
* @param ScopeConfigInterface $config | ||
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList | ||
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource | ||
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
SwatchCollectionFactory $swatchCollectionFactory, | ||
DirectoryList $directoryList, | ||
Configuration $configuration, | ||
CloudinaryImageManager $cldImageManager, | ||
\Magento\Framework\Model\Context $context, | ||
\Magento\Framework\Registry $registry, | ||
ScopeConfigInterface $config, | ||
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, | ||
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, | ||
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, | ||
array $data = [] | ||
) | ||
{ | ||
$this->swatchCollectionFactory = $swatchCollectionFactory; | ||
$this->directoryList = $directoryList; | ||
$this->_configuration = $configuration; | ||
$this->_cldImageManager = $cldImageManager; | ||
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); | ||
} | ||
|
||
|
||
public function getMediaPath($filepath) | ||
{ | ||
return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . self::SWATCH_MEDIA_PATH . $filepath; | ||
} | ||
/** | ||
* After saving the configuration, upload swatch images to the CDN | ||
* | ||
* @return $this | ||
* @throws LocalizedException | ||
*/ | ||
public function afterSave() | ||
{ | ||
$originalValue = $this->getOldValue(); | ||
// Check if the CDN setting is enabled | ||
if ($this->getvalue() && $this->getValue() != $originalValue) { | ||
// Get all swatches of type 2 (visual swatches) | ||
$swatchCollection = $this->swatchCollectionFactory->create() | ||
->addFieldToFilter('type', ['eq' => Swatch::SWATCH_TYPE_VISUAL_IMAGE]); | ||
|
||
foreach ($swatchCollection as $swatch) { | ||
$file = $swatch->getData('value'); | ||
$imagePath = $this->getMediaPath($file); | ||
$image = $this->_configuration->getMediaBaseUrl() . self::SWATCH_MEDIA_PATH . $swatch->getValue(); | ||
try { | ||
$cldImage = $this->_cldImageManager->uploadAndSynchronise( | ||
Image::fromPath($imagePath) | ||
); | ||
} catch (\Exception $e) { | ||
throw new ValidatorException(self::ERROR_DEFAULT); | ||
} | ||
} | ||
} | ||
|
||
return parent::afterSave(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
namespace Cloudinary\Cloudinary\Plugin; | ||
|
||
use Cloudinary\Cloudinary\Core\CloudinaryImageManager; | ||
use Cloudinary\Cloudinary\Core\Image; | ||
use Cloudinary\Cloudinary\Model\Configuration; | ||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute as AttributeResource; | ||
use Magento\Eav\Model\Entity\Attribute; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Swatches\Model\Swatch; | ||
class AttributeSavePlugin | ||
{ | ||
const SWATCH_MEDIA_PATH = 'attribute/swatch'; | ||
/** | ||
* @var Configuration | ||
*/ | ||
protected $_configuration; | ||
/** | ||
* @var CloudinaryImageManager | ||
*/ | ||
protected $_cldImageManager; | ||
/** | ||
* @var SwatchCollectionFactory | ||
*/ | ||
protected $_swatchFactory; | ||
/** | ||
* @var DirectoryList | ||
*/ | ||
|
||
protected $directoryList; | ||
|
||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
protected $resource; | ||
|
||
/** | ||
* @param Configuration $configuration | ||
* @param CloudinaryImageManager $cldImageManager | ||
* @param SwatchCollectionFactory $swatchFactory | ||
* @param ResourceConnection $resource | ||
* @param DirectoryList $directoryList | ||
*/ | ||
public function __construct( | ||
Configuration $configuration, | ||
CloudinaryImageManager $cldImageManager, | ||
SwatchCollectionFactory $swatchFactory, | ||
ResourceConnection $resource, | ||
DirectoryList $directoryList | ||
|
||
) | ||
{ | ||
$this->_configuration = $configuration; | ||
$this->_cldImageManager = $cldImageManager; | ||
$this->_swatchFactory = $swatchFactory; | ||
$this->resource = $resource; | ||
$this->directoryList = $directoryList; | ||
|
||
} | ||
|
||
|
||
/** | ||
* @param $filepath | ||
* @return string | ||
* @throws \Magento\Framework\Exception\FileSystemException | ||
*/ | ||
public function getMediaPath($filepath) | ||
{ | ||
return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . self::SWATCH_MEDIA_PATH . $filepath; | ||
} | ||
|
||
|
||
|
||
|
||
/** | ||
* After save plugin to update swatch image URLs | ||
* | ||
* @param AttributeResource $subject | ||
* @param Attribute $attribute | ||
* @return Attribute | ||
*/ | ||
public function afterSave(AttributeResource $subject, Attribute $attribute) | ||
{ | ||
if ($this->_configuration->isEnabled() && $this->_configuration->isLoadSwatchesFromCloudinary()) { | ||
|
||
if ($attribute->getFrontendInput() === 'select' && $attribute->getData('swatch_input_type') === 'visual') { | ||
$connection = $this->resource->getConnection(); | ||
$optionTable = $connection->getTableName('eav_attribute_option'); | ||
|
||
// Get all option IDs for the current attribute | ||
$optionIds = $connection->fetchCol( | ||
$connection->select() | ||
->from($optionTable, 'option_id') | ||
->where('attribute_id = ?', $attribute->getId()) | ||
); | ||
|
||
if (!empty($optionIds)) { | ||
$swatchCollection = $this->_swatchFactory->create() | ||
->addFieldToFilter('option_id', ['in' => $optionIds]) | ||
->addFieldToFilter('type', ['eq' => Swatch::SWATCH_TYPE_VISUAL_IMAGE]); | ||
|
||
foreach ($swatchCollection as $swatch) { | ||
if ( $swatch->getValue()) { | ||
// Visual Image means | ||
$imagePath = $this->getMediaPath($swatch->getValue()); | ||
$image = $this->_configuration->getMediaBaseUrl() . self::SWATCH_MEDIA_PATH . $swatch->getValue(); | ||
try { | ||
$cldImage = $this->_cldImageManager->uploadAndSynchronise( | ||
Image::fromPath($imagePath) | ||
); | ||
} catch (\Exception $e) { | ||
throw new LocalizedException($e->getMessage()); | ||
} | ||
|
||
//$swatch->save() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $attribute; | ||
} | ||
} |
Oops, something went wrong.