Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/v1.19.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronen-Michaeli-22 committed Aug 29, 2023
2 parents c084d1f + 8f680c9 commit 762a793
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 5 deletions.
130 changes: 130 additions & 0 deletions Controller/Adminhtml/Ajax/UpdateAdminImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
namespace Cloudinary\Cloudinary\Controller\Adminhtml\Ajax;

use Cloudinary\Api\BaseApiClient;
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\ConfigurationBuilder;
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
use Cloudinary\Cloudinary\Core\UrlGenerator;
use Cloudinary\Configuration\Configuration;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\UrlInterface;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\RawFactory as ResultRawFactory;
use Magento\Backend\App\Action\Context;
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
use Magento\Framework\Filesystem as FileSysten;
use Magento\Catalog\Helper\Image as CatalogImageHelper;
use Cloudinary\Cloudinary\Core\Image;
use Cloudinary\Asset\Media;
use Cloudinary\Cloudinary\Core\Image\Transformation;

class UpdateAdminImage extends Action
{
/**
* @var ConfigurationInterface
*/
protected $configuration;
/**
* @var UrlGenerator
*/
protected $urlGenerator;
/**
* @var ImageFactory
*/
protected $imageFactory;
/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var UrlInterface
*/
protected $urlInterface;

protected $resultFactory;

protected $imageContent;

protected $filesystem;

private $_authorised;

protected $configurationBuilder;

protected $transformation;

/**
* @param ImageFactory $imageFactory
* @param UrlGenerator $urlGenerator
* @param ConfigurationInterface $configuration
* @param StoreManagerInterface $storeManager
* @param UrlInterface $urlInterface
*/
public function __construct(
Context $context,
ImageFactory $imageFactory,
UrlGenerator $urlGenerator,
ConfigurationInterface $configuration,
StoreManagerInterface $storeManager,
UrlInterface $urlInterface,
ResultRawFactory $resultFactory,
FileSysten $filesystem,
ConfigurationBuilder $configurationBuilder,
Transformation $transformation
) {
parent::__construct($context);
$this->imageFactory = $imageFactory;
$this->urlGenerator = $urlGenerator;
$this->configuration = $configuration;
$this->storeManager = $storeManager;
$this->urlInterface = $urlInterface;
$this->resultFactory = $resultFactory;
$this->filesystem = $filesystem;
$this->configurationBuilder = $configurationBuilder;
$this->transformation = $transformation;
}

private function authorise()
{
if (!$this->_authorised && $this->configuration->isEnabled()) {
Configuration::instance($this->configurationBuilder->build());
BaseApiClient::$userPlatform = $this->configuration->getUserPlatform();
$this->_authorised = true;
}
}

public function execute()
{
$this->authorise();
if ($this->configuration->isEnabled()) {
try{
$remoteImageUrl = $this->getRequest()->getParam('remote_image');
$filedId = str_replace($this->storeManager->getStore()->getBaseUrl(), '', $remoteImageUrl);

$result = Media::fromParams(
$filedId,
[ 'transformation' => $this->transformation->build(),
'secure' => true,
'sign_url' => $this->configuration->getUseSignedUrls(),
'version' => 1
]
) . '?_i=AB';




} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
}


$response = $this->resultFactory->create();
$response->setHeader('Content-type', 'text/plain');
$response->setContents(json_encode($result));
return $response;
}
}
3 changes: 1 addition & 2 deletions Helper/MediaLibraryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public function getCloudinaryMLshowOptions($resourceType = null, $path = "")
*/
public function getCname()
{
$cname = isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null;
return $cname;
return $this->configuration->isEnabled() && isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudinary/cloudinary-magento2",
"description": "Cloudinary Magento 2 Integration.",
"type": "magento2-module",
"version": "1.19.6",
"version": "1.19.7",
"license": "MIT",
"require": {
"cloudinary/cloudinary_php": ">=2.7 <2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Cloudinary_Cloudinary" setup_version="1.19.6">
<module name="Cloudinary_Cloudinary" setup_version="1.19.7">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
14 changes: 14 additions & 0 deletions view/adminhtml/layout/cms_block_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Backend\Block\Template" template="Cloudinary_Cloudinary::cms/images.phtml" name="cms_block_edit_images_update" />
</referenceContainer>
</body>
</page>
15 changes: 15 additions & 0 deletions view/adminhtml/layout/cms_page_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="editor"/>
<body>
<referenceContainer name="content">
<block class="Magento\Backend\Block\Template" template="Cloudinary_Cloudinary::cms/images.phtml" name="cms_page_edit_images_update" />
</referenceContainer>
</body>
</page>
6 changes: 5 additions & 1 deletion view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var config = {
cloudinarySpinsetModal: 'Cloudinary_Cloudinary/js/cloudinary-spinset-modal',
cldspinsetDialog: 'Cloudinary_Cloudinary/js/cloudinary-spinset-dialog',
productGallery: 'Cloudinary_Cloudinary/js/product-gallery',
cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload'
cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload',
updateCmsImages: 'Cloudinary_Cloudinary/js/cms/preview-update',
}
},
paths: {
Expand All @@ -24,6 +25,9 @@ var config = {
'uiComponent': {
deps: ['jquery']
},
'Cloudinary_Cloudinary/js/cms/preview-update': {
deps: ['jquery']
}
},
config: {
mixins: {
Expand Down
12 changes: 12 additions & 0 deletions view/adminhtml/templates/cms/images.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @var Magento\Backend\Block\Template $block
*/
?>
<script type="text/x-magento-init">
{"*": {
"updateCmsImages": { "ajaxUrl": "<?= $block->getUrl('cloudinary/ajax/updateadminimage') ?>" }
}}

</script>

53 changes: 53 additions & 0 deletions view/adminhtml/web/js/cms/preview-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
define(
['jquery','Magento_PageBuilder/js/events','mage/url'],
function($,_PBEvents, urlBuilder){
'use strict'
return function (config, element) {
var updateHandler = {
images: [],
init: function () {
let self = this;
_PBEvents.on('image:renderAfter', function (event){
let elem = event.element, key = event.id;
let image = $(elem).find('img');
let src = {'remote_image': image.attr('src')};
self.images.push(src);

self.update(key);
});
$('#save-button').on('click', function (e){
self.images.each(function(elem){
if (elem.cld_image) {
let cld_src = elem.cld_image;
let img = $('img[src="' + cld_src +'"]');
return (img.length) ? img.attr('src', elem.remote_image) : '';
}
});
});
},
update: function(key) {
let self = this;
this.images.each(function(elem,ind){
$.ajax({
url: config.ajaxUrl,
type: 'POST',
dataType: 'json',
data: elem,
success: function(image) {
self.images[ind].cld_image = image;
let img = $('img[src="' + self.images[ind].remote_image +'"]');
if (img.length) {
$('img[src="' + self.images[ind].remote_image +'"]').attr('src', self.images[ind].cld_image);
}
},
error: function(xhr, textStatus, errorThrown) {
console.log('Error:', textStatus, errorThrown);
}
});

})
}
};
return updateHandler.init();
}
});

0 comments on commit 762a793

Please sign in to comment.