Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ee version #8

Open
wants to merge 9 commits into
base: main-ee
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
use Magento\MediaStorage\Model\File\Uploader as FileUploader;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Scaleflex\Filerobot\Model\FilerobotConfig;
use Magento\Catalog\Model\Product\Gallery\CreateHandler as BaseCreateHandler;

class CreateHandlerOverride implements ExtensionInterface
class CreateHandler extends BaseCreateHandler
{
/**
* @var \Magento\Framework\EntityManager\EntityMetadata
Expand Down
126 changes: 126 additions & 0 deletions Model/Product/Gallery/Staging/CreateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Scaleflex\Filerobot\Model\Product\Gallery\Staging;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Gallery\CopyHandler;
use Scaleflex\Filerobot\Model\FilerobotConfig;
use Scaleflex\Filerobot\Model\Product\Gallery\CreateHandler as ProductGalleryCreateHandler;
use Scaleflex\Filerobot\Model\Product\Gallery\UpdateHandler;
use Magento\Catalog\Model\Product\Media\Config;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Json\Helper\Data;
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\Staging\Model\ResourceModel\Db\ReadEntityVersion;
use Magento\Staging\Model\VersionHistoryInterface;
use Magento\Staging\Model\VersionManager;


class CreateHandler extends ProductGalleryCreateHandler implements ExtensionInterface
{
/**
* @var UpdateHandler
*/
private $updateHandler;

/**
* @var CopyHandler
*/
private $copyHandler;

/**
* @var VersionHistoryInterface
*/
private $versionHistory;

/**
* @var ReadEntityVersion
*/
private $readEntityVersion;

/**
* @param MetadataPool $metadataPool
* @param ProductAttributeRepositoryInterface $attributeRepository
* @param Gallery $resourceModel
* @param Data $jsonHelper
* @param Config $mediaConfig
* @param Filesystem $filesystem
* @param Database $fileStorageDb
* @param UpdateHandler $updateHandler
* @param CopyHandler|null $copyHandler
* @param VersionHistoryInterface|null $versionHistory
* @param ReadEntityVersion|null $readEntityVersion
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MetadataPool $metadataPool,
ProductAttributeRepositoryInterface $attributeRepository,
Gallery $resourceModel,
Data $jsonHelper,
Config $mediaConfig,
Filesystem $filesystem,
Database $fileStorageDb,
UpdateHandler $updateHandler,
FilerobotConfig $filerobotConfig,
?CopyHandler $copyHandler = null,
?VersionHistoryInterface $versionHistory = null,
?ReadEntityVersion $readEntityVersion = null
) {
$this->updateHandler = $updateHandler;

parent::__construct(
$metadataPool,
$attributeRepository,
$resourceModel,
$jsonHelper,
$mediaConfig,
$filesystem,
$fileStorageDb,
$filerobotConfig,
);
$this->copyHandler = $copyHandler ?? ObjectManager::getInstance()->get(CopyHandler::class);
$this->versionHistory = $versionHistory ?? ObjectManager::getInstance()->get(VersionHistoryInterface::class);
$this->readEntityVersion = $readEntityVersion ?? ObjectManager::getInstance()->get(ReadEntityVersion::class);
}

/**
* Execute create handler
*
* @param Product $product
* @param array $arguments
* @return bool|object
* @throws LocalizedException
*/
public function execute($product, $arguments = [])
{
if (isset($arguments['origin_in'])) {
$originId = $arguments['origin_in'];
} elseif (isset($arguments['copy_origin_in'])) {
$originId = $arguments['copy_origin_in'];
} else {
$originId = $this->versionHistory->getCurrentId();
}

if (!in_array($product->getData('created_in'), [VersionManager::MIN_VERSION, $originId], true)) {
$arguments['original_link_id'] = $this->readEntityVersion->getVersionRowId(
ProductInterface::class,
$product->getData($this->metadata->getIdentifierField()),
$originId
);
$this->copyHandler->execute($product, $arguments);
if (!empty($arguments['is_rollback'])) {
return $product;
}
}
return $product->isObjectNew()
? parent::execute($product, $arguments)
: $this->updateHandler->execute($product, $arguments);
}
}
147 changes: 147 additions & 0 deletions Model/Product/Gallery/Staging/UpdateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace Scaleflex\Filerobot\Model\Product\Gallery\Staging;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Gallery\CopyHandler;
use Magento\Catalog\Model\Product\Gallery\DeleteHandler;
use Magento\Catalog\Model\Product\Gallery\ReadHandler;
use Magento\Catalog\Model\Product\Media\Config;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Eav\Model\ResourceModel\AttributeValue;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Filesystem;
use Magento\Framework\Json\Helper\Data;
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\Staging\Model\ResourceModel\Db\ReadEntityVersion;
use Magento\Store\Model\StoreManagerInterface;
use Scaleflex\Filerobot\Model\FilerobotConfig;
use Scaleflex\Filerobot\Model\Product\Gallery\UpdateHandler as BaseUpdateHandler;

