diff --git a/AdobeStockAsset/Model/AppendAttributes.php b/AdobeStockAsset/Model/AppendAttributes.php new file mode 100644 index 000000000000..1cc75d07bec3 --- /dev/null +++ b/AdobeStockAsset/Model/AppendAttributes.php @@ -0,0 +1,125 @@ +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; + } +} diff --git a/AdobeStockAsset/Model/GetAssetList.php b/AdobeStockAsset/Model/GetAssetList.php index 7a0b4d4acd82..248472442eca 100644 --- a/AdobeStockAsset/Model/GetAssetList.php +++ b/AdobeStockAsset/Model/GetAssetList.php @@ -22,6 +22,11 @@ */ class GetAssetList implements GetAssetListInterface { + /** + * @var AppendAttributes + */ + private $appendAttributes; + /** * @var ClientInterface */ @@ -40,17 +45,20 @@ class GetAssetList implements GetAssetListInterface /** * GetAssetList constructor. * @param ClientInterface $client - * @param UrlInterface $url + * @param UrlInterface $url * @param LoggerInterface $log + * @param AppendAttributes $appendAttributes */ public function __construct( ClientInterface $client, UrlInterface $url, - LoggerInterface $log + LoggerInterface $log, + AppendAttributes $appendAttributes ) { $this->client = $client; $this->url = $url; $this->log = $log; + $this->appendAttributes = $appendAttributes; } /** @@ -59,7 +67,10 @@ public function __construct( public function execute(SearchCriteriaInterface $searchCriteria): SearchResultInterface { try { - return $this->client->search($searchCriteria); + $searchResult = $this->client->search($searchCriteria); + $this->appendAttributes->execute($searchResult); + + return $searchResult; } catch (AuthenticationException $exception) { throw new LocalizedException( __( diff --git a/AdobeStockAsset/Model/ResourceModel/Asset/Command/Save.php b/AdobeStockAsset/Model/ResourceModel/Asset/Command/Save.php index 71e64d833be1..26a2b078219c 100644 --- a/AdobeStockAsset/Model/ResourceModel/Asset/Command/Save.php +++ b/AdobeStockAsset/Model/ResourceModel/Asset/Command/Save.php @@ -43,14 +43,14 @@ public function __construct( } /** - * Multiple save category + * Save asset * - * @param AssetInterface $assets + * @param AssetInterface $asset * @return void */ - public function execute(AssetInterface $assets): void + public function execute(AssetInterface $asset): void { - $data = $this->hydrator->extract($assets); + $data = $this->hydrator->extract($asset); $tableName = $this->resourceConnection->getTableName( AssetResourceModel::ADOBE_STOCK_ASSET_TABLE_NAME ); diff --git a/AdobeStockAsset/Model/ResourceModel/Asset/LoadByIds.php b/AdobeStockAsset/Model/ResourceModel/Asset/LoadByIds.php new file mode 100644 index 000000000000..43a5b313ef5e --- /dev/null +++ b/AdobeStockAsset/Model/ResourceModel/Asset/LoadByIds.php @@ -0,0 +1,74 @@ +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; + } +} diff --git a/AdobeStockAsset/etc/di.xml b/AdobeStockAsset/etc/di.xml index 2916ddeebaf9..decbdacfee01 100644 --- a/AdobeStockAsset/etc/di.xml +++ b/AdobeStockAsset/etc/di.xml @@ -69,4 +69,14 @@ + + + + + adobe_stock_asset + id + + + + diff --git a/AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/image-preview.js b/AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/image-preview.js index 19ca75eefda8..e63f9c251e03 100644 --- a/AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/image-preview.js +++ b/AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/image-preview.js @@ -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) { + $(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); diff --git a/AdobeStockImageAdminUi/view/adminhtml/web/js/media-gallery.js b/AdobeStockImageAdminUi/view/adminhtml/web/js/media-gallery.js new file mode 100644 index 000000000000..a98a0449dc3b --- /dev/null +++ b/AdobeStockImageAdminUi/view/adminhtml/web/js/media-gallery.js @@ -0,0 +1,59 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'jquery' +], function ($) { + 'use strict'; + + return { + jsTreeRootFolderName: 'Storage Root', + jsTreeFolderNameMaxLength: 20, + + /** + * Locate downloaded image in media browser + * + * @param {String} path + */ + locate: function (path) { + $.ajaxSetup({async: false}); + var imagePath = path.replace(/^\/+/, ''); + var imagePathParts = imagePath.split('/'); + var imageFilename = imagePath; + var imageFolderName = this.jsTreeRootFolderName; + + if (imagePathParts.length > 1) { + imageFilename = imagePathParts[imagePathParts.length - 1]; + imageFolderName = imagePathParts[imagePathParts.length - 2]; + + for (var i = 0; i < imagePathParts.length - 2; i++) { + var folderName = imagePathParts[i]; + + /* folder name is being cut in file browser */ + if (folderName.length > this.jsTreeFolderNameMaxLength) { + folderName = folderName.substring(0, this.jsTreeFolderNameMaxLength) + '...'; + } + + //var folderSelector = ".jstree a:contains('" + folderName + "')"; + var openFolderChildrenButton = $(".jstree a:contains('" + folderName + "')").prev('.jstree-icon'); + if (openFolderChildrenButton.length) { + openFolderChildrenButton.click(); + } + } + } + + //select folder + var imageFolder = $(".jstree a:contains('" + imageFolderName + "')"); + if (imageFolder.length) { + imageFolder[0].click(); + //select image + var locatedImage = $("div[data-row='file']:has(img[alt=\"" + imageFilename + "\"])"); + if (locatedImage.length) { + locatedImage.click(); + } + } + $.ajaxSetup({async: true}); + } + } +}); diff --git a/AdobeStockImageAdminUi/view/adminhtml/web/template/grid/column/image-preview.html b/AdobeStockImageAdminUi/view/adminhtml/web/template/grid/column/image-preview.html index 2562d091dcc4..b6aee22a174e 100644 --- a/AdobeStockImageAdminUi/view/adminhtml/web/template/grid/column/image-preview.html +++ b/AdobeStockImageAdminUi/view/adminhtml/web/template/grid/column/image-preview.html @@ -35,9 +35,12 @@

- +