Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RonenGiritCloudinary committed Aug 20, 2024
2 parents a21195a + 262aa7d commit 7ef16ec
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Helper/ProductGalleryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getCloudinaryPGOptions($refresh = false, $ignoreDisabled = false
$transformation = [];
$videoSettings = $this->configuration->getAllVideoSettings();
$videoFreeParams = $videoSettings['video_free_params'] ?? null;
$videoControls = $videoSettings['controls'] ?? null;
$videoControls = $videoSettings['controls'] ?? "none";
if ($videoFreeParams) {
$config = json_decode($videoFreeParams, true);
$config = array_shift($config);
Expand Down
108 changes: 108 additions & 0 deletions Model/Config/Backend/SwatchUpload.php
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();
}

}
13 changes: 13 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Configuration implements ConfigurationInterface
const CONFIG_PATH_PG_API_QUEUE_LIMIT = 'cloudinary/advanced/product_gallery_api_queue_limit';
const CONFIG_PATH_PG_API_QUEUE_MAX_TRYOUTS = 'cloudinary/advanced/product_gallery_api_queue_max_tryouts';
const CONFIG_PATH_ENABLE_PRODUCT_FREE_TRANSFORMATIONS = 'cloudinary/advanced/enable_product_free_transformations';
const CONFIG_PATH_LOAD_SWATCHES_FROM_CLOUDINARY = 'cloudinary/advanced/load_cloudinary_swatches';

//= Product Gallery
const CONFIG_PATH_PG_ALL = 'cloudinary/product_gallery';
Expand Down Expand Up @@ -179,6 +180,8 @@ class Configuration implements ConfigurationInterface
*/
private $messageManager;

protected $directoryList;

/**
* @param ScopeConfigInterface $configReader
* @param WriterInterface $configWriter
Expand Down Expand Up @@ -494,6 +497,10 @@ public function getAllVideoSettings()
return (array) $this->configReader->getValue(self::CONFIG_PATH_CLD_VIDEO_SETTINGS_ALL);
}

public function isLoadSwatchesFromCloudinary(){
return (bool) $this->configReader->getValue(self::CONFIG_PATH_LOAD_SWATCHES_FROM_CLOUDINARY);
}

public function isEnabledLazyload()
{
return (bool) $this->configReader->getValue(self::XML_PATH_LAZYLOAD_ENABLED);
Expand Down Expand Up @@ -696,6 +703,12 @@ public function getCldVideoAutoplayMode()
return $this->configReader->getValue(self::CONFIG_PATH_CLD_VIDEO_PLAYER_AUTOPLAY);
}

public function mediaRelativePath($filepath)
{
$pubPath = DirectoryList::getPath(DirectoryList::PUB) . DIRECTORY_SEPARATOR;
return (strpos($filepath, $pubPath) === 0) ? str_replace($pubPath, '', $filepath) : $filepath;
}

/**
* Parse Cloudinary URL
* @method parseCloudinaryUrl
Expand Down
128 changes: 128 additions & 0 deletions Plugin/AttributeSavePlugin.php
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;
}
}
Loading

0 comments on commit 7ef16ec

Please sign in to comment.