From 09f1d5d86f6a3e226eb22e1d086c6971e2feafe2 Mon Sep 17 00:00:00 2001 From: Volodymyr Vygovskyi Date: Sat, 31 Aug 2019 17:48:59 +0300 Subject: [PATCH 1/6] #359: added Locate button for saved preview images --- .../Model/AddIsDownloadedToSearchResult.php | 80 +++++++++++++++++++ AdobeStockAsset/Model/GetAssetList.php | 17 +++- .../components/grid/column/image-preview.js | 26 ++++++ .../template/grid/column/image-preview.html | 5 +- 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 AdobeStockAsset/Model/AddIsDownloadedToSearchResult.php diff --git a/AdobeStockAsset/Model/AddIsDownloadedToSearchResult.php b/AdobeStockAsset/Model/AddIsDownloadedToSearchResult.php new file mode 100644 index 000000000000..31f157a457d9 --- /dev/null +++ b/AdobeStockAsset/Model/AddIsDownloadedToSearchResult.php @@ -0,0 +1,80 @@ +resourceConnection = $resourceConnection; + $this->attributeValueFactory = $attributeValueFactory; + } + + /** + * @param SearchResultInterface $searchResult + * @return SearchResultInterface + */ + public function execute(SearchResultInterface $searchResult): SearchResultInterface + { + $items = $searchResult->getItems(); + $itemIds = []; + + foreach ($items as $key => $item) { + $itemIds[$key] = $item->getId(); + } + + if (count($itemIds)) { + $connection = $this->resourceConnection->getConnection(); + $select = $connection->select() + ->from(self::TABLE_ASSET, ['id']) + ->where('id in (?)', $itemIds); + $downloadedIds = $connection->fetchCol($select, 'id'); + + foreach ($items as $key => $item) { + $customAttributes = $item->getCustomAttributes(); + + $attribute = $this->attributeValueFactory->create(); + $attribute->setAttributeCode(self::ATTRIBUTE_CODE_IS_DOWNLOADED); + $attribute->setValue((int)in_array($item->getId(), $downloadedIds)); + + $customAttributes[self::ATTRIBUTE_CODE_IS_DOWNLOADED] = $attribute; + $item->setCustomAttributes($customAttributes); + } + } + + return $searchResult; + } +} diff --git a/AdobeStockAsset/Model/GetAssetList.php b/AdobeStockAsset/Model/GetAssetList.php index 7a0b4d4acd82..97c992d3e6e8 100644 --- a/AdobeStockAsset/Model/GetAssetList.php +++ b/AdobeStockAsset/Model/GetAssetList.php @@ -22,6 +22,11 @@ */ class GetAssetList implements GetAssetListInterface { + /** + * @var AddIsDownloadedToSearchResult + */ + private $addIsDownloadedToSearchResult; + /** * @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 AddIsDownloadedToSearchResult $addIsDownloadedToSearchResult */ public function __construct( ClientInterface $client, UrlInterface $url, - LoggerInterface $log + LoggerInterface $log, + AddIsDownloadedToSearchResult $addIsDownloadedToSearchResult ) { $this->client = $client; $this->url = $url; $this->log = $log; + $this->addIsDownloadedToSearchResult = $addIsDownloadedToSearchResult; } /** @@ -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->addIsDownloadedToSearchResult->execute($searchResult); + + return $searchResult; } catch (AuthenticationException $exception) { throw new LocalizedException( __( 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 a73cd77fef89..ff730c567eea 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 @@ -300,6 +300,20 @@ define([ return this.visibility()[record._rowIndex] || false; }, + /** + * 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 * @@ -327,6 +341,17 @@ define([ }); }, + /** + * Locate downloaded in media browser (draft) + * + * @param record + */ + locate: function (record) { + var mediaBrowser = $(this.mediaGallerySelector).data('mageMediabrowser'); + $(this.adobeStockModalSelector).trigger('closeModal'); + mediaBrowser.reload(true); + }, + /** * Save record as image * @@ -347,6 +372,7 @@ define([ }, context: this, success: function () { + record.is_downloaded(1); $(this.adobeStockModalSelector).trigger('processStop'); $(this.adobeStockModalSelector).trigger('closeModal'); mediaBrowser.reload(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 b6d0321a78d0..66ea4dea69bb 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 @@

- +