Skip to content
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

#397 : "More from this model" image preview section #408

Merged
merged 11 commits into from
Sep 23, 2019
Merged
3 changes: 2 additions & 1 deletion AdobeStockClient/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
<argument name="filters" xsi:type="array">
<item name="colors_filter" xsi:type="string">setFilterColors</item>
<item name="serie_id" xsi:type="string">setSerieId</item>
<item name="model_id" xsi:type="string">setModelId</item>
</argument>
</arguments>
</type>
</config>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
use Psr\Log\LoggerInterface;

/**
* Class GetImageSeries
* Class GetRelatedImages
*/
class GetImageSeries
class GetRelatedImages
{
/*
* Series ID.
*/
const SERIE_ID = 'serie_id';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid code duplication, it's better to use the field as a method parameter or as a constructor parameter provided by DI and use cycles instead of similar code blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @sivaschenko ,

We can avoid code duplication by replacing Magento\Framework\Api\Search\SearchCriteriaBuilder to Magento\Framework\Api\SearchCriteriaBuilder so that we can pass array of filter but we have already used Magento\Framework\Api\Search\SearchCriteriaBuilder in so many places that there are lot of chance some functionality might broke. Let me know if you want to move forward with this logic or any other logic from your side is always welcome

Thanks!


/*
* Model ID.
*/
const MODEL_ID = 'model_id';

/**
* @var GetImageListInterface
*/
Expand All @@ -42,7 +52,7 @@ class GetImageSeries
private $logger;

/**
* GetImageSeries constructor.
* GetRelatedImages constructor.
* @param GetImageListInterface $getImageList
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param FilterBuilder $filterBuilder
Expand All @@ -63,55 +73,72 @@ public function __construct(
/**
* Get image related image series.
*
* @param int $serieId
* @param int $imageId
* @param int $limit
*
* @return array
* @throws IntegrationException
*/
public function execute(int $serieId, int $limit): array
public function execute(int $imageId, int $limit): array
{
try {
$filter = $this->filterBuilder->setField('serie_id')->setValue($serieId)->create();
$searchCriteria = $this->searchCriteriaBuilder->addFilter($filter)->setPageSize($limit)->create();
$seriesFilter = $this->filterBuilder->setField(self::SERIE_ID)->setValue($imageId)->create();
$modelFilter = $this->filterBuilder->setField(self::MODEL_ID)->setValue($imageId)->create();
$serieSearchCriteria = $this->searchCriteriaBuilder
->addFilter($seriesFilter)
->setPageSize($limit)
->create();
$modelSearchCriteria = $this->searchCriteriaBuilder
->addFilter($modelFilter)
->setPageSize($limit)
->create();

return $this->serializeImageSeries(
$this->getImageList->execute($searchCriteria)->getItems()
return $this->serializeRelatedImages(
$this->getImageList->execute($serieSearchCriteria)->getItems(),
$this->getImageList->execute($modelSearchCriteria)->getItems()
);
} catch (\Exception $exception) {
$message = __('Get image series list failed: %s', $exception->getMessage());
$message = __('Get related images list failed: %s', $exception->getMessage());
throw new IntegrationException($message, $exception);
}
}

/**
* Serialize image series data.
* Serialize related image data.
*
* @param Document[] $series
*
* @param Document[] $models
* @return array
* @throws SerializationException
*/
private function serializeImageSeries(array $series): array
private function serializeRelatedImages(array $series, array $models): array
{
$data = [];
$seriesData = [];
$modelData = [];
try {
/** @var Document $seriesItem */
foreach ($series as $seriesItem) {
$item['id'] = $seriesItem->getId();
$item['title'] = $seriesItem->getCustomAttribute('title')->getValue();
$item['thumbnail_url'] = $seriesItem->getCustomAttribute('thumbnail_240_url')->getValue();
$data[] = $item;
$seriesData[] = $item;
}
/** @var Document $modelItem */
foreach ($models as $modelItem) {
$item['id'] = $modelItem->getId();
$item['title'] = $modelItem->getCustomAttribute('title')->getValue();
$item['thumbnail_url'] = $modelItem->getCustomAttribute('thumbnail_240_url')->getValue();
$modelData[] = $item;
}

$result = [
'type' => 'series',
'series' => $data,
'series' => $seriesData,
'model' => $modelData
];

return $result;
} catch (\Exception $exception) {
$message = __('An error occurred during image series serialization: %s', $exception->getMessage());
$message = __('An error occurred during related images serialization: %s', $exception->getMessage());
throw new SerializationException($message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

namespace Magento\AdobeStockImageAdminUi\Controller\Adminhtml\Preview;

use Magento\AdobeStockImage\Model\GetImageSeries;
use Magento\AdobeStockImage\Model\GetRelatedImages;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Psr\Log\LoggerInterface;

/**
* Class Series
* Class RelatedImages
*/
class Series extends Action
class RelatedImages extends Action
{
/**
* Successful get image serie result code.
* Successful get related image result code.
*/
const HTTP_OK = 200;

Expand All @@ -29,29 +30,29 @@ class Series extends Action
const HTTP_INTERNAL_ERROR = 500;

/**
* @var GetImageSeries
* @var GetRelatedImages
*/
private $getImageSeries;
private $getRelatedImages;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Series constructor.
* RelatedImages constructor.
*
* @param Action\Context $context
* @param GetImageSeries $getImageSeries
* @param LoggerInterface $logger
* @param Action\Context $context
* @param GetRelatedImages $getRelatedImages
* @param LoggerInterface $logger
*/
public function __construct(
Action\Context $context,
GetImageSeries $getImageSeries,
GetRelatedImages $getRelatedImages,
LoggerInterface $logger
) {
parent::__construct($context);
$this->getImageSeries = $getImageSeries;
$this->getRelatedImages = $getRelatedImages;
$this->logger = $logger;
}
/**
Expand All @@ -61,27 +62,27 @@ public function execute()
{
try {
$params = $params = $this->getRequest()->getParams();
$serieId = (int) $params['serie_id'];
$imageId = (int) $params['image_id'];
$limit = (int) ($params['limit'] ?? 4);
$imageSeries = $this->getImageSeries->execute($serieId, $limit);
$relatedImages = $this->getRelatedImages->execute($imageId, $limit);

$responseCode = self::HTTP_OK;
$responseContent = [
'success' => true,
'message' => __('Get image series finished successfully'),
'result' => $imageSeries,
'message' => __('Get related images finished successfully'),
'result' => $relatedImages,
];
} catch (\Exception $exception) {
$responseCode = self::HTTP_INTERNAL_ERROR;
$logMessage = __('An error occurred during get image serie data: %1', $exception->getMessage());
$logMessage = __('An error occurred during get related images data: %1', $exception->getMessage());
$this->logger->critical($logMessage);
$responseContent = [
'success' => false,
'message' => __('An error occurred while getting image series. Contact support.'),
'message' => __('An error occurred while getting related images. Contact support.'),
];
}

/** @var \Magento\Framework\Controller\Result\Json $resultJson */
/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setHttpResponseCode($responseCode);
$resultJson->setData($responseContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function prepare()
[
'downloadImagePreviewUrl' => $this->urlBuilder->getUrl('adobe_stock/preview/download'),
'getQuotaUrl' => $this->urlBuilder->getUrl('adobe_stock/license/getquota'),
'imageSeriesUrl' => $this->urlBuilder->getUrl('adobe_stock/preview/series'),
'imageSeriesUrl' => $this->urlBuilder->getUrl('adobe_stock/preview/relatedimages'),
'authConfig' => [
'url' => $this->config->getAuthUrl(),
'isAuthorized' => $this->isAuthorized(),
Expand Down
135 changes: 68 additions & 67 deletions AdobeStockImageAdminUi/view/adminhtml/web/css/source/_module.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,86 @@
// */

& when (@media-common = true) {
.masonry-image-preview {
.title {
font-weight: bold;
}
.info {
width: ~"calc(40% - 30px)";
float: right;
text-align: left;

.title {
margin: 0 0 20px;
}

.author {
margin: 0 0 30px;
}

.action-buttons {
text-align: left;
margin: 0 0 30px;
}
}

.attributes {
.attribute {
display: inline-block;
margin-right: 2em;

.value {
margin-top: 1em;
.masonry-image-preview {
.title {
font-weight: bold;
}
}
}

.keywords {
.info {
width: ~"calc(40% - 30px)";
float: right;
text-align: left;

.title {
margin: 0 0 20px;
}

.title {
margin-top: 10px;
}
.author {
margin: 0 0 30px;
}

.keyword {
&.hide {
display: none;
.action-buttons {
text-align: left;
margin: 0 0 30px;
}
}

display: inline-block;
margin: 15px 15px 15px 0;
.attributes {
.attribute {
display: inline-block;
margin-right: 2em;

.value {
font-size: 15px;
text-transform: capitalize;
.value {
margin-top: 1em;
}
}
}
}
}

.series {
height: 120px;
vertical-align: middle;
.thumbnail {
display: inline-block;
margin: 10px 10px 10px 0;
img {
max-height: 100px;
max-width: 160px;
.keywords {

.title {
margin-top: 10px;
}

.keyword {
&.hide {
display: none;
}

display: inline-block;
margin: 15px 15px 15px 0;

.value {
font-size: 15px;
text-transform: capitalize;
}
}
}
}

}
}
#adobe-stock-tabs-content {
height: 120px;
vertical-align: middle;
.thumbnail {
display: inline-block;
margin: 10px 10px 10px 0;

.admin__adobe-stock-image-checkbox {
.admin__field-control._with-tooltip {
.admin__form-field-label {
display: inline-block;
}
img {
max-height: 100px;
max-width: 160px;
}
}
}

.admin__adobe-stock-image-checkbox {
.admin__field-control._with-tooltip {
.admin__form-field-label {
display: inline-block;
}

.admin__field-tooltip {
margin: -5px 0 0 5px;
}
.admin__field-tooltip {
margin: -5px 0 0 5px;
}
}
}
}
}
}
Loading