Skip to content

Commit

Permalink
Merge pull request #1509 from Nazar65/ASI-1488
Browse files Browse the repository at this point in the history
Introduce massaction mode for media gallery
  • Loading branch information
sivaschenko authored Jul 2, 2020
2 parents bcfd944 + d9dc62a commit 19de047
Show file tree
Hide file tree
Showing 31 changed files with 802 additions and 183 deletions.
21 changes: 10 additions & 11 deletions AdobeStockImageAdminUi/Plugin/AddAdobeStockImageDetailsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,36 @@ public function __construct(
*
* @param GetDetailsByAssetId $getImageDetailsByAssetId
* @param array $imageDetails
* @param int $assetId
* @param array $assetIds
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterExecute(
GetDetailsByAssetId $getImageDetailsByAssetId,
array $imageDetails,
int $assetId
array $assetIds
): array {
try {
$mediaGalleryIdFilter = $this->filterBuilder->setField(self::MEDIA_GALLERY_ID)
->setValue($assetId)
->create();
->setValue(implode(",", $assetIds))
->setConditionType('in')
->create();

$searchCriteria = $this->searchCriteriaBuilder
->addFilter($mediaGalleryIdFilter)
->setPageSize(1)
->create();

/** @var AssetSearchResultsInterface $result */
$result = $this->assetRepository->getList($searchCriteria);
$adobeStockInfo = [];
if ($result->getTotalCount() > 0) {
$item = $result->getItems();
/** @var AssetInterface $asset */
$asset = reset($item);
$adobeStockInfo = $this->loadAssetsInfo($asset);

foreach ($item as $asset) {
$imageDetails[$asset->getMediaGalleryId()]['adobe_stock'] = $this->loadAssetsInfo($asset);
}
}
$imageDetails['adobe_stock'] = $adobeStockInfo;
} catch (Exception $exception) {
$this->logger->critical($exception);
$imageDetails['adobe_stock'] = [];
}

return $imageDetails;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See COPYING.txt for license details.
*/
-->
<div class="adobe-stock-image-details image-details-section" if="$parent.image().adobe_stock.length">
<div class="adobe-stock-image-details image-details-section" if="$parent.image().adobe_stock">
<h3 data-bind="i18n: 'Adobe Stock'"></h3>
<div class="attributes">
<each args="$parent.image().adobe_stock">
Expand Down
24 changes: 15 additions & 9 deletions MediaGalleryUi/Controller/Adminhtml/Image/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$imageId = (int) $this->getRequest()->getParam('id');
$imageIds = $this->getRequest()->getParam('ids');

if ($imageId === 0) {
if (empty($imageIds) || !is_array($imageIds)) {
$responseContent = [
'success' => false,
'message' => __('Image ID is required.'),
'message' => __('Image Ids are required and must be of type array.'),
];
$resultJson->setHttpResponseCode(self::HTTP_BAD_REQUEST);
$resultJson->setData($responseContent);
Expand All @@ -99,14 +99,20 @@ public function execute()
}

try {
$assets = $this->getAssetsByIds->execute([$imageId]);
/** @var AssetInterface $asset */
$asset = current($assets);
$this->deleteImage->execute($asset);
$assets = $this->getAssetsByIds->execute($imageIds);
$this->deleteImage->execute($assets);
$responseCode = self::HTTP_OK;
$message = count($imageIds) > 1 ?
'assets have been' :
' asset "' . current($assets)->getTitle() . '" has been';
$responseContent = [
'success' => true,
'message' => __('You have successfully removed the image "%image"', ['image' => $asset->getTitle()]),
'message' => __(
'The %message successfully deleted',
[
'message' => $message
]
),
];
} catch (LocalizedException $exception) {
$responseCode = self::HTTP_BAD_REQUEST;
Expand All @@ -119,7 +125,7 @@ public function execute()
$responseCode = self::HTTP_INTERNAL_ERROR;
$responseContent = [
'success' => false,
'message' => __('An error occurred on attempt to save image.'),
'message' => __('An error occurred on attempt to delete image.'),
];
}

Expand Down
8 changes: 4 additions & 4 deletions MediaGalleryUi/Controller/Adminhtml/Image/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$id = (int) $this->getRequest()->getParam('id');
$ids = $this->getRequest()->getParam('ids');

if ($id === 0) {
if (empty($ids) || !is_array($ids)) {
$responseContent = [
'success' => false,
'message' => __('Asset ID is required.'),
'message' => __('Assets Ids is required, and must be of type array.'),
];
$resultJson->setHttpResponseCode(self::HTTP_BAD_REQUEST);
$resultJson->setData($responseContent);
Expand All @@ -80,7 +80,7 @@ public function execute()
}

try {
$details = $this->getDetailsByAssetId->execute($id);
$details = $this->getDetailsByAssetId->execute($ids);

$responseCode = self::HTTP_OK;
$responseContent = [
Expand Down
6 changes: 2 additions & 4 deletions MediaGalleryUi/Model/AssetDetailsProvider/UsedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,14 @@ private function getUsedIn(int $assetId): array
foreach ($contentIdentities as $contentIdentity) {
$entityId = $contentIdentity->getEntityId();
$type = $this->contentTypes[$contentIdentity->getEntityType()] ?? $contentIdentity->getEntityType();
$singleType = $this->contentTypes[$contentIdentity->getEntityType() . '_single'] ?? $type;

if (!isset($entityIds[$type])) {
$usedIn[$singleType] = 1;
$usedIn[$type] = 1;
} elseif ($entityIds[$type]['entity_id'] !== $entityId) {
if (isset($usedIn[$type])) {
$usedIn[$type] +=1;
} else {
$usedIn[$type] = $usedIn[$singleType]+1;
unset($usedIn[$singleType]);
$usedIn[$type] = $usedIn[$type]+1;
}
}
$entityIds[$type]['entity_id'] = $entityId;
Expand Down
28 changes: 19 additions & 9 deletions MediaGalleryUi/Model/DeleteImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,27 @@ public function __construct(
*
* @see \Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage
*
* @param AssetInterface $asset
* @throws LocalizedException
* @param AssetInterface[] $assets
*/
public function execute(AssetInterface $asset): void
public function execute(array $assets): void
{
if ($this->isPathExcluded->execute($asset->getPath())) {
throw new LocalizedException(__('Could not delete image: destination directory is restricted.'));
}
$failedAssets = [];
foreach ($assets as $asset) {
if ($this->isPathExcluded->execute($asset->getPath())) {
$failedAssets[] = $asset->getPath();
}

$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$absolutePath = $mediaDirectory->getAbsolutePath($asset->getPath());
$this->imagesStorage->deleteFile($absolutePath);
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$absolutePath = $mediaDirectory->getAbsolutePath($asset->getPath());
$this->imagesStorage->deleteFile($absolutePath);
}
if (!empty($failedAssets)) {
throw new LocalizedException(
__(
'Could not delete "%image": destination directory is restricted.',
['image' => implode(",", $failedAssets)]
)
);
}
}
}
44 changes: 25 additions & 19 deletions MediaGalleryUi/Model/GetDetailsByAssetId.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GetDetailsByAssetId
/**
* @var GetAssetsByIdsInterface
*/
private $getAssetById;
private $getAssetsById;

/**
* @var StoreManagerInterface
Expand Down Expand Up @@ -63,36 +63,42 @@ public function __construct(
GetAssetsKeywordsInterface $getAssetKeywords
) {
$this->detailsProviderPool = $detailsProviderPool;
$this->getAssetById = $getAssetById;
$this->getAssetsById = $getAssetById;
$this->storeManager = $storeManager;
$this->sourceIconProvider = $sourceIconProvider;
$this->getAssetKeywords = $getAssetKeywords;
}

/**
* Get image details by asset ID
* Get image details by assets Ids
*
* @param int $assetId
* @param array $assetIds
* @throws LocalizedException
* @throws Exception
* @return array
*/
public function execute(int $assetId): array
public function execute(array $assetIds): array
{
$asset = current($this->getAssetById->execute([$assetId]));

return [
'image_url' => $this->getUrl($asset->getPath()),
'title' => $asset->getTitle(),
'path' => $asset->getPath(),
'description' => $asset->getDescription(),
'id' => $assetId,
'details' => $this->detailsProviderPool->execute($asset),
'size' => $asset->getSize(),
'tags' => $this->getKeywords($asset),
'source' => $asset->getSource() ? $this->sourceIconProvider->getSourceIconUrl($asset->getSource()) : null,
'content_type' => strtoupper(str_replace('image/', '', $asset->getContentType())),
];
$assets = $this->getAssetsById->execute($assetIds);

$details = [];
foreach ($assets as $asset) {
$details[$asset->getId()] = [
'image_url' => $this->getUrl($asset->getPath()),
'title' => $asset->getTitle(),
'path' => $asset->getPath(),
'description' => $asset->getDescription(),
'id' => $asset->getId(),
'details' => $this->detailsProviderPool->execute($asset),
'size' => $asset->getSize(),
'tags' => $this->getKeywords($asset),
'source' => $asset->getSource() ?
$this->sourceIconProvider->getSourceIconUrl($asset->getSource()) :
null,
'content_type' => strtoupper(str_replace('image/', '', $asset->getContentType())),
];
}
return $details;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminEnhancedMediaGalleryAssertImagesDeletedInBulkActionGroup">
<annotations>
<description>Asserts images has been deleted in mass action.</description>
</annotations>

<see userInput='The assets have been successfully deleted' stepKey="verifyDeleteImages"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminEnhancedMediaGalleryAssertMassActionModeDetailsActionGroup">
<annotations>
<description>Asserts that massaction mode can be enabled and disabled, verify massaction view after switch to massaction mode</description>
</annotations>
<arguments>
<argument name="imageName" type="string"/>
</arguments>
<click selector="{{AdminEnhancedMediaGalleryMassActionSection.massActionCheckbox(imageName)}}" stepKey="selectImageInGridToDelte"/>
<see selector="{{AdminEnhancedMediaGalleryMassActionSection.totalSelected}}" userInput="(1 Selected)" stepKey="verifySelectedCount"/>
<click selector="{{AdminEnhancedMediaGalleryMassActionSection.cancelMassActionMode}}" stepKey="cancelMassAction"/>
<dontSeeElement selector="{{AdminEnhancedMediaGalleryMassActionSection.totalSelected}}" stepKey="verifyTeminateMAssAction"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminEnhancedMediaGalleryEnableMassActionModeActionGroup">
<annotations>
<description>Activate massaction mode by click on Delete Selected..</description>
</annotations>

<click selector="{{AdminEnhancedMediaGalleryMassActionSection.deleteSelected}}" stepKey="clickOnMassActionButton"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminEnhancedMediaGalleryImageDeleteImagesInBulkActionGroup">
<annotations>
<description>Delete images in bulk using mass action mode.</description>
</annotations>
<arguments>
<argument name="imageName" type="string" defaultValue="magento"/>
</arguments>

<click selector="{{AdminEnhancedMediaGalleryMassActionSection.deleteSelected}}" stepKey="clickDeleteImages"/>
<waitForLoadingMaskToDisappear stepKey="waitForDeleteModal"/>
<click selector="{{AdminEnhancedMediaGalleryDeleteModalSection.confirmDelete}}" stepKey="confirmDelete"/>
<waitForLoadingMaskToDisappear stepKey="waitForDeletingProcces"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminEnhancedMediaGallerySelectImageForMassActionActionGroup">
<annotations>
<description>Select images in grid by clicking on mass action checkbox</description>
</annotations>
<arguments>
<argument name="imageName" type="string" defaultValue="magento"/>
</arguments>

<click selector="{{AdminEnhancedMediaGalleryMassActionSection.massActionCheckbox(imageName)}}" stepKey="selectImageInGridToDelte"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<arguments>
<argument name="imageName" type="string" defaultValue="magento"/>
</arguments>
<see userInput='You have successfully removed the image "{{imageName}}"' stepKey="verifyDeleteImage"/>
<see userInput='The asset "{{imageName}}" has been successfully deleted' stepKey="verifyDeleteImage"/>
<dontSeeElement selector="{{AdminEnhancedMediaGalleryImageActionsSection.imageInGrid(imageName)}}" stepKey="dontSeeImage"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminEnhancedMediaGalleryMassActionSection">
<element name="massActionCheckbox" type="button" selector="//input[@type='checkbox'][@data-ui-id ='{{imageName}}']" parameterized="true"/>
<element name="totalSelected" type="text" selector=".mediagallery-massaction-items-count > .selected_count_text"/>
<element name="cancelMassActionMode" type="button" selector="#cancel_massaction"/>
<element name="deleteSelected" type="button" selector="#delete_massaction"/>
</section>
</sections>
Loading

0 comments on commit 19de047

Please sign in to comment.