-
Notifications
You must be signed in to change notification settings - Fork 90
#359: added Locate button for saved preview images #406
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
Merged
sivaschenko
merged 10 commits into
magento:develop
from
vovsky:359-locate-saved-preview-image
Oct 1, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09f1d5d
#359: added Locate button for saved preview images
vovsky 55dd365
#359: locate downloaded image in media browser
vovsky e312123
Merge branch 'develop' into 359-locate-saved-preview-image
vovsky f815215
#359 attempt to locate image in folder
vovsky 3430c50
Merge branch 'develop' of github.com:magento/adobe-stock-integration …
sivaschenko 020b861
#359 fixed case with image location in subfolders
vovsky d5e4e2d
Merge branch '359-locate-saved-preview-image' of github.com:vovsky/ad…
vovsky 8bad080
Merge branch 'develop' into 359-locate-saved-preview-image
sivaschenko 97d1343
magento/adobe-stock-integration#406: Extracted loading from db and lo…
sivaschenko caf2c35
magento/adobe-stock-integration#406: Fixed static tests
sivaschenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,125 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\AdobeStockAsset\Model; | ||
|
||
use Magento\AdobeStockAsset\Model\ResourceModel\Asset\LoadByIds; | ||
use Magento\Framework\Api\AttributeValueFactory; | ||
use Magento\Framework\Api\Search\DocumentInterface; | ||
use Magento\Framework\Api\Search\SearchResultInterface; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
/** | ||
* Class AddIsDownloadedToSearchResult | ||
*/ | ||
class AppendAttributes | ||
{ | ||
const ATTRIBUTE_CODE_IS_DOWNLOADED = 'is_downloaded'; | ||
const ATTRIBUTE_CODE_PATH = 'path'; | ||
|
||
/** | ||
* @var AttributeValueFactory | ||
*/ | ||
private $attributeValueFactory; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var LoadByIds | ||
*/ | ||
private $loadByIds; | ||
|
||
/** | ||
* AppendAttributes constructor. | ||
* @param ResourceConnection $resourceConnection | ||
* @param AttributeValueFactory $attributeValueFactory | ||
* @param LoadByIds $loadByIds | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection, | ||
AttributeValueFactory $attributeValueFactory, | ||
LoadByIds $loadByIds | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
$this->attributeValueFactory = $attributeValueFactory; | ||
$this->loadByIds = $loadByIds; | ||
} | ||
|
||
/** | ||
* Add additional asset attributes | ||
* | ||
* @param SearchResultInterface $searchResult | ||
* @return SearchResultInterface | ||
*/ | ||
public function execute(SearchResultInterface $searchResult): SearchResultInterface | ||
{ | ||
$items = $searchResult->getItems(); | ||
|
||
if (empty($items)) { | ||
return $searchResult; | ||
} | ||
|
||
$ids = array_map( | ||
function ($item) { | ||
return $item->getId(); | ||
}, | ||
$items | ||
); | ||
|
||
$assets = $this->loadByIds->execute($ids); | ||
|
||
foreach ($items as $key => $item) { | ||
if (!isset($assets[$item->getId()])) { | ||
$this->addAttributes( | ||
$item, | ||
[ | ||
self::ATTRIBUTE_CODE_IS_DOWNLOADED => 0, | ||
self::ATTRIBUTE_CODE_PATH => '' | ||
] | ||
); | ||
continue; | ||
} | ||
|
||
$this->addAttributes( | ||
$item, | ||
[ | ||
self::ATTRIBUTE_CODE_IS_DOWNLOADED => 1, | ||
self::ATTRIBUTE_CODE_PATH => $assets[$item->getId()]->getPath() | ||
] | ||
); | ||
} | ||
|
||
return $searchResult; | ||
} | ||
|
||
/** | ||
* Add attributes to document | ||
* | ||
* @param DocumentInterface $document | ||
* @param array $attributes [code => value] | ||
* @return DocumentInterface | ||
*/ | ||
private function addAttributes(DocumentInterface $document, array $attributes): DocumentInterface | ||
{ | ||
$customAttributes = $document->getCustomAttributes(); | ||
|
||
foreach ($attributes as $code => $value) { | ||
$attribute = $this->attributeValueFactory->create(); | ||
$attribute->setAttributeCode($code); | ||
$attribute->setValue($value); | ||
$customAttributes[$code] = $attribute; | ||
} | ||
|
||
$document->setCustomAttributes($customAttributes); | ||
|
||
return $document; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\AdobeStockAsset\Model\ResourceModel\Asset; | ||
|
||
use Magento\AdobeStockAssetApi\Api\Data\AssetInterfaceFactory; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\AdobeStockAssetApi\Api\Data\AssetInterface; | ||
use Magento\AdobeStockAsset\Model\ResourceModel\Asset as AssetResourceModel; | ||
use Magento\Framework\EntityManager\Hydrator; | ||
|
||
/** | ||
* Save multiple asset service. | ||
*/ | ||
class LoadByIds | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var Hydrator $hydrator | ||
*/ | ||
private $hydrator; | ||
|
||
/** | ||
* @var AssetInterfaceFactory | ||
*/ | ||
private $factory; | ||
|
||
/** | ||
* @param AssetInterfaceFactory $factory | ||
* @param ResourceConnection $resourceConnection | ||
* @param Hydrator $hydrate | ||
*/ | ||
public function __construct( | ||
AssetInterfaceFactory $factory, | ||
ResourceConnection $resourceConnection, | ||
Hydrator $hydrate | ||
) { | ||
$this->factory = $factory; | ||
$this->resourceConnection = $resourceConnection; | ||
$this->hydrator = $hydrate; | ||
} | ||
|
||
/** | ||
* Load assets by ids | ||
* | ||
* @param \int[] $ids | ||
* @return AssetInterface[] | ||
*/ | ||
public function execute(array $ids): array | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$select = $connection->select() | ||
->from(AssetResourceModel::ADOBE_STOCK_ASSET_TABLE_NAME) | ||
->where('id in (?)', $ids); | ||
$data = $connection->fetchAssoc($select); | ||
|
||
$assets = []; | ||
|
||
foreach ($data as $id => $assetData) { | ||
$asset = $this->factory->create(); | ||
$assets[$id] = $this->hydrator->hydrate($asset, $assetData); | ||
} | ||
|
||
return $assets; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -10,12 +10,13 @@ define([ | |
'Magento_AdobeIms/js/action/authorization', | ||
'Magento_AdobeUi/js/components/grid/column/image-preview', | ||
'Magento_AdobeStockImageAdminUi/js/model/messages', | ||
'Magento_AdobeStockImageAdminUi/js/media-gallery', | ||
'Magento_Ui/js/modal/confirm', | ||
'Magento_Ui/js/modal/prompt', | ||
'Magento_AdobeIms/js/user', | ||
'Magento_AdobeStockAdminUi/js/config', | ||
'mage/backend/tabs' | ||
], function (_, $, ko, translate, authorizationAction, imagePreview, messages, confirmation, prompt, user, config) { | ||
], function (_, $, ko, translate, authorizationAction, imagePreview, messages, mediaGallery, confirmation, prompt, user, config) { | ||
'use strict'; | ||
|
||
return imagePreview.extend({ | ||
|
@@ -320,6 +321,20 @@ define([ | |
this.chipInputValue(keyword); | ||
}, | ||
|
||
/** | ||
* Returns is_downloaded flag as observable for given record | ||
* | ||
* @param record | ||
* @returns {observable} | ||
*/ | ||
isDownloaded: function(record) { | ||
if (!ko.isObservable((record.is_downloaded))){ | ||
record.is_downloaded = ko.observable(record.is_downloaded); | ||
} | ||
|
||
return record.is_downloaded; | ||
}, | ||
|
||
/** | ||
* Get styles for preview | ||
* | ||
|
@@ -347,6 +362,16 @@ define([ | |
}); | ||
}, | ||
|
||
/** | ||
* Locate downloaded image in media browser | ||
* | ||
* @param record | ||
*/ | ||
locate: function (record) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that implementation considers only the filename. Images can be saved to different directories in the media gallery. So after modal is closed it's needed to:
|
||
$(config.adobeStockModalSelector).trigger('closeModal'); | ||
mediaGallery.locate(record.path); | ||
}, | ||
|
||
/** | ||
* Save preview | ||
* | ||
|
@@ -404,6 +429,8 @@ define([ | |
}, | ||
context: this, | ||
success: function () { | ||
record.is_downloaded(1); | ||
record.path = destinationPath; | ||
$(config.adobeStockModalSelector).trigger('processStop'); | ||
$(config.adobeStockModalSelector).trigger('closeModal'); | ||
mediaBrowser.reload(true); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd extract a couple of private functions to make this class easier to read