Skip to content
Merged
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
125 changes: 125 additions & 0 deletions AdobeStockAsset/Model/AppendAttributes.php
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
Copy link
Member

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

{
$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;
}
}
17 changes: 14 additions & 3 deletions AdobeStockAsset/Model/GetAssetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class GetAssetList implements GetAssetListInterface
{
/**
* @var AppendAttributes
*/
private $appendAttributes;

/**
* @var ClientInterface
*/
Expand All @@ -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;
}

/**
Expand All @@ -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(
__(
Expand Down
8 changes: 4 additions & 4 deletions AdobeStockAsset/Model/ResourceModel/Asset/Command/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
74 changes: 74 additions & 0 deletions AdobeStockAsset/Model/ResourceModel/Asset/LoadByIds.php
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;
}
}
10 changes: 10 additions & 0 deletions AdobeStockAsset/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\EntityManager\MetadataPool">
<arguments>
<argument name="metadata" xsi:type="array">
<item name="Magento\AdobeStockAssetApi\Api\Data\AssetInterface" xsi:type="array">
<item name="entityTableName" xsi:type="string">adobe_stock_asset</item>
<item name="identifierField" xsi:type="string">id</item>
</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -347,6 +362,16 @@ define([
});
},

/**
* Locate downloaded image in media browser
*
* @param record
*/
locate: function (record) {
Copy link
Member

Choose a reason for hiding this comment

The 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:

  • Divide the record.path to directory path and filename
  • Use directory path to open corresponding folder in media gallery
  • Use filename to select corresponding image

$(config.adobeStockModalSelector).trigger('closeModal');
mediaGallery.locate(record.path);
},

/**
* Save preview
*
Expand Down Expand Up @@ -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);
Expand Down
Loading