class UpdateHandler extends BaseUpdateHandler
{
/**
* @var CopyHandler
*/
private $copyHandler;

/**
* @var DeleteHandler
*/
private $deleteHandler;

/**
* @var ReadHandler
*/
private $readHandler;

/**
* @var ReadEntityVersion
*/
private $readEntityVersion;

/**
* @var string[]
*/
private $mediaAttributesWithLabels = [
'image',
'small_image',
'thumbnail'
];

/**
* @param MetadataPool $metadataPool
* @param ProductAttributeRepositoryInterface $attributeRepository
* @param Gallery $resourceModel
* @param Data $jsonHelper
* @param Config $mediaConfig
* @param Filesystem $filesystem
* @param Database $fileStorageDb
* @param StoreManagerInterface $storeManager
* @param AttributeValue $attributeValue
* @param CopyHandler $copyHandler
* @param DeleteHandler $deleteHandler
* @param ReadHandler $readHandler
* @param ReadEntityVersion $readEntityVersion
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MetadataPool $metadataPool,
ProductAttributeRepositoryInterface $attributeRepository,
Gallery $resourceModel,
Data $jsonHelper,
Config $mediaConfig,
Filesystem $filesystem,
Database $fileStorageDb,
StoreManagerInterface $storeManager,
AttributeValue $attributeValue,
CopyHandler $copyHandler,
DeleteHandler $deleteHandler,
ReadHandler $readHandler,
ReadEntityVersion $readEntityVersion,
FilerobotConfig $filerobotConfig
) {
parent::__construct(
$metadataPool,
$attributeRepository,
$resourceModel,
$jsonHelper,
$mediaConfig,
$filesystem,
$filerobotConfig,
$fileStorageDb,
$storeManager,
$attributeValue
);
$this->copyHandler = $copyHandler;
$this->deleteHandler = $deleteHandler;
$this->readHandler = $readHandler;
$this->readEntityVersion = $readEntityVersion;
}

/**
* Update product media gallery
*
* @param Product $product
* @param array $arguments
* @return object
*/
public function execute($product, $arguments = [])
{
if (!empty($arguments['is_rollback']) && !empty($arguments['copy_origin_in'])) {
$arguments['media_attribute_codes'] = $this->getMediaAttributeCodes();
$arguments['original_link_id'] = $this->readEntityVersion->getVersionRowId(
ProductInterface::class,
$product->getData($this->metadata->getIdentifierField()),
$arguments['copy_origin_in']
);
$this->deleteHandler->execute($product, $arguments);
$this->copyHandler->execute($product, $arguments);
// reload gallery data as new value IDs are auto generated
$this->readHandler->execute($product, $arguments);
return $product;
}
return parent::execute($product, $arguments);
}

/**
* Get all media attributes codes including their corresponding labels
*
* @return array
*/
private function getMediaAttributeCodes(): array
{
$attributeCodes = [];
foreach ($this->mediaConfig->getMediaAttributeCodes() as $attributeCode) {
$attributeCodes[] = $attributeCode;
if (in_array($attributeCode, $this->mediaAttributesWithLabels)) {
$attributeCodes[] = $attributeCode . '_label';
}
}
return $attributeCodes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
use Scaleflex\Filerobot\Model\FilerobotConfig;
use Scaleflex\Filerobot\Model\Product\Gallery\CreateHandlerOverride;
use Magento\Catalog\Model\Product\Media\Config;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Eav\Model\ResourceModel\AttributeValue;
Expand All @@ -18,7 +17,7 @@
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

class UpdateHandlerOverride extends CreateHandlerOverride
class UpdateHandler extends CreateHandler
{
/**
* @var AttributeValue
Expand Down
2 changes: 1 addition & 1 deletion Plugin/AddImageToGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function afterGetGalleryImages(Gallery $gallery, $images)
foreach ($images as $image) {
if ($this->fileRobotConfig->isFilerobot($image->getData('file'))) {
$url = $image->getData('file');
$entityId = $image->getData('entity_id');
$entityId = $image->getData('entity_id') ? $image->getData('entity_id') : $image->getData('row_id');
$product = $this->productRepository->getById($entityId);
if ($product) {
$thumbImageSize = $this->imageHelper->init($product, 'product_thumbnail_image');
Expand Down
20 changes: 11 additions & 9 deletions Ui/DataProvider/Product/Form/Modifier/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ public function afterModifyData(
) {
foreach ($result as $key => $value) {
if ($key !== 'config') {
$links = $value['links'];
foreach ($links as &$link) {
if (!empty($link)) {
foreach ($link as &$linked) {
$product = $this->productRepository->getById($linked['id']);
$images = $product->getMediaAttributeValues();
if (!empty($images) && $images['thumbnail'] && $this->fileRobotConfig->isFilerobot($images['thumbnail'])) {
$linked['thumbnail'] = $images['thumbnail'];
if (array_key_exists('links', $value)) {
$links = $value['links'];
foreach ($links as &$link) {
if (!empty($link)) {
foreach ($link as &$linked) {
$product = $this->productRepository->getById($linked['id']);
$images = $product->getMediaAttributeValues();
if (!empty($images) && $images['thumbnail'] && $this->fileRobotConfig->isFilerobot($images['thumbnail'])) {
$linked['thumbnail'] = $images['thumbnail'];
}
}
}
}
$result[$key]['links'] = $links;
}
$result[$key]['links'] = $links;
}
}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scaleflex/module-filerobot",
"description": "Scaleflex Filerobot Magento Extension",
"name": "scaleflex/module-filerobot-enterprise",
"description": "Scaleflex Filerobot Magento Enterprise Extension",
"type": "magento2-module",
"license": "GPL-3.0",
"authors": [
Expand Down
8 changes: 6 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Product\Gallery\CreateHandler"
type="Scaleflex\Filerobot\Model\Product\Gallery\CreateHandlerOverride" />
type="Scaleflex\Filerobot\Model\Product\Gallery\CreateHandler" />
<preference for="Magento\Catalog\Model\Product\Gallery\UpdateHandler"
type="Scaleflex\Filerobot\Model\Product\Gallery\UpdateHandlerOverride" />
type="Scaleflex\Filerobot\Model\Product\Gallery\UpdateHandler" />
<preference for="Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail"
type="Scaleflex\Filerobot\Ui\Component\Listing\Columns\Thumbnail" />
<preference for="Magento\CatalogStaging\Model\Product\Gallery\CreateHandler"
type="Scaleflex\Filerobot\Model\Product\Gallery\Staging\CreateHandler" />
<preference for="Magento\CatalogStaging\Model\Product\Gallery\UpdateHandler"
type="Scaleflex\Filerobot\Model\Product\Gallery\Staging\UpdateHandler" />
</config>
2 changes: 2 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?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="Scaleflex_Filerobot" setup_version="1.0.0">
<module name="Magento_Eav"/>
<module name="Magento_Ui"/>
<module name="Magento_Catalog"/>
<module name="Magento_CatalogStaging"/>
<module name="Magento_Customer"/>
<module name="Magento_Store"/>
</module>
Expand Down
Loading